Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 namespace ogdf {
38 
40 
53 template<class X, class INDEX = int>
55 private:
56  Array<X, INDEX> data; // array starts at index 1
57  INDEX num;
58 
59 public:
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 
105 protected:
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 
147 template<class X, class INDEX = int>
148 class Top10Heap : protected BinaryHeapSimple<X, INDEX> { // favors the 10 highest values...
149 public:
152 
154  static bool successful(PushResult r) { return r != PushResult::Rejected; }
155 
158 
160  Top10Heap() : BinaryHeapSimple<X, INDEX>(10) { }
161 
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;
197  return PushResult::Rejected;
198  }
199  out = BinaryHeapSimple<X, INDEX>::pop(); // remove worst first
200  ret = PushResult::Swapped;
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 
246 template<class X, class Priority = double, class STATICCOMPARER = StdComparer<X>, class INDEX = int>
247 class DeletingTop10Heap : public Top10Heap<Prioritized<X*, Priority>, INDEX> {
248 public:
250  explicit DeletingTop10Heap(int size) : Top10Heap<Prioritized<X*, Priority>, INDEX>(size) { }
251 
253 
258  void pushAndDelete(X* x, Priority p) {
260  Prioritized<X*, Priority> nv(x, p);
261  if (this->returnedSomething(Top10Heap<Prioritized<X*, Priority>, INDEX>::push(nv, vo))) {
262  delete vo.item();
263  }
264  }
265 
267  inline void insertAndDelete(X* x, Priority p) { pushAndDelete(x, p); }
268 
270 
274  void pushAndDeleteNoRedundancy(X* x, Priority p) {
275  for (INDEX i = Top10Heap<Prioritized<X*, Priority>, INDEX>::size(); i-- > 0;) {
276  X* k = Top10Heap<Prioritized<X*, Priority>, INDEX>::operator[](i).item();
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 
290  inline void insertAndDeleteNoRedundancy(X* x, Priority p) { pushAndDeleteNoRedundancy(p, x); }
291 };
292 
293 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::DeletingTop10Heap::pushAndDelete
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
ogdf::Top10Heap::successful
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
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::DeletingTop10Heap::insertAndDeleteNoRedundancy
void insertAndDeleteNoRedundancy(X *x, Priority p)
Alternative name for pushAndKillNoRedundancy().
Definition: MinHeap.h:290
ogdf::Top10Heap::Top10Heap
Top10Heap()
Constructor generating a heap which holds the 10 elements with highest value ever added to the heap.
Definition: MinHeap.h:160
ogdf::Top10Heap::PushResult::Rejected
@ Rejected
ogdf::DeletingTop10Heap::insertAndDelete
void insertAndDelete(X *x, Priority p)
Alternative name for pushAndDelete().
Definition: MinHeap.h:267
ogdf::BinaryHeapSimple::insert
void insert(X &x)
Adds an element to the heap [Same as push(), O(log n)].
Definition: MinHeap.h:89
ogdf::BinaryHeapSimple::num
INDEX num
Definition: MinHeap.h:57
ogdf::Top10Heap
A variant of BinaryHeapSimple which always holds only the 10 elements with the highest keys.
Definition: MinHeap.h:148
ogdf::BinaryHeapSimple::size
INDEX size() const
Returns the number of elements in the heap.
Definition: MinHeap.h:67
ogdf::BinaryHeapSimple::operator[]
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
ogdf::BinaryHeapSimple::pop
X pop()
Returns the top (i.e., smallest) element and removed it from the heap [Same as extractMin(),...
Definition: MinHeap.h:92
ogdf::Top10Heap::pushBlind
void pushBlind(X &x)
Simple (and slightly faster) variant of Top10Heap::push.
Definition: MinHeap.h:213
ogdf::Top10Heap::PushResult::Accepted
@ Accepted
ogdf::BinaryHeapSimple
Dynamically growing binary heap tuned for efficiency on a small interface (compared to BinaryHeap).
Definition: MinHeap.h:54
ogdf::DeletingTop10Heap::pushAndDeleteNoRedundancy
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
r
int r[]
Definition: hierarchical-ranking.cpp:13
ogdf::BinaryHeapSimple::top
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
ogdf::DeletingTop10Heap
A variant of Top10Heap which deletes the elements that get rejected from the heap.
Definition: MinHeap.h:247
ogdf::Top10Heap::full
bool full() const
Returns true if the heap is completely filled (i.e. the next push operation will return something)
Definition: MinHeap.h:165
ogdf::BinaryHeapSimple::BinaryHeapSimple
BinaryHeapSimple(INDEX size)
Construtor, giving initial array size.
Definition: MinHeap.h:61
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:219
ogdf::Top10Heap::PushResult::Swapped
@ Swapped
ogdf::BinaryHeapSimple::heapdown
void heapdown()
Definition: MinHeap.h:121
ogdf::BinaryHeapSimple::empty
bool empty() const
Returns true if the heap is empty.
Definition: MinHeap.h:64
ogdf::BinaryHeapSimple::push
void push(X &x)
Adds an element to the heap [Same as insert(), O(log n)].
Definition: MinHeap.h:79
ogdf::BinaryHeapSimple::getMin
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
ogdf::Top10Heap::push
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
ogdf::Top10Heap::returnedSomething
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
ogdf::TargetComparer
A static comparer which compares the target of pointers ("content"), instead of the pointer's adresse...
Definition: comparer.h:118
ogdf::BinaryHeapSimple::heapup
void heapup(INDEX idx)
Definition: MinHeap.h:109
ogdf::DeletingTop10Heap::DeletingTop10Heap
DeletingTop10Heap(int size)
Construct a DeletingTop10Heap of given maximal capacity.
Definition: MinHeap.h:250
ogdf::BinaryHeapSimple::capacity
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
Array.h
Declaration and implementation of Array class and Array algorithms.
ogdf::Prioritized
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition: comparer.h:295
ogdf::Top10Heap::operator[]
const X & operator[](INDEX idx) const
obtain const references to the element at index idx
Definition: MinHeap.h:231
ogdf::Top10Heap::insert
PushResult insert(X &x, X &out)
Alternative name for push().
Definition: MinHeap.h:207
ogdf::BinaryHeapSimple::clear
void clear()
empties the heap [O(1)]
Definition: MinHeap.h:70
ogdf::Top10Heap::insertBlind
void insertBlind(X &x)
Alternative name for pushBlind().
Definition: MinHeap.h:224
ogdf::BinaryHeapSimple::extractMin
X extractMin()
Returns the top (i.e., smallest) element and removed it from the heap [Same as pop(),...
Definition: MinHeap.h:99
ogdf::BinaryHeapSimple::data
Array< X, INDEX > data
Definition: MinHeap.h:56
comparer.h
Declarations for Comparer objects.
ogdf::Top10Heap< Prioritized< X *, double >, int >::PushResult
PushResult
The type for results of a Top10Heap::push operation.
Definition: MinHeap.h:151
ogdf::Prioritized::item
X item() const
Returns the data of the element.
Definition: comparer.h:313