void merge()

in src/main/java/org/apache/datasketches/sampling/EbppsItemsSample.java [200:246]


  void merge(final EbppsItemsSample<T> other) {
    //double cInt = Math.floor(c_);
    final double cFrac = c_ % 1;
    final double otherCFrac = other.c_ % 1;

    // update c_ here but do NOT recompute fractional part yet
    c_ += other.c_;

    if (other.data_ != null) {
      data_.addAll(other.data_);
    }

    // This modifies the original algorithm slightly due to numeric
    // precision issues. Specifically, the test if cFrac + otherCFrac == 1.0
    // happens before tests for < 1.0 or > 1.0 and can also be triggered
    // if c_ == floor(c_) (the updated value of c_, not the input).
    //
    // We can still run into issues where cFrac + otherCFrac == epsilon
    // and the first case would have ideally triggered. As a result, we must
    // check if the partial item exists before adding to the data_ vector.

    if (cFrac == 0.0 && otherCFrac == 0.0) {
      partialItem_ = null;
    } else if (cFrac + otherCFrac == 1.0 || c_ == Math.floor(c_)) {
      if (rand_.nextDouble() <= cFrac) {
        if (partialItem_ != null) {
          data_.add(partialItem_);
        }
      } else {
        if (other.partialItem_ != null) {
          data_.add(other.partialItem_);
        }
      }
      partialItem_ = null;
    } else if (cFrac + otherCFrac < 1.0) {
      if (rand_.nextDouble() > cFrac / (cFrac + otherCFrac)) {
        partialItem_ = other.partialItem_;
      }
    } else { // cFrac + otherCFrac > 1
      if (rand_.nextDouble() <= (1 - cFrac) / ((1 - cFrac) + (1 - otherCFrac))) {
        data_.add(other.partialItem_);
      } else {
        data_.add(partialItem_);
        partialItem_ = other.partialItem_;
      }
    }
  }