Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'        Slide program -- Keyboard shortcut: 't'    The class IntCompSeq.Lecture 8 - slide 26 : 37
Program 4
public class IntCompSeq: IntSequence{

  private IntSequence s1, s2;

  public IntCompSeq(IntSequence s1, IntSequence s2) {
    this.s1 = s1;
    this.s2 = s2;
  }

  public override int Min{
    get {return Math.Min(s1.Min,s2.Min);}
  }

  public override int Max{
    get {return Math.Max(s1.Max, s2.Max);}
  }

  public override IEnumerator GetEnumerator (){
    return new CompositeEnumerator(this);
  }

  private class CompositeEnumerator: IEnumerator{

    private IntSequence s1, s2;
    private int idx;   //  0: not started.  
                       //  1: s1 is current.  2:  s2 is current.
    private IEnumerator idxEnumerator;
 
    public CompositeEnumerator (IntCompSeq outerIntCompSeq){
      this.s1 = outerIntCompSeq.s1;
      this.s2 = outerIntCompSeq.s2;
      idx = 0;   // 0: outside.   1: at s1.   2: at s2
      idxEnumerator = null;
    }
 
    public Object Current{ 
     get {return idxEnumerator.Current;}
    }

    public bool MoveNext (){
       if (idx == 0){    // At start position.
         idx = 1;
         idxEnumerator = s1.GetEnumerator();
         return idxEnumerator.MoveNext();    
      } else if (idx == 1){      // At left sequence
         bool hasMoved1 = idxEnumerator.MoveNext();
         if (hasMoved1) 
           return true;
         else{
           idxEnumerator = s2.GetEnumerator(); idx = 2;
           return idxEnumerator.MoveNext();
        }
       } else if (idx == 2) {    // At right sequence
           bool hasMoved2 = idxEnumerator.MoveNext();
           if (hasMoved2) 
             return true;
           else return false;
       } else return false;
    }

    public void Reset(){
      idx = 0;         
    }
  }
}