jmud-0.11/
jmud-0.11/bin/
jmud-0.11/doc/
jmud-0.11/rec/
jmud-0.11/rec/mun/
jmud-0.11/rec/mun/grecia/
jmud-0.11/rec/mun/gunnar/
jmud-0.11/rec/qua/
jmud-0.11/src/bool/
jmud-0.11/src/clone/
jmud-0.11/src/integer/
jmud-0.11/src/misc/
jmud-0.11/src/string/
jmud-0.11/src/util/bit/
jmud-0.11/src/util/color/
jmud-0.11/src/util/file/
jmud-0.11/src/util/jgp/adaptor/
jmud-0.11/src/util/jgp/algorithm/
jmud-0.11/src/util/jgp/container/
jmud-0.11/src/util/jgp/functor/
jmud-0.11/src/util/jgp/interfaces/
jmud-0.11/src/util/jgp/predicate/
jmud-0.11/src/util/log/
jmud-0.11/src/util/state/
jmud-0.11/trash/
package util.list;

import java.util.NoSuchElementException;
import java.util.Enumeration;

public class Vector implements Enumerable {

  private Object elem[]  = null;
  private int    theSize = 0;

  public Vector() {
  }

  public Vector(int cap) {
    changeCapacity(cap);
  }

  public Vector(Enumerable n) {
    int cap = 0;
    for (Enumeration enum = n.elements(); enum.hasMoreElements(); )
      ++cap;

    changeCapacity(cap);

    for (Enumeration enum = n.elements(); enum.hasMoreElements(); )
      insert(enum.nextElement());
  }

  public int getSize() {
    return theSize;
  }

  public int getCapacity() {
    return elem == null ? 0 : elem.length;
  }

  public void removeAll() {
    elem = null;
    theSize = 0;
  }

  private void changeCapacity(int newCap) {
    if (newCap == 0) {
      removeAll();
      return;
    }

    Object newElem[] = new Object[newCap];

    if (theSize > newCap)
      theSize = newCap;

    for (int i = 0; i < getSize(); ++i)
      newElem[i] = elem[i];
    elem = newElem;
  }

  public void insert(Object obj) {
    int cap = getCapacity();

    if (getSize() >= cap)
      changeCapacity(cap == 0 ? 1 : cap << 1);

    elem[theSize++] = obj;
  }

    public void insertAt(int ind, Object obj) 
	throws ArrayIndexOutOfBoundsException {
	if (ind >= theSize)
	    throw new ArrayIndexOutOfBoundsException(ind);
	
	int cap = getCapacity();

	if (getSize() >= cap)
	    changeCapacity(cap == 0 ? 1 : cap << 1);

	++theSize;

	int from = ind;
	int to   = ind + 1;
	while (to < theSize)
	    elem[to++] = elem[from++];

	elem[ind] = obj;
    }

  public void append(Vector v) {
    if (v == this)
      v = new Vector(v);

    int cap = getSize() + v.getSize();

    if (cap > getCapacity())
      changeCapacity(cap);

    for (Enumeration enum = v.elements(); enum.hasMoreElements(); )
      insert(enum.nextElement());
  }

  public boolean isEmpty() {
    return theSize == 0;
  }

  public Object remove() throws NoSuchElementException {
    if (isEmpty())
      throw new NoSuchElementException();

    return elem[--theSize];
  }

    public Object removeFrom(int ind) 
	throws ArrayIndexOutOfBoundsException {
	if (ind >= theSize)
	    throw new ArrayIndexOutOfBoundsException(ind);

	Object obj = elem[ind];

	int to   = ind;
	int from = ind + 1;
	while (from < theSize)
	    elem[to++] = elem[from++];
	--theSize;

	/*
	--theSize;
	int from = theSize;
	int to   = theSize - 1;

	while (from > ind)
	    elem[to--] = elem[from--];
	*/

	return obj;
    }

  public void trim() {
    changeCapacity(getSize());
  }

  public Object getElementAt(int index) throws ArrayIndexOutOfBoundsException {
    if (index < 0 || index >= getSize())
      throw new ArrayIndexOutOfBoundsException(index);

    return elem[index];
  }

  public void setElementAt(int index, Object obj) 
    throws ArrayIndexOutOfBoundsException {
    if (index < 0 || index >= getSize())
      throw new ArrayIndexOutOfBoundsException(index);

    elem[index] = obj;
  }

  public Enumeration elements() {
    return new VectorEnumeration(this);
  }
}