/** * Computes the sum of products of key-value pairs in {@code that} * @param that * @return */ public static double sumOfProducts(Counter<Integer> that) { final double[] result = new double[1]; // use a single-element array to make this variable 'final' result[0] = 0; that.m_map.forEachEntry(new TObjectDoubleProcedure<Integer>() { private boolean first = true; public boolean execute(Integer key, double value) { if ( first ) first = false; result[0] += key * value; return true; } }); return result[0]; } }
/** Returns the key in map that has the greatest score */ public static Object argmax (TObjectDoubleHashMap map) { // A local class! Yes, Virginia, this is legal Java. class Accumulator implements TObjectDoubleProcedure { double bestVal = Double.NEGATIVE_INFINITY; Object bestObj = null; public boolean execute (Object a, double b) { if (b > bestVal) { bestVal = b; bestObj = a; } return true; } } Accumulator procedure = new Accumulator (); map.forEachEntry (procedure); return procedure.bestObj; }
/** Returns the key in map that has the greatest score */ public static Object argmax (TObjectDoubleHashMap map) { // A local class! Yes, Virginia, this is legal Java. class Accumulator implements TObjectDoubleProcedure { double bestVal = Double.NEGATIVE_INFINITY; Object bestObj = null; public boolean execute (Object a, double b) { if (b > bestVal) { bestVal = b; bestObj = a; } return true; } } Accumulator procedure = new Accumulator (); map.forEachEntry (procedure); return procedure.bestObj; }
/** Returns the key in map that has the greatest score */ public static Object argmax (TObjectDoubleHashMap map) { // A local class! Yes, Virginia, this is legal Java. class Accumulator implements TObjectDoubleProcedure { double bestVal = Double.NEGATIVE_INFINITY; Object bestObj = null; public boolean execute (Object a, double b) { if (b > bestVal) { bestVal = b; bestObj = a; } return true; } } Accumulator procedure = new Accumulator (); map.forEachEntry (procedure); return procedure.bestObj; }
/** Returns the key in map that has the greatest score */ public static Object argmax (TObjectDoubleHashMap map) { // A local class! Yes, Virginia, this is legal Java. class Accumulator implements TObjectDoubleProcedure { double bestVal = Double.NEGATIVE_INFINITY; Object bestObj = null; public boolean execute (Object a, double b) { if (b > bestVal) { bestVal = b; bestObj = a; } return true; } } Accumulator procedure = new Accumulator (); map.forEachEntry (procedure); return procedure.bestObj; }
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; } }
public Counter<T> add(final double val) { final Counter<T> result = new Counter<T>(); m_map.forEachEntry(new TObjectDoubleProcedure<T>() { private boolean first = true; public boolean execute(T key, double value) { if ( first ) first = false; double newValue = value + val; result.set(key, newValue); return true; } }); return result; }
final String KVSEP = kvSep; final StringBuilder buf = new StringBuilder("{"); m_map.forEachEntry(new TObjectDoubleProcedure<T>() { private boolean first = true; public boolean execute(T key, double value) {
/** * For each entry in this counter, looks up the corresponding entry in {@code that} * and stores their product in the result. * @param that */ public Counter<T> scaleAllBy(final Counter<? super T> that) { final Counter<T> result = new Counter<T>(); m_map.forEachEntry(new TObjectDoubleProcedure<T>() { private boolean first = true; public boolean execute(T key, double value) { if ( first ) first = false; double newValue = value * that.getT(key); result.set(key, newValue); return true; } }); return result; }
/** * For each entry in this counter, looks up the corresponding entry in {@code that} * and stores their ratio in the result. * @param that */ public Counter<T> divideAllBy(final Counter<? super T> that) { final Counter<T> result = new Counter<T>(); m_map.forEachEntry(new TObjectDoubleProcedure<T>() { private boolean first = true; public boolean execute(T key, double value) { if ( first ) first = false; double newValue = value / that.getT(key); result.set(key, newValue); return true; } }); return result; }
/** * Compares this map with another map for equality of their stored * entries. * * @param other an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean equals(Object other) { if (! (other instanceof TObjectDoubleHashMap)) { return false; } TObjectDoubleHashMap that = (TObjectDoubleHashMap)other; if (that.size() != this.size()) { return false; } return forEachEntry(new EqProcedure(that)); }