/** * Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is * already less, nothing is done. If the map contains more items than the specified capacity, * nothing is done. */ public void shrink(int maximumCapacity) { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; maximumCapacity = ObjectMap.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); }
/** * Creates a new map with the specified initial capacity and load factor. This map will hold * initialCapacity * loadFactor items before growing the backing table. */ public IdentityMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); if (capacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = ObjectMap.nextPowerOfTwo(initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("loadFactor must be > 0: " + loadFactor); this.loadFactor = loadFactor; threshold = (int) (capacity * loadFactor); mask = capacity - 1; hashShift = 31 - Integer.numberOfTrailingZeros(capacity); stashCapacity = Math.max(3, (int) Math.ceil(Math.log(capacity)) * 2); pushIterations = Math.max(Math.min(capacity, 8), (int) Math.sqrt(capacity) / 8); keyTable = (K[]) new Object[capacity + stashCapacity]; valueTable = (V[]) new Object[keyTable.length]; }
/** * Increases the size of the backing array to acommodate the specified number of additional items. * Useful before adding many items to avoid multiple backing array resizes. */ public void ensureCapacity(int additionalCapacity) { int sizeNeeded = size + additionalCapacity; if (sizeNeeded >= threshold) resize(ObjectMap.nextPowerOfTwo((int) (sizeNeeded / loadFactor))); }