Embedded Template Library 1.0
Loading...
Searching...
No Matches
priority_queue.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2015 John Wellbelove, rlindeman
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_PRIORITY_QUEUE_INCLUDED
32#define ETL_PRIORITY_QUEUE_INCLUDED
33
34#include "platform.h"
35#include "algorithm.h"
36#include "error_handler.h"
37#include "exception.h"
38#include "functional.h"
39#include "iterator.h"
40#include "parameter_type.h"
41#include "type_traits.h"
42#include "utility.h"
43#include "vector.h"
44
45#include <stddef.h>
46
47//*****************************************************************************
52//*****************************************************************************
53
54namespace etl
55{
56 //***************************************************************************
59 //***************************************************************************
60 class priority_queue_exception : public exception
61 {
62 public:
63
64 priority_queue_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
65 : exception(reason_, file_name_, line_number_)
66 {
67 }
68 };
69
70 //***************************************************************************
73 //***************************************************************************
74 class priority_queue_full : public etl::priority_queue_exception
75 {
76 public:
77
78 priority_queue_full(string_type file_name_, numeric_type line_number_)
79 : priority_queue_exception(ETL_ERROR_TEXT("priority_queue:full", ETL_PRIORITY_QUEUE_FILE_ID"A"), file_name_, line_number_)
80 {
81 }
82 };
83
84 //***************************************************************************
87 //***************************************************************************
88 class priority_queue_iterator : public etl::priority_queue_exception
89 {
90 public:
91
92 priority_queue_iterator(string_type file_name_, numeric_type line_number_)
93 : priority_queue_exception(ETL_ERROR_TEXT("priority_queue:iterator", ETL_PRIORITY_QUEUE_FILE_ID"B"), file_name_, line_number_)
94 {
95 }
96 };
97
98 //***************************************************************************
101 //***************************************************************************
102 class priority_queue_empty : public etl::priority_queue_exception
103 {
104 public:
105
106 priority_queue_empty(string_type file_name_, numeric_type line_number_)
107 : priority_queue_exception(ETL_ERROR_TEXT("priority_queue:empty", ETL_PRIORITY_QUEUE_FILE_ID"C"), file_name_, line_number_)
108 {
109 }
110 };
111
112 //***************************************************************************
128 //***************************************************************************
129 template <typename T, typename TContainer, typename TCompare = etl::less<T> >
131 {
132 public:
133
134 typedef T value_type;
135 typedef TContainer container_type;
136 typedef TCompare compare_type;
137 typedef T& reference;
138 typedef const T& const_reference;
139#if ETL_USING_CPP11
140 typedef T&& rvalue_reference;
141#endif
142 typedef typename TContainer::size_type size_type;
143 typedef typename etl::iterator_traits< typename TContainer::iterator>::difference_type difference_type;
144
145 //*************************************************************************
150 //*************************************************************************
152 {
153 ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(priority_queue_empty));
154 return container.front();
155 }
156
157 //*************************************************************************
162 //*************************************************************************
164 {
165 ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(priority_queue_empty));
166 return container.front();
167 }
168
169 //*************************************************************************
174 //*************************************************************************
176 {
178
179 // Put element at end
180 container.push_back(value);
181 // Make elements in container into heap
182 etl::push_heap(container.begin(), container.end(), compare);
183 }
184
185#if ETL_USING_CPP11
186 //*************************************************************************
191 //*************************************************************************
192 void push(rvalue_reference value)
193 {
195
196 // Put element at end
197 container.push_back(etl::move(value));
198 // Make elements in container into heap
199 etl::push_heap(container.begin(), container.end(), compare);
200 }
201#endif
202
203#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_PRIORITY_QUEUE_FORCE_CPP03_IMPLEMENTATION)
204 //*************************************************************************
209 //*************************************************************************
210 template <typename... Args>
211 void emplace(Args&&... args)
212 {
214
215 // Put element at end
216 container.emplace_back(etl::forward<Args>(args)...);
217 // Make elements in container into heap
218 etl::push_heap(container.begin(), container.end(), compare);
219 }
220#else
221 //*************************************************************************
226 //*************************************************************************
227 void emplace()
228 {
230
231 // Put element at end
232 container.emplace_back();
233 // Make elements in container into heap
234 etl::push_heap(container.begin(), container.end(), compare);
235 }
236
237 //*************************************************************************
242 //*************************************************************************
243 template <typename T1>
244 void emplace(const T1& value1)
245 {
247
248 // Put element at end
249 container.emplace_back(value1);
250 // Make elements in container into heap
251 etl::push_heap(container.begin(), container.end(), compare);
252 }
253
254 //*************************************************************************
259 //*************************************************************************
260 template <typename T1, typename T2>
261 void emplace(const T1& value1, const T2& value2)
262 {
264
265 // Put element at end
266 container.emplace_back(value1, value2);
267 // Make elements in container into heap
268 etl::push_heap(container.begin(), container.end(), compare);
269 }
270
271 //*************************************************************************
276 //*************************************************************************
277 template <typename T1, typename T2, typename T3>
278 void emplace(const T1& value1, const T2& value2, const T3& value3)
279 {
281
282 // Put element at end
283 container.emplace_back(value1, value2, value3);
284 // Make elements in container into heap
285 etl::push_heap(container.begin(), container.end(), compare);
286 }
287
288 //*************************************************************************
293 //*************************************************************************
294 template <typename T1, typename T2, typename T3, typename T4>
295 void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
296 {
298
299 // Put element at end
300 container.emplace_back(value1, value2, value3, value4);
301 // Make elements in container into heap
302 etl::push_heap(container.begin(), container.end(), compare);
303 }
304#endif
305
306 //*************************************************************************
314 //*************************************************************************
315 template <typename TIterator>
316 void assign(TIterator first, TIterator last)
317 {
318#if ETL_IS_DEBUG_BUILD
319 difference_type d = etl::distance(first, last);
320 ETL_ASSERT(d >= 0, ETL_ERROR(etl::priority_queue_iterator));
321 ETL_ASSERT(static_cast<size_t>(d) <= max_size(), ETL_ERROR(etl::priority_queue_full));
322#endif
323
324 clear();
325 container.assign(first, last);
326 etl::make_heap(container.begin(), container.end(), compare);
327 }
328
329 //*************************************************************************
334 //*************************************************************************
335 void pop()
336 {
337 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(!empty(), ETL_ERROR(priority_queue_empty));
338 // Move largest element to end
339 etl::pop_heap(container.begin(), container.end(), compare);
340 // Actually remove largest element at end
341 container.pop_back();
342 }
343
344 //*************************************************************************
347 //*************************************************************************
348 void pop_into(reference destination)
349 {
350 destination = ETL_MOVE(top());
351 pop();
352 }
353
354 //*************************************************************************
356 //*************************************************************************
358 {
359 return container.size();
360 }
361
362 //*************************************************************************
364 //*************************************************************************
366 {
367 return container.max_size();
368 }
369
370 //*************************************************************************
373 //*************************************************************************
374 bool empty() const
375 {
376 return container.empty();
377 }
378
379 //*************************************************************************
383 //*************************************************************************
384 bool full() const
385 {
386 return container.size() == container.max_size();
387 }
388
389 //*************************************************************************
392 //*************************************************************************
394 {
395 return container.max_size() - container.size();
396 }
397
398 //*************************************************************************
400 //*************************************************************************
401 void clear()
402 {
403 container.clear();
404 }
405
406 //*************************************************************************
408 //*************************************************************************
410 {
411 if (&rhs != this)
412 {
413 clone(rhs);
414 }
415
416 return *this;
417 }
418
419#if ETL_USING_CPP11
420 //*************************************************************************
422 //*************************************************************************
424 {
425 if (&rhs != this)
426 {
427 clear();
428 move(etl::move(rhs));
429 }
430
431 return *this;
432 }
433#endif
434
435 protected:
436
437 //*************************************************************************
439 //*************************************************************************
440 void clone(const ipriority_queue& other)
441 {
442 assign(other.container.cbegin(), other.container.cend());
443 }
444
445#if ETL_USING_CPP11
446 //*************************************************************************
448 //*************************************************************************
449 void move(ipriority_queue&& other)
450 {
451 while (!other.empty())
452 {
453 push(etl::move(other.top()));
454 other.pop();
455 }
456 }
457#endif
458
459 //*************************************************************************
461 //*************************************************************************
463
464 private:
465
466 // Disable copy construction.
468
470 TContainer container;
471
472 TCompare compare;
473 };
474
475 //***************************************************************************
481 //***************************************************************************
482 template <typename T, const size_t SIZE, typename TContainer = etl::vector<T, SIZE>,
483 typename TCompare = etl::less<typename TContainer::value_type> >
484 class priority_queue : public etl::ipriority_queue<T, TContainer, TCompare>
485 {
486 public:
487
488 typedef typename TContainer::size_type size_type;
489 typedef TContainer container_type;
490
491 static ETL_CONSTANT size_type MAX_SIZE = size_type(SIZE);
492
493 //*************************************************************************
495 //*************************************************************************
497 : etl::ipriority_queue<T, TContainer, TCompare>()
498 {
499 }
500
501 //*************************************************************************
503 //*************************************************************************
505 : etl::ipriority_queue<T, TContainer, TCompare>()
506 {
508 }
509
510#if ETL_USING_CPP11
511 //*************************************************************************
513 //*************************************************************************
515 : etl::ipriority_queue<T, TContainer, TCompare>()
516 {
518 }
519#endif
520
521 //*************************************************************************
526 //*************************************************************************
527 template <typename TIterator>
528 priority_queue(TIterator first, TIterator last)
529 : etl::ipriority_queue<T, TContainer, TCompare>()
530 {
532 }
533
534 //*************************************************************************
536 //*************************************************************************
541
542 //*************************************************************************
544 //*************************************************************************
546 {
547 if (&rhs != this)
548 {
550 }
551
552 return *this;
553 }
554
555#if ETL_USING_CPP11
556 //*************************************************************************
558 //*************************************************************************
560 {
561 if (&rhs != this)
562 {
565 }
566
567 return *this;
568 }
569#endif
570 };
571
572 template <typename T, const size_t SIZE, typename TContainer, typename TCompare>
573 ETL_CONSTANT typename priority_queue<T, SIZE, TContainer, TCompare>::size_type priority_queue<T, SIZE, TContainer, TCompare>::MAX_SIZE;
574} // namespace etl
575
576#endif
Definition priority_queue.h:485
~priority_queue()
Destructor.
Definition priority_queue.h:537
priority_queue(const priority_queue &rhs)
Copy constructor.
Definition priority_queue.h:504
priority_queue & operator=(const priority_queue &rhs)
Assignment operator.
Definition priority_queue.h:545
priority_queue()
Default constructor.
Definition priority_queue.h:496
priority_queue(TIterator first, TIterator last)
Definition priority_queue.h:528
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
bool full() const
Definition priority_queue.h:384
void pop_into(reference destination)
Definition priority_queue.h:348
bool empty() const
Definition priority_queue.h:374
size_type max_size() const
Returns the maximum number of items that can be queued.
Definition priority_queue.h:365
void assign(TIterator first, TIterator last)
Definition priority_queue.h:316
void emplace(const T1 &value1, const T2 &value2, const T3 &value3)
Definition priority_queue.h:278
void push(const_reference value)
Definition priority_queue.h:175
T & reference
A reference to the type used in the queue.
Definition priority_queue.h:137
void emplace(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition priority_queue.h:295
ipriority_queue & operator=(const ipriority_queue &rhs)
Assignment operator.
Definition priority_queue.h:409
TCompare compare_type
The comparison type.
Definition priority_queue.h:136
reference top()
Definition priority_queue.h:151
const T & const_reference
A const reference to the type used in the queue.
Definition priority_queue.h:138
T value_type
The type stored in the queue.
Definition priority_queue.h:134
size_type size() const
Returns the current number of items in the priority queue.
Definition priority_queue.h:357
void clone(const ipriority_queue &other)
Make this a clone of the supplied priority queue.
Definition priority_queue.h:440
const_reference top() const
Definition priority_queue.h:163
void clear()
Clears the queue to the empty state.
Definition priority_queue.h:401
void pop()
Definition priority_queue.h:335
void emplace(const T1 &value1)
Definition priority_queue.h:244
size_type available() const
Definition priority_queue.h:393
void emplace()
Definition priority_queue.h:227
TContainer::size_type size_type
The type used for determining the size of the queue.
Definition priority_queue.h:142
TContainer container_type
The container type used for priority queue.
Definition priority_queue.h:135
ipriority_queue()
The constructor that is called from derived classes.
Definition priority_queue.h:462
void emplace(const T1 &value1, const T2 &value2)
Definition priority_queue.h:261
This is the base for all priority queues that contain a particular type.
Definition priority_queue.h:131
Definition priority_queue.h:103
Definition priority_queue.h:61
Definition priority_queue.h:75
Definition priority_queue.h:89
bitset_ext
Definition absolute.h:40
Definition compare.h:51