private void ensureCapacity(int minCapacity) { // adapted from java.util.ArrayList.ensureCapacity(int) modCount++; int oldCapacity = array.length()[0]; if (minCapacity > oldCapacity) { MultiDimensionalArray<V> oldData = array; int newCapacity = (oldCapacity * 3) / 2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: array = new SimplePackedArray<V>(newCapacity, array.length()[1]); MultiDimensionalArrays.copy(oldData, array); } }
@Override public void add(int index, Map<K, V> element) { if (index < 0 || index > size) throw new IndexOutOfBoundsException(); if (element == null) throw new NullPointerException(); ensureCapacity(size + 1); size++; try { MultiDimensionalArrays.copy(array, new int[] {index, 0}, array, new int[] {index + 1, 0}); set(index, element); } catch (RuntimeException e) { size--; throw e; } }
@SuppressWarnings("unchecked") @Override public Map<K, V> remove(int index) { if (index < 0 || index >= size()) throw new IndexOutOfBoundsException(); modCount++; Map<K, V> previous = get(index); int numMoved = size - index - 1; if (numMoved > 0) MultiDimensionalArrays.copy( array, new int[] {index + 1, 0}, array, new int[] {index, 0}, new int[] {numMoved, keys.length}); set(size - 1, (Map<K, V>) Collections.emptyMap()); size--; return previous; }