/** * @param <S> Type of Summary * @return an empty instance of Sketch */ public static <S extends Summary> Sketch<S> createEmptySketch() { return new CompactSketch<S>(null, null, Long.MAX_VALUE, true); }
/** * Gets the result of this operation * @return the result of this operation as a CompactSketch */ public CompactSketch<S> getResult() { if (count_ == 0) { return new CompactSketch<S>(null, null, theta_, isEmpty_); } final CompactSketch<S> result = new CompactSketch<S>(Arrays.copyOfRange(keys_, 0, count_), Arrays.copyOfRange(summaries_, 0, count_), theta_, isEmpty_); reset(); return result; }
/** * Gets the internal set as a CompactSketch * @return result of the intersections so far */ public CompactSketch<S> getResult() { if (isFirstCall_) { throw new SketchesStateException( "getResult() with no intervening intersections is not a legal result."); } if (sketch_ == null) { return new CompactSketch<>(null, null, theta_, isEmpty_); } return sketch_.compact(); }
/** * Instantiate Sketch from a given Memory * @param <S> Type of Summary * @param mem Memory object representing a Sketch * @param deserializer instance of SummaryDeserializer * @return Sketch created from its Memory representation */ public static <S extends Summary> Sketch<S> heapifySketch(final Memory mem, final SummaryDeserializer<S> deserializer) { final SerializerDeserializer.SketchType sketchType = SerializerDeserializer.getSketchType(mem); if (sketchType == SerializerDeserializer.SketchType.QuickSelectSketch) { return new QuickSelectSketch<S>(mem, deserializer, null); } return new CompactSketch<S>(mem, deserializer); }
/** * Converts the current state of the sketch into a compact sketch * @return compact sketch */ public CompactSketch<S> compact() { if (getRetainedEntries() == 0) { return new CompactSketch<>(null, null, theta_, isEmpty_); } final long[] keys = new long[getRetainedEntries()]; @SuppressWarnings("unchecked") final S[] summaries = (S[]) Array.newInstance(summaries_.getClass().getComponentType(), getRetainedEntries()); int i = 0; for (int j = 0; j < keys_.length; j++) { if (summaries_[j] != null) { keys[i] = keys_[j]; summaries[i] = summaries_[j].copy(); i++; } } return new CompactSketch<>(keys, summaries, theta_, isEmpty_); }
@Test public void emptyFromNonPublicConstructor() { long[] keys = new long[0]; DoubleSummary[] summaries = (DoubleSummary[]) java.lang.reflect.Array.newInstance(DoubleSummary.class, 0); CompactSketch<DoubleSummary> sketch = new CompactSketch<DoubleSummary>(keys, summaries, Long.MAX_VALUE, true); Assert.assertTrue(sketch.isEmpty()); Assert.assertFalse(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 0.0); Assert.assertEquals(sketch.getLowerBound(1), 0.0); Assert.assertEquals(sketch.getUpperBound(1), 0.0); Assert.assertEquals(sketch.getRetainedEntries(), 0); Assert.assertEquals(sketch.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch.getTheta(), 1.0); SketchIterator<DoubleSummary> it = sketch.iterator(); Assert.assertNotNull(it); Assert.assertFalse(it.next()); }
@Test public void emptyFromNonPublicConstructorNullArray() { CompactSketch<DoubleSummary> sketch = new CompactSketch<DoubleSummary>(null, null, Long.MAX_VALUE, true); Assert.assertTrue(sketch.isEmpty()); Assert.assertFalse(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 0.0); Assert.assertEquals(sketch.getLowerBound(1), 0.0); Assert.assertEquals(sketch.getUpperBound(1), 0.0); Assert.assertEquals(sketch.getRetainedEntries(), 0); Assert.assertEquals(sketch.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch.getTheta(), 1.0); SketchIterator<DoubleSummary> it = sketch.iterator(); Assert.assertNotNull(it); Assert.assertFalse(it.next()); }
/** * @param <S> Type of Summary * @return an empty instance of Sketch */ public static <S extends Summary> Sketch<S> createEmptySketch() { return new CompactSketch<S>(null, null, Long.MAX_VALUE, true); }
/** * Filters elements on the provided {@link Sketch} * * @param sketchIn The sketch against which apply the {@link Predicate} * @return A new Sketch with some of the entries filtered out based on the {@link Predicate} */ public CompactSketch<T> filter(final Sketch<T> sketchIn) { if (sketchIn == null) { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } final QuickSelectSketch<T> sketch = new QuickSelectSketch<>(sketchIn.getRetainedEntries(), ResizeFactor.X1.lg(), null); final SketchIterator<T> it = sketchIn.iterator(); while (it.next()) { final T summary = it.getSummary(); if (predicate.test(summary)) { sketch.insert(it.getKey(), summary.copy()); } } sketch.setThetaLong(sketchIn.getThetaLong()); if (!sketchIn.isEmpty()) { sketch.setNotEmpty(); } return sketch.compact(); } }
/** * Gets the result of this operation * @return the result of this operation as a CompactSketch */ public CompactSketch<S> getResult() { if (count_ == 0) { return new CompactSketch<S>(null, null, theta_, isEmpty_); } final CompactSketch<S> result = new CompactSketch<S>(Arrays.copyOfRange(keys_, 0, count_), Arrays.copyOfRange(summaries_, 0, count_), theta_, isEmpty_); reset(); return result; }
/** * Gets the internal set as a CompactSketch * @return result of the intersections so far */ public CompactSketch<S> getResult() { if (isFirstCall_) { throw new SketchesStateException( "getResult() with no intervening intersections is not a legal result."); } if (sketch_ == null) { return new CompactSketch<>(null, null, theta_, isEmpty_); } return sketch_.compact(); }
/** * Instantiate Sketch from a given Memory * @param <S> Type of Summary * @param mem Memory object representing a Sketch * @param deserializer instance of SummaryDeserializer * @return Sketch created from its Memory representation */ public static <S extends Summary> Sketch<S> heapifySketch(final Memory mem, final SummaryDeserializer<S> deserializer) { final SerializerDeserializer.SketchType sketchType = SerializerDeserializer.getSketchType(mem); if (sketchType == SerializerDeserializer.SketchType.QuickSelectSketch) { return new QuickSelectSketch<S>(mem, deserializer, null); } return new CompactSketch<S>(mem, deserializer); }
/** * Converts the current state of the sketch into a compact sketch * @return compact sketch */ public CompactSketch<S> compact() { if (getRetainedEntries() == 0) { return new CompactSketch<>(null, null, theta_, isEmpty_); } final long[] keys = new long[getRetainedEntries()]; @SuppressWarnings("unchecked") final S[] summaries = (S[]) Array.newInstance(summaries_.getClass().getComponentType(), getRetainedEntries()); int i = 0; for (int j = 0; j < keys_.length; j++) { if (summaries_[j] != null) { keys[i] = keys_[j]; summaries[i] = summaries_[j].copy(); i++; } } return new CompactSketch<>(keys, summaries, theta_, isEmpty_); }
/** * Filters elements on the provided {@link Sketch} * * @param sketchIn The sketch against which apply the {@link Predicate} * @return A new Sketch with some of the entries filtered out based on the {@link Predicate} */ public CompactSketch<T> filter(final Sketch<T> sketchIn) { if (sketchIn == null) { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } final QuickSelectSketch<T> sketch = new QuickSelectSketch<>(sketchIn.getRetainedEntries(), ResizeFactor.X1.lg(), null); final SketchIterator<T> it = sketchIn.iterator(); while (it.next()) { final T summary = it.getSummary(); if (predicate.test(summary)) { sketch.insert(it.getKey(), summary.copy()); } } sketch.setThetaLong(sketchIn.getThetaLong()); if (!sketchIn.isEmpty()) { sketch.setNotEmpty(); } return sketch.compact(); } }