merge one centroid from a sorted centroid stream
| 88 | |
| 89 | // merge one centroid from a sorted centroid stream |
| 90 | void Add(const Centroid& centroid) { |
| 91 | auto& td = *tdigest_; |
| 92 | const double weight = weight_so_far_ + centroid.weight; |
| 93 | if (weight <= weight_limit_) { |
| 94 | td.back().Merge(centroid); |
| 95 | } else { |
| 96 | const double quantile = weight_so_far_ / total_weight_; |
| 97 | const double next_weight_limit = total_weight_ * this->Q(this->K(quantile) + 1); |
| 98 | // weight limit should be strictly increasing, until the last centroid |
| 99 | if (next_weight_limit <= weight_limit_) { |
| 100 | weight_limit_ = total_weight_; |
| 101 | } else { |
| 102 | weight_limit_ = next_weight_limit; |
| 103 | } |
| 104 | td.push_back(centroid); // should never exceed capacity and trigger reallocation |
| 105 | } |
| 106 | weight_so_far_ = weight; |
| 107 | } |
| 108 | |
| 109 | // validate k-size of a tdigest |
| 110 | Status Validate(const std::vector<Centroid>& tdigest, double total_weight) const { |