@Test public void testContainsKey() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); assertTrue(map.containsKey((int) 11)); assertFalse(map.containsKey((int) 12)); }
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);
@Override public int adjustOrPutValue(int key, int newValue, int 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; } }
/** * 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); } }
/** * 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; }
@Override protected int hashForProbe(byte[] originalForm, int dataSize, String name, int probe) { Preconditions.checkArgument(dataSize == this.dataSize, "dataSize argument [" + dataSize + "] does not match expected dataSize [" + this.dataSize + ']'); int originalHashcode = originalForm.hashCode(); if (caches[probe].containsKey(originalHashcode)) { return caches[probe].get(originalHashcode); } int hash = super.hashForProbe(originalForm, dataSize, name, probe); caches[probe].put(originalHashcode, hash); return hash; } }
@Test @Repeat(iterations = 20) public void testAgainstReferenceOpenIntIntHashMap() { OpenIntIntHashMap base = new OpenIntIntHashMap(); HashMap<Integer, Integer> reference = new HashMap<>(); assertEquals(true, base.put(k, v)); } else { assertEquals(prevValue.intValue(), base.get(k)); assertEquals(false, base.put(k, v)); assertEquals(reference.containsKey(k), base.containsKey(k)); assertEquals(false, base.removeKey(k)); } else { assertEquals(removed.intValue(), base.get(k)); assertEquals(true, base.removeKey(k)); assertEquals(reference.containsKey(k), base.containsKey(k)); break; base.clear(); break; assertEquals(reference.isEmpty(), base.isEmpty()); break; assertEquals(reference.size(), base.size()); break;
@Test public void testAdjustOrPutValue() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); map.put((int) 13, (int) 24); map.put((int) 14, (int) 25); map.adjustOrPutValue((int)11, (int)1, (int)3); assertEquals(25, map.get((int)11) ); map.adjustOrPutValue((int)15, (int)1, (int)3); assertEquals(1, map.get((int)15) ); }
@Test public void testCopy() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); OpenIntIntHashMap map2 = (OpenIntIntHashMap) map.copy(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testClone() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); OpenIntIntHashMap map2 = (OpenIntIntHashMap) map.clone(); map.clear(); assertEquals(1, map2.size()); }
/** * @param dictionaryFilePath * @param sigma * @return * @throws IOException */ public OpenIntIntHashMap getFListMap(String dictionaryFilePath, int sigma) throws IOException { OpenIntIntHashMap fListMap = new OpenIntIntHashMap(); int[] colsToLoad = new int[2]; colsToLoad[0] = Constants.DOC_FREQ; colsToLoad[1] = Constants.ITEM_ID; this.load(null, dictionaryFilePath, colsToLoad, sigma, -1); int[] itemIdList = this.itemIds; for(int itemId : itemIdList) { int support = this.docFreqs[this.posOf(itemId)]; fListMap.put(itemId, support); } return fListMap; } //END OF DICTIONARY CONSTRUCTION METHODS
@Test public void testGet() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); assertEquals(22, map.get((int)11) ); assertEquals(0, map.get((int)0) ); }
int nextNode = tree.getHeaderNext(currentAttribute); OpenIntIntHashMap prevNode = new OpenIntIntHashMap(); int justPrevNode = -1; while (nextNode != -1) { if (prevNode.containsKey(parent)) { int prevNodeId = prevNode.get(parent); if (tree.childCount(prevNodeId) <= 1 && tree.childCount(nextNode) <= 1) { tree.addCount(prevNodeId, tree.count(nextNode)); prevNode.put(parent, nextNode);
@Test public void testEquals() { // since there are no other subclasses of // Abstractxxx available, we have to just test the // obvious. OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); map.put((int) 13, (int) 24); map.put((int) 14, (int) 25); map.removeKey((int) 13); OpenIntIntHashMap map2 = (OpenIntIntHashMap) 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 testKeysSortedByValue() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); map.put((int) 13, (int) 24); map.put((int) 14, (int) 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(); OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); map.put((int) 13, (int) 24); map.put((int) 14, (int) 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 testForEachPair() { final List<Pair> pairs = new ArrayList<Pair>(); OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); map.put((int) 13, (int) 24); map.put((int) 14, (int) 25); map.removeKey((int) 13); map.forEachPair(new IntIntProcedure() { map.forEachPair(new IntIntProcedure() { int count = 0;
@Test public void testValues() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); map.put((int) 12, (int) 23); map.put((int) 13, (int) 24); map.put((int) 14, (int) 25); map.removeKey((int) 13); IntArrayList values = new IntArrayList(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 testContainValue() { OpenIntIntHashMap map = new OpenIntIntHashMap(); map.put((int) 11, (int) 22); assertTrue(map.containsValue((int) 22)); assertFalse(map.containsValue((int) 23)); }