/** * Check if a value is associated with a key. * @param key key to check * @return true if a value is associated with key */ public boolean containsKey(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return true; } if (states[index] == FREE) { return false; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return true; } } return false; }
/** * Check if a value is associated with a key. * @param key key to check * @return true if a value is associated with key */ public boolean containsKey(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return true; } if (states[index] == FREE) { return false; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return true; } } return false; }
/** * Get the stored value associated with the given key * @param key key associated with the data * @return data associated with the key */ public double get(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return values[index]; } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return values[index]; } } return missingEntries; }
/** * Get the stored value associated with the given key * @param key key associated with the data * @return data associated with the key */ public double get(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return values[index]; } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return values[index]; } } return missingEntries; }
/** * Remove the value associated with a key. * @param key key to which the value is associated * @return removed value */ public double remove(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return doRemove(index); } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return doRemove(index); } } return missingEntries; }
/** * Remove the value associated with a key. * @param key key to which the value is associated * @return removed value */ public double remove(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return doRemove(index); } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return doRemove(index); } } return missingEntries; }
/** {@inheritDoc} */ public void setEntry(int index, double value) throws MatrixIndexException { checkIndex(index); if (!isDefaultValue(value)) { entries.put(index, value); } else if (entries.containsKey(index)) { entries.remove(index); } }
/** {@inheritDoc} */ public void setEntry(int index, double value) throws MatrixIndexException { checkIndex(index); if (!isDefaultValue(value)) { entries.put(index, value); } else if (entries.containsKey(index)) { entries.remove(index); } }
/** {@inheritDoc} */ @Override public OpenMapRealVector subtract(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); for (int i = 0; i < v.length; i++) { if (entries.containsKey(i)) { res.setEntry(i, entries.get(i) - v[i]); } else { res.setEntry(i, -v[i]); } } return res; }
/** {@inheritDoc} */ @Override public OpenMapRealVector subtract(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); for (int i = 0; i < v.length; i++) { if (entries.containsKey(i)) { res.setEntry(i, entries.get(i) - v[i]); } else { res.setEntry(i, -v[i]); } } return res; }
/** * Distance between two vectors. * <p>This method computes the distance consistent with * L<sub>1</sub> norm, i.e. the sum of the absolute values of * elements differences.</p> * @param v vector to which distance is requested * @return distance between two vectors. */ public double getL1Distance(OpenMapRealVector v) { double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = Math.abs(iter.value() - v.getEntry(iter.key())); max += delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { double delta = Math.abs(iter.value()); max += Math.abs(delta); } } return max; }
/** * Optimized method to compute LInfDistance. * @param v The vector to compute from * @return the LInfDistance */ private double getLInfDistance(OpenMapRealVector v) { double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = Math.abs(iter.value() - v.getEntry(iter.key())); if (delta > max) { max = delta; } } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { if (iter.value() > max) { max = iter.value(); } } } return max; }
/** * Optimized method to compute distance. * @param v The vector to compute distance to * @return The distance from <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public double getDistance(OpenMapRealVector v) throws IllegalArgumentException { Iterator iter = entries.iterator(); double res = 0; while (iter.hasNext()) { iter.advance(); int key = iter.key(); double delta; delta = iter.value() - v.getEntry(key); res += delta * delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { final double value = iter.value(); res += value * value; } } return Math.sqrt(res); }
/** * Optimized method to subtract OpenMapRealVectors. * @param v The vector to subtract from <code>this</code> * @return The difference of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector subtract(OpenMapRealVector v) throws IllegalArgumentException{ checkVectorDimensions(v.getDimension()); OpenMapRealVector res = copy(); Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key) - iter.value()); } else { res.setEntry(key, -iter.value()); } } return res; }
/** * Optimized method to subtract OpenMapRealVectors. * @param v The vector to subtract from <code>this</code> * @return The difference of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector subtract(OpenMapRealVector v) throws IllegalArgumentException{ checkVectorDimensions(v.getDimension()); OpenMapRealVector res = copy(); Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key) - iter.value()); } else { res.setEntry(key, -iter.value()); } } return res; }
/** * Optimized method to compute distance. * @param v The vector to compute distance to * @return The distance from <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public double getDistance(OpenMapRealVector v) throws IllegalArgumentException { Iterator iter = entries.iterator(); double res = 0; while (iter.hasNext()) { iter.advance(); int key = iter.key(); double delta; delta = iter.value() - v.getEntry(key); res += delta * delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { final double value = iter.value(); res += value * value; } } return FastMath.sqrt(res); }
/** * Optimized method to compute LInfDistance. * @param v The vector to compute from * @return the LInfDistance */ private double getLInfDistance(OpenMapRealVector v) { double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = FastMath.abs(iter.value() - v.getEntry(iter.key())); if (delta > max) { max = delta; } } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { if (iter.value() > max) { max = iter.value(); } } } return max; }
/** * Distance between two vectors. * <p>This method computes the distance consistent with * L<sub>1</sub> norm, i.e. the sum of the absolute values of * elements differences.</p> * @param v vector to which distance is requested * @return distance between two vectors. */ public double getL1Distance(OpenMapRealVector v) { double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = FastMath.abs(iter.value() - v.getEntry(iter.key())); max += delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { double delta = FastMath.abs(iter.value()); max += FastMath.abs(delta); } } return max; }
/** * Optimized method to add two OpenMapRealVectors. Copies the larger vector, iterates over the smaller. * @param v Vector to add with * @return The sum of <code>this</code> with <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector add(OpenMapRealVector v) throws IllegalArgumentException{ checkVectorDimensions(v.getDimension()); boolean copyThis = entries.size() > v.entries.size(); OpenMapRealVector res = copyThis ? this.copy() : v.copy(); Iterator iter = copyThis ? v.entries.iterator() : entries.iterator(); OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries; while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (randomAccess.containsKey(key)) { res.setEntry(key, randomAccess.get(key) + iter.value()); } else { res.setEntry(key, iter.value()); } } return res; }
/** * Optimized method to add two OpenMapRealVectors. Copies the larger vector, iterates over the smaller. * @param v Vector to add with * @return The sum of <code>this</code> with <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector add(OpenMapRealVector v) throws IllegalArgumentException{ checkVectorDimensions(v.getDimension()); boolean copyThis = entries.size() > v.entries.size(); OpenMapRealVector res = copyThis ? this.copy() : v.copy(); Iterator iter = copyThis ? v.entries.iterator() : entries.iterator(); OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries; while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (randomAccess.containsKey(key)) { res.setEntry(key, randomAccess.get(key) + iter.value()); } else { res.setEntry(key, iter.value()); } } return res; }