Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SortHelper< T > Class Template Reference

#include <sorthelper.h>

Classes

struct  SortPair
 

Public Member Functions

 SortHelper (int sizehint)
 
void Add (T value, int count)
 
int MaxCount (T *max_value) const
 
const GenericVector< SortPair
< T > > & 
SortByCount ()
 
const GenericVector< SortPair
< T > > & 
SortByValue ()
 

Static Public Member Functions

static int SortPairsByCount (const void *v1, const void *v2)
 
static int SortPairsByValue (const void *v1, const void *v2)
 

Detailed Description

template<typename T>
class SortHelper< T >

Definition at line 36 of file sorthelper.h.

Constructor & Destructor Documentation

template<typename T >
SortHelper< T >::SortHelper ( int  sizehint)
inlineexplicit

Definition at line 59 of file sorthelper.h.

59 : counts_(sizehint) {}

Member Function Documentation

template<typename T >
void SortHelper< T >::Add ( value,
int  count 
)
inline

Definition at line 63 of file sorthelper.h.

63  {
64  // Linear search for value.
65  for (int i = 0; i < counts_.size(); ++i) {
66  if (counts_[i].value == value) {
67  counts_[i].count += count;
68  return;
69  }
70  }
71  SortPair<T> new_pair = {value, count};
72  counts_.push_back(SortPair<T>(new_pair));
73  }
int count(LIST var_list)
Definition: oldlist.cpp:108
template<typename T >
int SortHelper< T >::MaxCount ( T *  max_value) const
inline

Definition at line 78 of file sorthelper.h.

78  {
79  int best_count = -MAX_INT32;
80  for (int i = 0; i < counts_.size(); ++i) {
81  if (counts_[i].count > best_count) {
82  best_count = counts_[i].count;
83  if (max_value != NULL)
84  *max_value = counts_[i].value;
85  }
86  }
87  return best_count;
88  }
#define NULL
Definition: host.h:144
#define MAX_INT32
Definition: host.h:120
int count(LIST var_list)
Definition: oldlist.cpp:108
template<typename T >
const GenericVector<SortPair<T> >& SortHelper< T >::SortByCount ( )
inline

Definition at line 91 of file sorthelper.h.

91  {
92  counts_.sort(&SortPairsByCount);
93  return counts_;
94  }
static int SortPairsByCount(const void *v1, const void *v2)
Definition: sorthelper.h:44
template<typename T >
const GenericVector<SortPair<T> >& SortHelper< T >::SortByValue ( )
inline

Definition at line 96 of file sorthelper.h.

96  {
97  counts_.sort(&SortPairsByValue);
98  return counts_;
99  }
static int SortPairsByValue(const void *v1, const void *v2)
Definition: sorthelper.h:50
template<typename T >
static int SortHelper< T >::SortPairsByCount ( const void *  v1,
const void *  v2 
)
inlinestatic

Definition at line 44 of file sorthelper.h.

44  {
45  const SortPair<T>* p1 = reinterpret_cast<const SortPair<T>*>(v1);
46  const SortPair<T>* p2 = reinterpret_cast<const SortPair<T>*>(v2);
47  return p2->count - p1->count;
48  }
template<typename T >
static int SortHelper< T >::SortPairsByValue ( const void *  v1,
const void *  v2 
)
inlinestatic

Definition at line 50 of file sorthelper.h.

50  {
51  const SortPair<T>* p1 = reinterpret_cast<const SortPair<T>*>(v1);
52  const SortPair<T>* p2 = reinterpret_cast<const SortPair<T>*>(v2);
53  if (p2->value - p1->value < 0) return -1;
54  if (p2->value - p1->value > 0) return 1;
55  return 0;
56  }

The documentation for this class was generated from the following file: