tesseract  4.00.00dev
statistc.cpp File Reference
#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

◆ choose_nth_item() [1/2]

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

Definition at line 636 of file statistc.cpp.

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

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

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

◆ swap_entries()

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

Definition at line 764 of file statistc.cpp.

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