using System; using System.Collections; public struct Interval{ private readonly int from, to; private bool empty; public Interval(int from, int to){ this.from = from; this.to = to; this.empty = false; } /* Constructs an empty interval. A Factory method. */ public static Interval MakeEmptyInterval (){ Interval res = new Interval(); res.empty = true; return res; } public int From{ get {return from;} } public int To{ get {return to;} } public bool Empty{ get {return empty;} } public int Length{ get {return Math.Abs(to - from) + 1;} } public int this[int i]{ get {if (from <= to){ if (i >= 0 && i <= Math.Abs(from-to)) return from + i; else throw new Exception("Index out of interval bounds"); } else if (from > to){ if (i >= 0 && i <= Math.Abs(from-to)) return from - i; else throw new Exception("Index out of interval bounds"); } else throw new Exception("Should not happen"); } } public static Interval operator +(Interval i, int j){ return new Interval(i.From + j, i.To + j); } public static Interval operator +(int j, Interval i){ return new Interval(i.From + j, i.To + j); } public static Interval operator >>(Interval i, int j){ return new Interval(i.From, i.To + j); } public static Interval operator <<(Interval i, int j){ return new Interval(i.From + j, i.To); } public static Interval operator *(Interval i, int j){ return new Interval(i.From * j, i.To * j); } public static Interval operator *(int j, Interval i){ return new Interval(i.From * j, i.To * j); } public static Interval operator -(Interval i, int j){ return new Interval(i.From - j, i.To - j); } public static Interval operator !(Interval i){ return new Interval(i.To, i.From); } private class IntervalEnumerator: IEnumerator{ private readonly Interval interval; private int idx; public IntervalEnumerator (Interval 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; } } public IEnumerator GetEnumerator (){ return new IntervalEnumerator(this); } }