tesseract  4.00.00dev
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

◆ choose_nth_item() [1/2]

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

Definition at line 638 of file statistc.cpp.

638  {
639  inT32 next_sample; // next one to do
640  inT32 next_lesser; // space for new
641  inT32 prev_greater; // last one saved
642  inT32 equal_count; // no of equal ones
643  float pivot; // proposed median
644  float sample; // current sample
645 
646  if (count <= 1)
647  return 0;
648  if (count == 2) {
649  if (array[0] < array[1]) {
650  return index >= 1 ? 1 : 0;
651  }
652  else {
653  return index >= 1 ? 0 : 1;
654  }
655  }
656  else {
657  if (index < 0)
658  index = 0; // ensure legal
659  else if (index >= count)
660  index = count - 1;
661  equal_count = (inT32) (rand() % count);
662  pivot = array[equal_count];
663  // fill gap
664  array[equal_count] = array[0];
665  next_lesser = 0;
666  prev_greater = count;
667  equal_count = 1;
668  for (next_sample = 1; next_sample < prev_greater;) {
669  sample = array[next_sample];
670  if (sample < pivot) {
671  // shuffle
672  array[next_lesser++] = sample;
673  next_sample++;
674  }
675  else if (sample > pivot) {
676  prev_greater--;
677  // juggle
678  array[next_sample] = array[prev_greater];
679  array[prev_greater] = sample;
680  }
681  else {
682  equal_count++;
683  next_sample++;
684  }
685  }
686  for (next_sample = next_lesser; next_sample < prev_greater;)
687  array[next_sample++] = pivot;
688  if (index < next_lesser)
689  return choose_nth_item (index, array, next_lesser);
690  else if (index < prev_greater)
691  return next_lesser; // in equal bracket
692  else
693  return choose_nth_item (index - prev_greater,
694  array + prev_greater,
695  count - prev_greater) + prev_greater;
696  }
697 }
int32_t inT32
Definition: host.h:38
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:638
Definition: cluster.h:32
int count(LIST var_list)
Definition: oldlist.cpp:103

◆ choose_nth_item() [2/2]

inT32 choose_nth_item ( inT32  index,
void *  array,
inT32  count,
size_t  size,
int(*)(const void *, const void *)  compar 
)

Definition at line 705 of file statistc.cpp.

706  {
707  int result; // of compar
708  inT32 next_sample; // next one to do
709  inT32 next_lesser; // space for new
710  inT32 prev_greater; // last one saved
711  inT32 equal_count; // no of equal ones
712  inT32 pivot; // proposed median
713 
714  if (count <= 1)
715  return 0;
716  if (count == 2) {
717  if (compar (array, (char *) array + size) < 0) {
718  return index >= 1 ? 1 : 0;
719  }
720  else {
721  return index >= 1 ? 0 : 1;
722  }
723  }
724  if (index < 0)
725  index = 0; // ensure legal
726  else if (index >= count)
727  index = count - 1;
728  pivot = (inT32) (rand () % count);
729  swap_entries (array, size, pivot, 0);
730  next_lesser = 0;
731  prev_greater = count;
732  equal_count = 1;
733  for (next_sample = 1; next_sample < prev_greater;) {
734  result =
735  compar ((char *) array + size * next_sample,
736  (char *) array + size * next_lesser);
737  if (result < 0) {
738  swap_entries (array, size, next_lesser++, next_sample++);
739  // shuffle
740  }
741  else if (result > 0) {
742  prev_greater--;
743  swap_entries(array, size, prev_greater, next_sample);
744  }
745  else {
746  equal_count++;
747  next_sample++;
748  }
749  }
750  if (index < next_lesser)
751  return choose_nth_item (index, array, next_lesser, size, compar);
752  else if (index < prev_greater)
753  return next_lesser; // in equal bracket
754  else
755  return choose_nth_item (index - prev_greater,
756  (char *) array + size * prev_greater,
757  count - prev_greater, size,
758  compar) + prev_greater;
759 }
void swap_entries(void *array, size_t size, inT32 index1, inT32 index2)
Definition: statistc.cpp:766
int32_t inT32
Definition: host.h:38
voidpf void uLong size
Definition: ioapi.h:39
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
Definition: statistc.cpp:638
int count(LIST var_list)
Definition: oldlist.cpp:103

◆ swap_entries()

void swap_entries ( void *  array,
size_t  size,
inT32  index1,
inT32  index2 
)

Definition at line 766 of file statistc.cpp.

769  {
770  char tmp;
771  char *ptr1; // to entries
772  char *ptr2;
773  size_t count; // of bytes
774 
775  ptr1 = static_cast<char*>(array) + index1 * size;
776  ptr2 = static_cast<char*>(array) + index2 * size;
777  for (count = 0; count < size; count++) {
778  tmp = *ptr1;
779  *ptr1++ = *ptr2;
780  *ptr2++ = tmp; // tedious!
781  }
782 }
voidpf void uLong size
Definition: ioapi.h:39
int count(LIST var_list)
Definition: oldlist.cpp:103