sclass DoubleBuffer is Iterable, IntSize { double[] data; int size; *() {} *(int size) { if (size != 0) data = new double[size]; } *(Iterable l) { addAll(l); } *(Cl l) { this(l(l)); addAll(l); } *(double... data) { this.data = data; size = l(data); } void add(double i) { if (size >= lDoubleArray(data)) { data = resizeDoubleArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lDoubleArray(data)*2L)))); if (size >= data.length) fail("DoubleBuffer too large: " + size); } data[size++] = i; } void addAll(Iterable l) { if (l != null) for (double i : l) add(i); } double[] toArray() { ret size == 0 ? null : resizeDoubleArray(data, size); } double[] toArrayNonNull() { ret unnull(toArray()); } L toList() { ret doubleArrayToList(data, 0, size); } L asVirtualList() { ret new RandomAccessAbstractList { public int size() { ret size; } public Double get(int i) { ret DoubleBuffer.this.get(i); } public Double set(int i, Double val) { Double a = get(i); data[i] = val; ret a; } }; } void reset { size = 0; } void clear { reset(); } public int size() { ret size; } double get(int idx) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); ret data[idx]; } void set(int idx, double value) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); data[idx] = value; } double popLast() { if (size == 0) fail("empty buffer"); ret data[--size]; } double last() { ret data[size-1]; } double nextToLast() { ret data[size-2]; } toString { ret squareBracket(joinWithSpace(toList())); } public Iterator iterator() { ret new ItIt { int i = 0; public bool hasNext() { ret i < size; } public Double next() { //if (!hasNext()) fail("Index out of bounds: " + i); ret data[i++]; } }; } /*public DoubleIterator doubleIterator() { ret new DoubleIterator { int i = 0; public bool hasNext() { ret i < size; } public int next() { //if (!hasNext()) fail("Index out of bounds: " + i); ret data[i++]; } toString { ret "Iterator@" + i + " over " + DoubleBuffer.this; } }; }*/ void trimToSize { data = resizeDoubleArray(data, size); } int indexOf(double b) { for i to size: if (data[i] == b) ret i; ret -1; } double[] subArray(int start, int end) { ret subDoubleArray(data, start, min(end, size)); } }