• 加强型无穷集合: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;
            }
        }
    
  • 相关阅读:
    记录排序算法
    Redis 记录
    ELK Windows环境 强行记录
    前端组件 bootstrap-select 下拉 多选 搜索
    记一次微信点赞小网站的事故
    来自加班的吐槽
    .net 比较器
    做一个.net core 小项目 遇到的一些坑
    即使通讯架构
    resultMap 映射
  • 原文地址:https://www.cnblogs.com/ly45/p/5515863.html
Copyright © 2020-2023  润新知