tesseract  5.0.0-alpha-619-ge9db
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

◆ SortHelper()

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

Definition at line 59 of file sorthelper.h.

59  {
60  counts_.reserve(sizehint);
61  }

Member Function Documentation

◆ Add()

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

Definition at line 65 of file sorthelper.h.

65  {
66  // Linear search for value.
67  for (int i = 0; i < counts_.size(); ++i) {
68  if (counts_[i].value == value) {
69  counts_[i].count += count;
70  return;
71  }
72  }
73  SortPair<T> new_pair = {value, count};
74  counts_.push_back(SortPair<T>(new_pair));
75  }

◆ MaxCount()

template<typename T >
int SortHelper< T >::MaxCount ( T *  max_value) const
inline

Definition at line 80 of file sorthelper.h.

80  {
81  int best_count = -INT32_MAX;
82  for (int i = 0; i < counts_.size(); ++i) {
83  if (counts_[i].count > best_count) {
84  best_count = counts_[i].count;
85  if (max_value != nullptr)
86  *max_value = counts_[i].value;
87  }
88  }
89  return best_count;
90  }

◆ SortByCount()

template<typename T >
const GenericVector<SortPair<T> >& SortHelper< T >::SortByCount ( )
inline

Definition at line 93 of file sorthelper.h.

93  {
94  counts_.sort(&SortPairsByCount);
95  return counts_;
96  }

◆ SortByValue()

template<typename T >
const GenericVector<SortPair<T> >& SortHelper< T >::SortByValue ( )
inline

Definition at line 98 of file sorthelper.h.

98  {
99  counts_.sort(&SortPairsByValue);
100  return counts_;
101  }

◆ SortPairsByCount()

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 auto* p1 = static_cast<const SortPair<T>*>(v1);
46  const auto* p2 = static_cast<const SortPair<T>*>(v2);
47  return p2->count - p1->count;
48  }

◆ SortPairsByValue()

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 auto* p1 = static_cast<const SortPair<T>*>(v1);
52  const auto* p2 = static_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:
SortHelper::SortPairsByCount
static int SortPairsByCount(const void *v1, const void *v2)
Definition: sorthelper.h:44
count
int count(LIST var_list)
Definition: oldlist.cpp:79
SortHelper::SortPairsByValue
static int SortPairsByValue(const void *v1, const void *v2)
Definition: sorthelper.h:50