tesseract  4.0.0-1-g2a2b
MinK< Key, Value > Class Template Reference

Classes

struct  Element
 

Public Member Functions

 MinK (Key max_key, int k)
 
 ~MinK ()
 
bool insert (Key k, Value v)
 
const Key & max_insertable_key ()
 
int elements_count ()
 
const Elementelements ()
 

Detailed Description

template<typename Key, typename Value>
class MinK< Key, Value >

Store the k smallest-keyed key-value pairs.

Definition at line 52 of file kdtree.cpp.

Constructor & Destructor Documentation

◆ MinK()

template<typename Key, typename Value >
MinK< Key, Value >::MinK ( Key  max_key,
int  k 
)

Definition at line 80 of file kdtree.cpp.

80  :
81  max_key_(max_key), elements_count_(0), k_(k < 1 ? 1 : k), max_index_(0) {
82  elements_ = new Element[k_];
83 }

◆ ~MinK()

template<typename Key , typename Value >
MinK< Key, Value >::~MinK ( )

Definition at line 86 of file kdtree.cpp.

86  {
87  delete []elements_;
88 }

Member Function Documentation

◆ elements()

template<typename Key, typename Value>
const Element* MinK< Key, Value >::elements ( )
inline

Definition at line 69 of file kdtree.cpp.

69 { return elements_; }

◆ elements_count()

template<typename Key, typename Value>
int MinK< Key, Value >::elements_count ( )
inline

Definition at line 68 of file kdtree.cpp.

68 { return elements_count_; }

◆ insert()

template<typename Key, typename Value>
bool MinK< Key, Value >::insert ( Key  k,
Value  v 
)

Definition at line 98 of file kdtree.cpp.

98  {
99  if (elements_count_ < k_) {
100  elements_[elements_count_++] = Element(key, value);
101  if (key > elements_[max_index_].key)
102  max_index_ = elements_count_ - 1;
103  return true;
104  } else if (key < elements_[max_index_].key) {
105  // evict the largest element.
106  elements_[max_index_] = Element(key, value);
107  // recompute max_index_
108  for (int i = 0; i < elements_count_; i++) {
109  if (elements_[i].key > elements_[max_index_].key)
110  max_index_ = i;
111  }
112  return true;
113  }
114  return false;
115 }

◆ max_insertable_key()

template<typename Key , typename Value >
const Key & MinK< Key, Value >::max_insertable_key ( )

Definition at line 91 of file kdtree.cpp.

91  {
92  if (elements_count_ < k_)
93  return max_key_;
94  return elements_[max_index_].key;
95 }

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