Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
statistc.cpp File Reference
#include "mfcpch.h"
#include "statistc.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "helpers.h"
#include "scrollview.h"
#include "tprintf.h"

Go to the source code of this file.

Functions

inT32 choose_nth_item (inT32 index, float *array, inT32 count)
 
inT32 choose_nth_item (inT32 index, void *array, inT32 count, size_t size, int(*compar)(const void *, const void *))
 
void swap_entries (void *array, size_t size, inT32 index1, inT32 index2)
 

Function Documentation

inT32 choose_nth_item ( inT32  index,
float *  array,
inT32  count 
)

Definition at line 550 of file statistc.cpp.

550  {
551  inT32 next_sample; // next one to do
552  inT32 next_lesser; // space for new
553  inT32 prev_greater; // last one saved
554  inT32 equal_count; // no of equal ones
555  float pivot; // proposed median
556  float sample; // current sample
557 
558  if (count <= 1)
559  return 0;
560  if (count == 2) {
561  if (array[0] < array[1]) {
562  return index >= 1 ? 1 : 0;
563  }
564  else {
565  return index >= 1 ? 0 : 1;
566  }
567  }
568  else {
569  if (index < 0)
570  index = 0; // ensure legal
571  else if (index >= count)
572  index = count - 1;
573  equal_count = (inT32) (rand() % count);
574  pivot = array[equal_count];
575  // fill gap
576  array[equal_count] = array[0];
577  next_lesser = 0;
578  prev_greater = count;
579  equal_count = 1;
580  for (next_sample = 1; next_sample < prev_greater;) {
581  sample = array[next_sample];
582  if (sample < pivot) {
583  // shuffle
584  array[next_lesser++] = sample;
585  next_sample++;
586  }
587  else if (sample > pivot) {
588  prev_greater--;
589  // juggle
590  array[next_sample] = array[prev_greater];
591  array[prev_greater] = sample;
592  }
593  else {
594  equal_count++;
595  next_sample++;
596  }
597  }
598  for (next_sample = next_lesser; next_sample < prev_greater;)
599  array[next_sample++] = pivot;
600  if (index < next_lesser)
601  return choose_nth_item (index, array, next_lesser);
602  else if (index < prev_greater)
603  return next_lesser; // in equal bracket
604  else
605  return choose_nth_item (index - prev_greater,
606  array + prev_greater,
607  count - prev_greater) + prev_greater;
608  }
609 }
Definition: cluster.h:32
int inT32
Definition: host.h:102
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:550
int count(LIST var_list)
Definition: oldlist.cpp:108
inT32 choose_nth_item ( inT32  index,
void *  array,
inT32  count,
size_t  size,
int(*)(const void *, const void *)  compar 
)

Definition at line 617 of file statistc.cpp.

618  {
619  int result; // of compar
620  inT32 next_sample; // next one to do
621  inT32 next_lesser; // space for new
622  inT32 prev_greater; // last one saved
623  inT32 equal_count; // no of equal ones
624  inT32 pivot; // proposed median
625 
626  if (count <= 1)
627  return 0;
628  if (count == 2) {
629  if (compar (array, (char *) array + size) < 0) {
630  return index >= 1 ? 1 : 0;
631  }
632  else {
633  return index >= 1 ? 0 : 1;
634  }
635  }
636  if (index < 0)
637  index = 0; // ensure legal
638  else if (index >= count)
639  index = count - 1;
640  pivot = (inT32) (rand () % count);
641  swap_entries (array, size, pivot, 0);
642  next_lesser = 0;
643  prev_greater = count;
644  equal_count = 1;
645  for (next_sample = 1; next_sample < prev_greater;) {
646  result =
647  compar ((char *) array + size * next_sample,
648  (char *) array + size * next_lesser);
649  if (result < 0) {
650  swap_entries (array, size, next_lesser++, next_sample++);
651  // shuffle
652  }
653  else if (result > 0) {
654  prev_greater--;
655  swap_entries(array, size, prev_greater, next_sample);
656  }
657  else {
658  equal_count++;
659  next_sample++;
660  }
661  }
662  if (index < next_lesser)
663  return choose_nth_item (index, array, next_lesser, size, compar);
664  else if (index < prev_greater)
665  return next_lesser; // in equal bracket
666  else
667  return choose_nth_item (index - prev_greater,
668  (char *) array + size * prev_greater,
669  count - prev_greater, size,
670  compar) + prev_greater;
671 }
int inT32
Definition: host.h:102
void swap_entries(void *array, size_t size, inT32 index1, inT32 index2)
Definition: statistc.cpp:678
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:550
int count(LIST var_list)
Definition: oldlist.cpp:108
void swap_entries ( void *  array,
size_t  size,
inT32  index1,
inT32  index2 
)

Definition at line 678 of file statistc.cpp.

681  {
682  char tmp;
683  char *ptr1; // to entries
684  char *ptr2;
685  size_t count; // of bytes
686 
687  ptr1 = reinterpret_cast<char*>(array) + index1 * size;
688  ptr2 = reinterpret_cast<char*>(array) + index2 * size;
689  for (count = 0; count < size; count++) {
690  tmp = *ptr1;
691  *ptr1++ = *ptr2;
692  *ptr2++ = tmp; // tedious!
693  }
694 }
int count(LIST var_list)
Definition: oldlist.cpp:108