/** * Clears the receiver, then adds all (key,value) pairs of <tt>other</tt>values to it. * * @param other the other map to be copied into the receiver. */ public void assign(AbstractLongDoubleMap other) { clear(); other.forEachPair( new LongDoubleProcedure() { @Override public boolean apply(long key, double value) { put(key, value); return true; } } ); }
/** * Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result. * * @return a deep copy of the receiver. */ public AbstractLongDoubleMap copy() { return (AbstractLongDoubleMap) clone(); }
/** * Check the map for a key. If present, add an increment to the value. If absent, * store a specified value. * @param key the key. * @param newValue the value to store if the key is not currently in the map. * @param incrValue the value to be added to the current value in the map. **/ public double adjustOrPutValue(long key, double newValue, double incrValue) { boolean present = containsKey(key); if (present) { newValue = (double)(get(key) + incrValue); put(key, newValue); } else { put(key, newValue); } return newValue; } }
@Override public boolean apply(long key, double value) { return other.containsKey(key) && other.get(key) == value; } }
/** * Assigns the result of a function to each value; <tt>v[i] = function(v[i])</tt>. * * @param function a function object taking as argument the current association's value. */ public void assign(final DoubleFunction function) { copy().forEachPair( new LongDoubleProcedure() { @Override public boolean apply(long key, double value) { put(key, function.apply(value)); return true; } } ); }
/** * Returns a string representation of the receiver, containing the String representation of each key-value pair, * sorted ascending by key. */ public String toString() { LongArrayList theKeys = keys(); //theKeys.sort(); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { long key = theKeys.get(i); buf.append(String.valueOf(key)); buf.append("->"); buf.append(String.valueOf(get(key))); if (i < maxIndex) { buf.append(", "); } } buf.append(']'); return buf.toString(); }
if (other.size() != size()) { return false; forEachPair( new LongDoubleProcedure() { @Override other.forEachPair( new LongDoubleProcedure() { @Override
/** * Returns a list filled with all keys contained in the receiver. The returned list has a size that equals * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by method {@link * #forEachKey(LongProcedure)}. <p> This method can be used to iterate over the keys of the receiver. * * @return the keys. */ public LongArrayList keys() { LongArrayList list = new LongArrayList(size()); keys(list); return list; }
/** * Fills all keys <i>sorted ascending by their associated value</i> into the specified list. Fills into the list, * starting at index 0. After this call returns the specified list has a new size that equals <tt>this.size()</tt>. * Primary sort criterium is "value", secondary sort criterium is "key". This means that if any two values are equal, * the smaller key comes first. <p> <b>Example:</b> <br> <tt>keys = (8,7,6), values = (1,2,2) --> keyList = * (8,6,7)</tt> * * @param keyList the list to be filled, can have any size. */ public void keysSortedByValue(LongArrayList keyList) { pairsSortedByValue(keyList, new DoubleArrayList(size())); }
/** * Returns <tt>true</tt> if the receiver contains the specified key. * * @return <tt>true</tt> if the receiver contains the specified key. */ public boolean containsKey(final long key) { return !forEachKey( new LongProcedure() { @Override public boolean apply(long iterKey) { return key != iterKey; } } ); }
/** * Returns a string representation of the receiver, containing the String representation of each key-value pair, * sorted ascending by value. */ public String toStringByValue() { LongArrayList theKeys = new LongArrayList(); keysSortedByValue(theKeys); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { long key = theKeys.get(i); buf.append(String.valueOf(key)); buf.append("->"); buf.append(String.valueOf(get(key))); if (i < maxIndex) { buf.append(", "); } } buf.append(']'); return buf.toString(); }
@Override public boolean apply(long key) { list.add(get(key)); return true; } }
/** * Returns <tt>true</tt> if the receiver contains the specified value. * * @return <tt>true</tt> if the receiver contains the specified value. */ public boolean containsValue(final double value) { return !forEachPair( new LongDoubleProcedure() { @Override public boolean apply(long iterKey, double iterValue) { return (value != iterValue); } } ); }
@Override public boolean apply(long key, double value) { return containsKey(key) && get(key) == value; } }
/** * Fills all keys and values <i>sorted ascending by key</i> into the specified lists. Fills into the lists, starting * at index 0. After this call returns the specified lists both have a new size that equals <tt>this.size()</tt>. <p> * <b>Example:</b> <br> <tt>keys = (8,7,6), values = (1,2,2) --> keyList = (6,7,8), valueList = (2,2,1)</tt> * * @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. */ public void pairsSortedByKey(LongArrayList keyList, DoubleArrayList valueList) { keys(keyList); keyList.sort(); valueList.setSize(keyList.size()); for (int i = keyList.size(); --i >= 0;) { valueList.setQuick(i, get(keyList.getQuick(i))); } }
public int hashCode() { final int[] buf = new int[size()]; forEachPair( new LongDoubleProcedure() { int i = 0; @Override public boolean apply(long key, double value) { buf[i++] = HashUtils.hash(key) ^ HashUtils.hash(value); return true; } } ); Arrays.sort(buf); return IntBuffer.wrap(buf).hashCode(); }
/** * Returns a list filled with all keys contained in the receiver. The returned list has a size that equals * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by method {@link * #forEachKey(LongProcedure)}. <p> This method can be used to iterate over the keys of the receiver. * * @return the keys. */ public LongArrayList keys() { LongArrayList list = new LongArrayList(size()); keys(list); return list; }
/** * Fills all keys <i>sorted ascending by their associated value</i> into the specified list. Fills into the list, * starting at index 0. After this call returns the specified list has a new size that equals <tt>this.size()</tt>. * Primary sort criterium is "value", secondary sort criterium is "key". This means that if any two values are equal, * the smaller key comes first. <p> <b>Example:</b> <br> <tt>keys = (8,7,6), values = (1,2,2) --> keyList = * (8,6,7)</tt> * * @param keyList the list to be filled, can have any size. */ public void keysSortedByValue(LongArrayList keyList) { pairsSortedByValue(keyList, new DoubleArrayList(size())); }
/** * Assigns the result of a function to each value; <tt>v[i] = function(v[i])</tt>. * * @param function a function object taking as argument the current association's value. */ public void assign(final DoubleFunction function) { copy().forEachPair( new LongDoubleProcedure() { @Override public boolean apply(long key, double value) { put(key, function.apply(value)); return true; } } ); }