@Override public long adjustOrPutValue(short 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; } }
@Test public void testAdjustOrPutValue() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.adjustOrPutValue((short)11, (long)1, (long)3); assertEquals(25, map.get((short)11) ); map.adjustOrPutValue((short)15, (long)1, (long)3); assertEquals(1, map.get((short)15) ); }
@Test public void testKeysSortedByValue() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.removeKey((short) 13); ShortArrayList keys = new ShortArrayList(); map.keysSortedByValue(keys); short[] keysArray = keys.toArray(new short[keys.size()]); assertArrayEquals(new short[] {11, 12, 14}, keysArray ); }
@Test public void testForEachKey() { final ShortArrayList keys = new ShortArrayList(); OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.removeKey((short) 13); map.forEachKey(new ShortProcedure() { @Override public boolean apply(short element) { keys.add(element); return true; } }); short[] keysArray = keys.toArray(new short[keys.size()]); Arrays.sort(keysArray); assertArrayEquals(new short[] {11, 12, 14}, keysArray ); }
final List<Pair> pairs = new ArrayList<Pair>(); OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.removeKey((short) 13); map.forEachPair(new ShortLongProcedure() {
int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); rehash(newCapacity); return put(key, value);
@Test public void testValues() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.removeKey((short) 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 testEquals() { // since there are no other subclasses of // Abstractxxx available, we have to just test the // obvious. OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.removeKey((short) 13); OpenShortLongHashMap map2 = (OpenShortLongHashMap) map.copy(); assertEquals(map, map2); assertTrue(map2.equals(map)); assertFalse("Hello Sailor".equals(map)); assertFalse(map.equals("hello sailor")); map2.removeKey((short) 11); assertFalse(map.equals(map2)); assertFalse(map2.equals(map)); }
@Test public void testClone() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); OpenShortLongHashMap map2 = (OpenShortLongHashMap) map.clone(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testContainsKey() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); assertTrue(map.containsKey((short) 11)); assertFalse(map.containsKey((short) 12)); }
@Test public void testContainValue() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); assertTrue(map.containsValue((long) 22)); assertFalse(map.containsValue((long) 23)); }
@Test public void testCopy() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); OpenShortLongHashMap map2 = (OpenShortLongHashMap) map.copy(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testPairsMatching() { ShortArrayList keyList = new ShortArrayList(); LongArrayList valueList = new LongArrayList(); OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 23); map.put((short) 13, (long) 24); map.put((short) 14, (long) 25); map.removeKey((short) 13); map.pairsMatching(new ShortLongProcedure() { @Override public boolean apply(short 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) ); }
@Test public void testKeys() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); map.put((short) 12, (long) 22); ShortArrayList keys = new ShortArrayList(); map.keys(keys); keys.sort(); assertEquals(11, keys.get(0) ); assertEquals(12, keys.get(1) ); ShortArrayList k2 = map.keys(); k2.sort(); assertEquals(keys, k2); }
@Test public void testClear() { OpenShortLongHashMap map = new OpenShortLongHashMap(); map.put((short) 11, (long) 22); assertEquals(1, map.size()); map.clear(); assertEquals(0, map.size()); assertEquals(0, map.get((short) 11), 0.0000001); }
@Override public long adjustOrPutValue(short 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; } }
@Override public long adjustOrPutValue(short 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; } }
int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); rehash(newCapacity); return put(key, value);