/** * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * given value. * @param numValues Number of double values to keep for each key * @return maximum required storage bytes given nomEntries and numValues */ public static int getMaxBytes(final int nomEntries, final int numValues) { return ArrayOfDoublesQuickSelectSketch.getMaxBytes(nomEntries, numValues); }
void merge(final long key, final double[] values) { setNotEmpty(); if (key < theta_) { final int index = findOrInsertKey(key); if (index < 0) { incrementCount(); setValues(~index, values); } else { updateValues(index, values); } rebuildIfNeeded(); } }
void rebuildIfNeeded() { if (getRetainedEntries() < rebuildThreshold_) { return; } if (getCurrentCapacity() > getNominalEntries()) { setThetaLong(getNewTheta()); rebuild(); } else { rebuild(getCurrentCapacity() * getResizeFactor().getValue()); } }
@Override public void trim() { if (getRetainedEntries() > getNominalEntries()) { setThetaLong(getNewTheta()); rebuild(); } }
void setRebuildThreshold() { if (getCurrentCapacity() > getNominalEntries()) { rebuildThreshold_ = (int) (getCurrentCapacity() * REBUILD_THRESHOLD); } else { rebuildThreshold_ = (int) (getCurrentCapacity() * RESIZE_THRESHOLD); } }
final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { sketch_.insert(it.getKey(), it.getValues()); final int matchSize = min(sketch_.getRetainedEntries(), sketchIn.getRetainedEntries()); final long[] matchKeys = new long[matchSize]; final double[][] matchValues = new double[matchSize][]; final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { final double[] values = sketch_.find(it.getKey()); if (values != null) { matchKeys[matchCount] = it.getKey(); sketch_ = createSketch(matchCount, numValues_, seed_); for (int i = 0; i < matchCount; i++) { sketch_.insert(matchKeys[i], matchValues[i]); sketch_.setThetaLong(theta_); sketch_.setNotEmpty();
long getNewTheta() { final long[] keys = new long[getRetainedEntries()]; int i = 0; for (int j = 0; j < getCurrentCapacity(); j++) { final long key = getKey(j); if (key != 0) { keys[i++] = key; } } return QuickSelect.select(keys, 0, getRetainedEntries() - 1, getNominalEntries()); }
ArrayOfDoublesUnion(final ArrayOfDoublesQuickSelectSketch sketch) { nomEntries_ = sketch.getNominalEntries(); numValues_ = sketch.getNumValues(); seed_ = sketch.getSeed(); seedHash_ = Util.computeSeedHash(seed_); sketch_ = sketch; theta_ = sketch.getThetaLong(); }
/** * Returns the resulting union in the form of a compact sketch * @param dstMem memory for the result (can be null) * @return compact sketch representing the union (off-heap if memory is provided) */ public ArrayOfDoublesCompactSketch getResult(final WritableMemory dstMem) { if (sketch_.getRetainedEntries() > sketch_.getNominalEntries()) { theta_ = Math.min(theta_, sketch_.getNewTheta()); } if (dstMem == null) { return new HeapArrayOfDoublesCompactSketch(sketch_, theta_); } return new DirectArrayOfDoublesCompactSketch(sketch_, theta_, dstMem); }
void rebuild() { rebuild(getCurrentCapacity()); }
/** * Updates the union by adding a set of entries from a given sketch * @param sketchIn sketch to add to the union */ public void update(final ArrayOfDoublesSketch sketchIn) { if (sketchIn == null) { return; } Util.checkSeedHashes(seedHash_, sketchIn.getSeedHash()); if (sketch_.getNumValues() != sketchIn.getNumValues()) { throw new SketchesArgumentException("Incompatible sketches: number of values mismatch " + sketch_.getNumValues() + " and " + sketchIn.getNumValues()); } if (sketchIn.isEmpty()) { return; } if (sketchIn.getThetaLong() < theta_) { theta_ = sketchIn.getThetaLong(); } final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { sketch_.merge(it.getKey(), it.getValues()); } }
/** * Gets the internal set as an off-heap compact sketch using the given memory. * @param dstMem Memory for the compact sketch (can be null). * @return Result of the intersections so far as a compact sketch. */ public ArrayOfDoublesCompactSketch getResult(final WritableMemory dstMem) { if (isFirstCall_) { throw new SketchesStateException( "getResult() with no intervening intersections is not a legal result."); } if (sketch_ == null) { return new HeapArrayOfDoublesCompactSketch( null, null, Long.MAX_VALUE, true, numValues_, seedHash_); } return sketch_.compact(dstMem); }
@Override public void trim() { if (getRetainedEntries() > getNominalEntries()) { setThetaLong(getNewTheta()); rebuild(); } }
final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { sketch_.insert(it.getKey(), it.getValues()); final int matchSize = min(sketch_.getRetainedEntries(), sketchIn.getRetainedEntries()); final long[] matchKeys = new long[matchSize]; final double[][] matchValues = new double[matchSize][]; final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { final double[] values = sketch_.find(it.getKey()); if (values != null) { matchKeys[matchCount] = it.getKey(); sketch_ = createSketch(matchCount, numValues_, seed_); for (int i = 0; i < matchCount; i++) { sketch_.insert(matchKeys[i], matchValues[i]); sketch_.setThetaLong(theta_); sketch_.setNotEmpty();
long getNewTheta() { final long[] keys = new long[getRetainedEntries()]; int i = 0; for (int j = 0; j < getCurrentCapacity(); j++) { final long key = getKey(j); if (key != 0) { keys[i++] = key; } } return QuickSelect.select(keys, 0, getRetainedEntries() - 1, getNominalEntries()); }
ArrayOfDoublesUnion(final ArrayOfDoublesQuickSelectSketch sketch) { nomEntries_ = sketch.getNominalEntries(); numValues_ = sketch.getNumValues(); seed_ = sketch.getSeed(); seedHash_ = Util.computeSeedHash(seed_); sketch_ = sketch; theta_ = sketch.getThetaLong(); }
/** * Returns the resulting union in the form of a compact sketch * @param dstMem memory for the result (can be null) * @return compact sketch representing the union (off-heap if memory is provided) */ public ArrayOfDoublesCompactSketch getResult(final WritableMemory dstMem) { if (sketch_.getRetainedEntries() > sketch_.getNominalEntries()) { theta_ = Math.min(theta_, sketch_.getNewTheta()); } if (dstMem == null) { return new HeapArrayOfDoublesCompactSketch(sketch_, theta_); } return new DirectArrayOfDoublesCompactSketch(sketch_, theta_, dstMem); }
void setRebuildThreshold() { if (getCurrentCapacity() > getNominalEntries()) { rebuildThreshold_ = (int) (getCurrentCapacity() * REBUILD_THRESHOLD); } else { rebuildThreshold_ = (int) (getCurrentCapacity() * RESIZE_THRESHOLD); } }
void rebuild() { rebuild(getCurrentCapacity()); }
/** * Updates the union by adding a set of entries from a given sketch * @param sketchIn sketch to add to the union */ public void update(final ArrayOfDoublesSketch sketchIn) { if (sketchIn == null) { return; } Util.checkSeedHashes(seedHash_, sketchIn.getSeedHash()); if (sketch_.getNumValues() != sketchIn.getNumValues()) { throw new SketchesArgumentException("Incompatible sketches: number of values mismatch " + sketch_.getNumValues() + " and " + sketchIn.getNumValues()); } if (sketchIn.isEmpty()) { return; } if (sketchIn.getThetaLong() < theta_) { theta_ = sketchIn.getThetaLong(); } final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { sketch_.merge(it.getKey(), it.getValues()); } }