Ponca  bab7704293a2c36e5bed9dea40def7ba839bfe08
Point Cloud Analysis library
Loading...
Searching...
No Matches
limitedPriorityQueue.h
1
8#pragma once
9
10#include <vector>
11#include <algorithm>
12#include <functional>
13#include "../defines.h"
14
15namespace Ponca
16{
17
76 template <class T, class CompareT = std::less<T>>
78 {
79 public:
80 using value_type = T;
81 using container_type = std::vector<T>;
82 using compare = CompareT;
83 using iterator = typename container_type::iterator;
84 using const_iterator = typename container_type::const_iterator;
86
87 // limited_priority_queue --------------------------------------------------
88 public:
89 PONCA_MULTIARCH inline limited_priority_queue();
90 PONCA_MULTIARCH inline limited_priority_queue(const this_type& other);
91 PONCA_MULTIARCH inline explicit limited_priority_queue(int capacity);
92 template <class InputIt>
93 PONCA_MULTIARCH inline limited_priority_queue(int capacity, InputIt first, InputIt last);
94
95 PONCA_MULTIARCH inline ~limited_priority_queue();
96
97 PONCA_MULTIARCH inline limited_priority_queue& operator=(const this_type& other);
98
99 // Iterator ----------------------------------------------------------------
100 public:
101 PONCA_MULTIARCH inline iterator begin();
102 PONCA_MULTIARCH inline const_iterator begin() const;
103 PONCA_MULTIARCH inline const_iterator cbegin() const;
104
105 PONCA_MULTIARCH inline iterator end();
106 PONCA_MULTIARCH inline const_iterator end() const;
107 PONCA_MULTIARCH inline const_iterator cend() const;
108
109 // Element access ----------------------------------------------------------
110 public:
111 PONCA_MULTIARCH inline const T& top() const;
112 PONCA_MULTIARCH inline const T& bottom() const;
113
114 PONCA_MULTIARCH inline T& top();
115 PONCA_MULTIARCH inline T& bottom();
116
117 // Capacity ----------------------------------------------------------------
118 public:
119 PONCA_MULTIARCH inline bool empty() const;
120 PONCA_MULTIARCH inline bool full() const;
121 PONCA_MULTIARCH inline size_t size() const;
122 PONCA_MULTIARCH inline size_t capacity() const;
123
124 // Modifiers ---------------------------------------------------------------
125 public:
126 PONCA_MULTIARCH inline bool push(const T& value);
127 PONCA_MULTIARCH inline bool push(T&& value);
128
129 PONCA_MULTIARCH inline void pop();
130
131 PONCA_MULTIARCH inline void reserve(int capacity);
132
133 PONCA_MULTIARCH inline void clear();
134
135 // Data --------------------------------------------------------------------
136 public:
137 PONCA_MULTIARCH inline const container_type& container() const;
138
139 protected:
140 container_type m_c;
141 compare m_comp;
142 size_t m_size{0};
143 };
144
149
150 // limited_priority_queue ------------------------------------------------------
151
152 template <class T, class Cmp>
153 limited_priority_queue<T, Cmp>::limited_priority_queue() : m_c(), m_comp(), m_size(0)
154 {
155 }
156
157 template <class T, class Cmp>
158 limited_priority_queue<T, Cmp>::limited_priority_queue(const this_type& other)
159 : m_c(other.m_c), m_comp(other.m_comp), m_size(other.m_size)
160 {
161 }
162
163 template <class T, class Cmp>
164 limited_priority_queue<T, Cmp>::limited_priority_queue(int capacity) : m_c(capacity), m_comp(), m_size(0)
165 {
166 }
167
168 template <class T, class Cmp>
169 template <class InputIt>
170 limited_priority_queue<T, Cmp>::limited_priority_queue(int capacity, InputIt first, InputIt last)
171 : m_c(capacity), m_comp(), m_size(0)
172 {
173 for (InputIt it = first; it < last; ++it)
174 {
175 push(*it);
176 }
177 }
178
179 template <class T, class Cmp>
180 limited_priority_queue<T, Cmp>::~limited_priority_queue()
181 {
182 }
183
184 template <class T, class Cmp>
185 limited_priority_queue<T, Cmp>& limited_priority_queue<T, Cmp>::operator=(const this_type& other)
186 {
187 m_c = other.m_c;
188 m_comp = other.m_comp;
189 m_size = other.m_size;
190 return *this;
191 }
192
193 // Iterator --------------------------------------------------------------------
194
195 template <class T, class Cmp>
196 typename limited_priority_queue<T, Cmp>::iterator limited_priority_queue<T, Cmp>::begin()
197 {
198 return m_c.begin();
199 }
200
201 template <class T, class Cmp>
202 typename limited_priority_queue<T, Cmp>::const_iterator limited_priority_queue<T, Cmp>::begin() const
203 {
204 return m_c.begin();
205 }
206
207 template <class T, class Cmp>
208 typename limited_priority_queue<T, Cmp>::const_iterator limited_priority_queue<T, Cmp>::cbegin() const
209 {
210 return m_c.cbegin();
211 }
212
213 template <class T, class Cmp>
214 typename limited_priority_queue<T, Cmp>::iterator limited_priority_queue<T, Cmp>::end()
215 {
216 return m_c.begin() + m_size;
217 }
218
219 template <class T, class Cmp>
220 typename limited_priority_queue<T, Cmp>::const_iterator limited_priority_queue<T, Cmp>::end() const
221 {
222 return m_c.begin() + m_size;
223 }
224
225 template <class T, class Cmp>
226 typename limited_priority_queue<T, Cmp>::const_iterator limited_priority_queue<T, Cmp>::cend() const
227 {
228 return m_c.cbegin() + m_size;
229 }
230
231 // Element access --------------------------------------------------------------
232
233 template <class T, class Cmp>
234 const T& limited_priority_queue<T, Cmp>::top() const
235 {
236 return m_c[0];
237 }
238
239 template <class T, class Cmp>
240 const T& limited_priority_queue<T, Cmp>::bottom() const
241 {
242 return m_c[m_size - 1];
243 }
244
245 template <class T, class Cmp>
246 T& limited_priority_queue<T, Cmp>::top()
247 {
248 return m_c[0];
249 }
250
251 template <class T, class Cmp>
252 T& limited_priority_queue<T, Cmp>::bottom()
253 {
254 return m_c[m_size - 1];
255 }
256
257 // Capacity --------------------------------------------------------------------
258
259 template <class T, class Cmp>
260 bool limited_priority_queue<T, Cmp>::empty() const
261 {
262 return m_size == 0;
263 }
264
265 template <class T, class Cmp>
266 bool limited_priority_queue<T, Cmp>::full() const
267 {
268 return m_size == capacity();
269 }
270
271 template <class T, class Cmp>
272 size_t limited_priority_queue<T, Cmp>::size() const
273 {
274 return m_size;
275 }
276
277 template <class T, class Cmp>
278 size_t limited_priority_queue<T, Cmp>::capacity() const
279 {
280 return m_c.size();
281 }
282
283 // Modifiers -------------------------------------------------------------------
284
285 template <class T, class Cmp>
286 bool limited_priority_queue<T, Cmp>::push(const T& value)
287 {
288 if (empty())
289 {
290 if (capacity() > 0)
291 {
292 m_c.front() = value;
293 ++m_size;
294 return true;
295 }
296 }
297 else
298 {
299 iterator it = std::upper_bound(begin(), end(), value, m_comp);
300 if (it == end())
301 {
302 if (!full())
303 {
304 *it = value;
305 ++m_size;
306 return true;
307 }
308 }
309 else
310 {
311 if (full())
312 {
313 std::copy_backward(it, end() - 1, end());
314 *it = value;
315 }
316 else
317 {
318 std::copy_backward(it, end(), end() + 1);
319 *it = value;
320 ++m_size;
321 }
322 return true;
323 }
324 }
325 return false;
326 }
327
328 template <class T, class Cmp>
329 bool limited_priority_queue<T, Cmp>::push(T&& value)
330 {
331 if (empty())
332 {
333 if (capacity() > 0)
334 {
335 m_c.front() = std::move(value);
336 ++m_size;
337 return true;
338 }
339 }
340 else
341 {
342 iterator it = std::upper_bound(begin(), end(), std::move(value), m_comp);
343 if (it == end())
344 {
345 if (!full())
346 {
347 *it = std::move(value);
348 ++m_size;
349 return true;
350 }
351 }
352 else
353 {
354 if (full())
355 {
356 std::copy_backward(it, end() - 1, end());
357 *it = std::move(value);
358 }
359 else
360 {
361 std::copy_backward(it, end(), end() + 1);
362 *it = std::move(value);
363 ++m_size;
364 }
365 return true;
366 }
367 }
368 return false;
369 }
370
371 template <class T, class Cmp>
372 void limited_priority_queue<T, Cmp>::pop()
373 {
374 --m_size;
375 }
376
377 template <class T, class Cmp>
378 void limited_priority_queue<T, Cmp>::reserve(int capacity)
379 {
380 if (m_size > capacity)
381 {
382 m_size = capacity;
383 }
384 m_c.resize(capacity);
385 }
386
387 template <class T, class Cmp>
388 void limited_priority_queue<T, Cmp>::clear()
389 {
390 m_size = 0;
391 }
392
393 // Data ------------------------------------------------------------------------
394
395 template <class T, class Cmp>
396 const typename limited_priority_queue<T, Cmp>::container_type& limited_priority_queue<T, Cmp>::container() const
397 {
398 return m_c;
399 }
400
401} // namespace Ponca
The limited_priority_queue class is similar to std::priority_queue but has a limited capacity and han...
This Source Code Form is subject to the terms of the Mozilla Public License, v.