All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::GenericHeap< Pair > Class Template Reference

#include <genericheap.h>

Public Member Functions

 GenericHeap ()
 
 GenericHeap (int initial_size)
 
bool empty () const
 
int size () const
 
int size_reserved () const
 
void clear ()
 
GenericVector< Pair > * heap ()
 
const Pair & get (int index) const
 
void Push (Pair *entry)
 
const Pair & PeekTop () const
 
bool Pop (Pair *entry)
 
bool PopWorst (Pair *entry)
 
void Reshuffle (Pair *pair)
 

Detailed Description

template<typename Pair>
class tesseract::GenericHeap< Pair >

Definition at line 58 of file genericheap.h.

Constructor & Destructor Documentation

template<typename Pair>
tesseract::GenericHeap< Pair >::GenericHeap ( )
inline

Definition at line 60 of file genericheap.h.

60 {}
template<typename Pair>
tesseract::GenericHeap< Pair >::GenericHeap ( int  initial_size)
inlineexplicit

Definition at line 63 of file genericheap.h.

63  {
64  heap_.reserve(initial_size);
65  }
void reserve(int size)

Member Function Documentation

template<typename Pair>
void tesseract::GenericHeap< Pair >::clear ( )
inline

Definition at line 77 of file genericheap.h.

77  {
78  // Clear truncates to 0 to keep the number reserved in tact.
79  heap_.truncate(0);
80  }
void truncate(int size)
template<typename Pair>
bool tesseract::GenericHeap< Pair >::empty ( ) const
inline

Definition at line 68 of file genericheap.h.

68  {
69  return heap_.empty();
70  }
bool empty() const
Definition: genericvector.h:84
template<typename Pair>
const Pair& tesseract::GenericHeap< Pair >::get ( int  index) const
inline

Definition at line 87 of file genericheap.h.

87  {
88  return heap_[index];
89  }
template<typename Pair>
GenericVector<Pair>* tesseract::GenericHeap< Pair >::heap ( )
inline

Definition at line 83 of file genericheap.h.

83  {
84  return &heap_;
85  }
template<typename Pair>
const Pair& tesseract::GenericHeap< Pair >::PeekTop ( ) const
inline

Definition at line 108 of file genericheap.h.

108  {
109  return heap_[0];
110  }
template<typename Pair>
bool tesseract::GenericHeap< Pair >::Pop ( Pair *  entry)
inline

Definition at line 116 of file genericheap.h.

116  {
117  int new_size = heap_.size() - 1;
118  if (new_size < 0)
119  return false; // Already empty.
120  if (entry != NULL)
121  *entry = heap_[0];
122  if (new_size > 0) {
123  // Sift the hole at the start of the heap_ downwards to match the last
124  // element.
125  Pair hole_pair = heap_[new_size];
126  heap_.truncate(new_size);
127  int hole_index = SiftDown(0, hole_pair);
128  heap_[hole_index] = hole_pair;
129  } else {
130  heap_.truncate(new_size);
131  }
132  return true;
133  }
int size() const
Definition: genericvector.h:72
void truncate(int size)
#define NULL
Definition: host.h:144
template<typename Pair>
bool tesseract::GenericHeap< Pair >::PopWorst ( Pair *  entry)
inline

Definition at line 138 of file genericheap.h.

138  {
139  int heap_size = heap_.size();
140  if (heap_size == 0) return false; // It cannot be empty!
141 
142  // Find the maximum element. Its index is guaranteed to be greater than
143  // the index of the parent of the last element, since by the heap invariant
144  // the parent must be less than or equal to the children.
145  int worst_index = heap_size - 1;
146  int end_parent = ParentNode(worst_index);
147  for (int i = worst_index - 1; i > end_parent; --i) {
148  if (heap_[worst_index] < heap_[i])
149  worst_index = i;
150  }
151  // Extract the worst element from the heap, leaving a hole at worst_index.
152  if (entry != NULL)
153  *entry = heap_[worst_index];
154  --heap_size;
155  if (heap_size > 0) {
156  // Sift the hole upwards to match the last element of the heap_
157  Pair hole_pair = heap_[heap_size];
158  int hole_index = SiftUp(worst_index, hole_pair);
159  heap_[hole_index] = hole_pair;
160  }
161  heap_.truncate(heap_size);
162  return true;
163  }
int size() const
Definition: genericvector.h:72
void truncate(int size)
#define NULL
Definition: host.h:144
template<typename Pair>
void tesseract::GenericHeap< Pair >::Push ( Pair *  entry)
inline

Definition at line 95 of file genericheap.h.

95  {
96  int hole_index = heap_.size();
97  // Make a hole in the end of heap_ and sift it up to be the correct
98  // location for the new *entry. To avoid needing a default constructor
99  // for primitive types, and to allow for use of DoublePtr in the Pair
100  // somewhere, we have to incur a double copy here.
101  heap_.push_back(*entry);
102  *entry = heap_.back();
103  hole_index = SiftUp(hole_index, *entry);
104  heap_[hole_index] = *entry;
105  }
int size() const
Definition: genericvector.h:72
int push_back(T object)
T & back() const
template<typename Pair>
void tesseract::GenericHeap< Pair >::Reshuffle ( Pair *  pair)
inline

Definition at line 174 of file genericheap.h.

174  {
175  int index = pair - &heap_[0];
176  Pair hole_pair = heap_[index];
177  index = SiftDown(index, hole_pair);
178  index = SiftUp(index, hole_pair);
179  heap_[index] = hole_pair;
180  }
template<typename Pair>
int tesseract::GenericHeap< Pair >::size ( ) const
inline

Definition at line 71 of file genericheap.h.

71  {
72  return heap_.size();
73  }
int size() const
Definition: genericvector.h:72
template<typename Pair>
int tesseract::GenericHeap< Pair >::size_reserved ( ) const
inline

Definition at line 74 of file genericheap.h.

74  {
75  return heap_.size_reserved();
76  }
int size_reserved() const
Definition: genericvector.h:75

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