persistable sclass SynchronizedLongBufferStoredAsLinearInts is ILongBuffer { int[] data; int size; // affine conversion to long (long = int*factor+base) long base, factor; *(long *base, long *factor) {} *(long *base, long *factor, int size) { if (size != 0) data = new int[size]; } *(long *base, long *factor, Iterable l) { if (l cast Cl) allocate(l.size()); addAll(l); } public synchronized void addRaw(int i) { if (size >= lIntArray(data)) { data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L)))); if (size >= data.length) fail(shortClassName(this) + " too large: " + size); } data[size++] = i; } public synchronized void add(long i) { addRaw(compress(i)); } synchronized void allocate(int n) { data = resizeIntArray(data, max(n, size())); } public synchronized void addAll(Iterable l) { if (l != null) for (long i : l) add(i); } public synchronized long[] toArray() { if (size == 0) null; long[] out = new[size]; for i to size: out[i] = expand(data[i]); ret out; } /*synchronized L toList() { ret longArrayToList(data, 0, size); }*/ public synchronized L asVirtualList() { ret listFromFunction get(size); } synchronized void reset { size = 0; } void clear { reset(); } synchronized public int size() { ret size; } public synchronized bool isEmpty() { ret size == 0; } public synchronized int getRaw(int idx) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); ret data[idx]; } public synchronized long get(int idx) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); ret expand(data[idx]); } synchronized void set(int idx, long value) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); data[idx] = compress(value); } public synchronized long popLast() { if (size == 0) fail("empty buffer"); ret expand(data[--size]); } public synchronized long last() { ret expand(data[size-1]); } synchronized long nextToLast() { ret expand(data[size-2]); } toString { ret squareBracket(joinWithSpace(asVirtualList())); } public synchronized Iterator iterator() { ret new ItIt { int i = 0; public bool hasNext() { ret i < size(); } public Long next() { synchronized(SynchronizedLongBufferStoredAsLinearInts.this) { if (!hasNext()) fail("Index out of bounds: " + i); ret expand(data[i++]); } } }; } public synchronized void trimToSize { data = resizeIntArray(data, size); } synchronized void remove(int idx) { arraycopy(data, idx+1, data, idx, size-1-idx); --size; } // don't rely on return value if buffer is empty public synchronized long poll() { ret size == 0 ? -1 : expand(data[--size]); } long expand(int value) { ret base+value*factor; } int compress(long value) { ret toInt(ldiv_round(value-base, factor); } public synchronized void insertAt(int idx, long[] l) { int n = l(l); if (n == 0) ret; int[] newData = new[size+n]; arraycopy(data, 0, newData, 0, idx); for i to n: newData[idx+i] = compress(l[i]); arraycopy(data, idx, newData, idx+n, size-idx); data = newData; size = newData.length; } public void integrityCheck { assertTrue("Size positive", size >= 0); assertTrue("Data length", l(data) >= size); } }