public void put(K key, V value) { if (size == keys.length) resize(Math.max(8, (int) (size * 1.75f))); int index = indexOfKey(key); if (index == -1) index = size++; keys[index] = key; values[index] = value; }
/** * Clears the map and reduces the size of the backing arrays to be the specified capacity if they * are larger. */ public void clear(int maximumCapacity) { if (keys.length <= maximumCapacity) { clear(); return; } size = 0; resize(maximumCapacity); }
public void putAll(ArrayMap map, int offset, int length) { if (offset + length > map.size) throw new IllegalArgumentException( "offset + length must be <= size: " + offset + " + " + length + " <= " + map.size); int sizeNeeded = size + length - offset; if (sizeNeeded >= keys.length) resize(Math.max(8, (int) (sizeNeeded * 1.75f))); System.arraycopy(map.keys, offset, keys, size, length); System.arraycopy(map.values, offset, values, size, length); size += length; }
public void put(K key, V value, int index) { if (size == keys.length) resize(Math.max(8, (int) (size * 1.75f))); int existingIndex = indexOfKey(key); if (existingIndex != -1) removeIndex(existingIndex); System.arraycopy(keys, index, keys, index + 1, size - index); System.arraycopy(values, index, values, index + 1, size - index); keys[index] = key; values[index] = value; size++; }
public void insert(int index, K key, V value) { if (index > size) throw new IndexOutOfBoundsException(String.valueOf(index)); if (size == keys.length) resize(Math.max(8, (int) (size * 1.75f))); if (ordered) { System.arraycopy(keys, index, keys, index + 1, size - index); System.arraycopy(values, index, values, index + 1, size - index); } else { keys[size] = keys[index]; values[size] = values[index]; } size++; keys[index] = key; values[index] = value; }
/** * Increases the size of the backing arrays to acommodate the specified number of additional * entries. Useful before adding many entries to avoid multiple backing array resizes. */ public void ensureCapacity(int additionalCapacity) { int sizeNeeded = size + additionalCapacity; if (sizeNeeded >= keys.length) resize(Math.max(8, sizeNeeded)); }
/** * Reduces the size of the backing arrays to the size of the actual number of entries. This is * useful to release memory when many items have been removed, or if it is known that more entries * will not be added. */ public void shrink() { if (keys.length == size) return; resize(size); }