int i = indexOfInsertion(key); if (i < 0) { // already contained i = -i - 1; int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); rehash(newCapacity); return put(key, value); int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); rehash(newCapacity);
/** * Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An * application can use this operation to minimize the storage of the receiver. */ @Override public void trimToSize() { // * 1.2 because open addressing's performance exponentially degrades beyond that point // so that even rehashing the table can take very long int newCapacity = nextPrime((int) (1 + 1.2 * size())); if (table.length > newCapacity) { rehash(newCapacity); } }
/** * Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called * automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water * mark. */ protected void rehash(int newCapacity) { int oldCapacity = table.length; //if (oldCapacity == newCapacity) return; int[] oldTable = table; byte[] oldValues = values; byte[] oldState = state; this.table = new int[newCapacity]; this.values = new byte[newCapacity]; this.state = new byte[newCapacity]; this.lowWaterMark = chooseLowWaterMark(newCapacity, this.minLoadFactor); this.highWaterMark = chooseHighWaterMark(newCapacity, this.maxLoadFactor); this.freeEntries = newCapacity - this.distinct; // delta for (int i = oldCapacity; i-- > 0;) { if (oldState[i] == FULL) { int element = oldTable[i]; int index = indexOfInsertion(element); this.table[index] = element; this.values[index] = oldValues[i]; this.state[index] = FULL; } } }
@Override public byte adjustOrPutValue(int key, byte newValue, byte incrValue) { int i = indexOfInsertion(key); if (i < 0) { //already contained i = -i - 1; this.values[i] += incrValue; return this.values[i]; } else { put(key, newValue); return newValue; } }
/** * Ensures that the receiver can hold at least the specified number of associations without needing to allocate new * internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver. <p> This * method never need be called; it is for performance tuning only. Calling this method before <tt>put()</tt>ing a * large number of associations boosts performance, because the receiver will grow only once instead of potentially * many times and hash collisions get less probable. * * @param minCapacity the desired minimum capacity. */ @Override public void ensureCapacity(int minCapacity) { if (table.length < minCapacity) { int newCapacity = nextPrime(minCapacity); rehash(newCapacity); } }
/** * Removes the given key with its associated element from the receiver, if present. * * @param key the key to be removed from the receiver. * @return <tt>true</tt> if the receiver contained the specified key, <tt>false</tt> otherwise. */ @Override public boolean removeKey(int key) { int i = indexOfKey(key); if (i < 0) { return false; } // key not contained this.state[i] = REMOVED; //this.values[i]=0; // delta this.distinct--; if (this.distinct < this.lowWaterMark) { int newCapacity = chooseShrinkCapacity(this.distinct, this.minLoadFactor, this.maxLoadFactor); rehash(newCapacity); } return true; }
@Test public void testClone() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); OpenIntByteHashMap map2 = (OpenIntByteHashMap) map.clone(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testCopy() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); OpenIntByteHashMap map2 = (OpenIntByteHashMap) map.copy(); map.clear(); assertEquals(1, map2.size()); }
int capacity = initialCapacity; super.setUp(capacity, minLoadFactor, maxLoadFactor); capacity = nextPrime(capacity); if (capacity == 0) { capacity = 1; this.highWaterMark = chooseHighWaterMark(capacity, this.maxLoadFactor);
@Test public void testEquals() { // since there are no other subclasses of // Abstractxxx available, we have to just test the // obvious. OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.removeKey((int) 13); OpenIntByteHashMap map2 = (OpenIntByteHashMap) map.copy(); assertEquals(map, map2); assertTrue(map2.equals(map)); assertFalse("Hello Sailor".equals(map)); assertFalse(map.equals("hello sailor")); map2.removeKey((int) 11); assertFalse(map.equals(map2)); assertFalse(map2.equals(map)); }
@Test public void testAdjustOrPutValue() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.adjustOrPutValue((int)11, (byte)1, (byte)3); assertEquals(25, map.get((int)11) ); map.adjustOrPutValue((int)15, (byte)1, (byte)3); assertEquals(1, map.get((int)15) ); }
@Test public void testKeysSortedByValue() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.removeKey((int) 13); IntArrayList keys = new IntArrayList(); map.keysSortedByValue(keys); int[] keysArray = keys.toArray(new int[keys.size()]); assertArrayEquals(new int[] {11, 12, 14}, keysArray ); }
@Test public void testForEachKey() { final IntArrayList keys = new IntArrayList(); OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.removeKey((int) 13); map.forEachKey(new IntProcedure() { @Override public boolean apply(int element) { keys.add(element); return true; } }); int[] keysArray = keys.toArray(new int[keys.size()]); Arrays.sort(keysArray); assertArrayEquals(new int[] {11, 12, 14}, keysArray ); }
@Test public void testValues() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.removeKey((int) 13); ByteArrayList values = new ByteArrayList(100); map.values(values); assertEquals(3, values.size()); values.sort(); assertEquals(22, values.get(0) ); assertEquals(23, values.get(1) ); assertEquals(25, values.get(2) ); }
@Test public void testForEachPair() { final List<Pair> pairs = new ArrayList<Pair>(); OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.removeKey((int) 13); map.forEachPair(new IntByteProcedure() { map.forEachPair(new IntByteProcedure() { int count = 0;
@Test public void testGet() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); assertEquals(22, map.get((int)11) ); assertEquals(0, map.get((int)0) ); }
@Test public void testContainsKey() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); assertTrue(map.containsKey((int) 11)); assertFalse(map.containsKey((int) 12)); }
@Test public void testPairsMatching() { IntArrayList keyList = new IntArrayList(); ByteArrayList valueList = new ByteArrayList(); OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); map.put((int) 12, (byte) 23); map.put((int) 13, (byte) 24); map.put((int) 14, (byte) 25); map.removeKey((int) 13); map.pairsMatching(new IntByteProcedure() { @Override public boolean apply(int first, byte second) { return (first % 2) == 0; }}, keyList, valueList); keyList.sort(); valueList.sort(); assertEquals(2, keyList.size()); assertEquals(2, valueList.size()); assertEquals(12, keyList.get(0) ); assertEquals(14, keyList.get(1) ); assertEquals(23, valueList.get(0) ); assertEquals(25, valueList.get(1) ); }
@Test public void testContainValue() { OpenIntByteHashMap map = new OpenIntByteHashMap(); map.put((int) 11, (byte) 22); assertTrue(map.containsValue((byte) 22)); assertFalse(map.containsValue((byte) 23)); }