in src/main/java/org/apache/datasketches/quantiles/DoublesUnionImpl.java [159:247]
static UpdateDoublesSketch updateLogic(final int myMaxK, final UpdateDoublesSketch myQS,
final DoublesSketch other) {
int sw1 = ((myQS == null) ? 0 : myQS.isEmpty() ? 4 : 8);
sw1 |= ((other == null) ? 0 : other.isEmpty() ? 1 : 2);
int outCase = 0; //0=null, 1=NOOP, 2=copy, 3=merge
switch (sw1) {
case 0: outCase = 0; break; //myQS = null, other = null ; return null
case 1: outCase = 4; break; //myQS = null, other = empty; create empty-heap(myMaxK)
case 2: outCase = 2; break; //myQS = null, other = valid; stream or downsample to myMaxK
case 4: outCase = 1; break; //myQS = empty, other = null ; no-op
case 5: outCase = 1; break; //myQS = empty, other = empty; no-op
case 6: outCase = 3; break; //myQS = empty, other = valid; merge
case 8: outCase = 1; break; //myQS = valid, other = null ; no-op
case 9: outCase = 1; break; //myQS = valid, other = empty: no-op
case 10: outCase = 3; break; //myQS = valid, other = valid; merge
default: break; //This cannot happen
}
UpdateDoublesSketch ret = null;
switch (outCase) {
case 0: ret = null; break; //return null
case 1: ret = myQS; break; //no-op
case 2: { //myQS = null, other = valid; stream or downsample to myMaxK
assert other != null;
if (!other.isEstimationMode()) { //other is exact, stream items in
ret = HeapUpdateDoublesSketch.newInstance(myMaxK);
// exact mode, only need copy base buffer
final DoublesSketchAccessor otherAccessor = DoublesSketchAccessor.wrap(other);
for (int i = 0; i < otherAccessor.numItems(); ++i) {
ret.update(otherAccessor.get(i));
}
}
else { //myQS = null, other is est mode
ret = (myMaxK < other.getK())
? other.downSampleInternal(other, myMaxK, null) //null mem
: DoublesUtil.copyToHeap(other); //copy required because caller has handle
}
break;
}
case 3: { //myQS = empty/valid, other = valid; merge
assert other != null;
assert myQS != null;
if (!other.isEstimationMode()) { //other is exact, stream items in
ret = myQS;
// exact mode, only need copy base buffer
final DoublesSketchAccessor otherAccessor = DoublesSketchAccessor.wrap(other);
for (int i = 0; i < otherAccessor.numItems(); ++i) {
ret.update(otherAccessor.get(i));
}
}
else { //myQS = empty/valid, other = valid and in est mode
if (myQS.getK() <= other.getK()) { //I am smaller or equal, thus the target
DoublesMergeImpl.mergeInto(other, myQS);
ret = myQS;
}
else { //Bigger: myQS.getK() > other.getK(), must effectively downsize me or swap
if (myQS.isEmpty()) {
if (myQS.hasMemory()) {
final WritableMemory mem = myQS.getMemory(); //myQS is empty, ok to reconfigure
other.putMemory(mem, false); // not compact, but BB ordered
ret = DirectUpdateDoublesSketch.wrapInstance(mem);
} else { //myQS is empty and on heap
ret = DoublesUtil.copyToHeap(other);
}
}
else { //Not Empty: myQS has data, downsample to tmp
final UpdateDoublesSketch tmp = DoublesSketch.builder().setK(other.getK()).build();
DoublesMergeImpl.downSamplingMergeInto(myQS, tmp); //myData -> tmp
ret = (myQS.hasMemory())
? DoublesSketch.builder().setK(other.getK()).build(myQS.getMemory())
: DoublesSketch.builder().setK(other.getK()).build();
DoublesMergeImpl.mergeInto(tmp, ret);
DoublesMergeImpl.mergeInto(other, ret);
}
}
}
break;
}
case 4: { //myQS = null, other = empty; create empty-heap(myMaxK)
ret = HeapUpdateDoublesSketch.newInstance(myMaxK);
break;
}
default: break; //This cannot happen
}
return ret;
}