tesseract  3.05.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
statistc.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: statistc.c (Formerly stats.c)
3  * Description: Simple statistical package for integer values.
4  * Author: Ray Smith
5  * Created: Mon Feb 04 16:56:05 GMT 1991
6  *
7  * (C) Copyright 1991, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 // Include automatically generated configuration file if running autoconf.
21 #ifdef HAVE_CONFIG_H
22 #include "config_auto.h"
23 #endif
24 
25 #include "statistc.h"
26 #include <string.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include "helpers.h"
30 #include "scrollview.h"
31 #include "tprintf.h"
32 
34 
35 /**********************************************************************
36  * STATS::STATS
37  *
38  * Construct a new stats element by allocating and zeroing the memory.
39  **********************************************************************/
40 STATS::STATS(inT32 min_bucket_value, inT32 max_bucket_value_plus_1) {
41  if (max_bucket_value_plus_1 <= min_bucket_value) {
42  min_bucket_value = 0;
43  max_bucket_value_plus_1 = 1;
44  }
45  rangemin_ = min_bucket_value; // setup
46  rangemax_ = max_bucket_value_plus_1;
47  buckets_ = new inT32[rangemax_ - rangemin_];
48  clear();
49 }
50 
52  rangemax_ = 0;
53  rangemin_ = 0;
54  buckets_ = NULL;
55 }
56 
57 /**********************************************************************
58  * STATS::set_range
59  *
60  * Alter the range on an existing stats element.
61  **********************************************************************/
62 bool STATS::set_range(inT32 min_bucket_value, inT32 max_bucket_value_plus_1) {
63  if (max_bucket_value_plus_1 <= min_bucket_value) {
64  return false;
65  }
66  if (rangemax_ - rangemin_ != max_bucket_value_plus_1 - min_bucket_value) {
67  delete [] buckets_;
68  buckets_ = new inT32[max_bucket_value_plus_1 - min_bucket_value];
69  }
70  rangemin_ = min_bucket_value; // setup
71  rangemax_ = max_bucket_value_plus_1;
72  clear(); // zero it
73  return true;
74 }
75 
76 /**********************************************************************
77  * STATS::clear
78  *
79  * Clear out the STATS class by zeroing all the buckets.
80  **********************************************************************/
81 void STATS::clear() { // clear out buckets
82  total_count_ = 0;
83  if (buckets_ != NULL)
84  memset(buckets_, 0, (rangemax_ - rangemin_) * sizeof(buckets_[0]));
85 }
86 
87 /**********************************************************************
88  * STATS::~STATS
89  *
90  * Destructor for a stats class.
91  **********************************************************************/
93  if (buckets_ != NULL) {
94  delete [] buckets_;
95  buckets_ = NULL;
96  }
97 }
98 
99 /**********************************************************************
100  * STATS::add
101  *
102  * Add a set of samples to (or delete from) a pile.
103  **********************************************************************/
104 void STATS::add(inT32 value, inT32 count) {
105  if (buckets_ == NULL) {
106  return;
107  }
108  value = ClipToRange(value, rangemin_, rangemax_ - 1);
109  buckets_[value - rangemin_] += count;
110  total_count_ += count; // keep count of total
111 }
112 
113 /**********************************************************************
114  * STATS::mode
115  *
116  * Find the mode of a stats class.
117  **********************************************************************/
118 inT32 STATS::mode() const { // get mode of samples
119  if (buckets_ == NULL) {
120  return rangemin_;
121  }
122  inT32 max = buckets_[0]; // max cell count
123  inT32 maxindex = 0; // index of max
124  for (int index = rangemax_ - rangemin_ - 1; index > 0; --index) {
125  if (buckets_[index] > max) {
126  max = buckets_[index]; // find biggest
127  maxindex = index;
128  }
129  }
130  return maxindex + rangemin_; // index of biggest
131 }
132 
133 /**********************************************************************
134  * STATS::mean
135  *
136  * Find the mean of a stats class.
137  **********************************************************************/
138 double STATS::mean() const { //get mean of samples
139  if (buckets_ == NULL || total_count_ <= 0) {
140  return static_cast<double>(rangemin_);
141  }
142  inT64 sum = 0;
143  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
144  sum += static_cast<inT64>(index) * buckets_[index];
145  }
146  return static_cast<double>(sum) / total_count_ + rangemin_;
147 }
148 
149 /**********************************************************************
150  * STATS::sd
151  *
152  * Find the standard deviation of a stats class.
153  **********************************************************************/
154 double STATS::sd() const { //standard deviation
155  if (buckets_ == NULL || total_count_ <= 0) {
156  return 0.0;
157  }
158  inT64 sum = 0;
159  double sqsum = 0.0;
160  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
161  sum += static_cast<inT64>(index) * buckets_[index];
162  sqsum += static_cast<double>(index) * index * buckets_[index];
163  }
164  double variance = static_cast<double>(sum) / total_count_;
165  variance = sqsum / total_count_ - variance * variance;
166  if (variance > 0.0)
167  return sqrt(variance);
168  return 0.0;
169 }
170 
171 /**********************************************************************
172  * STATS::ile
173  *
174  * Returns the fractile value such that frac fraction (in [0,1]) of samples
175  * has a value less than the return value.
176  **********************************************************************/
177 double STATS::ile(double frac) const {
178  if (buckets_ == NULL || total_count_ == 0) {
179  return static_cast<double>(rangemin_);
180  }
181 #if 0
182  // TODO(rays) The existing code doesn't seem to be doing the right thing
183  // with target a double but this substitute crashes the code that uses it.
184  // Investigate and fix properly.
185  int target = IntCastRounded(frac * total_count_);
186  target = ClipToRange(target, 1, total_count_);
187 #else
188  double target = frac * total_count_;
189  target = ClipToRange(target, 1.0, static_cast<double>(total_count_));
190 #endif
191  int sum = 0;
192  int index = 0;
193  for (index = 0; index < rangemax_ - rangemin_ && sum < target;
194  sum += buckets_[index++]);
195  if (index > 0) {
196  ASSERT_HOST(buckets_[index - 1] > 0);
197  return rangemin_ + index -
198  static_cast<double>(sum - target) / buckets_[index - 1];
199  } else {
200  return static_cast<double>(rangemin_);
201  }
202 }
203 
204 /**********************************************************************
205  * STATS::min_bucket
206  *
207  * Find REAL minimum bucket - ile(0.0) isn't necessarily correct
208  **********************************************************************/
209 inT32 STATS::min_bucket() const { // Find min
210  if (buckets_ == NULL || total_count_ == 0) {
211  return rangemin_;
212  }
213  inT32 min = 0;
214  for (min = 0; (min < rangemax_ - rangemin_) && (buckets_[min] == 0); min++);
215  return rangemin_ + min;
216 }
217 
218 /**********************************************************************
219  * STATS::max_bucket
220  *
221  * Find REAL maximum bucket - ile(1.0) isn't necessarily correct
222  **********************************************************************/
223 
224 inT32 STATS::max_bucket() const { // Find max
225  if (buckets_ == NULL || total_count_ == 0) {
226  return rangemin_;
227  }
228  inT32 max;
229  for (max = rangemax_ - rangemin_ - 1; max > 0 && buckets_[max] == 0; max--);
230  return rangemin_ + max;
231 }
232 
233 /**********************************************************************
234  * STATS::median
235  *
236  * Finds a more useful estimate of median than ile(0.5).
237  *
238  * Overcomes a problem with ile() - if the samples are, for example,
239  * 6,6,13,14 ile(0.5) return 7.0 - when a more useful value would be midway
240  * between 6 and 13 = 9.5
241  **********************************************************************/
242 double STATS::median() const { //get median
243  if (buckets_ == NULL) {
244  return static_cast<double>(rangemin_);
245  }
246  double median = ile(0.5);
247  int median_pile = static_cast<int>(floor(median));
248  if ((total_count_ > 1) && (pile_count(median_pile) == 0)) {
249  inT32 min_pile;
250  inT32 max_pile;
251  /* Find preceding non zero pile */
252  for (min_pile = median_pile; pile_count(min_pile) == 0; min_pile--);
253  /* Find following non zero pile */
254  for (max_pile = median_pile; pile_count(max_pile) == 0; max_pile++);
255  median = (min_pile + max_pile) / 2.0;
256  }
257  return median;
258 }
259 
260 /**********************************************************************
261  * STATS::local_min
262  *
263  * Return TRUE if this point is a local min.
264  **********************************************************************/
265 bool STATS::local_min(inT32 x) const {
266  if (buckets_ == NULL) {
267  return false;
268  }
269  x = ClipToRange(x, rangemin_, rangemax_ - 1) - rangemin_;
270  if (buckets_[x] == 0)
271  return true;
272  inT32 index; // table index
273  for (index = x - 1; index >= 0 && buckets_[index] == buckets_[x]; --index);
274  if (index >= 0 && buckets_[index] < buckets_[x])
275  return false;
276  for (index = x + 1; index < rangemax_ - rangemin_ &&
277  buckets_[index] == buckets_[x]; ++index);
278  if (index < rangemax_ - rangemin_ && buckets_[index] < buckets_[x])
279  return false;
280  else
281  return true;
282 }
283 
284 /**********************************************************************
285  * STATS::smooth
286  *
287  * Apply a triangular smoothing filter to the stats.
288  * This makes the modes a bit more useful.
289  * The factor gives the height of the triangle, i.e. the weight of the
290  * centre.
291  **********************************************************************/
292 void STATS::smooth(inT32 factor) {
293  if (buckets_ == NULL || factor < 2) {
294  return;
295  }
296  STATS result(rangemin_, rangemax_);
297  int entrycount = rangemax_ - rangemin_;
298  for (int entry = 0; entry < entrycount; entry++) {
299  //centre weight
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);
306  }
307  result.add(entry + rangemin_, count);
308  }
309  total_count_ = result.total_count_;
310  memcpy(buckets_, result.buckets_, entrycount * sizeof(buckets_[0]));
311 }
312 
313 /**********************************************************************
314  * STATS::cluster
315  *
316  * Cluster the samples into max_cluster clusters.
317  * Each call runs one iteration. The array of clusters must be
318  * max_clusters+1 in size as cluster 0 is used to indicate which samples
319  * have been used.
320  * The return value is the current number of clusters.
321  **********************************************************************/
322 
323 inT32 STATS::cluster(float lower, // thresholds
324  float upper,
325  float multiple, // distance threshold
326  inT32 max_clusters, // max no to make
327  STATS *clusters) { // array of clusters
328  BOOL8 new_cluster; // added one
329  float *centres; // cluster centres
330  inT32 entry; // bucket index
331  inT32 cluster; // cluster index
332  inT32 best_cluster; // one to assign to
333  inT32 new_centre = 0; // residual mode
334  inT32 new_mode; // pile count of new_centre
335  inT32 count; // pile to place
336  float dist; // from cluster
337  float min_dist; // from best_cluster
338  inT32 cluster_count; // no of clusters
339 
340  if (buckets_ == NULL || max_clusters < 1)
341  return 0;
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;
346  cluster_count++) {
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_
352  && pile_count(entry) <= pile_count(entry + 1);
353  entry--) {
354  count = pile_count(entry) - clusters[0].pile_count(entry);
355  if (count > 0) {
356  clusters[cluster_count].add(entry, count);
357  clusters[0].add (entry, count);
358  }
359  }
360  for (entry = new_centre + 1; entry - centres[cluster_count] < lower
361  && entry < rangemax_
362  && pile_count(entry) <= pile_count(entry - 1);
363  entry++) {
364  count = pile_count(entry) - clusters[0].pile_count(entry);
365  if (count > 0) {
366  clusters[cluster_count].add(entry, count);
367  clusters[0].add(entry, count);
368  }
369  }
370  }
371  cluster_count--;
372 
373  if (cluster_count == 0) {
374  clusters[0].set_range(rangemin_, rangemax_);
375  }
376  do {
377  new_cluster = FALSE;
378  new_mode = 0;
379  for (entry = 0; entry < rangemax_ - rangemin_; entry++) {
380  count = buckets_[entry] - clusters[0].buckets_[entry];
381  //remaining pile
382  if (count > 0) { //any to handle
383  min_dist = static_cast<float>(MAX_INT32);
384  best_cluster = 0;
385  for (cluster = 1; cluster <= cluster_count; cluster++) {
386  dist = entry + rangemin_ - centres[cluster];
387  //find distance
388  if (dist < 0)
389  dist = -dist;
390  if (dist < min_dist) {
391  min_dist = dist; //find least
392  best_cluster = cluster;
393  }
394  }
395  if (min_dist > upper //far enough for new
396  && (best_cluster == 0
397  || entry + rangemin_ > centres[best_cluster] * multiple
398  || entry + rangemin_ < centres[best_cluster] / multiple)) {
399  if (count > new_mode) {
400  new_mode = count;
401  new_centre = entry + rangemin_;
402  }
403  }
404  }
405  }
406  // need new and room
407  if (new_mode > 0 && cluster_count < max_clusters) {
408  cluster_count++;
409  new_cluster = TRUE;
410  if (!clusters[cluster_count].set_range(rangemin_, rangemax_)) {
411  delete [] centres;
412  return 0;
413  }
414  centres[cluster_count] = static_cast<float>(new_centre);
415  clusters[cluster_count].add(new_centre, new_mode);
416  clusters[0].add(new_centre, new_mode);
417  for (entry = new_centre - 1; centres[cluster_count] - entry < lower
418  && entry >= rangemin_
419  && pile_count (entry) <= pile_count(entry + 1); entry--) {
420  count = pile_count(entry) - clusters[0].pile_count(entry);
421  if (count > 0) {
422  clusters[cluster_count].add(entry, count);
423  clusters[0].add(entry, count);
424  }
425  }
426  for (entry = new_centre + 1; entry - centres[cluster_count] < lower
427  && entry < rangemax_
428  && pile_count (entry) <= pile_count(entry - 1); entry++) {
429  count = pile_count(entry) - clusters[0].pile_count(entry);
430  if (count > 0) {
431  clusters[cluster_count].add(entry, count);
432  clusters[0].add (entry, count);
433  }
434  }
435  centres[cluster_count] =
436  static_cast<float>(clusters[cluster_count].ile(0.5));
437  }
438  } while (new_cluster && cluster_count < max_clusters);
439  delete [] centres;
440  return cluster_count;
441 }
442 
443 // Helper tests that the current index is still part of the peak and gathers
444 // the data into the peak, returning false when the peak is ended.
445 // src_buckets[index] - used_buckets[index] is the unused part of the histogram.
446 // prev_count is the histogram count of the previous index on entry and is
447 // updated to the current index on return.
448 // total_count and total_value are accumulating the mean of the peak.
449 static bool GatherPeak(int index, const int* src_buckets, int* used_buckets,
450  int* prev_count, int* total_count, double* total_value) {
451  int pile_count = src_buckets[index] - used_buckets[index];
452  if (pile_count <= *prev_count && pile_count > 0) {
453  // Accumulate count and index.count product.
454  *total_count += pile_count;
455  *total_value += index * pile_count;
456  // Mark this index as used
457  used_buckets[index] = src_buckets[index];
458  *prev_count = pile_count;
459  return true;
460  } else {
461  return false;
462  }
463 }
464 
465 // Finds (at most) the top max_modes modes, well actually the whole peak around
466 // each mode, returning them in the given modes vector as a <mean of peak,
467 // total count of peak> pair in order of decreasing total count.
468 // Since the mean is the key and the count the data in the pair, a single call
469 // to sort on the output will re-sort by increasing mean of peak if that is
470 // more useful than decreasing total count.
471 // Returns the actual number of modes found.
472 int STATS::top_n_modes(int max_modes,
473  GenericVector<KDPairInc<float, int> >* modes) const {
474  if (max_modes <= 0) return 0;
475  int src_count = rangemax_ - rangemin_;
476  // Used copies the counts in buckets_ as they get used.
477  STATS used(rangemin_, rangemax_);
478  modes->truncate(0);
479  // Total count of the smallest peak found so far.
480  int least_count = 1;
481  // Mode that is used as a seed for each peak
482  int max_count = 0;
483  do {
484  // Find an unused mode.
485  max_count = 0;
486  int max_index = 0;
487  for (int src_index = 0; src_index < src_count; src_index++) {
488  int pile_count = buckets_[src_index] - used.buckets_[src_index];
489  if (pile_count > max_count) {
490  max_count = pile_count;
491  max_index = src_index;
492  }
493  }
494  if (max_count > 0) {
495  // Copy the bucket count to used so it doesn't get found again.
496  used.buckets_[max_index] = max_count;
497  // Get the entire peak.
498  double total_value = max_index * max_count;
499  int total_count = max_count;
500  int prev_pile = max_count;
501  for (int offset = 1; max_index + offset < src_count; ++offset) {
502  if (!GatherPeak(max_index + offset, buckets_, used.buckets_,
503  &prev_pile, &total_count, &total_value))
504  break;
505  }
506  prev_pile = buckets_[max_index];
507  for (int offset = 1; max_index - offset >= 0; ++offset) {
508  if (!GatherPeak(max_index - offset, buckets_, used.buckets_,
509  &prev_pile, &total_count, &total_value))
510  break;
511  }
512  if (total_count > least_count || modes->size() < max_modes) {
513  // We definitely want this mode, so if we have enough discard the least.
514  if (modes->size() == max_modes)
515  modes->truncate(max_modes - 1);
516  int target_index = 0;
517  // Linear search for the target insertion point.
518  while (target_index < modes->size() &&
519  (*modes)[target_index].data >= total_count)
520  ++target_index;
521  float peak_mean =
522  static_cast<float>(total_value / total_count + rangemin_);
523  modes->insert(KDPairInc<float, int>(peak_mean, total_count),
524  target_index);
525  least_count = modes->back().data;
526  }
527  }
528  } while (max_count > 0);
529  return modes->size();
530 }
531 
532 /**********************************************************************
533  * STATS::print
534  *
535  * Prints a summary and table of the histogram.
536  **********************************************************************/
537 void STATS::print() const {
538  if (buckets_ == NULL) {
539  return;
540  }
541  inT32 min = min_bucket() - rangemin_;
542  inT32 max = max_bucket() - rangemin_;
543 
544  int num_printed = 0;
545  for (int index = min; index <= max; index++) {
546  if (buckets_[index] != 0) {
547  tprintf("%4d:%-3d ", rangemin_ + index, buckets_[index]);
548  if (++num_printed % 8 == 0)
549  tprintf ("\n");
550  }
551  }
552  tprintf ("\n");
553  print_summary();
554 }
555 
556 
557 
558 /**********************************************************************
559  * STATS::print_summary
560  *
561  * Print a summary of the stats.
562  **********************************************************************/
563 void STATS::print_summary() const {
564  if (buckets_ == NULL) {
565  return;
566  }
567  inT32 min = min_bucket();
568  inT32 max = max_bucket();
569  tprintf("Total count=%d\n", total_count_);
570  tprintf("Min=%.2f Really=%d\n", ile(0.0), min);
571  tprintf("Lower quartile=%.2f\n", ile(0.25));
572  tprintf("Median=%.2f, ile(0.5)=%.2f\n", median(), ile(0.5));
573  tprintf("Upper quartile=%.2f\n", ile(0.75));
574  tprintf("Max=%.2f Really=%d\n", ile(1.0), max);
575  tprintf("Range=%d\n", max + 1 - min);
576  tprintf("Mean= %.2f\n", mean());
577  tprintf("SD= %.2f\n", sd());
578 }
579 
580 
581 /**********************************************************************
582  * STATS::plot
583  *
584  * Draw a histogram of the stats table.
585  **********************************************************************/
586 
587 #ifndef GRAPHICS_DISABLED
588 void STATS::plot(ScrollView* window, // to draw in
589  float xorigin, // bottom left
590  float yorigin,
591  float xscale, // one x unit
592  float yscale, // one y unit
593  ScrollView::Color colour) const { // colour to draw in
594  if (buckets_ == NULL) {
595  return;
596  }
597  window->Pen(colour);
598 
599  for (int index = 0; index < rangemax_ - rangemin_; index++) {
600  window->Rectangle( xorigin + xscale * index, yorigin,
601  xorigin + xscale * (index + 1),
602  yorigin + yscale * buckets_[index]);
603  }
604 }
605 #endif
606 
607 
608 /**********************************************************************
609  * STATS::plotline
610  *
611  * Draw a histogram of the stats table. (Line only)
612  **********************************************************************/
613 
614 #ifndef GRAPHICS_DISABLED
615 void STATS::plotline(ScrollView* window, // to draw in
616  float xorigin, // bottom left
617  float yorigin,
618  float xscale, // one x unit
619  float yscale, // one y unit
620  ScrollView::Color colour) const { // colour to draw in
621  if (buckets_ == NULL) {
622  return;
623  }
624  window->Pen(colour);
625  window->SetCursor(xorigin, yorigin + yscale * buckets_[0]);
626  for (int index = 0; index < rangemax_ - rangemin_; index++) {
627  window->DrawTo(xorigin + xscale * index,
628  yorigin + yscale * buckets_[index]);
629  }
630 }
631 #endif
632 
633 
634 /**********************************************************************
635  * choose_nth_item
636  *
637  * Returns the index of what would b the nth item in the array
638  * if the members were sorted, without actually sorting.
639  **********************************************************************/
640 
641 inT32 choose_nth_item(inT32 index, float *array, inT32 count) {
642  inT32 next_sample; // next one to do
643  inT32 next_lesser; // space for new
644  inT32 prev_greater; // last one saved
645  inT32 equal_count; // no of equal ones
646  float pivot; // proposed median
647  float sample; // current sample
648 
649  if (count <= 1)
650  return 0;
651  if (count == 2) {
652  if (array[0] < array[1]) {
653  return index >= 1 ? 1 : 0;
654  }
655  else {
656  return index >= 1 ? 0 : 1;
657  }
658  }
659  else {
660  if (index < 0)
661  index = 0; // ensure legal
662  else if (index >= count)
663  index = count - 1;
664  equal_count = (inT32) (rand() % count);
665  pivot = array[equal_count];
666  // fill gap
667  array[equal_count] = array[0];
668  next_lesser = 0;
669  prev_greater = count;
670  equal_count = 1;
671  for (next_sample = 1; next_sample < prev_greater;) {
672  sample = array[next_sample];
673  if (sample < pivot) {
674  // shuffle
675  array[next_lesser++] = sample;
676  next_sample++;
677  }
678  else if (sample > pivot) {
679  prev_greater--;
680  // juggle
681  array[next_sample] = array[prev_greater];
682  array[prev_greater] = sample;
683  }
684  else {
685  equal_count++;
686  next_sample++;
687  }
688  }
689  for (next_sample = next_lesser; next_sample < prev_greater;)
690  array[next_sample++] = pivot;
691  if (index < next_lesser)
692  return choose_nth_item (index, array, next_lesser);
693  else if (index < prev_greater)
694  return next_lesser; // in equal bracket
695  else
696  return choose_nth_item (index - prev_greater,
697  array + prev_greater,
698  count - prev_greater) + prev_greater;
699  }
700 }
701 
702 /**********************************************************************
703  * choose_nth_item
704  *
705  * Returns the index of what would be the nth item in the array
706  * if the members were sorted, without actually sorting.
707  **********************************************************************/
708 inT32 choose_nth_item(inT32 index, void *array, inT32 count, size_t size,
709  int (*compar)(const void*, const void*)) {
710  int result; // of compar
711  inT32 next_sample; // next one to do
712  inT32 next_lesser; // space for new
713  inT32 prev_greater; // last one saved
714  inT32 equal_count; // no of equal ones
715  inT32 pivot; // proposed median
716 
717  if (count <= 1)
718  return 0;
719  if (count == 2) {
720  if (compar (array, (char *) array + size) < 0) {
721  return index >= 1 ? 1 : 0;
722  }
723  else {
724  return index >= 1 ? 0 : 1;
725  }
726  }
727  if (index < 0)
728  index = 0; // ensure legal
729  else if (index >= count)
730  index = count - 1;
731  pivot = (inT32) (rand () % count);
732  swap_entries (array, size, pivot, 0);
733  next_lesser = 0;
734  prev_greater = count;
735  equal_count = 1;
736  for (next_sample = 1; next_sample < prev_greater;) {
737  result =
738  compar ((char *) array + size * next_sample,
739  (char *) array + size * next_lesser);
740  if (result < 0) {
741  swap_entries (array, size, next_lesser++, next_sample++);
742  // shuffle
743  }
744  else if (result > 0) {
745  prev_greater--;
746  swap_entries(array, size, prev_greater, next_sample);
747  }
748  else {
749  equal_count++;
750  next_sample++;
751  }
752  }
753  if (index < next_lesser)
754  return choose_nth_item (index, array, next_lesser, size, compar);
755  else if (index < prev_greater)
756  return next_lesser; // in equal bracket
757  else
758  return choose_nth_item (index - prev_greater,
759  (char *) array + size * prev_greater,
760  count - prev_greater, size,
761  compar) + prev_greater;
762 }
763 
764 /**********************************************************************
765  * swap_entries
766  *
767  * Swap 2 entries of arbitrary size in-place in a table.
768  **********************************************************************/
769 void swap_entries(void *array, // array of entries
770  size_t size, // size of entry
771  inT32 index1, // entries to swap
772  inT32 index2) {
773  char tmp;
774  char *ptr1; // to entries
775  char *ptr2;
776  size_t count; // of bytes
777 
778  ptr1 = reinterpret_cast<char*>(array) + index1 * size;
779  ptr2 = reinterpret_cast<char*>(array) + index2 * size;
780  for (count = 0; count < size; count++) {
781  tmp = *ptr1;
782  *ptr1++ = *ptr2;
783  *ptr2++ = tmp; // tedious!
784  }
785 }
int count(LIST var_list)
Definition: oldlist.cpp:108
#define TRUE
Definition: capi.h:45
Definition: cluster.h:32
inT32 mode() const
Definition: statistc.cpp:118
inT32 max_bucket() const
Definition: statistc.cpp:224
#define MAX_INT32
Definition: host.h:120
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:641
bool local_min(inT32 x) const
Definition: statistc.cpp:265
int inT32
Definition: host.h:102
void SetCursor(int x, int y)
Definition: scrollview.cpp:525
double mean() const
Definition: statistc.cpp:138
double ile(double frac) const
Definition: statistc.cpp:177
#define tprintf(...)
Definition: tprintf.h:31
void Pen(Color color)
Definition: scrollview.cpp:726
inT32 min_bucket() const
Definition: statistc.cpp:209
#define FALSE
Definition: capi.h:46
void smooth(inT32 factor)
Definition: statistc.cpp:292
void plotline(ScrollView *window, float xorigin, float yorigin, float xscale, float yscale, ScrollView::Color colour) const
Definition: statistc.cpp:615
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
Definition: statistc.h:33
long long int inT64
Definition: host.h:108
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:606
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
void swap_entries(void *array, size_t size, inT32 index1, inT32 index2)
Definition: statistc.cpp:769
void clear()
Definition: statistc.cpp:81
double sd() const
Definition: statistc.cpp:154
void print_summary() const
Definition: statistc.cpp:563
STATS()
Definition: statistc.cpp:51
~STATS()
Definition: statistc.cpp:92
int top_n_modes(int max_modes, GenericVector< tesseract::KDPairInc< float, int > > *modes) const
Definition: statistc.cpp:472
int IntCastRounded(double x)
Definition: helpers.h:172
unsigned char BOOL8
Definition: host.h:113
#define ASSERT_HOST(x)
Definition: errcode.h:84
void plot(ScrollView *window, float xorigin, float yorigin, float xscale, float yscale, ScrollView::Color colour) const
Definition: statistc.cpp:588
bool set_range(inT32 min_bucket_value, inT32 max_bucket_value_plus_1)
Definition: statistc.cpp:62
double median() const
Definition: statistc.cpp:242
void DrawTo(int x, int y)
Definition: scrollview.cpp:531
inT32 cluster(float lower, float upper, float multiple, inT32 max_clusters, STATS *clusters)
Definition: statistc.cpp:323
void add(inT32 value, inT32 count)
Definition: statistc.cpp:104
void print() const
Definition: statistc.cpp:537