Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinHeap.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include<ogdf/basic/Array.h>
35 #include<ogdf/basic/comparer.h>
36 
37 
38 namespace ogdf {
39 
41 
54 template<class X, class INDEX = int>
56 private:
57  Array<X,INDEX> data; // array starts at index 1
58  INDEX num;
59 public:
61  explicit BinaryHeapSimple(INDEX size) : data(1, size), num(0) {}
62 
64  bool empty() const { return num == 0; }
66  INDEX size() const { return num; }
67 
69  void clear() { num = 0; }
70 
72  const X& top() const {
73  return data[1];
74  }
76  inline const X& getMin() const {
77  return top();
78  }
79 
81  void push(X& x) {
82  X y;
83  if(num == capacity())
84  data.grow(capacity(),y); // double the size & init with nulls
85  data[++num] = x;
86  heapup(num);
87  }
89  inline void insert(X& x) {
90  push(x);
91  }
92 
94  X pop() {
95  data.swap(1, num--);
96  heapdown();
97  return data[num+1];
98  }
100  inline X extractMin() {
101  return pop();
102  }
103 
105  const X& operator[](INDEX idx) const {
106  return data[idx+1];
107  }
108 
109 
110 protected:
112  INDEX capacity() const { return data.size(); }
113 
114  void heapup(INDEX idx) {
115  INDEX papa;
116  while( (papa = idx/2) > 0) {
117  if( data[papa] > data[idx] ) {
118  data.swap(papa, idx);
119  idx = papa;
120  } else return; //done
121  }
122  }
123 
124  void heapdown() {
125  INDEX papa = 1;
126  INDEX son;
127  while(true) {
128  if( (son = 2*papa) < num && data[son+1] < data[son] )
129  son++;
130  if( son <= num && data[son] < data[papa]) {
131  data.swap(papa, son);
132  papa = son;
133  } else return;
134  }
135  }
136 };
137 
139 
147 template<class X, class INDEX = int>
148 class Top10Heap : protected BinaryHeapSimple<X,INDEX> { // favors the 10 highest values...
149 public:
151  enum class PushResult { Accepted, Rejected, Swapped };
152 
154  static bool successful(PushResult r) { return r != PushResult::Rejected; }
156  static bool returnedSomething(PushResult r) { return r != PushResult::Accepted; }
157 
159  Top10Heap() : BinaryHeapSimple<X,INDEX>(10) {}
161 
163  bool full() const { return size() == capacity(); }
164 
170 
172 
188  PushResult push(X& x, X& out) { // returns element that got kicked out - out is uninitialized if heap wasn't full (i.e. PushResult equals Accepted)
189  PushResult ret = PushResult::Accepted;
190  if(capacity() == size()) {
191  if(BinaryHeapSimple<X,INDEX>::top() >= x) {// reject new item since it's too bad
192  out = x;
193  return PushResult::Rejected;
194  }
195  out = BinaryHeapSimple<X,INDEX>::pop(); // remove worst first
196  ret = PushResult::Swapped;
197  }
199  return ret;
200  }
202  inline PushResult insert(X& x, X& out) {
203  return push(x, out);
204  }
205 
207 
210  void pushBlind(X& x) {
211  if(capacity() == size()) {
212  if(BinaryHeapSimple<X,INDEX>::top() >= x) // reject new item since it's too bad
213  return;
214  BinaryHeapSimple<X,INDEX>::pop(); // remove worst first
215  }
217  }
219  inline void insertBlind(X& x) {
220  pushBlind(x);
221  }
222 
224 
228  const X& operator[](INDEX idx) const { // ATTN: simulate index starting at 0, to be "traditional" to the outside!!!
230  }
231 };
232 
234 
242 template<class X, class Priority=double, class STATICCOMPARER=StdComparer<X>, class INDEX = int >
243 class DeletingTop10Heap : public Top10Heap<Prioritized<X*,Priority>,INDEX > {
244 public:
246  explicit DeletingTop10Heap(int size) : Top10Heap<Prioritized<X*, Priority>,INDEX >(size) {}
248 
253  void pushAndDelete(X* x, Priority p) {
255  Prioritized<X*, Priority> nv(x, p);
256  if(this->returnedSomething( Top10Heap<Prioritized<X*, Priority>,INDEX >::push(nv, vo) ))
257  delete vo.item();
258  }
260  inline void insertAndDelete(X* x, Priority p) {
261  pushAndDelete(x, p);
262  }
264 
268  void pushAndDeleteNoRedundancy(X* x, Priority p) {
269  for(INDEX i = Top10Heap<Prioritized<X*,Priority>,INDEX >::size(); i-- > 0;) {
270  X* k = Top10Heap<Prioritized<X*,Priority>,INDEX >::operator[](i).item();
271 #if 0
272  OGDF_ASSERT(x);
273  OGDF_ASSERT(k);
274 #endif
276  delete x;
277  return;
278  }
279  }
280  pushAndDelete(x, p);
281  }
283  inline void insertAndDeleteNoRedundancy(X* x, Priority p) {
284  pushAndDeleteNoRedundancy(p, x);
285  }
286 };
287 
288 }
static bool returnedSomething(PushResult r)
Convenience function: Returns true if the PushResults states that push caused an element to be not/no...
Definition: MinHeap.h:156
void push(X &x)
Adds an element to the heap [Same as insert(), O(log n)].
Definition: MinHeap.h:81
const X & getMin() const
Returns a reference to the top (i.e., smallest) element of the heap. It does not remove it...
Definition: MinHeap.h:76
Declarations for Comparer objects.
A static comparer which compares the target of pointers ("content"), instead of the pointer&#39;s adresse...
Definition: comparer.h:106
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
Definition: Array.h:493
Array< X, INDEX > data
Definition: MinHeap.h:57
PushResult
The type for results of a Top10Heap::push operation.
Definition: MinHeap.h:151
A variant of BinaryHeapSimple which always holds only the X (e.g. X=10) elements with the highest key...
Definition: MinHeap.h:148
void insertAndDeleteNoRedundancy(X *x, Priority p)
Alternative name for pushAndKillNoRedundancy().
Definition: MinHeap.h:283
void pushAndDeleteNoRedundancy(X *x, Priority p)
Analogous to pushandDelete(), but furthermore rejects (and deletes) an element if an equal element is...
Definition: MinHeap.h:268
Declaration and implementation of Array class and Array algorithms.
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer...
Definition: comparer.h:279
Dynamically growing binary heap tuned for efficiency on a small interface (compared to BinaryHeap)...
Definition: MinHeap.h:55
void insertBlind(X &x)
Alternative name for pushBlind().
Definition: MinHeap.h:219
PushResult push(X &x, X &out)
Tries to push the element x onto the heap (and may return a removed element as out).
Definition: MinHeap.h:188
void insertAndDelete(X *x, Priority p)
Alternative name for pushAndDelete().
Definition: MinHeap.h:260
const X & operator[](INDEX idx) const
obtain const references to the element at index idx (the smallest array index is 0, as for traditional C-arrays)
Definition: MinHeap.h:105
INDEX size() const
Returns the number of elements in the heap.
Definition: MinHeap.h:66
const X & top() const
Returns a reference to the top (i.e., smallest) element of the heap. It does not remove it...
Definition: MinHeap.h:72
bool full() const
Returns true if the heap is completely filled (i.e. the next push operation will return something) ...
Definition: MinHeap.h:163
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:282
PushResult insert(X &x, X &out)
Alternative name for push().
Definition: MinHeap.h:202
BinaryHeapSimple(INDEX size)
Construtor, giving initial array size.
Definition: MinHeap.h:61
X item() const
Returns the data of the element.
Definition: comparer.h:297
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:789
bool empty() const
Returns true if the heap is empty.
Definition: MinHeap.h:64
void insert(X &x)
Adds an element to the heap [Same as push(), O(log n)].
Definition: MinHeap.h:89
void pushBlind(X &x)
Simple (and slightly faster) variant of Top10Heap::push.
Definition: MinHeap.h:210
Top10Heap()
Constructor generating a heap which holds the 10 elements with highest value ever added to the heap...
Definition: MinHeap.h:159
INDEX capacity() const
Returns the current array-size of the heap, i.e., the number of elements which can be added before th...
Definition: MinHeap.h:112
A variant of Top10Heap which deletes the elements that get rejected from the heap.
Definition: MinHeap.h:243
static bool successful(PushResult r)
Convenience function: Returns true if the PushResults states that the newly pushed element is new in ...
Definition: MinHeap.h:154
void pushAndDelete(X *x, Priority p)
Inserts the element x into the heap with priority p and deletes the element with smallest priority if...
Definition: MinHeap.h:253
X pop()
Returns the top (i.e., smallest) element and removed it from the heap [Same as extractMin(), O(log n)].
Definition: MinHeap.h:94
void clear()
empties the heap [O(1)]
Definition: MinHeap.h:69
The namespace for all OGDF objects.
void heapup(INDEX idx)
Definition: MinHeap.h:114
X extractMin()
Returns the top (i.e., smallest) element and removed it from the heap [Same as pop(), O(log n)].
Definition: MinHeap.h:100
const X & operator[](INDEX idx) const
obtain const references to the element at index idx
Definition: MinHeap.h:228
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
DeletingTop10Heap(int size)
Construct a DeletingTop10Heap of given maximal capacity.
Definition: MinHeap.h:246