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/EdgeArray.h>
35 #include <ogdf/basic/HashArray.h>
36 #include <ogdf/basic/NodeArray.h>
38 
39 #include <functional>
40 #include <utility>
41 
42 namespace ogdf {
43 
44 
46 
59 template<typename T, class C = std::less<T>, template<typename, class> class Impl = PairingHeap>
61 public:
62  using size_type = std::size_t;
63 
64 private:
66  using SpecImpl = Impl<T, C>;
68 
69 public:
70  using value_type = T;
71  using handle = typename SpecImpl::Handle;
73  using const_reference = const value_type&;
74 
76 
80  explicit PriorityQueue(const C& cmp = C(), int initialSize = 128)
81  : m_size(0), m_impl(new SpecImpl(cmp, initialSize)) { }
82 
85  : m_size(other.m_size), m_impl(new SpecImpl(other.m_impl)) { }
86 
89  other.swap(*this);
90  }
91 
93 
98  template<class InputIt>
99  PriorityQueue(InputIt first, InputIt last, const C& cmp = C()) : PriorityQueue(cmp) {
100  push(first, last);
101  }
102 
104 
108  PriorityQueue(std::initializer_list<value_type> init, const C& cmp = C())
109  : PriorityQueue(std::begin(init), std::end(init), cmp) { }
110 
112  ~PriorityQueue() { delete m_impl; }
113 
116  other.swap(*this);
117  return *this;
118  }
119 
121 
125  PriorityQueue& operator=(std::initializer_list<value_type> ilist) {
126  PriorityQueue tmp(ilist);
127  tmp.swap(*this);
128  return *this;
129  }
130 
132  void swap(PriorityQueue& other) {
133  std::swap(m_size, other.m_size);
134  std::swap(m_impl, other.m_impl);
135  }
136 
138  friend void swap(PriorityQueue& a, PriorityQueue& b) { a.swap(b); }
139 
141  const T& top() const { return m_impl->top(); }
142 
144  /*
145  * @param value A value to be inserted.
146  * @return Handle to the inserted node.
147  */
149  m_size++;
150  return m_impl->push(value);
151  }
152 
154 
158  template<class InputIt>
159  void push(InputIt first, InputIt last) {
160  while (first != last) {
161  push(*(first++));
162  }
163  }
164 
166 
169  void push(std::initializer_list<value_type> ilist) { push(std::begin(ilist), std::end(ilist)); }
170 
172 
175  void pop() {
176  m_size--;
177  m_impl->pop();
178  }
179 
181 
188  void decrease(handle pos, const T& value) { m_impl->decrease(pos, value); }
189 
191 
196  void merge(PriorityQueue& other) {
197  m_impl->merge(*other.m_impl);
198  m_size += other.m_size;
199  other.m_size = 0;
200  }
201 
203  void clear() {
204  PriorityQueue tmp(m_impl->comparator());
205  tmp.swap(*this);
206  }
207 
209  bool empty() const { return size() == 0; }
210 
212  size_type size() const { return m_size; }
213 
220  const T& value(handle pos) const { return m_impl->value(pos); }
221 
223  const C& comparator() const { return m_impl->comparator(); }
224 };
225 
230 namespace pq_internal {
231 
233 template<typename T, typename C>
234 class Compare {
235 public:
236  Compare(const C& compare = C()) : m_compare(compare) { }
237 
238  Compare(const Compare& other) : m_compare(other.m_compare) { }
239 
240  bool operator()(const T& x, const T& y) const { return m_compare(x.priority(), y.priority()); }
241 
242 private:
244 };
245 
247 template<typename E, typename P>
249 public:
251 
253 
254  const E& element() const { return m_element; }
255 
256  const P& priority() const { return m_priority; }
257 
258 private:
261 };
262 
264 template<typename E, typename P, class C, template<typename, class> class Impl>
266 
268 template<typename E, typename P, class C, template<typename, class> class Impl>
269 class PrioritizedQueue : public SuperQueueTemplate<E, P, C, Impl> {
270 private:
273 
275 
276 public:
278  using Handle = typename SuperQueue::handle;
279 
280  PrioritizedQueue(const C& cmp = C(), int initialSize = 128)
281  : SuperQueue(Compare<PairTemplate<E, P>, C>(cmp), initialSize), m_comp(cmp) { }
282 
284  const E& topElement() const { return SuperQueue::top().element(); }
285 
287  const P& topPriority() const { return SuperQueue::top().priority(); }
288 
295  Handle push(const E& element, const P& priority) {
296  Pair pair(element, priority);
297  Handle result = SuperQueue::push(pair);
298  return result;
299  }
300 
301  void decrease(Handle pos, const P& priority) {
302  OGDF_ASSERT(m_comp(priority, this->value(pos).priority()));
303  Pair pair(this->value(pos).element(), priority);
304  SuperQueue::decrease(pos, pair);
305  }
306 };
307 
308 template<typename E, typename P, class C, template<typename, class> class Impl, typename Map>
309 class PrioritizedArrayQueueBase : public PrioritizedQueue<E, P, C, Impl> {
310 protected:
313 
314 public:
316  bool contains(const E& element) const { return m_handles[element] != nullptr; }
317 
318  /*
319  * Returns the priority of the key.
320  * Note, that the behaviour is undefined if the key is not present.
321  */
322  const P& priority(const E& element) const {
323  OGDF_ASSERT(contains(element));
324  return this->value(m_handles[element]).priority();
325  }
326 
331  void push(const E& element, const P& priority) {
332  OGDF_ASSERT(m_handles[element] == nullptr);
333  m_handles[element] = SuperQueue::push(element, priority);
334  }
335 
339  void pop() {
340  m_handles[SuperQueue::topElement()] = nullptr;
341  SuperQueue::pop();
342  }
343 
349  void decrease(const E& element, const P& priority) {
350  Handle pos = m_handles[element];
352  }
353 
355  void clear() {
357  m_handles.clear();
358  }
359 
360 protected:
361  PrioritizedArrayQueueBase(const C& cmp, int initialSize, const Map& map)
362  : SuperQueue(cmp, initialSize), m_handles(map) { }
363 
365 };
366 
367 }
368 
370 
383 template<typename E, typename P, class C = std::less<P>, template<typename, class> class Impl = PairingHeap>
385 
387 
407 template<typename E, typename P, class C = std::less<P>,
408  template<typename, class> class Impl = PairingHeap, template<typename> class HashFunc = DefHashFunc>
410  : public pq_internal::PrioritizedArrayQueueBase<E, P, C, Impl,
411  HashArray<E, typename PrioritizedQueue<E, P, C, Impl>::Handle, HashFunc<E>>> {
412 private:
413  using CustomHashArray =
416 
417 public:
424  PrioritizedMapQueue(const C& cmp = C(), int initialSize = 128)
425  : SuperQueue(cmp, initialSize, CustomHashArray(nullptr)) { }
426 };
427 
429 template<typename P, class C, template<typename, class> class Impl, template<typename> class HashFunc>
430 class PrioritizedMapQueue<node, P, C, Impl, HashFunc>
431  : public pq_internal::PrioritizedArrayQueueBase<node, P, C, Impl,
432  NodeArray<typename PrioritizedQueue<node, P, C, Impl>::Handle>> {
433 private:
436 
437 public:
446  PrioritizedMapQueue(const Graph& G, const C& cmp = C(), int initialSize = -1)
447  : SuperQueue(cmp, initialSize == -1 ? G.numberOfNodes() : initialSize,
448  NodeArray<Handle>(G, nullptr)) { }
449 
451  void clear() {
453  this->m_handles.init(*this->m_handles.graphOf(), nullptr);
454  }
455 };
456 
458 template<typename P, class C, template<typename, class> class Impl, template<typename> class HashFunc>
459 class PrioritizedMapQueue<edge, P, C, Impl, HashFunc>
460  : public pq_internal::PrioritizedArrayQueueBase<edge, P, C, Impl,
461  EdgeArray<typename PrioritizedQueue<edge, P, C, Impl>::Handle>> {
462 private:
466 
467 public:
476  PrioritizedMapQueue(const Graph& G, const C& cmp = C(), int initialSize = -1)
477  : SuperQueue(cmp, initialSize == -1 ? G.numberOfEdges() : initialSize,
478  EdgeArray<Handle>(G, nullptr)) { }
479 
481  void clear() {
483  this->m_handles.init(*this->m_handles.graphOf(), nullptr);
484  }
485 };
486 
487 
488 }
ogdf::pq_internal::PrioritizedQueue::PrioritizedQueue
PrioritizedQueue(const C &cmp=C(), int initialSize=128)
Definition: PriorityQueue.h:280
HashArray.h
Declaration and implementation of HashArray class.
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::PriorityQueue::size_type
std::size_t size_type
Definition: PriorityQueue.h:62
ogdf::DefHashFunc
Default hash functions.
Definition: Hashing.h:213
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(const PriorityQueue &other)
Copy constructor.
Definition: PriorityQueue.h:84
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:108
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::pq_internal::PrioritizedArrayQueueBase::pop
void pop()
Removes the topmost element from the queue.
Definition: PriorityQueue.h:339
ogdf::pq_internal::PrioritizedQueue::topPriority
const P & topPriority() const
Returns the priority of the topmost element in this queue.
Definition: PriorityQueue.h:287
ogdf::PriorityQueue::value
const T & value(handle pos) const
Returns the priority of that handle.
Definition: PriorityQueue.h:220
ogdf::pq_internal::PrioritizedArrayQueueBase::push
void push(const E &element, const P &priority)
Adds a new element to the queue.
Definition: PriorityQueue.h:331
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:451
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(PriorityQueue &&other)
Move constructor.
Definition: PriorityQueue.h:88
ogdf::PriorityQueue::~PriorityQueue
~PriorityQueue()
Destroys the underlying data structure.
Definition: PriorityQueue.h:112
ogdf::pq_internal::PrioritizedArrayQueueBase::priority
const P & priority(const E &element) const
Definition: PriorityQueue.h:322
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:446
ogdf::pq_internal::PrioritizedQueue::decrease
void decrease(Handle pos, const P &priority)
Definition: PriorityQueue.h:301
ogdf::pq_internal::PairTemplate::PairTemplate
PairTemplate()
Definition: PriorityQueue.h:250
ogdf::PriorityQueue::push
void push(std::initializer_list< value_type > ilist)
Inserts new elements specified by the given initializer list.
Definition: PriorityQueue.h:169
ogdf::PriorityQueue::push
void push(InputIt first, InputIt last)
Inserts new elements specified by the given range.
Definition: PriorityQueue.h:159
ogdf::pq_internal::PrioritizedQueue
Defines a queue for handling prioritized elements.
Definition: PriorityQueue.h:269
ogdf::PriorityQueue::m_size
size_type m_size
Number of entries.
Definition: PriorityQueue.h:65
ogdf::PriorityQueue::clear
void clear()
Removes all the entries from the queue.
Definition: PriorityQueue.h:203
ogdf::PrioritizedMapQueue< edge, P, C, Impl, HashFunc >::Handle
typename PrioritizedQueue< edge, P, C, Impl >::Handle Handle
Definition: PriorityQueue.h:463
ogdf::pq_internal::PrioritizedArrayQueueBase::contains
bool contains(const E &element) const
Returns whether this queue contains that key.
Definition: PriorityQueue.h:316
ogdf::PriorityQueue::pop
void pop()
Removes the top element from the heap.
Definition: PriorityQueue.h:175
ogdf::PriorityQueue::value_type
T value_type
Definition: PriorityQueue.h:70
ogdf::PriorityQueue::swap
friend void swap(PriorityQueue &a, PriorityQueue &b)
Swaps the contents.
Definition: PriorityQueue.h:138
ogdf::PriorityQueue::operator=
PriorityQueue & operator=(std::initializer_list< value_type > ilist)
Assigns the priority queue contents of the given initializer list.
Definition: PriorityQueue.h:125
ogdf::edge
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:67
ogdf::pq_internal::PairTemplate::m_element
E m_element
Definition: PriorityQueue.h:259
ogdf::pq_internal::Compare::Compare
Compare(const Compare &other)
Definition: PriorityQueue.h:238
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:209
ogdf::pq_internal::PairTemplate::element
const E & element() const
Definition: PriorityQueue.h:254
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(const C &cmp=C(), int initialSize=128)
Creates empty priority queue.
Definition: PriorityQueue.h:80
ogdf::PriorityQueue::push
handle push(const value_type &value)
Inserts a new element with given value into the queue.
Definition: PriorityQueue.h:148
ogdf::PriorityQueue::decrease
void decrease(handle pos, const T &value)
Decreases value of the element specified by handle to value.
Definition: PriorityQueue.h:188
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:295
ogdf::PairingHeap
Pairing heap implementation.
Definition: PairingHeap.h:70
ogdf::pq_internal::PairTemplate::priority
const P & priority() const
Definition: PriorityQueue.h:256
ogdf::PrioritizedMapQueue
Prioritized queue interface wrapper for heaps.
Definition: PriorityQueue.h:409
ogdf::PrioritizedMapQueue::PrioritizedMapQueue
PrioritizedMapQueue(const C &cmp=C(), int initialSize=128)
Creates a new queue with the given comparer.
Definition: PriorityQueue.h:424
EdgeArray.h
Declaration and implementation of EdgeArray class.
ogdf::pq_internal::PrioritizedArrayQueueBase::m_handles
Map m_handles
Definition: PriorityQueue.h:364
ogdf::pq_internal::PrioritizedArrayQueueBase
Definition: PriorityQueue.h:309
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:481
ogdf::pq_internal::Compare::operator()
bool operator()(const T &x, const T &y) const
Definition: PriorityQueue.h:240
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
PairingHeap.h
Implementation of pairing heap data structure.
ogdf::PriorityQueue::size
size_type size() const
Returns the number of enqueued elements.
Definition: PriorityQueue.h:212
ogdf::PriorityQueue::operator=
PriorityQueue & operator=(PriorityQueue other)
Copy and move assignment.
Definition: PriorityQueue.h:115
ogdf::PriorityQueue::PriorityQueue
PriorityQueue(InputIt first, InputIt last, const C &cmp=C())
Creates priority queue with contents of the given range.
Definition: PriorityQueue.h:99
ogdf::pq_internal::Compare::m_compare
C m_compare
Definition: PriorityQueue.h:243
ogdf::pq_internal::PairTemplate::PairTemplate
PairTemplate(const E &element, const P &priority)
Definition: PriorityQueue.h:252
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:284
ogdf::pq_internal::PairTemplate::m_priority
P m_priority
Definition: PriorityQueue.h:260
ogdf::pq_internal::PrioritizedArrayQueueBase::clear
void clear()
Removes all elements from this queue.
Definition: PriorityQueue.h:355
NodeArray.h
Declaration and implementation of NodeArray class.
ogdf::graphics::init
void init()
Definition: graphics.h:446
ogdf::PriorityQueue::const_reference
const value_type & const_reference
Definition: PriorityQueue.h:73
ogdf::pq_internal::PrioritizedArrayQueueBase::decrease
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Definition: PriorityQueue.h:349
ogdf::PriorityQueue::top
const T & top() const
Returns reference to the top element in the queue.
Definition: PriorityQueue.h:141
ogdf::pq_internal::Compare::Compare
Compare(const C &compare=C())
Definition: PriorityQueue.h:236
std
Definition: GML.h:110
ogdf::end
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
ogdf::pq_internal::PrioritizedQueue::m_comp
C m_comp
Definition: PriorityQueue.h:274
ogdf::PrioritizedMapQueue< node, P, C, Impl, HashFunc >::Handle
typename PrioritizedQueue< node, P, C, Impl >::Handle Handle
Definition: PriorityQueue.h:434
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:278
ogdf::pq_internal::PrioritizedArrayQueueBase::PrioritizedArrayQueueBase
PrioritizedArrayQueueBase(const C &cmp, int initialSize, const Map &map)
Definition: PriorityQueue.h:361
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::PriorityQueue::handle
typename SpecImpl::Handle handle
Definition: PriorityQueue.h:71
ogdf::PriorityQueue::swap
void swap(PriorityQueue &other)
Swaps the contents.
Definition: PriorityQueue.h:132
ogdf::pq_internal::PairTemplate
Pair for storing an element and a priority.
Definition: PriorityQueue.h:248
ogdf::PriorityQueue::m_impl
SpecImpl * m_impl
Underlying heap data structure.
Definition: PriorityQueue.h:67
ogdf::PriorityQueue
Priority queue interface wrapper for heaps.
Definition: PriorityQueue.h:60
ogdf::pq_internal::Compare
Used to compare elements with assigned priorities.
Definition: PriorityQueue.h:234
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:476
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::PriorityQueue::SpecImpl
Impl< T, C > SpecImpl
Definition: PriorityQueue.h:66
ogdf::PriorityQueue::merge
void merge(PriorityQueue &other)
Merges in enqueued values of other queue.
Definition: PriorityQueue.h:196
ogdf::PriorityQueue::comparator
const C & comparator() const
Returns the comparator used for ordering elements.
Definition: PriorityQueue.h:223
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709
ogdf::PriorityQueue::reference
value_type & reference
Definition: PriorityQueue.h:72