public Double get(final Double ok) { final double k = ((ok).doubleValue()); // The starting point. int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; // There's always an unused entry. while (used[pos]) { if ((strategy.equals((key[pos]), (k)))) return (Double.valueOf(value[pos])); pos = (pos + 1) & mask; } return (null); }
@SuppressWarnings("unchecked") public boolean containsKey(final double k) { // The starting point. int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; // There's always an unused entry. while (used[pos]) { if ((strategy.equals((key[pos]), (k)))) return true; pos = (pos + 1) & mask; } return false; }
/** * Resizes the map. * * <p>This method implements the basic rehashing strategy, and may be overriden by subclasses * implementing different rehashing strategies (e.g., disk-based rehashing). However, you should * not override this method unless you understand the internal workings of this class. * * @param newN the new size */ @SuppressWarnings("unchecked") protected void rehash(final int newN) { int i = 0, pos; final boolean used[] = this.used; double k; final double key[] = this.key; final double value[] = this.value; final int newMask = newN - 1; final double newKey[] = new double[newN]; final double newValue[] = new double[newN]; final boolean newUsed[] = new boolean[newN]; for (int j = size; j-- != 0; ) { while (!used[i]) i++; k = key[i]; pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & newMask; while (newUsed[pos]) pos = (pos + 1) & newMask; newUsed[pos] = true; newKey[pos] = k; newValue[pos] = value[i]; i++; } n = newN; mask = newMask; maxFill = maxFill(n, f); this.key = newKey; this.value = newValue; this.used = newUsed; }
@SuppressWarnings("unchecked") public double remove(final double k) { // The starting point. int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; // There's always an unused entry. while (used[pos]) { if ((strategy.equals((key[pos]), (k)))) { size--; final double v = value[pos]; shiftKeys(pos); return v; } pos = (pos + 1) & mask; } return defRetValue; }
@SuppressWarnings("unchecked") public Double remove(final Object ok) { final double k = ((((Double) (ok)).doubleValue())); // The starting point. int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; // There's always an unused entry. while (used[pos]) { if ((strategy.equals((key[pos]), (k)))) { size--; final double v = value[pos]; shiftKeys(pos); return (Double.valueOf(v)); } pos = (pos + 1) & mask; } return (null); }
/** * Adds an increment to value currently associated with a key. * * <p>Note that this method respects the {@linkplain #defaultReturnValue() default return value} * semantics: when called with a key that does not currently appears in the map, the key will be * associated with the default return value plus the given increment. * * @param k the key. * @param incr the increment. * @return the old value, or the {@linkplain #defaultReturnValue() default return value} if no * value was present for the given key. */ public double addTo(final double k, final double incr) { // The starting point. int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; // There's always an unused entry. while (used[pos]) { if ((strategy.equals((key[pos]), (k)))) { final double oldValue = value[pos]; value[pos] += incr; return oldValue; } pos = (pos + 1) & mask; } used[pos] = true; key[pos] = k; value[pos] = defRetValue + incr; if (++size >= maxFill) rehash(arraySize(size + 1, f)); if (ASSERTS) checkTable(); return defRetValue; }
/** * Returns a hash code for this map. * * <p>This method overrides the generic method provided by the superclass. Since <code>equals() * </code> is not overriden, it is important that the value returned by this method is the same * value as the one returned by the overriden method. * * @return a hash code for this map. */ public int hashCode() { int h = 0; for (int j = size, i = 0, t = 0; j-- != 0; ) { while (!used[i]) i++; t = (strategy.hashCode(key[i])); t ^= it.unimi.dsi.fastutil.HashCommon.double2int(value[i]); h += t; i++; } return h; }
public Double put(final Double ok, final Double ov) { final double v = ((ov).doubleValue()); final double k = ((ok).doubleValue()); // The starting point. int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; // There's always an unused entry. while (used[pos]) { if ((strategy.equals((key[pos]), (k)))) { final Double oldValue = (Double.valueOf(value[pos])); value[pos] = v; return oldValue; } pos = (pos + 1) & mask; } used[pos] = true; key[pos] = k; value[pos] = v; if (++size >= maxFill) rehash(arraySize(size + 1, f)); if (ASSERTS) checkTable(); return (null); }
/** * Shifts left entries with the specified hash code, starting at the specified position, and * empties the resulting free entry. * * @param pos a starting position. * @return the position cleared by the shifting process. */ protected final int shiftKeys(int pos) { // Shift entries with the same hash. int last, slot; for (; ; ) { pos = ((last = pos) + 1) & mask; while (used[pos]) { slot = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(key[pos]))) & mask; if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos) break; pos = (pos + 1) & mask; } if (!used[pos]) break; key[last] = key[pos]; value[last] = value[pos]; } used[last] = false; return last; }
@SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); n = arraySize(size, f); maxFill = maxFill(n, f); mask = n - 1; final double key[] = this.key = new double[n]; final double value[] = this.value = new double[n]; final boolean used[] = this.used = new boolean[n]; double k; double v; for (int i = size, pos = 0; i-- != 0; ) { k = s.readDouble(); v = s.readDouble(); pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3(strategy.hashCode(k))) & mask; while (used[pos]) pos = (pos + 1) & mask; used[pos] = true; key[pos] = k; value[pos] = v; } if (ASSERTS) checkTable(); }