final sclass SimpleStack extends RandomAccessAbstractList {
new ArrayList l;
int peak;
public A get(int i) { ret l.get(i); }
public int size() { ret l.size(); }
public A set(int i, A a) { ret l.set(i, a); }
public void add(int i, A a) { l.add(i, a); checkSize(); }
public A remove(int i) { A a = l.remove(i); checkSize(); ret a; }
void checkSize {
int n = size();
if (n > peak) peak = n; // record new peak size (~actual capacity)
if (n*2 < peak) { // shrink physically when below half of peak
ifdef SimpleStack_debug
print("Shrinking SimpleStack: " + peak + " => " + n);
endifdef
trimToSize(l);
peak = n;
}
}
}