22 #include "config_auto.h"
40 if (max_bucket_value_plus_1 <= min_bucket_value) {
42 max_bucket_value_plus_1 = 1;
44 rangemin_ = min_bucket_value;
45 rangemax_ = max_bucket_value_plus_1;
46 buckets_ =
new inT32[rangemax_ - rangemin_];
62 if (max_bucket_value_plus_1 <= min_bucket_value) {
65 if (rangemax_ - rangemin_ != max_bucket_value_plus_1 - min_bucket_value) {
67 buckets_ =
new inT32[max_bucket_value_plus_1 - min_bucket_value];
69 rangemin_ = min_bucket_value;
70 rangemax_ = max_bucket_value_plus_1;
83 memset(buckets_, 0, (rangemax_ - rangemin_) *
sizeof(buckets_[0]));
92 if (buckets_ !=
NULL) {
104 if (buckets_ ==
NULL) {
107 value =
ClipToRange(value, rangemin_, rangemax_ - 1);
108 buckets_[value - rangemin_] +=
count;
109 total_count_ +=
count;
118 if (buckets_ ==
NULL) {
121 inT32 max = buckets_[0];
123 for (
int index = rangemax_ - rangemin_ - 1; index > 0; --index) {
124 if (buckets_[index] > max) {
125 max = buckets_[index];
129 return maxindex + rangemin_;
138 if (buckets_ ==
NULL || total_count_ <= 0) {
139 return static_cast<double>(rangemin_);
142 for (
int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
143 sum +=
static_cast<inT64>(index) * buckets_[index];
145 return static_cast<double>(sum) / total_count_ + rangemin_;
154 if (buckets_ ==
NULL || total_count_ <= 0) {
159 for (
int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
160 sum +=
static_cast<inT64>(index) * buckets_[index];
161 sqsum +=
static_cast<double>(index) * index * buckets_[index];
163 double variance =
static_cast<double>(sum) / total_count_;
164 variance = sqsum / total_count_ - variance * variance;
166 return sqrt(variance);
177 if (buckets_ ==
NULL || total_count_ == 0) {
178 return static_cast<double>(rangemin_);
187 double target = frac * total_count_;
188 target =
ClipToRange(target, 1.0, static_cast<double>(total_count_));
192 for (index = 0; index < rangemax_ - rangemin_ && sum < target;
193 sum += buckets_[index++]);
196 return rangemin_ + index -
197 static_cast<double>(sum - target) / buckets_[index - 1];
199 return static_cast<double>(rangemin_);
209 if (buckets_ ==
NULL || total_count_ == 0) {
213 for (min = 0; (min < rangemax_ - rangemin_) && (buckets_[min] == 0); min++);
214 return rangemin_ + min;
225 if (buckets_ ==
NULL || total_count_ == 0) {
229 for (max = rangemax_ - rangemin_ - 1; max > 0 && buckets_[max] == 0; max--);
230 return rangemin_ + max;
243 if (buckets_ ==
NULL) {
244 return static_cast<double>(rangemin_);
247 int median_pile =
static_cast<int>(floor(median));
248 if ((total_count_ > 1) && (
pile_count(median_pile) == 0)) {
252 for (min_pile = median_pile;
pile_count(min_pile) == 0; min_pile--);
254 for (max_pile = median_pile;
pile_count(max_pile) == 0; max_pile++);
255 median = (min_pile + max_pile) / 2.0;
266 if (buckets_ ==
NULL) {
269 x =
ClipToRange(x, rangemin_, rangemax_ - 1) - rangemin_;
270 if (buckets_[x] == 0)
273 for (index = x - 1; index >= 0 && buckets_[index] == buckets_[x]; --index);
274 if (index >= 0 && buckets_[index] < buckets_[x])
276 for (index = x + 1; index < rangemax_ - rangemin_ &&
277 buckets_[index] == buckets_[x]; ++index);
278 if (index < rangemax_ - rangemin_ && buckets_[index] < buckets_[x])
293 if (buckets_ ==
NULL || factor < 2) {
296 STATS result(rangemin_, rangemax_);
297 int entrycount = rangemax_ - rangemin_;
298 for (
int entry = 0; entry < entrycount; entry++) {
300 int count = buckets_[entry] * factor;
301 for (
int offset = 1; offset < factor; offset++) {
302 if (entry - offset >= 0)
303 count += buckets_[entry - offset] * (factor - offset);
304 if (entry + offset < entrycount)
305 count += buckets_[entry + offset] * (factor - offset);
307 result.
add(entry + rangemin_, count);
309 total_count_ = result.total_count_;
310 memcpy(buckets_, result.buckets_, entrycount *
sizeof(buckets_[0]));
333 inT32 new_centre = 0;
340 if (buckets_ ==
NULL || max_clusters < 1)
342 centres =
new float[max_clusters + 1];
343 for (cluster_count = 1; cluster_count <= max_clusters
344 && clusters[cluster_count].buckets_ !=
NULL
345 && clusters[cluster_count].total_count_ > 0;
347 centres[cluster_count] =
348 static_cast<float>(clusters[cluster_count].
ile(0.5));
349 new_centre = clusters[cluster_count].
mode();
350 for (entry = new_centre - 1; centres[cluster_count] - entry < lower
351 && entry >= rangemin_
356 clusters[cluster_count].
add(entry, count);
357 clusters[0].
add (entry, count);
360 for (entry = new_centre + 1; entry - centres[cluster_count] < lower
366 clusters[cluster_count].
add(entry, count);
367 clusters[0].
add(entry, count);
373 if (cluster_count == 0) {
374 clusters[0].
set_range(rangemin_, rangemax_);
379 for (entry = 0; entry < rangemax_ - rangemin_; entry++) {
380 count = buckets_[entry] - clusters[0].buckets_[entry];
383 min_dist =
static_cast<float>(
MAX_INT32);
385 for (cluster = 1; cluster <= cluster_count; cluster++) {
386 dist = entry + rangemin_ - centres[
cluster];
390 if (dist < min_dist) {
396 && (best_cluster == 0
397 || entry + rangemin_ > centres[best_cluster] * multiple
398 || entry + rangemin_ < centres[best_cluster] / multiple)) {
399 if (count > new_mode) {
401 new_centre = entry + rangemin_;
407 if (new_mode > 0 && cluster_count < max_clusters) {
410 if (!clusters[cluster_count].
set_range(rangemin_, rangemax_))
412 centres[cluster_count] =
static_cast<float>(new_centre);
413 clusters[cluster_count].
add(new_centre, new_mode);
414 clusters[0].
add(new_centre, new_mode);
415 for (entry = new_centre - 1; centres[cluster_count] - entry < lower
416 && entry >= rangemin_
420 clusters[cluster_count].
add(entry, count);
421 clusters[0].
add(entry, count);
424 for (entry = new_centre + 1; entry - centres[cluster_count] < lower
429 clusters[cluster_count].
add(entry, count);
430 clusters[0].
add (entry, count);
433 centres[cluster_count] =
434 static_cast<float>(clusters[cluster_count].
ile(0.5));
436 }
while (new_cluster && cluster_count < max_clusters);
438 return cluster_count;
447 if (buckets_ ==
NULL) {
454 for (
int index = min; index <= max; index++) {
455 if (buckets_[index] != 0) {
456 tprintf(
"%4d:%-3d ", rangemin_ + index, buckets_[index]);
457 if (++num_printed % 8 == 0)
473 if (buckets_ ==
NULL) {
478 tprintf(
"Total count=%d\n", total_count_);
479 tprintf(
"Min=%.2f Really=%d\n",
ile(0.0), min);
483 tprintf(
"Max=%.2f Really=%d\n",
ile(1.0), max);
484 tprintf(
"Range=%d\n", max + 1 - min);
496 #ifndef GRAPHICS_DISABLED
503 if (buckets_ ==
NULL) {
508 for (
int index = 0; index < rangemax_ - rangemin_; index++) {
509 window->
Rectangle( xorigin + xscale * index, yorigin,
510 xorigin + xscale * (index + 1),
511 yorigin + yscale * buckets_[index]);
523 #ifndef GRAPHICS_DISABLED
530 if (buckets_ ==
NULL) {
534 window->
SetCursor(xorigin, yorigin + yscale * buckets_[0]);
535 for (
int index = 0; index < rangemax_ - rangemin_; index++) {
536 window->
DrawTo(xorigin + xscale * index,
537 yorigin + yscale * buckets_[index]);
561 if (array[0] < array[1]) {
562 return index >= 1 ? 1 : 0;
565 return index >= 1 ? 0 : 1;
571 else if (index >= count)
574 pivot = array[equal_count];
576 array[equal_count] = array[0];
578 prev_greater =
count;
580 for (next_sample = 1; next_sample < prev_greater;) {
581 sample = array[next_sample];
582 if (sample < pivot) {
584 array[next_lesser++] = sample;
587 else if (sample > pivot) {
590 array[next_sample] = array[prev_greater];
591 array[prev_greater] = sample;
598 for (next_sample = next_lesser; next_sample < prev_greater;)
599 array[next_sample++] = pivot;
600 if (index < next_lesser)
602 else if (index < prev_greater)
606 array + prev_greater,
607 count - prev_greater) + prev_greater;
618 int (*compar)(
const void*,
const void*)) {
629 if (compar (array, (
char *) array + size) < 0) {
630 return index >= 1 ? 1 : 0;
633 return index >= 1 ? 0 : 1;
638 else if (index >= count)
643 prev_greater =
count;
645 for (next_sample = 1; next_sample < prev_greater;) {
647 compar ((
char *) array + size * next_sample,
648 (
char *) array + size * next_lesser);
650 swap_entries (array, size, next_lesser++, next_sample++);
653 else if (result > 0) {
662 if (index < next_lesser)
664 else if (index < prev_greater)
668 (
char *) array + size * prev_greater,
669 count - prev_greater, size,
670 compar) + prev_greater;
687 ptr1 =
reinterpret_cast<char*
>(array) + index1 * size;
688 ptr2 =
reinterpret_cast<char*
>(array) + index2 * size;
689 for (count = 0; count < size; count++) {