def merge[T]()

in atlas-core/src/main/scala/com/netflix/atlas/core/util/ArrayHelper.scala [265:330]


  def merge[T](
    comparator: Comparator[T],
    aggrF: (T, T) => T,
    vs1: Array[T],
    vs1size: Int,
    vs2: Array[T],
    vs2size: Int,
    dst: Array[T]
  ): Int = {
    val limit = dst.length

    // Source 1, source 2, and destination indices
    var vs1idx = 0
    var vs2idx = 0
    var didx = 0

    // While both have data, merge and dedup
    while (vs1idx < vs1size && vs2idx < vs2size && didx < limit) {
      val v1 = vs1(vs1idx)
      val v2 = vs2(vs2idx)
      comparator.compare(v1, v2) match {
        case c if c < 0 =>
          dst(didx) = v1
          didx += 1
          vs1idx += 1
        case c if c == 0 =>
          dst(didx) = aggrF(v1, v2)
          didx += 1
          vs1idx += 1
          vs2idx += 1
        case c if c > 0 =>
          dst(didx) = v2
          didx += 1
          vs2idx += 1
      }
    }

    // Only source has data left, fill in the remainder
    if (vs1idx < vs1size && didx < limit) {
      val length = math.min(limit - didx, vs1size - vs1idx)
      System.arraycopy(vs1, vs1idx, dst, didx, length)
      vs1idx += length
      didx += length
    }

    // Update first position of source array with null if fully consumed
    if (vs1idx >= vs1size && vs1size > 0) {
      vs1(0) = null.asInstanceOf[T]
    }

    // Only the merge array has data left, fill in the remainder
    if (vs2idx < vs2size && didx < limit) {
      val length = math.min(limit - didx, vs2size - vs2idx)
      System.arraycopy(vs2, vs2idx, dst, didx, length)
      vs2idx += length
      didx += length
    }

    // Update first position of merge array with null if fully consumed
    if (vs2idx >= vs2size && vs2size > 0) {
      vs2(0) = null.asInstanceOf[T]
    }

    // Final output size
    didx
  }