Embedded Template Library  1.0
priority_queue.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2015 jwellbelove, rlindeman
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_PRIORITY_QUEUE_INCLUDED
32 #define ETL_PRIORITY_QUEUE_INCLUDED
33 
34 #include <stddef.h>
35 
36 #include "platform.h"
37 
38 #include "algorithm.h"
39 #include "utility.h"
40 #include "functional.h"
41 #include "container.h"
42 #include "vector.h"
43 #include "type_traits.h"
44 #include "parameter_type.h"
45 #include "error_handler.h"
46 #include "exception.h"
47 
48 #undef ETL_FILE
49 #define ETL_FILE "12"
50 
51 //*****************************************************************************
56 //*****************************************************************************
57 
58 namespace etl
59 {
60  //***************************************************************************
63  //***************************************************************************
65  {
66  public:
67 
68  priority_queue_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
69  : exception(reason_, file_name_, line_number_)
70  {
71  }
72  };
73 
74  //***************************************************************************
77  //***************************************************************************
79  {
80  public:
81 
82  priority_queue_full(string_type file_name_, numeric_type line_number_)
83  : priority_queue_exception(ETL_ERROR_TEXT("priority_queue:full", ETL_FILE"A"), file_name_, line_number_)
84  {
85  }
86  };
87 
88  //***************************************************************************
91  //***************************************************************************
93  {
94  public:
95 
96  priority_queue_iterator(string_type file_name_, numeric_type line_number_)
97  : priority_queue_exception(ETL_ERROR_TEXT("priority_queue:iterator", ETL_FILE"B"), file_name_, line_number_)
98  {
99  }
100  };
101 
102  //***************************************************************************
117  //***************************************************************************
118  template <typename T, typename TContainer, typename TCompare = etl::less<T> >
120  {
121  public:
122 
123  typedef T value_type;
124  typedef TContainer container_type;
125  typedef TCompare compare_type;
126  typedef T& reference;
127  typedef const T& const_reference;
128 #if ETL_CPP11_SUPPORTED
129  typedef T&& rvalue_reference;
130 #endif
131  typedef typename TContainer::size_type size_type;
132  typedef typename etl::iterator_traits<typename TContainer::iterator>::difference_type difference_type;
133 
134  //*************************************************************************
137  //*************************************************************************
139  {
140  return container.front();
141  }
142 
143  //*************************************************************************
146  //*************************************************************************
148  {
149  return container.front();
150  }
151 
152  //*************************************************************************
157  //*************************************************************************
158  void push(const_reference value)
159  {
160  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
161 
162  // Put element at end
163  container.push_back(value);
164  // Make elements in container into heap
165  etl::push_heap(container.begin(), container.end(), compare);
166  }
167 
168 #if ETL_CPP11_SUPPORTED
169  //*************************************************************************
174  //*************************************************************************
175  void push(rvalue_reference value)
176  {
177  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
178 
179  // Put element at end
180  container.push_back(etl::move(value));
181  // Make elements in container into heap
182  etl::push_heap(container.begin(), container.end(), compare);
183  }
184 #endif
185 
186 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && !defined(ETL_PRIORITY_QUEUE_FORCE_CPP03)
187  //*************************************************************************
192  //*************************************************************************
193  template <typename ... Args>
194  void emplace(Args && ... args)
195  {
196  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
197 
198  // Put element at end
199  container.emplace_back(etl::forward<Args>(args)...);
200  // Make elements in container into heap
201  etl::push_heap(container.begin(), container.end(), compare);
202  }
203 #else
204  //*************************************************************************
209  //*************************************************************************
210  template <typename T1>
211  void emplace(const T1& value1)
212  {
213  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
214 
215  // Put element at end
216  container.emplace_back(value1);
217  // Make elements in container into heap
218  etl::push_heap(container.begin(), container.end(), compare);
219  }
220 
221  //*************************************************************************
226  //*************************************************************************
227  template <typename T1, typename T2>
228  void emplace(const T1& value1, const T2& value2)
229  {
230  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
231 
232  // Put element at end
233  container.emplace_back(value1, value2);
234  // Make elements in container into heap
235  etl::push_heap(container.begin(), container.end(), compare);
236  }
237 
238  //*************************************************************************
243  //*************************************************************************
244  template <typename T1, typename T2, typename T3>
245  void emplace(const T1& value1, const T2& value2, const T3& value3)
246  {
247  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
248 
249  // Put element at end
250  container.emplace_back(value1, value2, value3);
251  // Make elements in container into heap
252  etl::push_heap(container.begin(), container.end(), compare);
253  }
254 
255  //*************************************************************************
260  //*************************************************************************
261  template <typename T1, typename T2, typename T3, typename T4>
262  void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
263  {
264  ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
265 
266  // Put element at end
267  container.emplace_back(value1, value2, value3, value4);
268  // Make elements in container into heap
269  etl::push_heap(container.begin(), container.end(), compare);
270  }
271 #endif
272 
273  //*************************************************************************
281  //*************************************************************************
282  template <typename TIterator>
283  void assign(TIterator first, TIterator last)
284  {
285 #if defined(ETL_DEBUG)
286  difference_type d = etl::distance(first, last);
287  ETL_ASSERT(d >= 0, ETL_ERROR(etl::priority_queue_iterator));
288  ETL_ASSERT(static_cast<size_t>(d) <= max_size(), ETL_ERROR(etl::priority_queue_full));
289 #endif
290 
291  clear();
292  container.assign(first, last);
293  etl::make_heap(container.begin(), container.end(), compare);
294  }
295 
296  //*************************************************************************
299  //*************************************************************************
300  void pop()
301  {
302  // Move largest element to end
303  etl::pop_heap(container.begin(), container.end(), compare);
304  // Actually remove largest element at end
305  container.pop_back();
306  }
307 
308  //*************************************************************************
311  //*************************************************************************
312  void pop_into(reference destination)
313  {
314  destination = top();
315  pop();
316  }
317 
318  //*************************************************************************
320  //*************************************************************************
321  size_type size() const
322  {
323  return container.size();
324  }
325 
326  //*************************************************************************
328  //*************************************************************************
330  {
331  return container.max_size();
332  }
333 
334  //*************************************************************************
337  //*************************************************************************
338  bool empty() const
339  {
340  return container.empty();
341  }
342 
343  //*************************************************************************
346  //*************************************************************************
347  bool full() const
348  {
349  return container.size() == container.max_size();
350  }
351 
352  //*************************************************************************
355  //*************************************************************************
357  {
358  return container.max_size() - container.size();
359  }
360 
361  //*************************************************************************
363  //*************************************************************************
364  void clear()
365  {
366  container.clear();
367  }
368 
369  protected:
370 
371  //*************************************************************************
373  //*************************************************************************
374  void clone(const ipriority_queue& other)
375  {
376  assign(other.container.cbegin(), other.container.cend());
377  }
378 
379 #if ETL_CPP11_SUPPORTED
380  //*************************************************************************
382  //*************************************************************************
383  void move(ipriority_queue&& other)
384  {
385  while (!other.empty())
386  {
387  push(etl::move(other.top()));
388  other.pop();
389  }
390  }
391 #endif
392 
393  //*************************************************************************
395  //*************************************************************************
397  {
398  }
399 
400  private:
401 
402  // Disable copy construction.
404 
406  TContainer container;
407 
408  TCompare compare;
409  };
410 
411  //***************************************************************************
417  //***************************************************************************
418  template <typename T, const size_t SIZE, typename TContainer = etl::vector<T, SIZE>, typename TCompare = etl::less<typename TContainer::value_type> >
419  class priority_queue : public etl::ipriority_queue<T, TContainer, TCompare>
420  {
421  public:
422 
423  typedef typename TContainer::size_type size_type;
424 
425  static const size_type MAX_SIZE = size_type(SIZE);
426 
427  //*************************************************************************
429  //*************************************************************************
431  : etl::ipriority_queue<T, TContainer, TCompare>()
432  {
433  }
434 
435  //*************************************************************************
437  //*************************************************************************
439  : etl::ipriority_queue<T, TContainer, TCompare>()
440  {
442  }
443 
444 #if ETL_CPP11_SUPPORTED
445  //*************************************************************************
447  //*************************************************************************
449  : etl::ipriority_queue<T, TContainer, TCompare>()
450  {
452  }
453 #endif
454 
455  //*************************************************************************
460  //*************************************************************************
461  template <typename TIterator>
462  priority_queue(TIterator first, TIterator last)
463  : etl::ipriority_queue<T, TContainer, TCompare>()
464  {
466  }
467 
468  //*************************************************************************
470  //*************************************************************************
472  {
474  }
475 
476  //*************************************************************************
478  //*************************************************************************
480  {
481  if (&rhs != this)
482  {
484  }
485 
486  return *this;
487  }
488 
489 #if ETL_CPP11_SUPPORTED
490  //*************************************************************************
492  //*************************************************************************
494  {
495  if (&rhs != this)
496  {
498  }
499 
500  return *this;
501  }
502 #endif
503  };
504 }
505 
506 #undef ETL_FILE
507 
508 #endif
Definition: priority_queue.h:420
~priority_queue()
Destructor.
Definition: priority_queue.h:471
priority_queue(const priority_queue &rhs)
Copy constructor.
Definition: priority_queue.h:438
priority_queue & operator=(const priority_queue &rhs)
Assignment operator.
Definition: priority_queue.h:479
priority_queue()
Default constructor.
Definition: priority_queue.h:430
priority_queue(TIterator first, TIterator last)
Definition: priority_queue.h:462
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
bool full() const
Definition: priority_queue.h:347
void pop_into(reference destination)
Definition: priority_queue.h:312
bool empty() const
Definition: priority_queue.h:338
size_type max_size() const
Returns the maximum number of items that can be queued.
Definition: priority_queue.h:329
void assign(TIterator first, TIterator last)
Definition: priority_queue.h:283
void emplace(const T1 &value1, const T2 &value2, const T3 &value3)
Definition: priority_queue.h:245
void push(const_reference value)
Definition: priority_queue.h:158
T & reference
A reference to the type used in the queue.
Definition: priority_queue.h:126
void emplace(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition: priority_queue.h:262
TCompare compare_type
The comparison type.
Definition: priority_queue.h:125
reference top()
Definition: priority_queue.h:138
const T & const_reference
A const reference to the type used in the queue.
Definition: priority_queue.h:127
T value_type
The type stored in the queue.
Definition: priority_queue.h:123
size_type size() const
Returns the current number of items in the priority queue.
Definition: priority_queue.h:321
void clone(const ipriority_queue &other)
Make this a clone of the supplied priority queue.
Definition: priority_queue.h:374
const_reference top() const
Definition: priority_queue.h:147
void clear()
Clears the queue to the empty state.
Definition: priority_queue.h:364
void pop()
Definition: priority_queue.h:300
void emplace(const T1 &value1)
Definition: priority_queue.h:211
size_type available() const
Definition: priority_queue.h:356
TContainer::size_type size_type
The type used for determining the size of the queue.
Definition: priority_queue.h:131
TContainer container_type
The container type used for priority queue.
Definition: priority_queue.h:124
ipriority_queue()
The constructor that is called from derived classes.
Definition: priority_queue.h:396
void emplace(const T1 &value1, const T2 &value2)
Definition: priority_queue.h:228
This is the base for all priority queues that contain a particular type.
Definition: priority_queue.h:120
Definition: priority_queue.h:65
Definition: priority_queue.h:79
Definition: priority_queue.h:93
Definition: absolute.h:37
Definition: compare.h:52