Created
October 4, 2011 23:44
-
-
Save einarwh/1263173 to your computer and use it in GitHub Desktop.
An infinite sequence repeating another sequence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class BrokenRecord<T> : IEnumerable<T> | |
| { | |
| private readonly IEnumerable<T> _seq; | |
| public BrokenRecord(IEnumerable<T> seq) | |
| { | |
| _seq = seq; | |
| } | |
| public IEnumerator<T> GetEnumerator() | |
| { | |
| var e = _seq.GetEnumerator(); | |
| return new BrokenRecordEnumerator<T>(e); | |
| } | |
| IEnumerator IEnumerable.GetEnumerator() | |
| { | |
| return GetEnumerator(); | |
| } | |
| } | |
| public class BrokenRecordEnumerator<T> : IEnumerator<T> | |
| { | |
| private readonly IEnumerator<T> _seq; | |
| public BrokenRecordEnumerator(IEnumerator<T> seq) | |
| { | |
| _seq = seq; | |
| } | |
| public void Dispose() {} | |
| private bool ResetAndMoveNext() | |
| { | |
| Reset(); | |
| return _seq.MoveNext(); | |
| } | |
| public bool MoveNext() | |
| { | |
| return _seq.MoveNext() || ResetAndMoveNext(); | |
| } | |
| public void Reset() | |
| { | |
| _seq.Reset(); | |
| } | |
| public T Current | |
| { | |
| get | |
| { | |
| return _seq.Current; | |
| } | |
| } | |
| object IEnumerator.Current | |
| { | |
| get { return Current; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment