G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsBoundedPriorityQueue.h
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <map>
21 #include <algorithm>
22 #include <limits>
23 
24 namespace gismo
25 {
26 
80  template <class ValueType, class T=real_t>
82 public:
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 
151 private:
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
160 template <class ValueType, class T>
161 gsBoundedPriorityQueue<ValueType, T>::gsBoundedPriorityQueue(std::size_t maxSize)
162 {
163 maximumSize = 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.
168 template <class ValueType, class T>
169 void gsBoundedPriorityQueue<ValueType, T>::enqueue(const ValueType& value, T priority)
170 {
171 // Add the element to the collection.
172 elems.insert(std::make_pair(priority, value));
173 
174 // If there are too many elements in the queue, drop off the last one.
175 if (size() > maxSize()) {
176 typename std::multimap<T, ValueType>::iterator last = elems.end();
177 --last; // Now points to highest-priority element
178 elems.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.
184 template <class ValueType, class T>
185 ValueType gsBoundedPriorityQueue<ValueType, T>::dequeueMin()
186 {
187 // Copy the best value.
188 ValueType result = elems.begin()->second;
189 
190 // Remove it from the map.
191 elems.erase(elems.begin());
192 
193 return result;
194 }
195 
196 // size() and empty() call directly down to the underlying map.
197 template <class ValueType, class T>
198 std::size_t gsBoundedPriorityQueue<ValueType, T>::size() const
199 {
200 return elems.size();
201 }
202 
203 template <class ValueType, class T>
204 bool gsBoundedPriorityQueue<ValueType, T>::empty() const
205 {
206 return elems.empty();
207 }
208 
209 // maxSize just returns the appropriate data member.
210 template <class ValueType, class T>
211 std::size_t gsBoundedPriorityQueue<ValueType, T>::maxSize() const
212 {
213 return maximumSize;
214 }
215 
216 // The best() and worst() functions check if the queue is empty,
217 // and if so return infinity.
218 template <class ValueType, class T>
219 T gsBoundedPriorityQueue<ValueType, T>::best() const
220 {
221 return empty()? std::numeric_limits<T>::infinity() : elems.begin()->first;
222 }
223 
224 template <class ValueType, class T>
225 T gsBoundedPriorityQueue<ValueType, T>::worst() const
226 {
227 return empty()? std::numeric_limits<T>::infinity() : elems.rbegin()->first;
228 }
229 
230 template <class ValueType, class T>
232 {
233  os << "BPQ(" << size() << ") [" << best() << ".." << worst() << "].\n";
234 }
235 
236 } // namespace gismo
friend std::ostream & operator<<(std::ostream &os, const gsBoundedPriorityQueue &obj)
Print (as string) operator.
Definition: gsBoundedPriorityQueue.h:145
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