/** * Returns a deep copy of the receiver; uses <code>clone()</code> and casts the result. * * @return a deep copy of the receiver. */ public AbstractIntIntMap copy() { return (AbstractIntIntMap) 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 int adjustOrPutValue(int key, int newValue, int incrValue) { boolean present = containsKey(key); if (present) { newValue = (int)(get(key) + incrValue); put(key, newValue); } else { put(key, newValue); } return newValue; } }
/** * 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 int key) { return !forEachKey( new IntProcedure() { @Override public boolean apply(int iterKey) { return key != iterKey; } } ); }
@Override public boolean apply(int key, int value) { return containsKey(key) && get(key) == value; } }
/** * 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(IntProcedure)}. <p> This method can be used to iterate over the keys of the receiver. * * @return the keys. */ public IntArrayList keys() { IntArrayList list = new IntArrayList(size()); keys(list); return list; }
/** * Returns a string representation of the receiver, containing the String representation of each key-value pair, * sorted ascending by key. */ public String toString() { IntArrayList theKeys = keys(); //theKeys.sort(); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { int 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 IntIntProcedure() { @Override other.forEachPair( new IntIntProcedure() { @Override
/** * 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(IntArrayList keyList) { pairsSortedByValue(keyList, new IntArrayList(size())); }
/** * Returns a string representation of the receiver, containing the String representation of each key-value pair, * sorted ascending by value. */ public String toStringByValue() { IntArrayList theKeys = new IntArrayList(); keysSortedByValue(theKeys); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { int 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(int key) { return procedure.apply(key, get(key)); } }
/** * 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 int value) { return !forEachPair( new IntIntProcedure() { @Override public boolean apply(int iterKey, int iterValue) { return (value != iterValue); } } ); }
final protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor) { int capacity = initialCapacity; super.setUp(capacity, minLoadFactor, maxLoadFactor); capacity = nextPrime(capacity); if (capacity == 0) {
@Override public boolean apply(int key, int value) { return other.containsKey(key) && other.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(IntArrayList keyList, IntArrayList 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 IntIntProcedure() { int i = 0; @Override public boolean apply(int key, int 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(IntProcedure)}. <p> This method can be used to iterate over the keys of the receiver. * * @return the keys. */ public IntArrayList keys() { IntArrayList list = new IntArrayList(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(IntArrayList keyList) { pairsSortedByValue(keyList, new IntArrayList(size())); }
/** * Returns a string representation of the receiver, containing the String representation of each key-value pair, * sorted ascending by value. */ public String toStringByValue() { IntArrayList theKeys = new IntArrayList(); keysSortedByValue(theKeys); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { int 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(); }