Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/main/java/org/apache/datasketches/tuple/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

package org.apache.datasketches.tuple;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.function.Predicate;

import org.apache.datasketches.common.ResizeFactor;

/**
* Class for filtering entries from a {@link Sketch} given a {@link Summary}
*
Expand Down Expand Up @@ -52,23 +52,25 @@ 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 long[] hashes = new long[sketchIn.getRetainedEntries()];
T[] summaries = null; // lazy init to get class from the first entry
int i = 0;
final TupleSketchIterator<T> it = sketchIn.iterator();
while (it.next()) {
final T summary = it.getSummary();
if (predicate.test(summary)) {
sketch.insert(it.getHash(), (T)summary.copy());
hashes[i] = it.getHash();
if (summaries == null) {
summaries = (T[]) Array.newInstance(summary.getClass(), sketchIn.getRetainedEntries());
}
summaries[i++] = (T) summary.copy();
}
}

sketch.setThetaLong(sketchIn.getThetaLong());
if (!sketchIn.isEmpty()) {
sketch.setEmpty(false);
final boolean isEmpty = i == 0 && !sketchIn.isEstimationMode();
if (i == 0) {
return new CompactSketch<>(null, null, sketchIn.getThetaLong(), isEmpty);
}

return sketch.compact();
return new CompactSketch<>(Arrays.copyOf(hashes, i), Arrays.copyOf(summaries, i), sketchIn.getThetaLong(), isEmpty);
}
}