328 int32_t best_cluster;
329 int32_t new_centre = 0;
334 int32_t cluster_count;
336 if (buckets_ ==
nullptr || max_clusters < 1)
338 centres =
new float[max_clusters + 1];
339 for (cluster_count = 1; cluster_count <= max_clusters
340 && clusters[cluster_count].buckets_ !=
nullptr 341 && clusters[cluster_count].total_count_ > 0;
343 centres[cluster_count] =
344 static_cast<float>(clusters[cluster_count].
ile(0.5));
345 new_centre = clusters[cluster_count].
mode();
346 for (entry = new_centre - 1; centres[cluster_count] - entry < lower
347 && entry >= rangemin_
352 clusters[cluster_count].
add(entry,
count);
356 for (entry = new_centre + 1; entry - centres[cluster_count] < lower
362 clusters[cluster_count].
add(entry,
count);
369 if (cluster_count == 0) {
370 clusters[0].
set_range(rangemin_, rangemax_);
375 for (entry = 0; entry < rangemax_ - rangemin_; entry++) {
376 count = buckets_[entry] - clusters[0].buckets_[entry];
379 min_dist =
static_cast<float>(INT32_MAX);
382 dist = entry + rangemin_ - centres[
cluster];
386 if (dist < min_dist) {
392 && (best_cluster == 0
393 || entry + rangemin_ > centres[best_cluster] * multiple
394 || entry + rangemin_ < centres[best_cluster] / multiple)) {
395 if (
count > new_mode) {
397 new_centre = entry + rangemin_;
403 if (new_mode > 0 && cluster_count < max_clusters) {
406 if (!clusters[cluster_count].
set_range(rangemin_, rangemax_)) {
410 centres[cluster_count] =
static_cast<float>(new_centre);
411 clusters[cluster_count].
add(new_centre, new_mode);
412 clusters[0].
add(new_centre, new_mode);
413 for (entry = new_centre - 1; centres[cluster_count] - entry < lower
414 && entry >= rangemin_
418 clusters[cluster_count].
add(entry,
count);
422 for (entry = new_centre + 1; entry - centres[cluster_count] < lower
427 clusters[cluster_count].
add(entry,
count);
431 centres[cluster_count] =
432 static_cast<float>(clusters[cluster_count].
ile(0.5));
434 }
while (new_cluster && cluster_count < max_clusters);
436 return cluster_count;
int32_t pile_count(int32_t value) const
double ile(double frac) const
bool set_range(int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
void add(int32_t value, int32_t count)
int32_t cluster(float lower, float upper, float multiple, int32_t max_clusters, STATS *clusters)