Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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  explicit Top10Heap(INDEX size) : BinaryHeapSimple<X,INDEX>(size) {}
162 
164  bool empty() const { return BinaryHeapSimple<X,INDEX>::empty(); }
166  bool full() const { return size() == capacity(); }
168  INDEX size() const { return BinaryHeapSimple<X,INDEX>::size(); }
170  INDEX capacity() const { return BinaryHeapSimple<X,INDEX>::capacity(); }
171 
174 
176 
192  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)
193  PushResult ret = PushResult::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  }
206  inline PushResult insert(X& x, X& out) {
207  return push(x, out);
208  }
209 
211 
214  void pushBlind(X& x) {
215  if(capacity() == size()) {
216  if(BinaryHeapSimple<X,INDEX>::top() >= x) // reject new item since it's too bad
217  return;
218  BinaryHeapSimple<X,INDEX>::pop(); // remove worst first
219  }
221  }
223  inline void insertBlind(X& x) {
224  pushBlind(x);
225  }
226 
228 
232  const X& operator[](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) {}
252 
257  void pushAndDelete(X* x, Priority p) {
259  Prioritized<X*, Priority> nv(x, p);
260  if(this->returnedSomething( Top10Heap<Prioritized<X*, Priority>,INDEX >::push(nv, vo) ))
261  delete vo.item();
262  }
264  inline void insertAndDelete(X* x, Priority p) {
265  pushAndDelete(x, p);
266  }
268 
272  void pushAndDeleteNoRedundancy(X* x, Priority p) {
273  for(INDEX i = Top10Heap<Prioritized<X*,Priority>,INDEX >::size(); i-- > 0;) {
274  X* k = Top10Heap<Prioritized<X*,Priority>,INDEX >::operator[](i).item();
275 #if 0
276  OGDF_ASSERT(x);
277  OGDF_ASSERT(k);
278 #endif
280  delete x;
281  return;
282  }
283  }
284  pushAndDelete(x, p);
285  }
287  inline void insertAndDeleteNoRedundancy(X* x, Priority p) {
288  pushAndDeleteNoRedundancy(p, x);
289  }
290 };
291 
292 } // end namespace ogdf
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.
INDEX capacity() const
Returns the size of the heap specified when constructing: this is the number of top elements stored...
Definition: MinHeap.h:170
A static comparer which compares the target of pointers ("content"), instead of the pointer&#39;s adresse...
Definition: comparer.h:104
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
Definition: Array.h:316
Array< X, INDEX > data
Definition: MinHeap.h:57
PushResult
The type for results of a Top10Heap::push operation.
Definition: MinHeap.h:151
Top10Heap(INDEX size)
Constructor generating a heap which holds the size elements with highest value ever added to the heap...
Definition: MinHeap.h:161
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:287
void pushAndDeleteNoRedundancy(X *x, Priority p)
Analogous to pushandDelete(), but furthermore rejects (and deletes) an element if an equal element is...
Definition: MinHeap.h:272
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:278
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:223
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:192
void insertAndDelete(X *x, Priority p)
Alternative name for pushAndDelete().
Definition: MinHeap.h:264
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:166
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:134
PushResult insert(X &x, X &out)
Alternative name for push().
Definition: MinHeap.h:206
BinaryHeapSimple(INDEX size)
Construtor, giving initial array size.
Definition: MinHeap.h:61
X item() const
Returns the data of the element.
Definition: comparer.h:296
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:611
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:214
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:247
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:257
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
INDEX size() const
Returns the number of elements in the heap.
Definition: MinHeap.h:168
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:232
bool empty() const
Returns true if the heap contains no elements.
Definition: MinHeap.h:164
void clear()
empties the heap
Definition: MinHeap.h:173
#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:250