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