@Nullable
public MinKSet<RowSnapshot> add(@Nullable MinKSet<RowSnapshot>left,
@Nullable MinKSet<RowSnapshot> right) {
assert left != null;
assert right != null;
Comparator<RowSnapshot> comp = left.comp;
RowSnapshot minRS, maxRS;
long present = left.presentCount + right.presentCount;
if (left.presentCount == 0) {
minRS = right.min;
maxRS = right.max;
} else if (right.presentCount == 0) {
minRS = right.min;
maxRS = right.max;
} else {
minRS = comp.compare(left.min, right.min) < 0 ? left.min : right.min;
maxRS = comp.compare(left.max, right.max) > 0 ? left.max : right.max;
}
Long2ObjectRBTreeMap<RowSnapshot> data = new Long2ObjectRBTreeMap<>();
data.putAll(left.data);
data.putAll(right.data);
while (data.size() > this.numSamples) {
long maxKey = data.lastLongKey();
data.remove(maxKey);
}
return new MinKSet<RowSnapshot>(this.numSamples, data, comp, minRS, maxRS, present, 0);
}
}