Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
PriorityQueue.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/Graph.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
44namespace ogdf {
45
46
48
61template<typename T, class C = std::less<T>, template<typename, class> class Impl = PairingHeap>
63public:
64 using size_type = std::size_t;
65
66private:
68 using SpecImpl = Impl<T, C>;
70
71public:
72 using value_type = T;
73 using handle = typename SpecImpl::Handle;
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
232namespace pq_internal {
233
235template<typename T, typename C>
236class Compare {
237public:
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
244private:
246};
247
249template<typename E, typename P>
251public:
253
255
256 const E& element() const { return m_element; }
257
258 const P& priority() const { return m_priority; }
259
260private:
263};
264
266template<typename E, typename P, class C, template<typename, class> class Impl>
268
270template<typename E, typename P, class C, template<typename, class> class Impl>
271class PrioritizedQueue : public SuperQueueTemplate<E, P, C, Impl> {
272private:
275
277
278public:
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
310template<typename E, typename P, class C, template<typename, class> class Impl, typename Map>
311class PrioritizedArrayQueueBase : public PrioritizedQueue<E, P, C, Impl> {
312protected:
315
316public:
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() {
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
362protected:
363 PrioritizedArrayQueueBase(const C& cmp, int initialSize, const Map& map)
364 : SuperQueue(cmp, initialSize), m_handles(map) { }
365
367};
368
369}
370
372
385template<typename E, typename P, class C = std::less<P>, template<typename, class> class Impl = PairingHeap>
387
389
409template<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>>> {
414private:
418
419public:
426 PrioritizedMapQueue(const C& cmp = C(), int initialSize = 128)
427 : SuperQueue(cmp, initialSize, CustomHashArray(nullptr)) { }
428};
429
431template<typename P, class C, template<typename, class> class Impl, template<typename> class HashFunc>
432class PrioritizedMapQueue<node, P, C, Impl, HashFunc>
433 : public pq_internal::PrioritizedArrayQueueBase<node, P, C, Impl,
434 NodeArray<typename PrioritizedQueue<node, P, C, Impl>::Handle>> {
435private:
438
439public:
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
460template<typename P, class C, template<typename, class> class Impl, template<typename> class HashFunc>
461class PrioritizedMapQueue<edge, P, C, Impl, HashFunc>
462 : public pq_internal::PrioritizedArrayQueueBase<edge, P, C, Impl,
463 EdgeArray<typename PrioritizedQueue<edge, P, C, Impl>::Handle>> {
464private:
468
469public:
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}
Includes declaration of graph class.
Declaration and implementation of HashArray class.
Declaration of classes used for hashing.
Implementation of pairing heap data structure.
Basic declarations, included by all source files.
Default hash functions.
Definition Hashing.h:216
Class for the representation of edges.
Definition Graph_d.h:364
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Indexed arrays using hashing for element access.
Definition HashArray.h:93
Class for the representation of nodes.
Definition Graph_d.h:241
Pairing heap implementation.
Definition PairingHeap.h:71
void clear()
Removes all elements from this queue.
PrioritizedMapQueue(const Graph &G, const C &cmp=C(), int initialSize=-1)
Creates a new queue with the given comparer.
typename PrioritizedQueue< edge, P, C, Impl >::Handle Handle
typename PrioritizedQueue< node, P, C, Impl >::Handle Handle
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.
Prioritized queue interface wrapper for heaps.
PrioritizedMapQueue(const C &cmp=C(), int initialSize=128)
Creates a new queue with the given comparer.
Priority queue interface wrapper for heaps.
void pop()
Removes the top element from the heap.
void push(std::initializer_list< value_type > ilist)
Inserts new elements specified by the given initializer list.
const value_type & const_reference
void push(InputIt first, InputIt last)
Inserts new elements specified by the given range.
PriorityQueue & operator=(PriorityQueue other)
Copy and move assignment.
void swap(PriorityQueue &other)
Swaps the contents.
const T & value(handle pos) const
Returns the priority of that handle.
typename SpecImpl::Handle handle
value_type & reference
PriorityQueue(std::initializer_list< value_type > init, const C &cmp=C())
Creates priority queue with contents of the given initializer list.
PriorityQueue(PriorityQueue &&other)
Move constructor.
handle push(const value_type &value)
Inserts a new element with given value into the queue.
PriorityQueue(InputIt first, InputIt last, const C &cmp=C())
Creates priority queue with contents of the given range.
PriorityQueue & operator=(std::initializer_list< value_type > ilist)
Assigns the priority queue contents of the given initializer list.
Impl< T, C > SpecImpl
~PriorityQueue()
Destroys the underlying data structure.
void merge(PriorityQueue &other)
Merges in enqueued values of other queue.
PriorityQueue(const PriorityQueue &other)
Copy constructor.
PriorityQueue(const C &cmp=C(), int initialSize=128)
Creates empty priority queue.
std::size_t size_type
void decrease(handle pos, const T &value)
Decreases value of the element specified by handle to value.
size_type m_size
Number of entries.
void clear()
Removes all the entries from the queue.
friend void swap(PriorityQueue &a, PriorityQueue &b)
Swaps the contents.
SpecImpl * m_impl
Underlying heap data structure.
bool empty() const
Checks whether the queue is empty.
const T & top() const
Returns reference to the top element in the queue.
const C & comparator() const
Returns the comparator used for ordering elements.
size_type size() const
Returns the number of enqueued elements.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
Used to compare elements with assigned priorities.
bool operator()(const T &x, const T &y) const
Compare(const Compare &other)
Compare(const C &compare=C())
Pair for storing an element and a priority.
PairTemplate(const E &element, const P &priority)
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
bool contains(const E &element) const
Returns whether this queue contains that key.
typename PrioritizedQueue< E, P, C, Impl >::Handle Handle
void pop()
Removes the topmost element from the queue.
void clear()
Removes all elements from this queue.
PrioritizedArrayQueueBase(const C &cmp, int initialSize, const Map &map)
void push(const E &element, const P &priority)
Adds a new element to the queue.
const P & priority(const E &element) const
Defines a queue for handling prioritized elements.
const E & topElement() const
Returns the topmost element in the queue.
const P & topPriority() const
Returns the priority of the topmost element in this queue.
Handle push(const E &element, const P &priority)
Pushes a new element with the respective priority to the queue.
typename SuperQueue::handle Handle
The type of handle for accessing the elements of this queue.
PrioritizedQueue(const C &cmp=C(), int initialSize=128)
void decrease(Handle pos, const P &priority)
EdgeElement * edge
The type of edges.
Definition Graph_d.h:75
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
Definition GML.h:111