public IntegerDoubleFrequency() { counts = new IntDoubleHashMap(); total = 0; }
@Override public void clear() { owner.clear(); }
/** * Return true if all keys of some other container exist in this container. */ protected boolean equalElements(IntDoubleHashMap other) { if (other.size() != size()) { return false; } for (IntDoubleCursor c : other) { int key = c.key; if (!containsKey(key) || !(Double.doubleToLongBits(get(key)) == Double.doubleToLongBits(c.value))) { return false; } } return true; }
@Override public int removeAll(final int e) { final boolean hasKey = owner.containsKey(e); if (hasKey) { owner.remove(e); return 1; } else { return 0; } } };
/** * Creates a hash map from two index-aligned arrays of key-value pairs. */ public static IntDoubleHashMap from(int[] keys, double[] values) { if (keys.length != values.length) { throw new IllegalArgumentException("Arrays of keys and values must have an identical length."); } IntDoubleHashMap map = new IntDoubleHashMap(keys.length); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } return map; }
/** * If <code>key</code> exists, <code>putValue</code> is inserted into the map, * otherwise any existing value is incremented by <code>additionValue</code>. * * @param key * The key of the value to adjust. * @param putValue * The value to put if <code>key</code> does not exist. * @param incrementValue * The value to add to the existing value if <code>key</code> exists. * @return Returns the current value associated with <code>key</code> (after * changes). */ @Override public double putOrAdd(int key, double putValue, double incrementValue) { assert assigned < mask + 1; if (containsKey(key)) { putValue = get(key); putValue = (double) (((putValue) + (incrementValue))); } put(key, putValue); return putValue; }
if(!longMetrics.isEmpty() || !doubleMetrics.isEmpty()){ outputTable.addRule(); outputTable.addRow("Custom Metrics",""); outputTable.addRow(names[i], NumberFormat.getInstance().format(value)); }else if(doubleMetrics.containsKey(i)){ double value = doubleMetrics.get(i); if(value != 0){ outputTable.addRow(names[i], NumberFormat.getInstance().format(value));
final int before = size(); if (other.size() >= size() && other instanceof IntLookupContainer) { if (hasEmptyKey) { if (!((existing = keys[slot]) == 0) && other.contains(existing)) { shiftConflictingKeys(slot); } else { slot++; this.remove( c.value); return before - size();
/** * {@inheritDoc} */ @Override public int removeAll(IntPredicate predicate) { final int before = size(); if (hasEmptyKey) { if (predicate.apply(0)) { hasEmptyKey = false; values[mask + 1] = 0d; } } final int[] keys = this.keys; for (int slot = 0, max = this.mask; slot <= max;) { int existing; if (!((existing = keys[slot]) == 0) && predicate.apply(existing)) { // Shift, do not increment slot. shiftConflictingKeys(slot); } else { slot++; } } return before - size(); }
/** * {@inheritDoc} */ public boolean isEmpty() { return size() == 0; }
/** * This method is invoked when there is a new key/ value pair to be inserted into * the buffers but there is not enough empty slots to do so. * * New buffers are allocated. If this succeeds, we know we can proceed * with rehashing so we assign the pending element to the previous buffer * (possibly violating the invariant of having at least one empty slot) * and rehash all keys, substituting new buffers at the end. */ protected void allocateThenInsertThenRehash(int slot, int pendingKey, double pendingValue) { assert assigned == resizeAt && (( keys[slot]) == 0) && !((pendingKey) == 0); // Try to allocate new buffers first. If we OOM, we leave in a consistent state. final int[] prevKeys = this.keys; final double[] prevValues = this.values; allocateBuffers(nextBufferSize(mask + 1, size(), loadFactor)); assert this.keys.length > prevKeys.length; // We have succeeded at allocating new data so insert the pending key/value at // the free slot in the old arrays before rehashing. prevKeys[slot] = pendingKey; prevValues[slot] = pendingValue; // Rehash old keys, including the pending key. rehash(prevKeys, prevValues); }
/** * <a href="http://trove4j.sourceforge.net">Trove</a>-inspired API method. An equivalent * of the following code: * <pre> * if (!map.containsKey(key)) map.put(value); * </pre> * * @param key The key of the value to check. * @param value The value to put if <code>key</code> does not exist. * @return <code>true</code> if <code>key</code> did not exist and <code>value</code> * was placed in the map. */ public boolean putIfAbsent(int key, double value) { if (!containsKey(key)) { put(key, value); return true; } else { return false; } }
/** * Ensure this container can hold at least the * given number of keys (entries) without resizing its buffers. * * @param expectedElements The total number of keys, inclusive. */ @Override public void ensureCapacity(int expectedElements) { if (expectedElements > resizeAt || keys == null) { final int[] prevKeys = this.keys; final double[] prevValues = this.values; allocateBuffers(minBufferSize(expectedElements, loadFactor)); if (prevKeys != null && !isEmpty()) { rehash(prevKeys, prevValues); } } }
/** * OperatorStats merger - to merge stats from other OperatorStats * this is needed in case some processing is multithreaded that needs to have * separate OperatorStats to deal with * WARN - this will only work for metrics that can be added * @param from - OperatorStats from where to merge to "this" * @return OperatorStats - for convenience so one can merge multiple stats in one go */ public OperatorStats mergeMetrics(OperatorStats from) { final IntLongHashMap fromMetrics = from.longMetrics; final Iterator<IntLongCursor> iter = fromMetrics.iterator(); while (iter.hasNext()) { final IntLongCursor next = iter.next(); longMetrics.putOrAdd(next.key, next.value, next.value); } final IntDoubleHashMap fromDMetrics = from.doubleMetrics; final Iterator<IntDoubleCursor> iterD = fromDMetrics.iterator(); while (iterD.hasNext()) { final IntDoubleCursor next = iterD.next(); doubleMetrics.putOrAdd(next.key, next.value, next.value); } return this; }
@Override public boolean isEmpty() { return owner.isEmpty(); }
public void put(int key, double value) { g_map.put(key, value); }
@Override public <T extends IntProcedure> T forEach(final T procedure) { owner.forEach(new IntDoubleProcedure() { @Override public void apply(int key, double value) { procedure.apply(key); } }); return procedure; }
/** * Adds <code>incrementValue</code> to any existing value for the given <code>key</code> * or inserts <code>incrementValue</code> if <code>key</code> did not previously exist. * * @param key The key of the value to adjust. * @param incrementValue The value to put or add to the existing value if <code>key</code> exists. * @return Returns the current value associated with <code>key</code> (after changes). */ @Override public double addTo(int key, double incrementValue) { return putOrAdd(key, incrementValue, incrementValue); }