in src/main/java/org/apache/datasketches/quantiles/ItemsMergeImpl.java [66:125]
static <T> void mergeInto(final ItemsSketch<T> src, final ItemsSketch<T> tgt) {
final int srcK = src.getK();
final int tgtK = tgt.getK();
final long srcN = src.getN();
final long tgtN = tgt.getN();
if (srcK != tgtK) {
downSamplingMergeInto(src, tgt);
return;
}
//The remainder of this code is for the case where the k's are equal
final Object[] srcCombBuf = src.getCombinedBuffer();
final long nFinal = tgtN + srcN;
for (int i = 0; i < src.getBaseBufferCount(); i++) { //update only the base buffer
tgt.update((T) srcCombBuf[i]);
}
ItemsUpdateImpl.maybeGrowLevels(tgt, nFinal);
final Object[] scratchBuf = new Object[2 * tgtK];
long srcBitPattern = src.getBitPattern();
assert srcBitPattern == (srcN / (2L * srcK));
for (int srcLvl = 0; srcBitPattern != 0L; srcLvl++, srcBitPattern >>>= 1) {
if ((srcBitPattern & 1L) > 0L) { //only one level above base buffer
ItemsUpdateImpl.inPlacePropagateCarry(
srcLvl,
(T[]) srcCombBuf, (2 + srcLvl) * tgtK,
(T[]) scratchBuf, 0,
false,
tgt);
// won't update tgt.n_ until the very end
}
}
tgt.n_ = nFinal;
assert (tgt.getN() / (2L * tgtK)) == tgt.getBitPattern(); // internal consistency check
final T srcMax = src.isEmpty() ? null : src.getMaxItem();
final T srcMin = src.isEmpty() ? null : src.getMinItem();
final T tgtMax = tgt.isEmpty() ? null : tgt.getMaxItem();
final T tgtMin = tgt.isEmpty() ? null : tgt.getMinItem();
if ((srcMax != null) && (tgtMax != null)) {
tgt.maxItem_ = (src.getComparator().compare(srcMax, tgtMax) > 0) ? srcMax : tgtMax;
} //only one could be null
else if (tgtMax == null) { //if srcMax were null we would leave tgt alone
tgt.maxItem_ = srcMax;
}
if ((srcMin != null) && (tgtMin != null)) {
tgt.minItem_ = (src.getComparator().compare(srcMin, tgtMin) > 0) ? tgtMin : srcMin;
} //only one could be null
else if (tgtMin == null) { //if srcMin were null we would leave tgt alone
tgt.minItem_ = srcMin;
}
}