// Has long indices!
persistable sclass SimpleCircularBuffer is Iterable, IntSize {
A[] buffer;
long base; // elements dropped due to overcapacity
int size; // elements actually contained
*(int capacity) {
buffer = (A[]) new O[capacity];
}
synchronized void add(A a) {
if (size == buffer.length) {
--size;
++base;
}
buffer[(int) ((base+size) % buffer.length)] = a;
++size;
}
synchronized A get(long pos) {
if (pos < base || pos >= base+size) null;
ret buffer[(int) (pos % buffer.length)];
}
synchronized A getFromBase(long pos) {
ret get(pos+base);
}
public synchronized int size() {
ret size;
}
public Iterator iterator() {
ret new ItIt {
long i;
public bool hasNext() { ret i < size(); }
public A next() { ret get(base + (i++)); }
};
}
synchronized int capacity() {
ret buffer.length;
}
synchronized bool isFull() {
ret size() == capacity();
}
public bool isEmpty() {
ret size() == 0;
}
synchronized long getBase aka base() {
ret base;
}
// pop first actually contained element
synchronized A popFirst aka remove() {
if (isEmpty()) null;
A a = get(base);
--size;
++base;
ret a;
}
synchronized A nextToLast() { ret get(base+size-2); }
synchronized A last() { ret get(base+size-1); }
synchronized A first() { ret get(base); }
L asList() {
ret main asList(this);
}
}