package vectorpkg;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */

public class VectorAry implements Vector{
  private Object[] myList;
  private int myCt;
  private int myCapIncrement;

  /* Col array - For all of the methods in this class you will assume that col is an array
	is filled.  In other words col.length = number number of elements in the array 

     myCapIncrement will always be 0 except for the 1st constructor

     ensureCapacity should be called when adding element(s) to the array

  */

/******************* Constructors ******************************/
  //<1> construct an array with the specified initial capacity
//    and capacity increment
public VectorAry(int intCap, int capIncrement){
	myCt = intCap;
	myCapIncrement = capIncrement;
	myList = new Object[myCt];
  }

  //<2> construct an array with the specified initial capacity
  public VectorAry(int intCap){
	this(intCap, 0);
  }

  //<3> construct an empty array with a default capacity of 10
  public VectorAry(){
	this(10);
  }

  //<4> construct a array with objects copied from array col
  public VectorAry(Object[] col){
	this(col.length);
	int i;
	for(i=0; i<col.length; i++) {
		myList[i] = col[i];
	}
  }

  /********************* Accessors *************************/

  // return a string representation of the array that contains the
  // string representation of all the objects
  // Note: remember that some of the valid elements could be null. You will need
  //       to test for this and concatinate the string "null"
  public String toString(){
    String s = "";
    for(int k = 0; k < myCt; k++){
      if(myList[k] == null)
        s += " null ";
      else
        s += myList[k].toString() + " ";
    }
    return s;
  }

  // return the number of objects that CAN be stored in the array
  public int capacity(){
    return myList.length;
  }

  // return the number of objects in the list
  public int size(){
    return myCt;
  }

  // return a copy of the array.  The new array's capacity = myCt i.e. is filled
  public Object[] makeCopy(){
	int i;
	Object temp[] = new Object[myCt];
	for(i=0; i<myCt; i++) {
		temp[i] = myList[i];
	}
	return temp;
  }

  // return the object at position pos;
  // raise ArrayIndexOutOfBoundsException if pos < 0 or pos >= myCt
  public Object elementAt(int pos){
	if(pos < 0 || pos >= myCt) {
		throw new ArrayIndexOutOfBoundsException("Index was out of bounds.");
	}
	return myList[pos];
  }

  // return the first element in the array;
  // raise ArrayIndexOutOfBoundsException if the array is empty
  public Object firstElement(){
	if(isEmpty()) {
		throw new ArrayIndexOutOfBoundsException("Vector is empty.");
	}
	return myList[0];
  }

  // return true if the list contains no elements, false otherwise
  public boolean isEmpty(){
	if(myCt <= 0) {
		return true;
	}
	return false;
  }

  // return the last element in the array;
  // raise ArrayIndexOutOfBoundsException if the array is empty
  public Object lastElement(){
	if(isEmpty()) {
		throw new ArrayIndexOutOfBoundsException("Vector is empty.");
	}
	return myList[myCt - 1];
  }

  // return true if the current list and array col contain equal
  // objects in the same order
  public boolean equals(Object[] col){
	int i;
	if(col.length != myCt) {
		return false;
	}
	for(i=0; i<myCt; i++) {
		if(!myList[i].equals(col[i])) {
			return false;
		}
	}
	return true;
  }

// copy objects from myList to the object array objArr;
// throw IndexOutOfBoundsException if the array is not large enough
// to accommodate all objects from myList
  public void copyInto(Object[] objArr){
	int i;
	if(objArr.length < myCt) {
		throw new IndexOutOfBoundsException("objArr is not big enough.");
	}
	for(i=0; i<myCt; i++) {
		objArr[i] = myList[i];
	}
  }

// return the sublist of the list containing elements from first to last-1;
// The new list that is returned should only have the necessary capacity.
// raise ArrayIndexOutOfBoundsException if either first or last is out of range
// and IllegalArgumentException if last<=first
  public Object[] subList(int first, int last){

    return null;
  }

/************************** Modifiers ***************************************/

// remove all the objects from the list
public void clear() {
	myList = null;
	myCt = 0;
}

              /***************** Helpers ***********************/

// assign object obj to position pos and return the object that occupied this
// position before the assignment;
// raise ArrayIndexOutOfBoundsException if pos < 0 or pos >= myCt
  public Object setElementAt( Object obj, int pos){
    return null;
  }

// return the position of the first occurrence of object o in the list
// beginning the search at position pos; return -1 if 0 is not found of pos
// is invalid
public int indexOf(Object obj, int pos){
	int i;
	int(pos < 0 || pos >= myCt) {
		return -1;
	}
	for(i=0; i<myCt; i++) {
		if(myList[k].equals(obj)) {
			return k;
		}
	}
	return -1;
}

  //insert object obj at position pos after shifting elements at
  //positions following pos by one postion;
  //raise ArrayIndexOutOfBoundsException if pos > myCt or pos < 0
  public void addElement(int pos, Object obj){
	int i;
	ensureCapacity(myCt+1);
	for(i=myCt; i>pos; i--) {
		myList[i] = myList[i-1];
	}
	myList[i] = obj;
  }

  // return the position of the last occurrence of object obj in the list
  // beginning the backward search at position pos;
  // return -1 if o is not found
  public int lastIndexOf(Object obj, int pos){

    return -1;
  }

  // remove the object at position pos;
  // raise ArrayIndexOutOfBoundsException if pos <0 or pos >= myCt
  public Object removeElementAt(int pos){
	int i;
	Object temp = myList[pos];
	if(pos <  || pos >= myCt) {
		throw new ArrayIndexOutOfBoundsException();
	}
	for(i=pos; pos < myCt-1; pos++) {
		myList[i] = myList[i+1];
	}
	myCt--;
	return temp;
  }

// set size of the array to sz; if current size is less than sz,
// add new cells with null objects;  myCt remains the same
// if the current size is greater than sz, discard the overflowing objects
// if myCt > sz myCt should be changed to sz.
  public void setSize(int sz){

  }

// add all the elements from the array col at the position pos of the
// list after shifting the objects starting at position pos;
// raise ArrayIndexOutOfBoundsException if pos < 0 or pos > myCt
// return false if col is empty
  public boolean addAll(int pos, Object[] col){

    return false;
  }

              /******************************************/

  // insert object o at the end of the list.
   public void addElement(Object obj){
	addElement(myCt, obj);
  }

  // add all the elements from the array col to the end of the list;
  // return true if col is not empty
  public boolean addAll(Object[] col){
    return false;
  }

  // return the position of the first occurrence of object o in the list;
  // return -1 if o is not found
  public int indexOf(Object obj){
     return -1;
  }

  // return true if the list contains the object o
  public boolean contains(Object obj){
	return (indexOf(obj) > 0);
  }

  // return true if the list contains all of the objects in the array col
  public boolean containsAll(Object[] col){

    return false;
  }

  // return the position of the last occurrence of object o in the list
  // return -1 if o is not found
  public int lastIndexOf(Object obj){
    return -1;
  }

  // remove the first occurrence of o in the list and
  // return true if o was in the list
  public boolean removeElement(Object obj){
	int pos = indexOf(obj));
	if(pos >= 0) { 
		removeElementAt(pos);
		return true;
	}
	return false;
  }

  // remove from the list all the objects contained in array col;
  // return true if any element was removed.  This can be tricky if
  // you call removeElementAt which shifts the elements
  public boolean removeAll(Object[] col){
	
  }

  // remove objects starting at postion first and ending at last-1 and
  // then shift all the succeeding objects to fill the hole.
  // raise ArrayIndexOutOfBoundsException if either first or last is out of range
  // and IllegalArgumentException if last<=first
  public void removeRange(int first, int last){

  }

  // remove from the list all objects that are not in the array col;
  // return true if any object was removed
  public boolean retainAll(Object[] col){

    return false;
  }

  //change the capacity of the array to the number of objects currently
  //stored in it
  public void trimToSize(){
    setSize(myCt);
  }

  // extend the size of the array to accommodate at least minCap objects;
  // do nothing if the size of the array exceeds the minimum capacity minCap
  // if myCapIncrement = 0 the array should be doubled until mincap is
  //      reached or exceeded
  // if myCapIncrement != 0 the array should be increased in multiples
  //     of myCapIncrement. Example:  capacity = 3 myCapIncrement = 4
  //     to add 1 to 4 elements capacity should be 7. To add 5 to 8 elements
  //     the capacity should be 11
  public void ensureCapacity(int minCap){
    if(myList.length < minCap){
      int newSize;
      if(myCapIncrement != 0){
        int amtNeeded = minCap - myList.length;
        int size;
        if(amtNeeded % myCapIncrement == 0)
          size = amtNeeded/myCapIncrement * myCapIncrement;
        else
          size = (amtNeeded/myCapIncrement + 1) * myCapIncrement;
        newSize = size + myList.length;
      }
      else{
        int increment = 2;
        do{
          newSize = myList.length * increment;
          increment = increment + 2;
        }while ( newSize < minCap);
      }
        Object[] temp = new Object[newSize];
        for(int k = 0; k < myCt; k++){
          temp[k] = myList[k];
        }
        myList = temp;
      }
  }



}

