| 2266 | } |
| 2267 | |
| 2268 | void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight) |
| 2269 | { |
| 2270 | if (scores.empty()) { |
| 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | std::sort(scores.begin(), scores.end()); |
| 2275 | |
| 2276 | // 10th, 25th, 50th, 75th, and 90th percentile weight units. |
| 2277 | const double weights[NUM_GETBLOCKSTATS_PERCENTILES] = { |
| 2278 | total_weight / 10.0, total_weight / 4.0, total_weight / 2.0, (total_weight * 3.0) / 4.0, (total_weight * 9.0) / 10.0 |
| 2279 | }; |
| 2280 | |
| 2281 | int64_t next_percentile_index = 0; |
| 2282 | int64_t cumulative_weight = 0; |
| 2283 | for (const auto& element : scores) { |
| 2284 | cumulative_weight += element.second; |
| 2285 | while (next_percentile_index < NUM_GETBLOCKSTATS_PERCENTILES && cumulative_weight >= weights[next_percentile_index]) { |
| 2286 | result[next_percentile_index] = element.first; |
| 2287 | ++next_percentile_index; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | // Fill any remaining percentiles with the last value. |
| 2292 | for (int64_t i = next_percentile_index; i < NUM_GETBLOCKSTATS_PERCENTILES; i++) { |
| 2293 | result[i] = scores.back().first; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | template<typename T> |
| 2298 | static inline bool SetHasKeys(const std::set<T>& set) {return false;} |