📄 tdoubledoublehashmap.java
字号:
forEachEntry(p); return p.getHashCode(); } private final class HashProcedure implements TDoubleDoubleProcedure { private int h = 0; public int getHashCode() { return h; } public final boolean execute(double key, double value) { h += (_hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value)); return true; } } private static final class EqProcedure implements TDoubleDoubleProcedure { private final TDoubleDoubleHashMap _otherMap; EqProcedure(TDoubleDoubleHashMap otherMap) { _otherMap = otherMap; } public final boolean execute(double key, double value) { int index = _otherMap.index(key); if (index >= 0 && eq(value, _otherMap.get(key))) { return true; } return false; } /** * Compare two doubles for equality. */ private final boolean eq(double v1, double v2) { return v1 == v2; } } /** * removes the mapping at <tt>index</tt> from the map. * * @param index an <code>int</code> value */ protected void removeAt(int index) { super.removeAt(index); // clear key, state; adjust size _values[index] = (double)0; } /** * Returns the values of the map. * * @return a <code>Collection</code> value */ public double[] getValues() { double[] vals = new double[size()]; double[] v = _values; byte[] states = _states; for (int i = v.length, j = 0; i-- > 0;) { if (states[i] == FULL) { vals[j++] = v[i]; } } return vals; } /** * returns the keys of the map. * * @return a <code>Set</code> value */ public double[] keys() { double[] keys = new double[size()]; double[] k = _set; byte[] states = _states; for (int i = k.length, j = 0; i-- > 0;) { if (states[i] == FULL) { keys[j++] = k[i]; } } return keys; } /** * checks for the presence of <tt>val</tt> in the values of the map. * * @param val an <code>double</code> value * @return a <code>boolean</code> value */ public boolean containsValue(double val) { byte[] states = _states; double[] vals = _values; for (int i = vals.length; i-- > 0;) { if (states[i] == FULL && val == vals[i]) { return true; } } return false; } /** * checks for the present of <tt>key</tt> in the keys of the map. * * @param key an <code>double</code> value * @return a <code>boolean</code> value */ public boolean containsKey(double key) { return contains(key); } /** * Executes <tt>procedure</tt> for each key in the map. * * @param procedure a <code>TDoubleProcedure</code> value * @return false if the loop over the keys terminated because * the procedure returned false for some key. */ public boolean forEachKey(TDoubleProcedure procedure) { return forEach(procedure); } /** * Executes <tt>procedure</tt> for each value in the map. * * @param procedure a <code>TDoubleProcedure</code> value * @return false if the loop over the values terminated because * the procedure returned false for some value. */ public boolean forEachValue(TDoubleProcedure procedure) { byte[] states = _states; double[] values = _values; for (int i = values.length; i-- > 0;) { if (states[i] == FULL && ! procedure.execute(values[i])) { return false; } } return true; } /** * Executes <tt>procedure</tt> for each key/value entry in the * map. * * @param procedure a <code>TODoubleDoubleProcedure</code> value * @return false if the loop over the entries terminated because * the procedure returned false for some entry. */ public boolean forEachEntry(TDoubleDoubleProcedure procedure) { byte[] states = _states; double[] keys = _set; double[] values = _values; for (int i = keys.length; i-- > 0;) { if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) { return false; } } return true; } /** * Retains only those entries in the map for which the procedure * returns a true value. * * @param procedure determines which entries to keep * @return true if the map was modified. */ public boolean retainEntries(TDoubleDoubleProcedure procedure) { boolean modified = false; byte[] states = _states; double[] keys = _set; double[] values = _values; for (int i = keys.length; i-- > 0;) { if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) { removeAt(i); modified = true; } } return modified; } /** * Transform the values in this map using <tt>function</tt>. * * @param function a <code>TDoubleFunction</code> value */ public void transformValues(TDoubleFunction function) { byte[] states = _states; double[] values = _values; for (int i = values.length; i-- > 0;) { if (states[i] == FULL) { values[i] = function.execute(values[i]); } } } /** * Increments the primitive value mapped to key by 1 * * @param key the key of the value to increment * @return true if a mapping was found and modified. */ public boolean increment(double key) { return adjustValue(key, (double)1); } /** * Adjusts the primitive value mapped to key. * * @param key the key of the value to increment * @param amount the amount to adjust the value by. * @return true if a mapping was found and modified. */ public boolean adjustValue(double key, double amount) { int index = index(key); if (index < 0) { return false; } else { _values[index] += amount; return true; } } private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); // number of entries stream.writeInt(_size); SerializationProcedure writeProcedure = new SerializationProcedure(stream); if (! forEachEntry(writeProcedure)) { throw writeProcedure.exception; } } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); int size = stream.readInt(); setUp(size); while (size-- > 0) { double key = stream.readDouble(); double val = stream.readDouble(); put(key, val); } }} // TDoubleDoubleHashMap
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -