Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

PairingHeap.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <functional>
35 
37 
38 namespace ogdf {
39 
40 
42 template<typename T>
44  template<typename, typename> friend class PairingHeap;
45 protected:
46  T value;
47 
51 
53  explicit PairingHeapNode(const T &valueOfNode)
54  : value(valueOfNode),
55  prev(nullptr), next(nullptr), child(nullptr)
56  {
57  }
58 };
59 
60 
62 
71 template<typename T, typename C>
72 class PairingHeap : public HeapBase<PairingHeap<T, C>, PairingHeapNode<T>, T, C> {
73 
75 
76 public:
83  explicit PairingHeap(const C &cmp = C(), int initialSize = -1);
84 
91  virtual ~PairingHeap();
92 
94  const T &top() const override;
95 
102  PairingHeapNode<T> *push(const T &value) override;
103 
109  void pop() override;
110 
120  void decrease(PairingHeapNode<T> *heapNode, const T &value) override;
121 
129  void merge(PairingHeap<T, C> &other) override;
130 
137  const T &value(PairingHeapNode<T> *heapNode) const override {
138  return heapNode->value;
139  }
140 
141 private:
142  PairingHeapNode<T> *m_root;
143 
145  PairingHeapNode<T> *pair(PairingHeapNode<T> *heapNode);
147  PairingHeapNode<T> *merge(PairingHeapNode<T> *a, PairingHeapNode<T> *b);
148 
150  static void link(PairingHeapNode<T> *parent, PairingHeapNode<T> *child);
152  static void unlink(PairingHeapNode<T> *heapNode);
153 
155  static void release(PairingHeapNode<T> *heapNode);
156 };
157 
158 
159 template<typename T, typename C>
160 PairingHeap<T, C>::PairingHeap(const C &cmp, int /* unused parameter */)
161 : base_type(cmp), m_root(nullptr) {}
162 
163 
164 template<typename T, typename C>
166 {
167  release(m_root);
168  m_root = nullptr;
169 }
170 
171 
172 template<typename T, typename C>
173 inline const T &PairingHeap<T, C>::top() const
174 {
175  return m_root->value;
176 }
177 
178 
179 template<typename T, typename C>
180 PairingHeapNode<T> *PairingHeap<T, C>::push(const T &value)
181 {
182  PairingHeapNode<T> *heapNode = new PairingHeapNode<T>(value);
183 
184  m_root = m_root == nullptr ? heapNode : merge(m_root, heapNode);
185  return heapNode;
186 }
187 
188 
189 template<typename T, typename C>
191 {
192  PairingHeapNode<T> *children = m_root->child;
193  delete m_root;
194 
195  m_root = pair(children);
196 }
197 
198 
199 template<typename T, typename C>
200 void PairingHeap<T, C>::decrease(PairingHeapNode<T> *heapNode, const T &value)
201 {
202  heapNode->value = value;
203  if (heapNode->prev != nullptr) {
204  unlink(heapNode);
205  m_root = merge(m_root, heapNode);
206  }
207 }
208 
209 
210 template<typename T, typename C>
212 {
213  m_root = merge(m_root, other.m_root);
214  other.m_root = nullptr;
215 }
216 
217 
218 template<typename T, typename C>
219 inline PairingHeapNode<T> *PairingHeap<T, C>::pair(
220  PairingHeapNode<T> *heapNode)
221 {
222  if(heapNode == nullptr) {
223  return nullptr;
224  }
225 
226  /*
227  * We move towards the end of list counting elements along the way. We do
228  * this for two reasons: to know whether the list has even or odd number of
229  * elements and for possible speed up of going-back through the list (loop
230  * unrolling is not applicable for iterators but works for integers).
231  */
232  size_t children = 1;
233  PairingHeapNode<T> *it = heapNode;
234  while (it->next != nullptr) {
235  it = it->next;
236  children++;
237  }
238 
239  PairingHeapNode<T> *result;
240 
241  if (children % 2 == 1) {
242  PairingHeapNode<T> *a = it;
243  it = it->prev;
244  a->prev = a->next = nullptr;
245  result = a;
246  } else {
247  PairingHeapNode<T> *a = it, *b = it->prev;
248  it = it->prev->prev;
249  a->prev = a->next = b->prev = b->next = nullptr;
250  result = merge(a, b);
251  }
252 
253  for (size_t i = 0; i < (children - 1) / 2; i++) {
254  PairingHeapNode<T> *a = it, *b = it->prev;
255  it = it->prev->prev;
256  a->prev = a->next = b->prev = b->next = nullptr;
257  result = merge(merge(a, b), result);
258  }
259 
260  return result;
261 }
262 
263 
264 template<typename T, typename C>
265 inline PairingHeapNode<T> *PairingHeap<T, C>::merge(
266  PairingHeapNode<T> *a, PairingHeapNode<T> *b)
267 {
268  if(this->comparator()(a->value, b->value)) {
269  link(a, b);
270  return a;
271  } else {
272  link(b, a);
273  return b;
274  }
275 }
276 
277 
278 template<typename T, typename C>
280  PairingHeapNode<T> *root, PairingHeapNode<T> *child)
281 {
282  if(root->child != nullptr) {
283  child->next = root->child;
284  root->child->prev = child;
285  }
286  child->prev = root;
287  root->child = child;
288 }
289 
290 
291 template<typename T, typename C>
293  PairingHeapNode<T> *heapNode)
294 {
295  if(heapNode->prev->child == heapNode) {
296  heapNode->prev->child = heapNode->next;
297  } else {
298  heapNode->prev->next = heapNode->next;
299  }
300  if(heapNode->next != nullptr) {
301  heapNode->next->prev = heapNode->prev;
302  }
303  heapNode->prev = nullptr;
304  heapNode->next = nullptr;
305 }
306 
307 
308 template<typename T, typename C>
309 inline void PairingHeap<T, C>::release(PairingHeapNode<T> *heapNode)
310 {
311  /*
312  * Recursive version of this function is infinitely prettier than that
313  * abomination. Please, make it prettier if you can.
314  */
315  PairingHeapNode<T> *it = heapNode;
316 
317  if(it == nullptr) {
318  return;
319  }
320 
321  for(;;) {
322  // Slide down as long as possible.
323  if(it->child != nullptr) {
324  it = it->child;
325  continue;
326  }
327  if(it->next != nullptr) {
328  it = it->next;
329  continue;
330  }
331 
332  // Climb up until you find first non-visited node.
333  for(;;) {
334  PairingHeapNode<T> *curr = it, *prev = it->prev;
335  delete it;
336 
337  if(prev == nullptr) {
338  return;
339  }
340  if(curr == prev->child && prev->next != nullptr) {
341  it = prev->next;
342  break;
343  } else {
344  it = prev;
345  }
346  }
347  }
348 }
349 
350 } // end namespace ogdf
PairingHeap(const C &cmp=C(), int initialSize=-1)
Creates empty pairing heap.
Definition: PairingHeap.h:160
static void link(PairingHeapNode< T > *parent, PairingHeapNode< T > *child)
Makes child node a child of parent node.
Definition: PairingHeap.h:279
Pairing heap node.
Definition: PairingHeap.h:43
Interface for heap implementations.
const T & value(PairingHeapNode< T > *heapNode) const override
Returns the value of the node.
Definition: PairingHeap.h:137
Pairing heap implementation.
Definition: PairingHeap.h:72
PairingHeapNode< T > * m_root
Root node of the heap.
Definition: PairingHeap.h:142
virtual ~PairingHeap()
Destructs pairing heap.
Definition: PairingHeap.h:165
void decrease(PairingHeapNode< T > *heapNode, const T &value) override
Decreases value of the given heapNode to value.
Definition: PairingHeap.h:200
PairingHeapNode< T > * next
Next sibling of the node.
Definition: PairingHeap.h:49
T value
Value contained in the node.
Definition: PairingHeap.h:46
PairingHeapNode< T > * child
First child of the node.
Definition: PairingHeap.h:50
void pop() override
Removes the top element from the heap.
Definition: PairingHeap.h:190
PairingHeapNode< T > * pair(PairingHeapNode< T > *heapNode)
Pairs list of heaps given as heapNode. Returns resulting list.
Definition: PairingHeap.h:219
PairingHeapNode< T > * push(const T &value) override
Inserts a new node with given value into a heap.
Definition: PairingHeap.h:180
virtual const C & comparator() const
Returns the comparator used to sort the values in the heap.
Definition: HeapBase.h:75
const T & top() const override
Returns reference to the top element in the heap.
Definition: PairingHeap.h:173
static void release(PairingHeapNode< T > *heapNode)
Releases memory occupied by list of heaps given as heapNode.
Definition: PairingHeap.h:309
static void unlink(PairingHeapNode< T > *heapNode)
Removes heapNode from its parent children list.
Definition: PairingHeap.h:292
Common interface for all heap classes.
Definition: HeapBase.h:55
friend class PairingHeap
Definition: PairingHeap.h:44
The namespace for all OGDF objects.
PairingHeapNode(const T &valueOfNode)
Creates heap node with a given valueOfNode.
Definition: PairingHeap.h:53
PairingHeapNode< T > * prev
Previous sibling of the node or parent.
Definition: PairingHeap.h:48
void merge(PairingHeap< T, C > &other) override
Merges in values of other heap.
Definition: PairingHeap.h:211