All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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

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

Definition at line 642 of file statistc.cpp.

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

Definition at line 709 of file statistc.cpp.

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

Definition at line 770 of file statistc.cpp.

773  {
774  char tmp;
775  char *ptr1; // to entries
776  char *ptr2;
777  size_t count; // of bytes
778 
779  ptr1 = reinterpret_cast<char*>(array) + index1 * size;
780  ptr2 = reinterpret_cast<char*>(array) + index2 * size;
781  for (count = 0; count < size; count++) {
782  tmp = *ptr1;
783  *ptr1++ = *ptr2;
784  *ptr2++ = tmp; // tedious!
785  }
786 }
int count(LIST var_list)
Definition: oldlist.cpp:108