tesseract  3.05.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
statistc.h File Reference
#include <stdio.h>
#include "host.h"
#include "kdpair.h"
#include "scrollview.h"

Go to the source code of this file.

Classes

class  GenericVector< T >
 
class  STATS
 

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 641 of file statistc.cpp.

641  {
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 }
int count(LIST var_list)
Definition: oldlist.cpp:108
Definition: cluster.h:32
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:641
int inT32
Definition: host.h:102
inT32 choose_nth_item ( inT32  index,
void *  array,
inT32  count,
size_t  size,
int(*)(const void *, const void *)  compar 
)

Definition at line 708 of file statistc.cpp.

709  {
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 }
int count(LIST var_list)
Definition: oldlist.cpp:108
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:641
int inT32
Definition: host.h:102
void swap_entries(void *array, size_t size, inT32 index1, inT32 index2)
Definition: statistc.cpp:769
void swap_entries ( void *  array,
size_t  size,
inT32  index1,
inT32  index2 
)

Definition at line 769 of file statistc.cpp.

772  {
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