Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

PriorityQueue.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <utility>
35 #include <functional>
36 
37 #include <ogdf/basic/NodeArray.h>
38 #include <ogdf/basic/EdgeArray.h>
39 #include <ogdf/basic/HashArray.h>
40 
42 
43 namespace ogdf {
44 
45 
47 
60 template<
61  typename T,
62  class C=std::less<T>,
63  template<typename, class> class Impl=PairingHeap
64 >
66 public:
67  using size_type = std::size_t;
68 
69 private:
71  const C &m_cmp;
72  using SpecImpl = Impl<T, C>;
74 
75 public:
76  using value_type = T;
77  using handle = typename SpecImpl::Handle;
78  using reference = value_type &;
79  using const_reference = const value_type &;
80 
82 
86  explicit PriorityQueue(const C &cmp = C(), int initialSize = 128)
87  : m_size(0), m_cmp(cmp), m_impl(new SpecImpl(cmp, initialSize))
88  {
89  }
90 
93  : m_size(other.m_size), m_impl(new SpecImpl(other.m_impl))
94  {
95  }
96 
99  : PriorityQueue(other.m_impl->comparator())
100  {
101  other.swap(*this);
102  }
103 
105 
110  template<class InputIt>
111  PriorityQueue(InputIt first, InputIt last, const C &cmp = C())
112  : PriorityQueue(cmp)
113  {
114  push(first, last);
115  }
116 
118 
122  PriorityQueue(std::initializer_list<value_type> init, const C &cmp = C())
123  : PriorityQueue(std::begin(init), std::end(init), cmp)
124  {
125  }
126 
129  delete m_impl;
130  }
131 
134  other.swap(*this);
135  return *this;
136  }
137 
139 
143  PriorityQueue &operator=(std::initializer_list<value_type> ilist) {
144  PriorityQueue tmp(ilist);
145  tmp.swap(*this);
146  return *this;
147  }
148 
150  void swap(PriorityQueue &other) {
151  using std::swap;
152  swap(m_size, other.m_size);
153  swap(m_impl, other.m_impl);
154  }
155 
157  friend void swap(PriorityQueue &a, PriorityQueue &b) {
158  a.swap(b);
159  }
160 
162  const T &top() const {
163  return m_impl->top();
164  }
165 
167  /*
168  * @param value A value to be inserted.
169  * @return Handle to the inserted node.
170  */
172  m_size++;
173  return m_impl->push(value);
174  }
175 
177 
181  template<class InputIt>
182  void push(InputIt first, InputIt last) {
183  while(first != last) {
184  push(*(first++));
185  }
186  }
187 
189 
192  void push(std::initializer_list<value_type> ilist) {
193  push(std::begin(ilist), std::end(ilist));
194  }
195 
197 
200  void pop() {
201  m_size--;
202  m_impl->pop();
203  }
204 
206 
213  void decrease(handle pos, const T &value) {
214  m_impl->decrease(pos, value);
215  }
216 
218 
223  void merge(PriorityQueue &other) {
224  m_impl->merge(other->m_impl);
225  m_size += other.m_size;
226  other.m_size = 0;
227  }
228 
230  void clear() {
231  PriorityQueue tmp(m_cmp);
232  tmp.swap(*this);
233  }
234 
236  bool empty() const {
237  return size() == 0;
238  }
239 
241  size_type size() const {
242  return m_size;
243  }
244 
251  const T &value(handle pos) const {
252  return m_impl->value(pos);
253  }
254 };
255 
260 namespace pq_internal {
261 
263 template<
264  typename T,
265  typename C
266 >
267 class Compare
268 {
269 public:
270  Compare(const C &compare = C()) : m_compare(compare) {}
271 
272  bool operator()(const T &x, const T &y) const {
273  return m_compare(x.priority(), y.priority());
274  }
275 private:
277 };
278 
280 template<
281  typename E,
282  typename P
283 >
285 {
286 public:
288  : m_priority(-1)
289  {
290  }
291 
292  PairTemplate(const E &element, const P &priority)
293  : m_element(element), m_priority(priority)
294  {
295  }
296 
298  {
299  m_element = pair.m_element;
300  m_priority = pair.m_priority;
301 
302  return *this;
303  }
304 
305  const E &element() const {
306  return m_element;
307  }
308 
309  const P &priority() const {
310  return m_priority;
311  }
312 
313 private:
316 };
317 
319 template<
320  typename E,
321  typename P,
322  class C,
323  template<typename, class> class Impl
324 >
326 
328 template<
329  typename E,
330  typename P,
331  class C,
332  template<typename, class> class Impl
333 >
334 class PrioritizedQueue : public SuperQueueTemplate<E, P, C, Impl>
335 {
336 private:
339 
341 
342 public:
344  using Handle = typename SuperQueue::handle;
345 
346  PrioritizedQueue(const C &cmp = C(), int initialSize = 128)
347  : SuperQueue(Compare<PairTemplate<E, P>, C>(cmp), initialSize), m_comp(cmp)
348  {
349  }
350 
352  const E &topElement() const {
353  return SuperQueue::top().element();
354  }
355 
357  const P &topPriority() const {
358  return SuperQueue::top().priority();
359  }
360 
367  Handle push(const E &element, const P &priority) {
368  Pair pair(element, priority);
369  Handle result = SuperQueue::push(pair);
370  return result;
371  }
372 
373  void decrease(Handle pos, const P &priority) {
374  OGDF_ASSERT(m_comp(priority, this->value(pos).priority()));
375  Pair pair(this->value(pos).element(), priority);
376  SuperQueue::decrease(pos, pair);
377  }
378 };
379 
380 template<
381  typename E,
382  typename P,
383  class C,
384  template<typename, class> class Impl,
385  typename Map
386 >
387 class PrioritizedArrayQueueBase : public PrioritizedQueue<E, P, C, Impl>
388 {
389 protected:
392 
393 public:
394 
396  bool contains(const E &element) const
397  {
398  return m_handles[element] != nullptr;
399  }
400 
401  /*
402  * Returns the priority of the key.
403  * Note, that the behaviour is undefined if the key is not present.
404  */
405  const P &priority(const E &element) const
406  {
407  OGDF_ASSERT(contains(element));
408  return this->value(m_handles[element]).priority();
409  }
410 
415  void push(const E &element, const P &priority) {
416  OGDF_ASSERT(m_handles[element] == nullptr);
417  m_handles[element] = SuperQueue::push(element, priority);
418  }
419 
423  void pop() {
424  m_handles[SuperQueue::topElement()] = nullptr;
425  SuperQueue::pop();
426  }
427 
433  void decrease(const E &element, const P &priority) {
434  Handle pos = m_handles[element];
435  SuperQueue::decrease(pos, priority);
436  }
437 
439  void clear() {
440  SuperQueue::clear();
441  m_handles.clear();
442  }
443 
444 protected:
445  PrioritizedArrayQueueBase(const C &cmp, int initialSize, const Map &map)
446  : SuperQueue(cmp, initialSize), m_handles(map)
447  {
448  }
449 
451 };
452 
453 } // end namespace pq_internal
454 
456 
469 template<
470  typename E,
471  typename P,
472  class C=std::less<P>,
473  template<typename, class> class Impl=PairingHeap
474 >
476 
478 
498 template<
499  typename E,
500  typename P,
501  class C=std::less<P>,
502  template<typename, class> class Impl=PairingHeap,
503  template<typename> class HashFunc=DefHashFunc
504 >
505 class PrioritizedMapQueue : public pq_internal::PrioritizedArrayQueueBase<E, P, C, Impl, HashArray<E, typename PrioritizedQueue<E, P, C, Impl>::Handle, HashFunc<E>>>
506 {
507 private:
510 
511 public:
512 
519  PrioritizedMapQueue(const C &cmp = C(), int initialSize = 128)
520  : SuperQueue(cmp, initialSize, CustomHashArray(nullptr))
521  {
522  }
523 };
524 
526 template<
527  typename P,
528  class C,
529  template<typename, class> class Impl,
530  template<typename> class HashFunc
531 >
532 class PrioritizedMapQueue<node, P, C, Impl, HashFunc> : public pq_internal::PrioritizedArrayQueueBase<node, P, C, Impl, NodeArray<typename PrioritizedQueue<node, P, C, Impl>::Handle>>
533 {
534 private:
537 
538 public:
539 
548  PrioritizedMapQueue(const Graph &G, const C &cmp = C(), int initialSize = -1)
549  : SuperQueue(cmp, initialSize == -1 ? G.numberOfNodes() : initialSize, NodeArray<Handle>(G, nullptr))
550  {
551  }
552 
554  void clear() {
555  SuperQueue::SuperQueue::clear();
556  this->m_handles.init(*this->m_handles.graphOf(), nullptr);
557  }
558 };
559 
561 template<
562  typename P,
563  class C,
564  template<typename, class> class Impl,
565  template<typename> class HashFunc
566 >
567 class PrioritizedMapQueue<edge, P, C, Impl, HashFunc> : public pq_internal::PrioritizedArrayQueueBase<edge, P, C, Impl, EdgeArray<typename PrioritizedQueue<edge, P, C, Impl>::Handle>>
568 {
569 private:
572 
573 public:
574 
583  PrioritizedMapQueue(const Graph &G, const C &cmp = C(), int initialSize = -1)
584  : SuperQueue(cmp, initialSize == -1 ? G.numberOfEdges() : initialSize, EdgeArray<Handle>(G, nullptr))
585  {
586  }
587 
589  void clear() {
590  SuperQueue::SuperQueue::clear();
591  this->m_handles.init(*this->m_handles.graphOf(), nullptr);
592  }
593 };
594 
595 
596 } // end namespace ogdf
Default hash functions.
Definition: Hashing.h:218
void pop()
Removes the topmost element from the queue.
Declaration and implementation of HashArray class.
typename SpecImpl::Handle handle
Definition: PriorityQueue.h:77
PriorityQueue & operator=(PriorityQueue other)
Copy and move assignment.
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
void push(const E &element, const P &priority)
Adds a new element to the queue.
PriorityQueue(const C &cmp=C(), int initialSize=128)
Creates empty priority queue.
Definition: PriorityQueue.h:86
size_type size() const
Returns the number of enqueued elements.
Pairing heap implementation.
Definition: PairingHeap.h:72
void swap(PriorityQueue &other)
Swaps the contents.
Indexed arrays using hashing for element access.
Definition: HashArray.h:94
void clear()
Removes all elements from this queue.
typename SuperQueue::handle Handle
The type of handle for accessing the elements of this queue.
PrioritizedMapQueue(const Graph &G, const C &cmp=C(), int initialSize=-1)
Creates a new queue with the given comparer.
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
const P & priority(const E &element) const
void clear()
Removes all elements from this queue.
Defines a queue for handling prioritized elements.
Prioritized queue interface wrapper for heaps.
PriorityQueue(const PriorityQueue &other)
Copy constructor.
Definition: PriorityQueue.h:92
Impl< T, C > SpecImpl
Definition: PriorityQueue.h:72
Used to compare elements with assigned priorities.
typename PrioritizedQueue< node, P, C, Impl >::Handle Handle
const T & value(handle pos) const
Returns the priority of that handle.
void merge(PriorityQueue &other)
Merges in enqueued values of other queue.
bool operator()(const T &x, const T &y) const
PrioritizedMapQueue(const C &cmp=C(), int initialSize=128)
Creates a new queue with the given comparer.
const value_type & const_reference
Definition: PriorityQueue.h:79
PrioritizedQueue(const C &cmp=C(), int initialSize=128)
void decrease(Handle pos, const P &priority)
std::size_t size_type
Definition: PriorityQueue.h:67
void push(InputIt first, InputIt last)
Inserts new elements specified by the given range.
Pair for storing an element and a priority.
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
const E & topElement() const
Returns the topmost element in the queue.
Declaration and implementation of EdgeArray class.
Implementation of pairing heap data structure.
void pop()
Removes the top element from the heap.
SpecImpl * m_impl
Underlying heap data structure.
Definition: PriorityQueue.h:73
handle push(const value_type &value)
Inserts a new element with given value into the queue.
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Compare(const C &compare=C())
PrioritizedArrayQueueBase(const C &cmp, int initialSize, const Map &map)
void clear()
Removes all the entries from the queue.
Class for the representation of edges.
Definition: Graph_d.h:291
friend void swap(PriorityQueue &a, PriorityQueue &b)
Swaps the contents.
PriorityQueue(PriorityQueue &&other)
Move constructor.
Definition: PriorityQueue.h:98
value_type & reference
Definition: PriorityQueue.h:78
Class for the representation of nodes.
Definition: Graph_d.h:167
Declaration and implementation of NodeArray class.
Priority queue interface wrapper for heaps.
Definition: PriorityQueue.h:65
PrioritizedMapQueue(const Graph &G, const C &cmp=C(), int initialSize=-1)
Creates a new queue with the given comparer.
void clear()
Removes all elements from this queue.
const T & top() const
Returns reference to the top element in the queue.
~PriorityQueue()
Destroys the underlying data structure.
void push(std::initializer_list< value_type > ilist)
Inserts new elements specified by the given initializer list.
bool empty() const
Checks whether the queue is empty.
const P & topPriority() const
Returns the priority of the topmost element in this queue.
PriorityQueue(std::initializer_list< value_type > init, const C &cmp=C())
Creates priority queue with contents of the given initializer list.
PairTemplate & operator=(const PairTemplate &pair)
bool contains(const E &element) const
Returns whether this queue contains that key.
The namespace for all OGDF objects.
PriorityQueue(InputIt first, InputIt last, const C &cmp=C())
Creates priority queue with contents of the given range.
void decrease(handle pos, const T &value)
Decreases value of the element specified by handle to value.
Handle push(const E &element, const P &priority)
Pushes a new element with the respective priotiy to the queue.
PriorityQueue & operator=(std::initializer_list< value_type > ilist)
Assigns the priority queue contents of the given initializer list.
typename PrioritizedQueue< edge, P, C, Impl >::Handle Handle
PairTemplate(const E &element, const P &priority)
size_type m_size
Number of entries.
Definition: PriorityQueue.h:70
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41