Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

PriorityQueue.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 #include <ogdf/basic/HashArray.h>
36 #include <ogdf/basic/Hashing.h>
37 #include <ogdf/basic/basic.h>
39 
40 #include <cstddef>
41 #include <functional>
42 #include <initializer_list>
43 
44 namespace ogdf {
45 
46 
48 
61 template<typename T, class C = std::less<T>, template<typename, class> class Impl = PairingHeap>
63 public:
64  using size_type = std::size_t;
65 
66 private:
68  using SpecImpl = Impl<T, C>;
70 
71 public:
72  using value_type = T;
73  using handle = typename SpecImpl::Handle;
75  using const_reference = const value_type&;
76 
78 
82  explicit PriorityQueue(const C& cmp = C(), int initialSize = 128)
83  : m_size(0), m_impl(new SpecImpl(cmp, initialSize)) { }
84 
87  : m_size(other.m_size), m_impl(new SpecImpl(other.m_impl)) { }
88 
91  other.swap(*this);
92  }
93 
95 
100  template<class InputIt>
101  PriorityQueue(InputIt first, InputIt last, const C& cmp = C()) : PriorityQueue(cmp) {
102  push(first, last);
103  }
104 
106 
110  PriorityQueue(std::initializer_list<value_type> init, const C& cmp = C())
111  : PriorityQueue(std::begin(init), std::end(init), cmp) { }
112 
114  ~PriorityQueue() { delete m_impl; }
115 
118  other.swap(*this);
119  return *this;
120  }
121 
123 
127  PriorityQueue& operator=(std::initializer_list<value_type> ilist) {
128  PriorityQueue tmp(ilist);
129  tmp.swap(*this);
130  return *this;
131  }
132 
134  void swap(PriorityQueue& other) {
135  std::swap(m_size, other.m_size);
136  std::swap(m_impl, other.m_impl);
137  }
138 
140  friend void swap(PriorityQueue& a, PriorityQueue& b) { a.swap(b); }
141 
143  const T& top() const { return m_impl->top(); }
144 
146  /*
147  * @param value A value to be inserted.
148  * @return Handle to the inserted node.
149  */
151  m_size++;
152  return m_impl->push(value);
153  }
154 
156 
160  template<class InputIt>
161  void push(InputIt first, InputIt last) {
162  while (first != last) {
163  push(*(first++));
164  }
165  }
166 
168 
171  void push(std::initializer_list<value_type> ilist) { push(std::begin(ilist), std::end(ilist)); }
172 
174 
177  void pop() {
178  m_size--;
179  m_impl->pop();
180  }
181 
183 
190  void decrease(handle pos, const T& value) { m_impl->decrease(pos, value); }
191 
193 
198  void merge(PriorityQueue& other) {
199  m_impl->merge(*other.m_impl);
200  m_size += other.m_size;
201  other.m_size = 0;
202  }
203 
205  void clear() {
206  PriorityQueue tmp(m_impl->comparator());
207  tmp.swap(*this);
208  }
209 
211  bool empty() const { return size() == 0; }
212 
214  size_type size() const { return m_size; }
215 
222  const T& value(handle pos) const { return m_impl->value(pos); }
223 
225  const C& comparator() const { return m_impl->comparator(); }
226 };
227 
232 namespace pq_internal {
233 
235 template<typename T, typename C>
236 class Compare {
237 public:
238  Compare(const C& compare = C()) : m_compare(compare) { }
239 
240  Compare(const Compare& other) : m_compare(other.m_compare) { }
241 
242  bool operator()(const T& x, const T& y) const { return m_compare(x.priority(), y.priority()); }
243 
244 private:
246 };
247 
249 template<typename E, typename P>
251 public:
253 
255 
256  const E& element() const { return m_element; }
257 
258  const P& priority() const { return m_priority; }
259 
260 private:
263 };
264 
266 template<typename E, typename P, class C, template<typename, class> class Impl>
268 
270 template<typename E, typename P, class C, template<typename, class> class Impl>
271 class PrioritizedQueue : public SuperQueueTemplate<E, P, C, Impl> {
272 private:
275 
277 
278 public:
280  using Handle = typename SuperQueue::handle;
281 
282  PrioritizedQueue(const C& cmp = C(), int initialSize = 128)
283  : SuperQueue(Compare<PairTemplate<E, P>, C>(cmp), initialSize), m_comp(cmp) { }
284 
286  const E& topElement() const { return SuperQueue::top().element(); }
287 
289  const P& topPriority() const { return SuperQueue::top().priority(); }
290 
297  Handle push(const E& element, const P& priority) {
298  Pair pair(element, priority);
299  Handle result = SuperQueue::push(pair);
300  return result;
301  }
302 
303  void decrease(Handle pos, const P& priority) {
304  OGDF_ASSERT(m_comp(priority, this->value(pos).priority()));
305  Pair pair(this->value(pos).element(), priority);
306  SuperQueue::decrease(pos, pair);
307  }
308 };
309 
310 template<typename E, typename P, class C, template<typename, class> class Impl, typename Map>
311 class PrioritizedArrayQueueBase : public PrioritizedQueue<E, P, C, Impl> {
312 protected:
315 
316 public:
318  bool contains(const E& element) const { return m_handles[element] != nullptr; }
319 
320  /*
321  * Returns the priority of the key.
322  * Note, that the behaviour is undefined if the key is not present.
323  */
324  const P& priority(const E& element) const {
325  OGDF_ASSERT(contains(element));
326  return this->value(m_handles[element]).priority();
327  }
328 
333  void push(const E& element, const P& priority) {
334  OGDF_ASSERT(m_handles[element] == nullptr);
335  m_handles[element] = SuperQueue::push(element, priority);
336  }
337 
341  void pop() {
342  m_handles[SuperQueue::topElement()] = nullptr;
343  SuperQueue::pop();
344  }
345 
351  void decrease(const E& element, const P& priority) {
352  Handle pos = m_handles[element];
354  }
355 
357  void clear() {
359  m_handles.clear();
360  }
361 
362 protected:
363  PrioritizedArrayQueueBase(const C& cmp, int initialSize, const Map& map)
364  : SuperQueue(cmp, initialSize), m_handles(map) { }
365 
367 };
368 
369 }
370 
372 
385 template<typename E, typename P, class C = std::less<P>, template<typename, class> class Impl = PairingHeap>
387 
389 
409 template<typename E, typename P, class C = std::less<P>,
410  template<typename, class> class Impl = PairingHeap, template<typename> class HashFunc = DefHashFunc>
412  : public pq_internal::PrioritizedArrayQueueBase<E, P, C, Impl,
413  HashArray<E, typename PrioritizedQueue<E, P, C, Impl>::Handle, HashFunc<E>>> {
414 private:
415  using CustomHashArray =
418 
419 public:
426  PrioritizedMapQueue(const C& cmp = C(), int initialSize = 128)
427  : SuperQueue(cmp, initialSize, CustomHashArray(nullptr)) { }
428 };
429 
431 template<typename P, class C, template<typename, class> class Impl, template<typename> class HashFunc>
432 class PrioritizedMapQueue<node, P, C, Impl, HashFunc>
433  : public pq_internal::PrioritizedArrayQueueBase<node, P, C, Impl,
434  NodeArray<typename PrioritizedQueue<node, P, C, Impl>::Handle>> {
435 private:
438 
439 public:
448  PrioritizedMapQueue(const Graph& G, const C& cmp = C(), int initialSize = -1)
449  : SuperQueue(cmp, initialSize == -1 ? G.numberOfNodes() : initialSize,
450  NodeArray<Handle>(G, nullptr)) { }
451 
453  void clear() {
455  this->m_handles.init(*this->m_handles.graphOf(), nullptr);
456  }
457 };
458 
460 template<typename P, class C, template<typename, class> class Impl, template<typename> class HashFunc>
461 class PrioritizedMapQueue<edge, P, C, Impl, HashFunc>
462  : public pq_internal::PrioritizedArrayQueueBase<edge, P, C, Impl,
463  EdgeArray<typename PrioritizedQueue<edge, P, C, Impl>::Handle>> {
464 private:
468 
469 public:
478  PrioritizedMapQueue(const Graph& G, const C& cmp = C(), int initialSize = -1)
479  : SuperQueue(cmp, initialSize == -1 ? G.numberOfEdges() : initialSize,
480  EdgeArray<Handle>(G, nullptr)) { }
481 
483  void clear() {
485  this->m_handles.init(*this->m_handles.graphOf(), nullptr);
486  }
487 };
488 
489 
490 }
ogdf::pq_internal::PrioritizedQueue::PrioritizedQueue
PrioritizedQueue(const C &cmp=C(), int initialSize=128)
Definition: PriorityQueue.h:282
HashArray.h
Declaration and implementation of HashArray class.
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::PriorityQueue::size_type
std::size_t size_type
Definition: PriorityQueue.h:64
ogdf::DefHashFunc
Default hash functions.
Definition: geometry.h:50
Graph.h
Includes declaration of graph class.
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(const PriorityQueue &other)
Copy constructor.
Definition: PriorityQueue.h:86
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(std::initializer_list< value_type > init, const C &cmp=C())
Creates priority queue with contents of the given initializer list.
Definition: PriorityQueue.h:110
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::pq_internal::PrioritizedArrayQueueBase::pop
void pop()
Removes the topmost element from the queue.
Definition: PriorityQueue.h:341
ogdf::pq_internal::PrioritizedQueue::topPriority
const P & topPriority() const
Returns the priority of the topmost element in this queue.
Definition: PriorityQueue.h:289
ogdf::PriorityQueue::value
const T & value(handle pos) const
Returns the priority of that handle.
Definition: PriorityQueue.h:222
Hashing.h
Declaration of classes used for hashing.
ogdf::pq_internal::PrioritizedArrayQueueBase::push
void push(const E &element, const P &priority)
Adds a new element to the queue.
Definition: PriorityQueue.h:333
ogdf::begin
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
ogdf::PrioritizedMapQueue< node, P, C, Impl, HashFunc >::clear
void clear()
Removes all elements from this queue.
Definition: PriorityQueue.h:453
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(PriorityQueue &&other)
Move constructor.
Definition: PriorityQueue.h:90
ogdf::PriorityQueue::~PriorityQueue
~PriorityQueue()
Destroys the underlying data structure.
Definition: PriorityQueue.h:114
ogdf::pq_internal::PrioritizedArrayQueueBase::priority
const P & priority(const E &element) const
Definition: PriorityQueue.h:324
ogdf::PrioritizedMapQueue< node, P, C, Impl, HashFunc >::PrioritizedMapQueue
PrioritizedMapQueue(const Graph &G, const C &cmp=C(), int initialSize=-1)
Creates a new queue with the given comparer.
Definition: PriorityQueue.h:448
ogdf::pq_internal::PrioritizedQueue::decrease
void decrease(Handle pos, const P &priority)
Definition: PriorityQueue.h:303
ogdf::pq_internal::PairTemplate::PairTemplate
PairTemplate()
Definition: PriorityQueue.h:252
ogdf::PriorityQueue::push
void push(std::initializer_list< value_type > ilist)
Inserts new elements specified by the given initializer list.
Definition: PriorityQueue.h:171
ogdf::PriorityQueue::push
void push(InputIt first, InputIt last)
Inserts new elements specified by the given range.
Definition: PriorityQueue.h:161
ogdf::pq_internal::PrioritizedQueue
Defines a queue for handling prioritized elements.
Definition: PriorityQueue.h:271
ogdf::PriorityQueue::m_size
size_type m_size
Number of entries.
Definition: PriorityQueue.h:67
ogdf::PriorityQueue::clear
void clear()
Removes all the entries from the queue.
Definition: PriorityQueue.h:205
ogdf::PrioritizedMapQueue< edge, P, C, Impl, HashFunc >::Handle
typename PrioritizedQueue< edge, P, C, Impl >::Handle Handle
Definition: PriorityQueue.h:465
ogdf::pq_internal::PrioritizedArrayQueueBase::contains
bool contains(const E &element) const
Returns whether this queue contains that key.
Definition: PriorityQueue.h:318
ogdf::PriorityQueue::pop
void pop()
Removes the top element from the heap.
Definition: PriorityQueue.h:177
ogdf::PriorityQueue::value_type
T value_type
Definition: PriorityQueue.h:72
ogdf::PriorityQueue::swap
friend void swap(PriorityQueue &a, PriorityQueue &b)
Swaps the contents.
Definition: PriorityQueue.h:140
ogdf::PriorityQueue::operator=
PriorityQueue & operator=(std::initializer_list< value_type > ilist)
Assigns the priority queue contents of the given initializer list.
Definition: PriorityQueue.h:127
ogdf::edge
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:74
ogdf::pq_internal::PairTemplate::m_element
E m_element
Definition: PriorityQueue.h:261
ogdf::pq_internal::Compare::Compare
Compare(const Compare &other)
Definition: PriorityQueue.h:240
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:211
ogdf::pq_internal::PairTemplate::element
const E & element() const
Definition: PriorityQueue.h:256
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(const C &cmp=C(), int initialSize=128)
Creates empty priority queue.
Definition: PriorityQueue.h:82
ogdf::PriorityQueue::push
handle push(const value_type &value)
Inserts a new element with given value into the queue.
Definition: PriorityQueue.h:150
ogdf::PriorityQueue::decrease
void decrease(handle pos, const T &value)
Decreases value of the element specified by handle to value.
Definition: PriorityQueue.h:190
ogdf::pq_internal::PrioritizedQueue::push
Handle push(const E &element, const P &priority)
Pushes a new element with the respective priority to the queue.
Definition: PriorityQueue.h:297
ogdf::PairingHeap
Pairing heap implementation.
Definition: PairingHeap.h:71
ogdf::pq_internal::PairTemplate::priority
const P & priority() const
Definition: PriorityQueue.h:258
ogdf::PrioritizedMapQueue
Prioritized queue interface wrapper for heaps.
Definition: PriorityQueue.h:411
ogdf::PrioritizedMapQueue::PrioritizedMapQueue
PrioritizedMapQueue(const C &cmp=C(), int initialSize=128)
Creates a new queue with the given comparer.
Definition: PriorityQueue.h:426
ogdf::pq_internal::PrioritizedArrayQueueBase::m_handles
Map m_handles
Definition: PriorityQueue.h:366
ogdf::pq_internal::PrioritizedArrayQueueBase
Definition: PriorityQueue.h:311
ogdf::HashArray
Indexed arrays using hashing for element access.
Definition: HashArray.h:93
ogdf::PrioritizedMapQueue< edge, P, C, Impl, HashFunc >::clear
void clear()
Removes all elements from this queue.
Definition: PriorityQueue.h:483
ogdf::pq_internal::Compare::operator()
bool operator()(const T &x, const T &y) const
Definition: PriorityQueue.h:242
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
PairingHeap.h
Implementation of pairing heap data structure.
ogdf::PriorityQueue::size
size_type size() const
Returns the number of enqueued elements.
Definition: PriorityQueue.h:214
ogdf::PriorityQueue::operator=
PriorityQueue & operator=(PriorityQueue other)
Copy and move assignment.
Definition: PriorityQueue.h:117
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(InputIt first, InputIt last, const C &cmp=C())
Creates priority queue with contents of the given range.
Definition: PriorityQueue.h:101
ogdf::pq_internal::Compare::m_compare
C m_compare
Definition: PriorityQueue.h:245
ogdf::pq_internal::PairTemplate::PairTemplate
PairTemplate(const E &element, const P &priority)
Definition: PriorityQueue.h:254
ogdf::HashingBase::init
void init(int tableSize)
Initializes the table for given table size.
ogdf::pq_internal::PrioritizedQueue::topElement
const E & topElement() const
Returns the topmost element in the queue.
Definition: PriorityQueue.h:286
ogdf::pq_internal::PairTemplate::m_priority
P m_priority
Definition: PriorityQueue.h:262
ogdf::pq_internal::PrioritizedArrayQueueBase::clear
void clear()
Removes all elements from this queue.
Definition: PriorityQueue.h:357
ogdf::graphics::init
void init()
Definition: graphics.h:450
ogdf::PriorityQueue::const_reference
const value_type & const_reference
Definition: PriorityQueue.h:75
ogdf::pq_internal::PrioritizedArrayQueueBase::decrease
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Definition: PriorityQueue.h:351
ogdf::PriorityQueue::top
const T & top() const
Returns reference to the top element in the queue.
Definition: PriorityQueue.h:143
ogdf::pq_internal::Compare::Compare
Compare(const C &compare=C())
Definition: PriorityQueue.h:238
basic.h
Basic declarations, included by all source files.
std
Definition: GML.h:111
ogdf::end
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
ogdf::pq_internal::PrioritizedQueue::m_comp
C m_comp
Definition: PriorityQueue.h:276
ogdf::PrioritizedMapQueue< node, P, C, Impl, HashFunc >::Handle
typename PrioritizedQueue< node, P, C, Impl >::Handle Handle
Definition: PriorityQueue.h:436
ogdf::pq_internal::PrioritizedQueue< Node, double >::Handle
typename SuperQueue::handle Handle
The type of handle for accessing the elements of this queue.
Definition: PriorityQueue.h:280
ogdf::pq_internal::PrioritizedArrayQueueBase::PrioritizedArrayQueueBase
PrioritizedArrayQueueBase(const C &cmp, int initialSize, const Map &map)
Definition: PriorityQueue.h:363
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::PriorityQueue::handle
typename SpecImpl::Handle handle
Definition: PriorityQueue.h:73
ogdf::PriorityQueue::swap
void swap(PriorityQueue &other)
Swaps the contents.
Definition: PriorityQueue.h:134
ogdf::pq_internal::PairTemplate
Pair for storing an element and a priority.
Definition: PriorityQueue.h:250
ogdf::PriorityQueue::m_impl
SpecImpl * m_impl
Underlying heap data structure.
Definition: PriorityQueue.h:69
ogdf::PriorityQueue
Priority queue interface wrapper for heaps.
Definition: PriorityQueue.h:62
ogdf::pq_internal::Compare
Used to compare elements with assigned priorities.
Definition: PriorityQueue.h:236
ogdf::PrioritizedMapQueue< edge, P, C, Impl, HashFunc >::PrioritizedMapQueue
PrioritizedMapQueue(const Graph &G, const C &cmp=C(), int initialSize=-1)
Creates a new queue with the given comparer.
Definition: PriorityQueue.h:478
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::PriorityQueue::SpecImpl
Impl< T, C > SpecImpl
Definition: PriorityQueue.h:68
ogdf::PriorityQueue::merge
void merge(PriorityQueue &other)
Merges in enqueued values of other queue.
Definition: PriorityQueue.h:198
ogdf::PriorityQueue::comparator
const C & comparator() const
Returns the comparator used for ordering elements.
Definition: PriorityQueue.h:225
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::PriorityQueue::reference
value_type & reference
Definition: PriorityQueue.h:74