tesseract v5.3.3.20231005
tesseract::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 std::vector< SortPair< T > > & SortByCount ()
 
const std::vector< 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 tesseract::SortHelper< T >

Definition at line 37 of file sorthelper.h.

Constructor & Destructor Documentation

◆ SortHelper()

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

Definition at line 65 of file sorthelper.h.

65 {
66 counts_.reserve(sizehint);
67 }

Member Function Documentation

◆ Add()

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

Definition at line 71 of file sorthelper.h.

71 {
72 // Linear search for value.
73 for (auto &it : counts_) {
74 if (it.value == value) {
75 it.count += count;
76 return;
77 }
78 }
79 SortPair<T> new_pair = {value, count};
80 counts_.push_back(SortPair<T>(new_pair));
81 }
int value
int * count

◆ MaxCount()

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

Definition at line 86 of file sorthelper.h.

86 {
87 int best_count = -INT32_MAX;
88 for (auto &it : counts_) {
89 if (it.count > best_count) {
90 best_count = it.count;
91 if (max_value != nullptr) {
92 *max_value = it.value;
93 }
94 }
95 }
96 return best_count;
97 }

◆ SortByCount()

template<typename T >
const std::vector< SortPair< T > > & tesseract::SortHelper< T >::SortByCount ( )
inline

Definition at line 100 of file sorthelper.h.

100 {
101 counts_.sort(&SortPairsByCount);
102 return counts_;
103 }
static int SortPairsByCount(const void *v1, const void *v2)
Definition: sorthelper.h:46

◆ SortByValue()

template<typename T >
const std::vector< SortPair< T > > & tesseract::SortHelper< T >::SortByValue ( )
inline

Definition at line 105 of file sorthelper.h.

105 {
106 counts_.sort(&SortPairsByValue);
107 return counts_;
108 }
static int SortPairsByValue(const void *v1, const void *v2)
Definition: sorthelper.h:52

◆ SortPairsByCount()

template<typename T >
static int tesseract::SortHelper< T >::SortPairsByCount ( const void *  v1,
const void *  v2 
)
inlinestatic

Definition at line 46 of file sorthelper.h.

46 {
47 const auto *p1 = static_cast<const SortPair<T> *>(v1);
48 const auto *p2 = static_cast<const SortPair<T> *>(v2);
49 return p2->count - p1->count;
50 }

◆ SortPairsByValue()

template<typename T >
static int tesseract::SortHelper< T >::SortPairsByValue ( const void *  v1,
const void *  v2 
)
inlinestatic

Definition at line 52 of file sorthelper.h.

52 {
53 const auto *p1 = static_cast<const SortPair<T> *>(v1);
54 const auto *p2 = static_cast<const SortPair<T> *>(v2);
55 if (p2->value - p1->value < 0) {
56 return -1;
57 }
58 if (p2->value - p1->value > 0) {
59 return 1;
60 }
61 return 0;
62 }

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