package util.jgp.container;
import java.util.NoSuchElementException;
import java.util.Enumeration;
import util.jgp.interfaces.Enumerable;
import util.jgp.interfaces.QuickSortable;
import util.jgp.adaptor.VectorEnumeration;
public class Vector implements Enumerable, QuickSortable {
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);
int from = theSize - 1;
int to = theSize;
while (to > ind)
elem[to--] = elem[from--];
++theSize;
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;
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);
}
}