Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
37namespace ogdf {
38
40
53template<class X, class INDEX = int>
55private:
56 Array<X, INDEX> data; // array starts at index 1
58
59public:
61 explicit BinaryHeapSimple(INDEX size) : data(1, size), num(0) { }
62
64 bool empty() const { return num == 0; }
65
67 INDEX size() const { return num; }
68
70 void clear() { num = 0; }
71
73 const X& top() const { return data[1]; }
74
76 inline const X& getMin() const { return top(); }
77
79 void push(X& x) {
80 X y;
81 if (num == capacity()) {
82 data.grow(capacity(), y); // double the size & init with nulls
83 }
84 data[++num] = x;
85 heapup(num);
86 }
87
89 inline void insert(X& x) { push(x); }
90
92 X pop() {
93 data.swap(1, num--);
94 heapdown();
95 return data[num + 1];
96 }
97
99 inline X extractMin() { return pop(); }
100
102 const X& operator[](INDEX idx) const { return data[idx + 1]; }
103
104
105protected:
107 INDEX capacity() const { return data.size(); }
108
109 void heapup(INDEX idx) {
110 INDEX papa;
111 while ((papa = idx / 2) > 0) {
112 if (data[papa] > data[idx]) {
113 data.swap(papa, idx);
114 idx = papa;
115 } else {
116 return; //done
117 }
118 }
119 }
120
121 void heapdown() {
122 INDEX papa = 1;
123 INDEX son;
124 while (true) {
125 if ((son = 2 * papa) < num && data[son + 1] < data[son]) {
126 son++;
127 }
128 if (son <= num && data[son] < data[papa]) {
129 data.swap(papa, son);
130 papa = son;
131 } else {
132 return;
133 }
134 }
135 }
136};
137
139
147template<class X, class INDEX = int>
148class Top10Heap : protected BinaryHeapSimple<X, INDEX> { // favors the 10 highest values...
149public:
152
154 static bool successful(PushResult r) { return r != PushResult::Rejected; }
155
158
161
162 using BinaryHeapSimple<X, INDEX>::BinaryHeapSimple;
163
165 bool full() const { return size() == capacity(); }
166
172
174
190 PushResult push(X& x, X& out) {
191 // returns element that got kicked out -
192 // out is uninitialized if heap wasn't full (i.e. PushResult equals Accepted)
194 if (capacity() == size()) {
195 if (BinaryHeapSimple<X, INDEX>::top() >= x) { // reject new item since it's too bad
196 out = x;
198 }
199 out = BinaryHeapSimple<X, INDEX>::pop(); // remove worst first
201 }
203 return ret;
204 }
205
207 inline PushResult insert(X& x, X& out) { return push(x, out); }
208
210
213 void pushBlind(X& x) {
214 if (capacity() == size()) {
215 if (BinaryHeapSimple<X, INDEX>::top() >= x) { // reject new item since it's too bad
216 return;
217 }
218 BinaryHeapSimple<X, INDEX>::pop(); // remove worst first
219 }
221 }
222
224 inline void insertBlind(X& x) { pushBlind(x); }
225
227
231 const X& operator[](
232 INDEX idx) const { // ATTN: simulate index starting at 0, to be "traditional" to the outside!!!
234 }
235};
236
238
246template<class X, class Priority = double, class STATICCOMPARER = StdComparer<X>, class INDEX = int>
247class DeletingTop10Heap : public Top10Heap<Prioritized<X*, Priority>, INDEX> {
248public:
251
253
258 void pushAndDelete(X* x, Priority p) {
262 delete vo.item();
263 }
264 }
265
267 inline void insertAndDelete(X* x, Priority p) { pushAndDelete(x, p); }
268
270
275 for (INDEX i = Top10Heap<Prioritized<X*, Priority>, INDEX>::size(); i-- > 0;) {
277#if 0
278 OGDF_ASSERT(x);
279 OGDF_ASSERT(k);
280#endif
282 delete x;
283 return;
284 }
285 }
286 pushAndDelete(x, p);
287 }
288
291};
292
293}
Declaration and implementation of Array class and Array algorithms.
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:214
Dynamically growing binary heap tuned for efficiency on a small interface (compared to BinaryHeap).
Definition MinHeap.h:54
Array< X, INDEX > data
Definition MinHeap.h:56
void heapup(INDEX idx)
Definition MinHeap.h:109
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
X pop()
Returns the top (i.e., smallest) element and removed it from the heap [Same as extractMin(),...
Definition MinHeap.h:92
INDEX size() const
Returns the number of elements in the heap.
Definition MinHeap.h:67
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:107
X extractMin()
Returns the top (i.e., smallest) element and removed it from the heap [Same as pop(),...
Definition MinHeap.h:99
void clear()
empties the heap [O(1)]
Definition MinHeap.h:70
BinaryHeapSimple(INDEX size)
Construtor, giving initial array size.
Definition MinHeap.h:61
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:73
void insert(X &x)
Adds an element to the heap [Same as push(), O(log n)].
Definition MinHeap.h:89
const X & operator[](INDEX idx) const
obtain const references to the element at index idx (the smallest array index is 0,...
Definition MinHeap.h:102
void push(X &x)
Adds an element to the heap [Same as insert(), O(log n)].
Definition MinHeap.h:79
bool empty() const
Returns true if the heap is empty.
Definition MinHeap.h:64
A variant of Top10Heap which deletes the elements that get rejected from the heap.
Definition MinHeap.h:247
void insertAndDelete(X *x, Priority p)
Alternative name for pushAndDelete().
Definition MinHeap.h:267
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:258
void pushAndDeleteNoRedundancy(X *x, Priority p)
Analogous to pushandDelete(), but furthermore rejects (and deletes) an element if an equal element is...
Definition MinHeap.h:274
DeletingTop10Heap(int size)
Construct a DeletingTop10Heap of given maximal capacity.
Definition MinHeap.h:250
void insertAndDeleteNoRedundancy(X *x, Priority p)
Alternative name for pushAndKillNoRedundancy().
Definition MinHeap.h:290
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition comparer.h:291
A static comparer which compares the target of pointers ("content"), instead of the pointer's adresse...
Definition comparer.h:114
A variant of BinaryHeapSimple which always holds only the 10 elements with the highest keys.
Definition MinHeap.h:148
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:157
const X & operator[](INDEX idx) const
obtain const references to the element at index idx
Definition MinHeap.h:231
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:190
void pushBlind(X &x)
Simple (and slightly faster) variant of Top10Heap::push.
Definition MinHeap.h:213
Top10Heap()
Constructor generating a heap which holds the 10 elements with highest value ever added to the heap.
Definition MinHeap.h:160
void insertBlind(X &x)
Alternative name for pushBlind().
Definition MinHeap.h:224
PushResult insert(X &x, X &out)
Alternative name for push().
Definition MinHeap.h:207
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
bool full() const
Returns true if the heap is completely filled (i.e. the next push operation will return something)
Definition MinHeap.h:165
PushResult
The type for results of a Top10Heap::push operation.
Definition MinHeap.h:151
Declarations for Comparer objects.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
int r[]
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.