zoukankan      html  css  js  c++  java
  • 加强型无穷集合: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调试总结
    vim常用命令
    Xshell
    JavaScript
    HTML+CSS
    解决VMware“该虚拟机似乎正在使用中”问题
    MVC dirname(——FILE——)
    各种编程语言中的指针和引用
    Go defer 原理和源码剖析
    软件架构定义的流派
  • 原文地址:https://www.cnblogs.com/ly45/p/5515863.html
Copyright © 2011-2022 走看看