tesseract  4.0.0-1-g2a2b
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 637 of file statistc.cpp.

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

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

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

◆ swap_entries()

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

Definition at line 765 of file statistc.cpp.

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