@Test public void testPairsMatching() { DoubleArrayList keyList = new DoubleArrayList(); List<TestClass> valueList = new ArrayList<TestClass>(); OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem2); map.put((double) 12, anotherItem3); map.put((double) 13, anotherItem4); map.put((double) 14, anotherItem5); map.removeKey((double) 13); map.pairsMatching( new DoubleObjectProcedure<TestClass>() { @Override public boolean apply(double first, TestClass second) { return (first % 2) == 0; } }, keyList, valueList); keyList.sort(); Collections.sort(valueList); assertEquals(2, keyList.size()); assertEquals(2, valueList.size()); assertEquals(12, keyList.get(0), (double) 0.000001); assertEquals(14, keyList.get(1), (double) 0.000001); assertSame(anotherItem3, valueList.get(0)); assertSame(anotherItem5, valueList.get(1)); }
@Test public void testPairsMatching() { ShortArrayList keyList = new ShortArrayList(); DoubleArrayList valueList = new DoubleArrayList(); OpenShortDoubleHashMap map = new OpenShortDoubleHashMap(); map.put((short) 11, (double) 22); map.put((short) 12, (double) 23); map.put((short) 13, (double) 24); map.put((short) 14, (double) 25); map.removeKey((short) 13); map.pairsMatching( new ShortDoubleProcedure() { @Override public boolean apply(short first, double 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), (double) 0.000001); assertEquals(25, valueList.get(1), (double) 0.000001); }
/** * Fills all keys contained in the receiver into the specified list. Fills the list, starting at * index 0. After this call returns the specified list has a new size that equals * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by * method {@link #forEachKey(DoubleProcedure)}. * * <p>This method can be used to iterate over the keys of the receiver. * * @param list the list to be filled, can have any size. */ @Override public void keys(DoubleArrayList list) { list.setSize(distinct); double[] elements = list.elements(); int j = 0; for (int i = table.length; i-- > 0; ) { if (state[i] == FULL) { elements[j++] = table[i]; } } }
/** * Fills all pairs satisfying a given condition into the specified lists. Fills into the lists, * starting at index 0. After this call returns the specified lists both have a new size, the * number of pairs satisfying the condition. Iteration order is guaranteed to be <i>identical</i> * to the order used by method {@link #forEachKey(DoubleProcedure)}. * * <p><b>Example:</b> <br> * * <pre> * DoubleByteProcedure condition = new DoubleByteProcedure() { // match even values only * public boolean apply(double key, byte value) { return value%2==0; } * } * keys = (8,7,6), values = (1,2,2) --> keyList = (6,8), valueList = (2,1)</tt> * </pre> * * @param condition the condition to be matched. Takes the current key as first and the current * value as second argument. * @param keyList the list to be filled with keys, can have any size. * @param valueList the list to be filled with values, can have any size. */ @Override public void pairsMatching( DoubleByteProcedure condition, DoubleArrayList keyList, ByteArrayList valueList) { keyList.clear(); valueList.clear(); for (int i = table.length; i-- > 0; ) { if (state[i] == FULL && condition.apply(table[i], values[i])) { keyList.add(table[i]); valueList.add(values[i]); } } }
@Test public void testKeysSortedByValue() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem5); map.put((double) 12, anotherItem4); map.put((double) 13, anotherItem3); map.put((double) 14, anotherItem2); map.removeKey((double) 13); DoubleArrayList keys = new DoubleArrayList(); map.keysSortedByValue(keys); double[] keysArray = keys.toArray(new double[keys.size()]); assertArrayEquals(new double[] {14, 12, 11}, keysArray, (double) 0.000001); }
@Test public void testKeys() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, item); map.put((double) 12, item); DoubleArrayList keys = new DoubleArrayList(); map.keys(keys); keys.sort(); assertEquals(11, keys.get(0), (double) 0.000001); assertEquals(12, keys.get(1), (double) 0.000001); DoubleArrayList k2 = map.keys(); k2.sort(); assertEquals(keys, k2); }
@Test public void testValues() { OpenShortDoubleHashMap map = new OpenShortDoubleHashMap(); map.put((short) 11, (double) 22); map.put((short) 12, (double) 23); map.put((short) 13, (double) 24); map.put((short) 14, (double) 25); map.removeKey((short) 13); DoubleArrayList values = new DoubleArrayList(100); map.values(values); assertEquals(3, values.size()); values.sort(); assertEquals(22, values.get(0), (double) 0.000001); assertEquals(23, values.get(1), (double) 0.000001); assertEquals(25, values.get(2), (double) 0.000001); }
/** * Returns a string representation of the receiver, containing the String representation of each * key-value pair, sorted ascending by key. */ public String toString() { DoubleArrayList theKeys = keys(); // theKeys.sort(); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { double key = theKeys.get(i); buf.append(String.valueOf(key)); if (i < maxIndex) { buf.append(", "); } } buf.append(']'); return buf.toString(); }
@Test public void testPairsSortedByValue() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem5); map.put((double) 12, anotherItem4); map.put((double) 13, anotherItem3); map.put((double) 14, anotherItem2); DoubleArrayList keys = new DoubleArrayList(); List<TestClass> values = new ArrayList<TestClass>(); map.pairsSortedByValue(keys, values); assertEquals((double) 11, keys.get(3), (double) 0.000001); assertEquals(anotherItem5, values.get(3)); assertEquals((double) 12, keys.get(2), (double) 0.000001); assertEquals(anotherItem4, values.get(2)); assertEquals((double) 13, keys.get(1), (double) 0.000001); assertEquals(anotherItem3, values.get(1)); assertEquals((double) 14, keys.get(0), (double) 0.000001); assertEquals(anotherItem2, values.get(0)); }
/** * Fills all keys contained in the receiver into the specified list. Fills the list, starting at * index 0. After this call returns the specified list has a new size that equals * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by * method {@link #forEachKey(DoubleProcedure)}. * * <p>This method can be used to iterate over the keys of the receiver. * * @param list the list to be filled, can have any size. */ public void keys(final DoubleArrayList list) { list.clear(); forEachKey( new DoubleProcedure() { @Override public boolean apply(double key) { list.add(key); return true; } }); }
@Test public void testForEachKey() { final DoubleArrayList keys = new DoubleArrayList(); OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem); map.put((double) 12, anotherItem2); map.put((double) 13, anotherItem3); map.put((double) 14, anotherItem4); map.removeKey((double) 13); map.forEachKey( new DoubleProcedure() { @Override public boolean apply(double element) { keys.add(element); return true; } }); double[] keysArray = keys.toArray(new double[keys.size()]); Arrays.sort(keysArray); assertArrayEquals(new double[] {11, 12, 14}, keysArray, (double) 0.000001); }
@Test public void testPairsSortedByKey() { OpenShortDoubleHashMap map = new OpenShortDoubleHashMap(); map.put((short) 11, (double) 100); map.put((short) 12, (double) 70); map.put((short) 13, (double) 30); map.put((short) 14, (double) 3); ShortArrayList keys = new ShortArrayList(); DoubleArrayList values = new DoubleArrayList(); map.pairsSortedByKey(keys, values); assertEquals(4, keys.size()); assertEquals(4, values.size()); assertEquals((short) 11, keys.get(0)); assertEquals((double) 100, values.get(0), (double) 0.000001); assertEquals((short) 12, keys.get(1)); assertEquals((double) 70, values.get(1), (double) 0.000001); assertEquals((short) 13, keys.get(2)); assertEquals((double) 30, values.get(2), (double) 0.000001); assertEquals((short) 14, keys.get(3)); assertEquals((double) 3, values.get(3), (double) 0.000001); keys.clear(); values.clear(); map.pairsSortedByValue(keys, values); assertEquals((short) 11, keys.get(3)); assertEquals((double) 100, values.get(3), (double) 0.000001); assertEquals((short) 12, keys.get(2)); assertEquals((double) 70, values.get(2), (double) 0.000001); assertEquals((short) 13, keys.get(1)); assertEquals((double) 30, values.get(1), (double) 0.000001); assertEquals((short) 14, keys.get(0)); assertEquals((double) 3, values.get(0), (double) 0.000001); }