tesseract  4.0.0-beta.1-59-g2cc4
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_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 636 of file statistc.cpp.

636  {
637  int32_t next_sample; // next one to do
638  int32_t next_lesser; // space for new
639  int32_t prev_greater; // last one saved
640  int32_t 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_t) (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 }
Definition: cluster.h:32
int count(LIST var_list)
Definition: oldlist.cpp:103
int32_t choose_nth_item(int32_t index, float *array, int32_t count)
Definition: statistc.cpp:636

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

704  {
705  int result; // of compar
706  int32_t next_sample; // next one to do
707  int32_t next_lesser; // space for new
708  int32_t prev_greater; // last one saved
709  int32_t equal_count; // no of equal ones
710  int32_t 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_t) (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 }
void swap_entries(void *array, size_t size, int32_t index1, int32_t index2)
Definition: statistc.cpp:764
int count(LIST var_list)
Definition: oldlist.cpp:103
int32_t choose_nth_item(int32_t index, float *array, int32_t count)
Definition: statistc.cpp:636

◆ swap_entries()

void swap_entries ( void *  array,
size_t  size,
int32_t  index1,
int32_t  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