class OList {
  final int dataInc=100;  // array size increment

  static public void main(String [] args) {
    System.out.println("Compiled and running!");

/*
    OList o=new OList();
    o.insert(new LString("Jeff"));
    o.insert(new LString("Eileen"));
    o.insert(new LString("Julie"));
    o.insert(new LString("Rachel"));
    o.insert(new LString("Dave"));
    System.out.println("OList:"+o);

    LObject l;
    OListIt it=new OListIt(o);
    it.reset();
    while((l=it.next())!=null) {
      System.out.println("entry:"+l);
    }
*/
  }

  public void clear() {
    myLen=0;
  }

  public int entries() {
    return myLen;
  }

  public boolean insert(LObject o) {
    int begin=0;
    int end=myLen-1;
    int check=0;
    int comp;

    // find place to put new entry
    if(dataLen<=myLen) {
      dataLen=dataLen+dataInc;
      LObject [] tempD=new LObject[dataLen];
      int [] tempI=new int[dataLen];
      if(data!=null) {
        for(int i=0;i<myLen;i++) {
          tempD[i]=data[i];
          tempI[i]=index[i];
        }
      }
      data=tempD;
      index=tempI;
    }

    // add first entry
    if(end<0) {
      myLen++;
      data[0]=o;
      return true;
    }
      
    // find proper position
    boolean done=false;
    while(!done) {  
      check=(begin+end)/2;
      comp=o.compareTo(data[index[check]]);
      if(comp==0) {  // already exists
        return false;
      }
      else if(comp<0) {  // is less than
        end=check-1;
        if(end<begin)
          done=true;  
      }
      else {  // is more than
        begin=check+1;
        if(end<begin) {
          done=true;
          check++;
        }
      }
    }  //while

    // make room for new entry in index
    for(int i=myLen;i>check;i--) {
      index[i]=index[i-1];
    }

    // store entry
    index[check]=myLen;
    data[myLen]=o;
    myLen++;
    return true;
  }

  public LObject get(int n) {
    if(n>=0 && n<myLen)
      return data[index[n]];
    else
      return null;
  }

  public int findIndex(LObject o) {
    int begin=0;
    int end=myLen-1;
    int check;
    int comp;

    while(true) {  
      check=(begin+end)/2;
      comp=o.compareTo(data[index[check]]);
      if(comp==0) {  // already exists
        return check;
      }
      else if(comp<0) {  // is less than
        end=check-1;
        if(end<begin)
          return -1;  
      }
      else {  // is more than
        begin=check+1;
        if(end<begin) 
          return -1;
      }
    }  //while
  }

  public boolean remove(LObject o) {
    int a=findIndex(o);
    int b=index[a];

    if(a>-1) {
      myLen--;
      
      // move data over
      for(int i=b;i<myLen;i++) {
        data[i]=data[i+1];
      }

      // move index over
      for(int i=a;i<myLen;i++) {
        index[i]=index[i+1];
      }

      // readjust index pointers
      for(int i=0;i<myLen;i++) {
        if(index[i]>b)
          index[i]--;
      }

      return true;
    }
    else return false;
  }

  public String toString() {
    String result="Length-"+Integer.toString(myLen)+"(";
    for(int i=0;i<myLen;i++) 
      result=result+data[index[i]].toString()+",";
    result=result+")";
    return result;
  }

  int myLen=0;
  int dataLen=0;
  LObject [] data;
  int [] index;
}
