public VArrayList(int initialCapacity) { array = new VBox<VArray<E>>(new VArray<E>(initialCapacity)); size = new VBoxInt(); }
private Cons<GarbageCollectable> doWriteback(int newTxNumber) { GarbageCollectable newLogNode = array.commit(newTxNumber, writesToCommit, logEntryIndexes); return Cons.<GarbageCollectable> empty().cons(newLogNode); } }
private void resize(int newCapacity) { // To resize the array, we create a new one and copy over the elements from the old one // As this operation is very time-sensitive, this implementation uses inside knowledge of // VArray to go faster int size = size(); VArray<E> oldVArray = array(); VArray<E> newVArray = new VArray<E>(newCapacity); // Get the AtomicReferenceArray from the underlying VArray AtomicReferenceArray<E> realArray = newVArray.values; // Copy elements over for (int i = 0; i < size; i++) { realArray.lazySet(i, oldVArray.get(i)); } // Update VBox this.array.put(newVArray); }
public void put(E newE, int ... coordinates) { array.put(coordinatesToIndex(coordinates), newE); }
public E get(int ... coordinates) { return array.get(coordinatesToIndex(coordinates)); }
@SuppressWarnings("static-access") public void put(int index, E newE) { rangeCheck(index); Transaction tx = Transaction.current(); if (tx == null) { tx = Transaction.begin(); tx.setArrayValue(new VArrayEntry<E>(this, index), newE); tx.commit(); } else { tx.setArrayValue(new VArrayEntry<E>(this, index), newE); } }
@Override @Atomic(speculativeReadOnly = false) public boolean addAll(Collection<? extends E> c) { if (c.size() == 0) { return false; } int size = size(); ensureCapacity(size + c.size()); VArray<E> array = array(); // Must be done AFTER ensureCapacity! for (E element : c) { array.put(size++, element); } this.size.putInt(size); return true; }
@Override public E get(int index) { if (index >= size()) { throw new IndexOutOfBoundsException(); } return array().get(index); }
@SuppressWarnings("static-access") public E get(int index) { rangeCheck(index); // TODO: Apply the same optimization as in VBox.get() Transaction tx = Transaction.current(); if (tx == null) { tx = Transaction.begin(true); E value = tx.getArrayValue(new VArrayEntry<E>(this, index)); tx.commit(); return value; } else { return tx.getArrayValue(new VArrayEntry<E>(this, index)); } }
@Override @Atomic(speculativeReadOnly = false) public void add(int index, E element) { int size = size(); if (index < 0 || index > size) { throw new IndexOutOfBoundsException(); } ensureCapacity(size + 1); VArray<E> array = array(); // Must be done AFTER ensureCapacity! for (int i = size - 1; i >= index; i--) { array.put(i + 1, array.get(i)); } array.put(index, element); this.size.put(size + 1); }
@Override @Atomic(speculativeReadOnly = true) public boolean add(E e) { int size = size(); ensureCapacity(size + 1); array().put(size, e); this.size.putInt(size + 1); return true; }
public VMultiArray(int ... dimensions) { if (dimensions.length <= 1) { throw new UnsupportedOperationException("Cannot create multidimensional array with less than two dimensions"); } int size = 1; for (int i : dimensions) { if (i <= 0) throw new NegativeArraySizeException(); size *= i; } this.array = new VArray<E>(size); this.dimensions = dimensions.clone(); // Keep our copy private, to avoid unpleasantries }
@SuppressWarnings("unchecked") @Override @Atomic(readOnly = true) public <T> T[] toArray(T[] a) { VArray<E> array = array(); int size = size(); if (a.length < size) { // Need to create a bigger array a = (T[]) Array.newInstance(a.getClass().getComponentType(), size); } for (int i = 0; i < size; i++) { a[i] = (T) array.get(i); } if (a.length > size) { a[size] = null; } return a; }
@Override @Atomic(speculativeReadOnly = false) public void clear() { // Let the GC do its job array.put(new VArray<E>(array().length)); size.putInt(0); }