• 加强型无穷集合:InfiniteList<T>,可指定遍历方向和偏移量,只要集合有元素并且偏移量不为 0,将永远遍历下去。


     主类:

        public class InfiniteList<T> : IEnumerable<T> {
            public List<T> SourceList { get; }
            int start { get; }
            public int Step { get; set; } = 1;
    
            public InfiniteList(IEnumerable<T> source) : this(0, source) {
            }
    
            public InfiniteList(int start, IEnumerable<T> source) {
                this.start = Math.Max(Math.Min(start, source.Count()), -1);
                SourceList = new List<T>(source);
            }
    
            public InfiniteEnumerator<T> GetInfiniteEnumerator() {
                return new InfiniteEnumerator<T>(this, start);
            }
    
            public IEnumerator<T> GetEnumerator() {
                return GetInfiniteEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator() {
                return GetEnumerator();
            }
        }
    

     迭代类:

        public class InfiniteEnumerator<T> : IEnumerator<T> {
            InfiniteList<T> list;
            int start;
    
            public int Index { get; set; }
            public int Step { get; set; }
            public InfiniteEnumerator(InfiniteList<T> source, int start) {
                list = source;
                Index = start - source.Step;
                this.start = start;
                Step = source.Step;
            }
    
            public T Current {
                get { return list.SourceList[Index]; }
            }
    
            object IEnumerator.Current {
                get { return Current; }
            }
    
            public void Dispose() {
            }
    
            public bool MoveNext() {
                if (list.SourceList.Count == 0) {
                    return false;
                }
                if (Step == 0) {
                    return false;
                }
                Index += Step;
                while (Index > list.SourceList.Count - 1) {
                    Index -= list.SourceList.Count;
                }
                while (Index < 0) {
                    Index += list.SourceList.Count;
                }
    
                return true;
            }
    
            public void Reset() {
                Index = start;
            }
        }
    
  • 相关阅读:
    学习笔记之正向代理和反向代理的区别
    PHP程序员的进阶之路
    go语言笔记——切片函数常见操作,增删改查和搜索、排序
    golang的垃圾回收(GC)机制
    堆栈的详细讲解
    springAop必导jar包
    sring框架的jdbc应用
    下载jar包方法
    mysql数据乱码
    Eclipse打包java工程
  • 原文地址:https://www.cnblogs.com/ly45/p/5515863.html
Copyright © 2020-2023  润新知