n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
fmdaryheap.hpp
1 
20 #ifndef FMDARYHEAP_H_
21 #define FMDARYHEAP_H_
22 
23 #include <boost/heap/d_ary_heap.hpp>
24 
25 #include <fast_methods/datastructures/fmcompare.hpp>
26 
28 template <class cell_t = FMCell> class FMDaryHeap {
29 
31  typedef boost::heap::d_ary_heap<const cell_t *, boost::heap::mutable_<true>, boost::heap::arity<2>, boost::heap::compare<FMCompare<cell_t>> > d_ary_heap_t;
32 
34  typedef typename d_ary_heap_t::handle_type handle_t;
35 
36  public:
37  FMDaryHeap () {}
38 
40  FMDaryHeap (const size_t & n) { handles_.resize(n); }
41 
42  virtual ~ FMDaryHeap() { clear(); }
43 
45  void setMaxSize
46  (const size_t & n) {
47  handles_.resize(n);
48  }
49 
51  void push
52  (const cell_t * c) {
53  handles_[c->getIndex()] = heap_.push(c);
54  }
55 
57  unsigned int popMinIdx
58  () {
59  const unsigned int idx = heap_.top()->getIndex();
60  heap_.pop();
61  return idx;
62  }
63 
65  size_t size
66  () const {
67  return heap_.size();
68  }
69 
71  void update
72  (const cell_t * c) {
73  heap_.update(handles_[c->getIndex()], c);
74  }
75 
79  void increase
80  (const cell_t * c) {
81  heap_.increase(handles_[c->getIndex()], c);
82  }
83 
85  void clear
86  () {
87  heap_.clear();
88  handles_.clear();
89  }
90 
92  bool empty
93  () const {
94  return heap_.empty();
95  }
96 
97  protected:
103  std::vector<handle_t> handles_;
104 };
105 
106 
107 #endif /* FMDARYHEAP_H_ */
std::vector< handle_t > handles_
Stores the handles of each cell by keeping the indices: handles_(0) is the handle for the cell with i...
Definition: fmdaryheap.hpp:103
d_ary_heap_t::handle_type handle_t
Shorthand for heap element handle type.
Definition: fmdaryheap.hpp:34
d_ary_heap_t heap_
The actual heap for cell_t.
Definition: fmdaryheap.hpp:99
void setMaxSize(const size_t &n)
Sets the maximum number of cells the heap will contain.
Definition: fmdaryheap.hpp:46
Wrap for the Boost D-ary Heap class to be used as a binary heap in the FM algorithms. Ready to be used with FMCell and derived types.
Definition: fmdaryheap.hpp:28
void increase(const cell_t *c)
Updates the position of the cell in the heap. Its priority can only increase. It is more efficient th...
Definition: fmdaryheap.hpp:80
boost::heap::d_ary_heap< const cell_t *, boost::heap::mutable_< true >, boost::heap::arity< 2 >, boost::heap::compare< FMCompare< cell_t > > > d_ary_heap_t
Shorthand for heap type.
Definition: fmdaryheap.hpp:31
void update(const cell_t *c)
Updates the position of the cell in the heap. Its priority can increase or decrease.
Definition: fmdaryheap.hpp:72
void clear()
Deallocates heap memory.
Definition: fmdaryheap.hpp:86
size_t size() const
Returns current size of the heap.
Definition: fmdaryheap.hpp:66
FMDaryHeap(const size_t &n)
Creates a heap with n maximum elements.
Definition: fmdaryheap.hpp:40
unsigned int popMinIdx()
Pops index of the element with lowest value and removes it from the heap.
Definition: fmdaryheap.hpp:58
bool empty() const
Returns true if the heap is empty.
Definition: fmdaryheap.hpp:93
void push(const cell_t *c)
Pushes a new element into the heap.
Definition: fmdaryheap.hpp:52