@Override public long adjustOrPutValue(int 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() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 23); map.put((int) 13, (long) 24); map.put((int) 14, (long) 25); map.adjustOrPutValue((int)11, (long)1, (long)3); assertEquals(25, map.get((int)11) ); map.adjustOrPutValue((int)15, (long)1, (long)3); assertEquals(1, map.get((int)15) ); }
@Test public void testKeysSortedByValue() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 23); map.put((int) 13, (long) 24); map.put((int) 14, (long) 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(); OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 23); map.put((int) 13, (long) 24); map.put((int) 14, (long) 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 ); }
int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor); rehash(newCapacity); return put(key, value);
@Test public void testValues() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 23); map.put((int) 13, (long) 24); map.put((int) 14, (long) 25); map.removeKey((int) 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. OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 23); map.put((int) 13, (long) 24); map.put((int) 14, (long) 25); map.removeKey((int) 13); OpenIntLongHashMap map2 = (OpenIntLongHashMap) 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 testContainsKey() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); assertTrue(map.containsKey((int) 11)); assertFalse(map.containsKey((int) 12)); }
@Test public void testCopy() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); OpenIntLongHashMap map2 = (OpenIntLongHashMap) map.copy(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testClone() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); OpenIntLongHashMap map2 = (OpenIntLongHashMap) map.clone(); map.clear(); assertEquals(1, map2.size()); }
@Test public void testContainValue() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); assertTrue(map.containsValue((long) 22)); assertFalse(map.containsValue((long) 23)); }
@Test public void testKeys() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 22); IntArrayList keys = new IntArrayList(); map.keys(keys); keys.sort(); assertEquals(11, keys.get(0) ); assertEquals(12, keys.get(1) ); IntArrayList k2 = map.keys(); k2.sort(); assertEquals(keys, k2); }
@Test public void testPairsMatching() { IntArrayList keyList = new IntArrayList(); LongArrayList valueList = new LongArrayList(); OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); map.put((int) 12, (long) 23); map.put((int) 13, (long) 24); map.put((int) 14, (long) 25); map.removeKey((int) 13); map.pairsMatching(new IntLongProcedure() { @Override public boolean apply(int 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 testClear() { OpenIntLongHashMap map = new OpenIntLongHashMap(); map.put((int) 11, (long) 22); assertEquals(1, map.size()); map.clear(); assertEquals(0, map.size()); assertEquals(0, map.get((int) 11), 0.0000001); }
@Override public long adjustOrPutValue(int 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(int 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 protected void setup(Context context) throws IOException, InterruptedException { super.setup(context); Configuration conf = context.getConfiguration(); //Path[] localFiles = HadoopUtil.getCachedFiles(conf); maxDf = conf.getLong(HighDFWordsPruner.MAX_DF, Long.MAX_VALUE); minDf = conf.getLong(HighDFWordsPruner.MIN_DF, -1); Path dictionaryFile = HadoopUtil.getSingleCachedFile(conf); // key is feature, value is the document frequency for (Pair<IntWritable, LongWritable> record : new SequenceFileIterable<IntWritable, LongWritable>(dictionaryFile, true, conf)) { dictionary.put(record.getFirst().get(), record.getSecond().get()); } } }