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; long[] oldTable = table; short[] oldValues = values; byte[] oldState = state; this.table = new long[newCapacity]; this.values = new short[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) { long element = oldTable[i]; int index = indexOfInsertion(element); this.table[index] = element; this.values[index] = oldValues[i]; this.state[index] = FULL; } } }
@Override public short adjustOrPutValue(long key, short newValue, short 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(long 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() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); OpenLongShortHashMap map2 = (OpenLongShortHashMap) map.clone(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testCopy() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); OpenLongShortHashMap map2 = (OpenLongShortHashMap) 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. OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.removeKey((long) 13); OpenLongShortHashMap map2 = (OpenLongShortHashMap) map.copy(); assertEquals(map, map2); assertTrue(map2.equals(map)); assertFalse("Hello Sailor".equals(map)); assertFalse(map.equals("hello sailor")); map2.removeKey((long) 11); assertFalse(map.equals(map2)); assertFalse(map2.equals(map)); }
@Test public void testAdjustOrPutValue() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.adjustOrPutValue((long)11, (short)1, (short)3); assertEquals(25, map.get((long)11) ); map.adjustOrPutValue((long)15, (short)1, (short)3); assertEquals(1, map.get((long)15) ); }
@Test public void testKeysSortedByValue() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.removeKey((long) 13); LongArrayList keys = new LongArrayList(); map.keysSortedByValue(keys); long[] keysArray = keys.toArray(new long[keys.size()]); assertArrayEquals(new long[] {11, 12, 14}, keysArray ); }
@Test public void testForEachKey() { final LongArrayList keys = new LongArrayList(); OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.removeKey((long) 13); map.forEachKey(new LongProcedure() { @Override public boolean apply(long element) { keys.add(element); return true; } }); long[] keysArray = keys.toArray(new long[keys.size()]); Arrays.sort(keysArray); assertArrayEquals(new long[] {11, 12, 14}, keysArray ); }
@Test public void testValues() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.removeKey((long) 13); ShortArrayList values = new ShortArrayList(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>(); OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.removeKey((long) 13); map.forEachPair(new LongShortProcedure() { map.forEachPair(new LongShortProcedure() { int count = 0;
@Test public void testGet() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); assertEquals(22, map.get((long)11) ); assertEquals(0, map.get((long)0) ); }
@Test public void testContainsKey() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); assertTrue(map.containsKey((long) 11)); assertFalse(map.containsKey((long) 12)); }
@Test public void testPairsMatching() { LongArrayList keyList = new LongArrayList(); ShortArrayList valueList = new ShortArrayList(); OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); map.put((long) 12, (short) 23); map.put((long) 13, (short) 24); map.put((long) 14, (short) 25); map.removeKey((long) 13); map.pairsMatching(new LongShortProcedure() { @Override public boolean apply(long first, short 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() { OpenLongShortHashMap map = new OpenLongShortHashMap(); map.put((long) 11, (short) 22); assertTrue(map.containsValue((short) 22)); assertFalse(map.containsValue((short) 23)); }