in atlas-core/src/main/scala/com/netflix/atlas/core/util/ArrayHelper.scala [170:221]
def merge(vs: List[T]): Merger[T] = {
// Source and destination indices
var sidx = 0
var didx = 0
// Pointer to head of merge list
var data = vs
// While both have data, merge and dedup
while (sidx < size && data.nonEmpty && didx < limit) {
val v1 = src(sidx)
val v2 = data.head
comparator.compare(v1, v2) match {
case c if c < 0 =>
dst(didx) = v1
didx += 1
sidx += 1
case c if c == 0 =>
dst(didx) = aggrF(v1, v2)
didx += 1
sidx += 1
data = data.tail
case c if c > 0 =>
dst(didx) = v2
didx += 1
data = data.tail
}
}
// Only source has data left, fill in the remainder
if (sidx < size && didx < limit) {
val length = math.min(limit - didx, size - sidx)
System.arraycopy(src, sidx, dst, didx, length)
didx += length
}
// Only the merge list has data left, fill in the remainder
while (data.nonEmpty && didx < limit) {
dst(didx) = data.head
data = data.tail
didx += 1
}
size = didx
// Swap the buffers
val tmp = src
src = dst
dst = tmp
this
}