sequence/1/Sequence.cs - The class IntInterval. | Lecture 8 - slide 26 : 37 Program 2 |
public class IntInterval: IntSequence{ private int from, to; public IntInterval(int from, int to){ this.from = from; this.to = to; } public override int Min{ get {return Math.Min(from,to);} } public override int Max{ get {return Math.Max(from,to);} } public override IEnumerator GetEnumerator (){ return new IntervalEnumerator(this); } private class IntervalEnumerator: IEnumerator{ private readonly IntInterval interval; private int idx; public IntervalEnumerator (IntInterval i){ this.interval = i; idx = -1; // position enumerator outside range } public Object Current{ get {return (interval.from < interval.to) ? interval.from + idx : interval.from - idx;} } public bool MoveNext (){ if ( idx < Math.Abs(interval.to - interval.from)) {idx++; return true;} else {return false;} } public void Reset(){ idx = -1; } } }