标签:
//有个 Step 属性用来指定遍历方向和偏移量,遍历期间被设置为 0 就终止遍历。
public class InfiniteList<T> : IEnumerable<T> { public List<T> Source { 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); Source = new List<T>(source); } public IEnumerator<T> GetEnumerator() { return new Enumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } class Enumerator : IEnumerator<T> { InfiniteList<T> list; int index = -1; public Enumerator(InfiniteList<T> source) { this.list = source; index = source.start - source.Step; } public T Current { get { return list.Source[index]; } } object IEnumerator.Current { get { return Current; } } public void Dispose() { } public bool MoveNext() { if (list.Source.Count == 0) { return false; } if (list.Step == 0) { return false; } index += list.Step; while (index > list.Source.Count - 1) { index -= list.Source.Count; } while (index < 0) { index += list.Source.Count; } return true; } public void Reset() { index = list.start; } } }
标签:
原文地址:http://www.cnblogs.com/ly45/p/5515863.html