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