sclass IntBuffer implements Iterable { int[] data; int size; *() {} *(int size) { if (size != 0) data = new int[size]; } *(Iterable l) { if (l cast Cl) allocate(l.size()); addAll(l); } void add(int i) { if (size >= lIntArray(data)) { data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L)))); if (size >= data.length) fail("IntBuffer too large: " + size); } data[size++] = i; } void allocate(int n) { data = resizeIntArray(data, max(n, size())); } void setSize(int n) { data = resizeIntArray(data, n); size = min(l(data), size); } void addAll(Iterable l) { if (l != null) for (int i : l) add(i); } void addAll(int... l) { if (l != null) for (int i : l) add(i); } // Note: may return the internal array int[] toArray aka toIntArray() { ret size == 0 ? null : resizeIntArray(data, size); } int[] subArray(int from, int to) { ret subIntArray(data, from, min(to, size)); } int[] toArrayNonNull() { ret unnull(toArray()); } // abandoned version /*L toList() { ret intArrayToList(data, 0, size); }*/ // now all these return a virtual list L toList aka asList aka asVirtualList() { ret new RandomAccessAbstractList { public int size() { ret size; } public Int get(int i) { ret IntBuffer.this.get(i); } public Int set(int i, Int val) { Int a = get(i); data[i] = val; ret a; } }; } void reset { size = 0; } void clear { reset(); } int size() { ret size; } bool isEmpty() { ret size == 0; } int get(int idx) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); ret data[idx]; } void set(int idx, int value) { if (idx >= size) fail("Index out of range: " + idx + "/" + size); data[idx] = value; } int popLast() { if (size == 0) fail("empty buffer"); ret data[--size]; } int last() { ret data[size-1]; } int 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 Int next() { //if (!hasNext()) fail("Index out of bounds: " + i); ret data[i++]; } }; } public IntegerIterator integerIterator() { ret new IntegerIterator { 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 " + IntBuffer.this; } }; } void trimToSize { data = resizeIntArray(data, size); } }