tesseract  4.1.1
statistc.cpp File Reference
#include "statistc.h"
#include <cstring>
#include <cmath>
#include <cstdlib>
#include "errcode.h"
#include "helpers.h"
#include "scrollview.h"
#include "tprintf.h"

Go to the source code of this file.

Functions

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

Function Documentation

◆ choose_nth_item() [1/2]

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

Definition at line 630 of file statistc.cpp.

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

◆ choose_nth_item() [2/2]

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

Definition at line 697 of file statistc.cpp.

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

◆ swap_entries()

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

Definition at line 758 of file statistc.cpp.

761  {
762  char tmp;
763  char *ptr1; // to entries
764  char *ptr2;
765  size_t count; // of bytes
766 
767  ptr1 = static_cast<char *>(array) + index1 * size;
768  ptr2 = static_cast<char *>(array) + index2 * size;
769  for (count = 0; count < size; count++) {
770  tmp = *ptr1;
771  *ptr1++ = *ptr2;
772  *ptr2++ = tmp; // tedious!
773  }
774 }
int count(LIST var_list)
Definition: oldlist.cpp:95