Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    The struct Interval.Lecture 5 - slide 30 : 45
Program 1
using System;
using System.Collections;

public struct Interval{

  private readonly int from, to;  

  public Interval(int from, int to){   
    this.from = from;
    this.to = to;
  }

  public int From{   
    get {return from;}
  }

  public int To{
    get {return to;}
  }

  public int Length{  
    get {return Math.Abs(to - from) + 1;}                
  }

  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);
  }    

  public static explicit operator int[] (Interval i){  
    int[] res = new int[i.Length];                     
    for (int j = 0; j < i.Length; j++) res[j] = i[j];
    return res; 
  }

  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);
  }

}
 
 
 
 
 
Interval data representation.
 
Interval constructor.
 
 
 
 
Access to from and to via properties.
 
 
 
 
 
 
 
The number of elements in the interval.
 
 
 
Implementing i + j, for Interval i and integer j.
 
 
 
Implementing j + i, for integer j and Interval i 
 
 
 
Implementing i >> j, for Interval i and integer j
 
 
 
Implementing i << j, for Interval i and integer j
 
 
 
Implementing i * j, for Interval i and integer j.
 
 
 
Implementing j * i, for integer j and Interval i.
 
 
 
Implementing i - j, for Interval i and integer j.
 
 
 
Implementing !i for Interval
 
 
 
Explicit type cast operation from Interval to 
an integer array.
 
 
 
 
Interval traversal via an enumerator. Implements
the machinery behind foreach traversal of an interval.