G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsBoundedPriorityQueue.h
Go to the documentation of this file.
1
18#pragma once
19
20#include <map>
21#include <algorithm>
22#include <limits>
23
24namespace gismo
25{
26
80 template <class ValueType, class T=real_t>
82public:
83 // Constructor: gsBoundedPriorityQueue(size_t maxSize);
84 // Usage: gsBoundedPriorityQueue<int> bpq(15);
85 // --------------------------------------------------
86 // Constructs a new, empty gsBoundedPriorityQueue with
87 // maximum size equal to the constructor argument.
89 explicit gsBoundedPriorityQueue(std::size_t maxSize);
90
91 // void enqueue(const ValueType& value, T priority);
92 // Usage: bpq.enqueue("Hi!", 2.71828);
93 // --------------------------------------------------
94 // Enqueues a new element into the gsBoundedPriorityQueue with
95 // the specified priority. If this overflows the maximum
96 // size of the queue, the element with the highest
97 // priority will be deleted from the queue. Note that
98 // this might be the element that was just added.
99 void enqueue(const ValueType& value, T priority);
100
101 // ValueType dequeueMin();
102 // Usage: int val = bpq.dequeueMin();
103 // --------------------------------------------------
104 // Returns the element from the gsBoundedPriorityQueue with the
105 // smallest priority value, then removes that element
106 // from the queue.
107 ValueType dequeueMin();
108
109 // size_t size() const;
110 // bool empty() const;
111 // Usage: while (!bpq.empty()) { ... }
112 // --------------------------------------------------
113 // Returns the number of elements in the queue and whether
114 // the queue is empty, respectively.
115 std::size_t size() const;
116 bool empty() const;
117
118 // size_t maxSize() const;
119 // Usage: size_t queueSize = bpq.maxSize();
120 // --------------------------------------------------
121 // Returns the maximum number of elements that can be
122 // stored in the queue.
123 std::size_t maxSize() const;
124
125 // T best() const;
126 // T worst() const;
127 // Usage: T highestPriority = bpq.worst();
128 // --------------------------------------------------
129 // best() returns the smallest priority of an element
130 // stored in the container (i.e. the priority of the
131 // element that will be dequeued first using dequeueMin).
132 // worst() returns the largest priority of an element
133 // stored in the container. If an element is enqueued
134 // with a priority above this value, it will automatically
135 // be deleted from the queue. Both functions return
136 // numeric_limits<T>::infinity() if the queue is
137 // empty.
138 T best() const;
139 T worst() const;
140
142 void print(std::ostream &os) const;
143
145 friend std::ostream &operator<<(std::ostream &os, const gsBoundedPriorityQueue &obj)
146 {
147 obj.print(os);
148 return os;
149 }
150
151private:
152 // This class is layered on top of a multimap mapping from priorities
153 // to elements with those priorities.
154 std::multimap<T, ValueType> elems;
155 std::size_t maximumSize;
156
157}; // class gsBoundedPriorityQueue
158
159// Constructor
160template <class ValueType, class T>
161gsBoundedPriorityQueue<ValueType, T>::gsBoundedPriorityQueue(std::size_t maxSize)
162{
163maximumSize = maxSize;
164}
165
166// enqueue adds the element to the map, then deletes the last element of the
167// map if there size exceeds the maximum size.
168template <class ValueType, class T>
169void gsBoundedPriorityQueue<ValueType, T>::enqueue(const ValueType& value, T priority)
170{
171// Add the element to the collection.
172elems.insert(std::make_pair(priority, value));
173
174// If there are too many elements in the queue, drop off the last one.
175if (size() > maxSize()) {
176typename std::multimap<T, ValueType>::iterator last = elems.end();
177--last; // Now points to highest-priority element
178elems.erase(last);
179}
180}
181
182// dequeueMin copies the lowest element of the map (the one pointed at by
183// begin()) and then removes it.
184template <class ValueType, class T>
185ValueType gsBoundedPriorityQueue<ValueType, T>::dequeueMin()
186{
187// Copy the best value.
188ValueType result = elems.begin()->second;
189
190// Remove it from the map.
191elems.erase(elems.begin());
192
193return result;
194}
195
196// size() and empty() call directly down to the underlying map.
197template <class ValueType, class T>
198std::size_t gsBoundedPriorityQueue<ValueType, T>::size() const
199{
200return elems.size();
201}
202
203template <class ValueType, class T>
204bool gsBoundedPriorityQueue<ValueType, T>::empty() const
205{
206return elems.empty();
207}
208
209// maxSize just returns the appropriate data member.
210template <class ValueType, class T>
211std::size_t gsBoundedPriorityQueue<ValueType, T>::maxSize() const
212{
213return maximumSize;
214}
215
216// The best() and worst() functions check if the queue is empty,
217// and if so return infinity.
218template <class ValueType, class T>
219T gsBoundedPriorityQueue<ValueType, T>::best() const
220{
221return empty()? std::numeric_limits<T>::infinity() : elems.begin()->first;
222}
223
224template <class ValueType, class T>
225T gsBoundedPriorityQueue<ValueType, T>::worst() const
226{
227return empty()? std::numeric_limits<T>::infinity() : elems.rbegin()->first;
228}
229
230template <class ValueType, class T>
232{
233 os << "BPQ(" << size() << ") [" << best() << ".." << worst() << "].\n";
234}
235
236} // namespace gismo
An implementation of the bounded priority queue abstraction.
Definition gsBoundedPriorityQueue.h:81
void print(std::ostream &os) const
Prints the object as a string.
Definition gsBoundedPriorityQueue.h:231
friend std::ostream & operator<<(std::ostream &os, const gsBoundedPriorityQueue &obj)
Print (as string) operator.
Definition gsBoundedPriorityQueue.h:145
The G+Smo namespace, containing all definitions for the library.