BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_priorityqueue.h
Go to the documentation of this file.
1/// @file bslstl_priorityqueue.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_priorityqueue.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_PRIORITYQUEUE
9#define INCLUDED_BSLSTL_PRIORITYQUEUE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_priorityqueue bslstl_priorityqueue
15/// @brief Provide container adapter class template `priority_queue`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_priorityqueue
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_priorityqueue-purpose"> Purpose</a>
25/// * <a href="#bslstl_priorityqueue-classes"> Classes </a>
26/// * <a href="#bslstl_priorityqueue-description"> Description </a>
27/// * <a href="#bslstl_priorityqueue-memory-allocation"> Memory Allocation </a>
28/// * <a href="#bslstl_priorityqueue-value-and-container-value_type"> VALUE and CONTAINER::value_type </a>
29/// * <a href="#bslstl_priorityqueue-operations"> Operations </a>
30/// * <a href="#bslstl_priorityqueue-usage"> Usage </a>
31/// * <a href="#bslstl_priorityqueue-example-1-task-scheduler"> Example 1: Task Scheduler </a>
32///
33/// # Purpose {#bslstl_priorityqueue-purpose}
34/// Provide container adapter class template @ref priority_queue .
35///
36/// # Classes {#bslstl_priorityqueue-classes}
37///
38/// - bsl::priority_queue: template of highest-priority-first data structure
39///
40/// **Canonical header:** bsl_queue.h
41///
42/// @see bslstl_queue, bslstl_stack
43///
44/// # Description {#bslstl_priorityqueue-description}
45/// This component defines a class template, `bsl::priority_queue`,
46/// holding a container (of a template parameter type, `CONTAINER`, containing
47/// elements of another template parameter type, `VALUE`), and adapting it to
48/// provide highest-priority-first priority queue data structure. The component
49/// takes a third template parameter type, `COMPARATOR`, for customized
50/// priorities comparison between two elements.
51///
52/// An instantiation of @ref priority_queue is an allocator-aware, value-semantic
53/// type whose salient attributes are its size (number of held elements) and the
54/// sorted sequence of values (of held elements). If @ref priority_queue is
55/// instantiated with a value type, that is not itself value-semantic, then it
56/// will not retain all of its value-semantic qualities. A @ref priority_queue
57/// cannot be tested for equality, but its value type must be able to be tested
58/// for comparing less by its comparator type.
59///
60/// The @ref priority_queue implemented here adheres to the C++11 standard when
61/// compiled with a C++11 compiler, and makes the best approximation when
62/// compiled with a C++03 compiler. In particular, for C++03 we emulate move
63/// semantics, but limit forwarding (in `emplace`) to `const` lvalues, and make
64/// no effort to emulate `noexcept` or initializer-lists.
65///
66/// ## Memory Allocation {#bslstl_priorityqueue-memory-allocation}
67///
68///
69/// The type supplied as an `ALLOCATOR` template parameter in some of
70/// @ref priority_queue constructors determines how the held container (of the
71/// (template parameter) type `CONTAINER`) will allocate memory. A
72/// @ref priority_queue supports allocators meeting the requirements of the C++11
73/// standard [17.6.3.5] as long as the held container does. In addition it
74/// supports scoped-allocators derived from the `bslma::Allocator` memory
75/// allocation protocol. Clients intending to use `bslma`-style allocators
76/// should use `bsl::allocator` as the `ALLOCATOR` template parameter,
77/// providing a C++11 standard-compatible adapter for a `bslma::Allocator`
78/// object.
79///
80/// ### VALUE and CONTAINER::value_type {#bslstl_priorityqueue-value-and-container-value_type}
81///
82///
83/// When the `CONTAINER` template parameter is omitted the `VALUE` template
84/// parameter specifies the `value_type` of `bsl::vector`, the default container
85/// type. The `VALUE` template has no other role.
86///
87/// For C++17 and later, the behavior is undefined unless:
88/// @code
89/// true == bsl::is_same<VALUE, typename CONTAINER::value_type>::value
90/// @endcode
91/// Prior to C++17, `CONTAINER::value_type` determines the contained value type
92/// and `VALUE` is simply ignored. The resulting code may work with instances
93/// of `VALUE` (e.g., `VALUE` is convertible to `CONTAINER::value_type`) or not
94/// (compiler errors).
95///
96/// ## Operations {#bslstl_priorityqueue-operations}
97///
98///
99/// The C++11 standard [23.6.4] declares any container type supporting
100/// operations `front`, `push_back`, and @ref pop_back can be used to instantiate
101/// the (template parameter) type `CONTAINER`. Below is a list of public
102/// methods of @ref priority_queue class that are effectively implemented as
103/// calling the corresponding operations in the held container (referenced as
104/// `c`).
105/// @code
106/// +--------------------------------------+--------------------------+
107/// | Public methods in 'priority_queue' | Operation in 'CONTAINER' |
108/// +======================================+==========================+
109/// | void push(const value_type& value); | c.push_back(value); |
110/// | void pop(); | c.pop_back(); |
111/// | void emplace(Args&&... args) | c.emplace_back(...) |
112/// +--------------------------------------+--------------------------+
113/// | bool empty() const; | c.empty(); |
114/// | size_type size() const; | c.size(); |
115/// | const_reference top() const; | c.front(); |
116/// +--------------------------------------+--------------------------+
117/// @endcode
118///
119/// ## Usage {#bslstl_priorityqueue-usage}
120///
121///
122/// In this section we show intended use of this component.
123///
124/// ### Example 1: Task Scheduler {#bslstl_priorityqueue-example-1-task-scheduler}
125///
126///
127/// In this example, we will use the `bsl::priority_queue` class to implement a
128/// task scheduler that schedules a group of tasks based on their designated
129/// priorities.
130///
131/// Suppose we want to write a background process that runs tasks needed by
132/// foreground applications. Each task has a task id, a priority, and a
133/// function pointer that can be invoked by the background process. This
134/// background process has two threads: one thread (receiving thread) receives
135/// requests from other applications, passing required tasks to a task
136/// scheduler; the other thread (processing thread) runs the task scheduler,
137/// executing the tasks one-by-one from higher to lower priorities. To
138/// implement this functionality, we can use `bsl::priority_queue` in the task
139/// scheduler to buffer received, but as yet unprocessed, tasks. The task
140/// scheduler adds newly received tasks into the priority queue in the receiving
141/// thread, and extracts tasks from the priority queue for execution according
142/// to their priorities in the processing thread.
143///
144/// First, we define a `TaskFunction` type:
145/// @code
146/// typedef void (*TaskFunction)(int, int, int);
147/// @endcode
148/// Then, we define a `Task` class, which contains a task id, a `TaskFunction`
149/// object and an associated task priority:
150/// @code
151/// class Task
152/// // This class represents a task that has an integer task id, a task
153/// // function, and an integer priority. The smaller the numerical value
154/// // of a priority, the higher the priority.
155/// {
156/// private:
157/// // DATA
158/// int d_taskId; // task id
159///
160/// TaskFunction d_taskFunction_p; // task function
161///
162/// int d_priority; // priority of the task
163///
164/// public:
165/// // CREATORS
166/// explicit Task(int taskId, TaskFunction taskFunction, int priority);
167/// // Create a 'Task' object having the specified 'taskId', the
168/// // specified 'd_taskFunction_p', and the specified 'priority'.
169///
170/// // ACCESSORS
171/// int getId() const;
172/// // Return the contained task id.
173///
174/// int getPriority() const;
175/// // Return the priority of the task.
176///
177/// TaskFunction getFunction() const;
178/// // Return the contained task function object.
179/// };
180///
181/// // CREATORS
182/// Task::Task(int taskId, TaskFunction taskFunction, int priority)
183/// : d_taskId(taskId)
184/// , d_taskFunction_p(taskFunction)
185/// , d_priority(priority)
186/// {
187/// }
188///
189/// // ACCESSORS
190/// inline
191/// int Task::getId() const
192/// {
193/// return d_taskId;
194/// }
195///
196/// inline
197/// int Task::getPriority() const
198/// {
199/// return d_priority;
200/// }
201///
202/// inline
203/// TaskFunction Task::getFunction() const
204/// {
205/// return d_taskFunction_p;
206/// }
207/// @endcode
208/// Next, we define a functor to compare the priorities of two `Task` objects:
209/// @code
210/// struct TaskComparator {
211/// // This 'struct' defines an ordering on 'Task' objects, allowing them
212/// // to be included in sorted data structures such as
213/// // 'bsl::priority_queue'.
214///
215/// bool operator()(const Task& lhs, const Task& rhs) const
216/// // Return 'true' if the priority of the specified 'lhs' is
217/// // numerically less than that of the specified 'rhs', and 'false'
218/// // otherwise. Note that the smaller the value returned by the
219/// // 'Task::getPriority' method, the higher the priority.
220/// {
221/// return lhs.getPriority() > rhs.getPriority();
222/// }
223/// };
224/// @endcode
225/// Then, we define a `TaskScheduler` class that provides methods to hold and
226/// schedule unprocessed tasks:
227/// @code
228/// class TaskScheduler {
229/// // This class holds and schedules tasks to execute.
230/// @endcode
231/// Here, we define a private data member that is an instantiation of
232/// `bsl::priority_queue`, which uses `Task` for its `VALUE` (template
233/// parameter) type, `bsl::vector<Task>` for its `CONTAINER` (template
234/// parameter) type, and `TaskComparator` for its `COMPARATOR` (template
235/// parameter) type:
236/// @code
237/// // DATA
238/// bsl::priority_queue<Task,
239/// bsl::vector<Task>,
240/// TaskComparator>
241/// d_taskPriorityQueue; // priority queue holding unprocessed tasks
242///
243/// // ...
244///
245/// public:
246/// // CREATORS
247/// explicit TaskScheduler(bslma::Allocator *basicAllocator = 0);
248/// // Create a 'TaskScheduler' object. Optionally specify a
249/// // 'basicAllocator' used to supply memory. If 'basicAllocator' is
250/// // 0, the currently installed default allocator is used.
251///
252/// // MANIPULATORS
253/// void addTask(int taskId, TaskFunction taskFunction, int priority);
254/// // Enqueue the specified 'task' having the specified 'priority'
255/// // onto this scheduler.
256///
257/// void processTasks(int verbose);
258/// // Dequeue the task having the highest priority in this scheduler,
259/// // and call its task function by passing in the specified 'verbose'
260/// // flag.
261/// };
262/// @endcode
263/// Next, we implement the `TaskScheduler` constructor:
264/// @code
265/// TaskScheduler::TaskScheduler(bslma::Allocator *basicAllocator)
266/// : d_taskPriorityQueue(basicAllocator)
267/// {
268/// }
269/// @endcode
270/// Notice that we pass to the contained `d_taskPriorityQueue` object the
271/// `bslma::Allocator` supplied to the `TaskScheduler` at construction.
272///
273/// Then, we implement the `addTask` method, which constructs a `Task` object
274/// and adds it into the priority queue:
275/// @code
276/// void TaskScheduler::addTask(int taskId,
277/// TaskFunction taskFunction,
278/// int priority)
279/// {
280/// // ... (some synchronization)
281///
282/// d_taskPriorityQueue.push(Task(taskId, taskFunction, priority));
283///
284/// // ...
285/// }
286/// @endcode
287/// Next, we implement the `processTasks` method, which extracts tasks from the
288/// priority queue in order of descending priorities, and executes them:
289/// @code
290/// void TaskScheduler::processTasks(int verbose)
291/// {
292/// // ... (some synchronization)
293///
294/// while (!d_taskPriorityQueue.empty()) {
295/// const Task& task = d_taskPriorityQueue.top();
296/// TaskFunction taskFunction = task.getFunction();
297/// if (taskFunction) {
298/// taskFunction(task.getId(), task.getPriority(), verbose);
299/// }
300/// d_taskPriorityQueue.pop();
301/// }
302///
303/// // ...
304/// }
305/// @endcode
306/// Note that the `top` method always returns the `Task` object having the
307/// highest priority in the priority queue.
308///
309/// Then, we define two task functions:
310/// @code
311/// void taskFunction1(int taskId, int priority, int verbose)
312/// {
313/// if (verbose) {
314/// printf("Executing task %d (priority = %d) in 'taskFunction1'.\n",
315/// taskId,
316/// priority);
317/// }
318/// }
319///
320/// void taskFunction2(int taskId, int priority, int verbose)
321/// {
322/// if (verbose) {
323/// printf("Executing task %d (priority = %d) in 'taskFunction2'.\n",
324/// taskId,
325/// priority);
326/// }
327/// }
328/// @endcode
329/// Next, we create a global `TaskScheduler` object:
330/// @code
331/// TaskScheduler taskScheduler;
332/// @endcode
333/// Now, we call the `addTask` method of `taskScheduler` in the receiving
334/// thread:
335/// @code
336/// // (in receiving thread)
337/// // ...
338///
339/// taskScheduler.addTask(1, taskFunction1, 50);
340///
341/// // ...
342///
343/// taskScheduler.addTask(2, taskFunction1, 99);
344///
345/// // ...
346///
347/// taskScheduler.addTask(3, taskFunction2, 4);
348///
349/// // ...
350/// @endcode
351/// Finally, we call the `processTasks` method of `taskScheduler` in the
352/// processing thread:
353/// @code
354/// // (in processing thread)
355/// // ...
356///
357/// taskScheduler.processTasks(veryVerbose);
358///
359/// // ...
360/// @endcode
361/// @}
362/** @} */
363/** @} */
364
365/** @addtogroup bsl
366 * @{
367 */
368/** @addtogroup bslstl
369 * @{
370 */
371/** @addtogroup bslstl_priorityqueue
372 * @{
373 */
374
375#include <bslscm_version.h>
376
377#include <bslstl_vector.h>
378#include <bslstl_iteratorutil.h>
379
380#include <bslalg_swaputil.h>
381
382#include <bslma_isstdallocator.h>
383#include <bslma_bslallocator.h>
385
386#include <bslmf_assert.h>
387#include <bslmf_enableif.h>
388#include <bslmf_isconvertible.h>
389#include <bslmf_issame.h>
390#include <bslmf_movableref.h>
392#include <bslmf_usesallocator.h>
393#include <bslmf_util.h> // 'forward(V)'
394
396#include <bsls_keyword.h>
397#include <bsls_libraryfeatures.h>
398#include <bsls_util.h> // 'forward<T>(V)'
399
400#include <algorithm>
401#include <functional>
402
403#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
404// Include version that can be compiled with C++03
405// Generated on Thu Oct 21 10:11:37 2021
406// Command line: sim_cpp11_features.pl bslstl_priorityqueue.h
407# define COMPILING_BSLSTL_PRIORITYQUEUE_H
409# undef COMPILING_BSLSTL_PRIORITYQUEUE_H
410#else
411
412namespace bsl {
413
414 // ====================
415 // class priority_queue
416 // ====================
417
418/// This class is a value-semantic class template, adapting a container of
419/// the (template parameter) type `CONTAINER`, that holds elements of the
420/// (template parameter) type `VALUE`, to provide a highest-priority-first
421/// priority queue data structure, where the priorities of elements are
422/// compared by a comparator of the template parameter type, `COMPARATOR`.
423/// The container object held by a @ref priority_queue class object is
424/// referenced as `c` in the following documentation.
425///
426/// See @ref bslstl_priorityqueue
427template <class VALUE,
428 class CONTAINER = vector<VALUE>,
429 class COMPARATOR = std::less<typename CONTAINER::value_type> >
431
432#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
433 // STATIC CHECK: Type mismatch is UB per C++17
435#endif
436
437 private:
438 // PRIVATE TYPES
439
440 /// This `typedef` is a convenient alias for the utility associated with
441 /// movable references.
442 typedef BloombergLP::bslmf::MovableRefUtil MoveUtil;
443
444 public:
445 // PUBLIC TYPES
446 typedef CONTAINER container_type;
447 typedef COMPARATOR value_compare;
448 typedef typename CONTAINER::value_type value_type;
449 typedef typename CONTAINER::reference reference;
450 typedef typename CONTAINER::const_reference const_reference;
451 typedef typename CONTAINER::size_type size_type;
452
453 protected:
454 // PROTECTED DATA
455 CONTAINER c; // container for elements in the 'priority_queue'. This
456 // data member exactly matches its definition in the
457 // C++11 standard [23.6.4].
458
459 COMPARATOR comp; // comparator that defines the priority order of elements
460 // in the @ref priority_queue . This data member exactly
461 // matches its definition in the C++11 standard [23.6.4].
462
463 public:
464 // TRAITS
467 BloombergLP::bslma::UsesBslmaAllocator,
468 BloombergLP::bslma::UsesBslmaAllocator<container_type>::value);
469
470 // CREATORS
471
472 /// Create an empty priority queue, adapting a default-constructed
473 /// container of the (template parameter) type `CONTAINER`. Use a
474 /// default-constructed comparator of the (template parameter) type
475 /// `COMPARATOR` to order elements in the priority queue.
477
478 /// Create an empty priority queue, adapting a default-constructed
479 /// container of the (template parameter) type `CONTAINER`, and having
480 /// the specified `comparator` of the (template parameter) type
481 /// `COMPARATOR` to order elements in the priority queue.
482 explicit priority_queue(const COMPARATOR& comparator);
483
484 /// Create a priority queue, adapting the specified `container` of the
485 /// (template parameter) type `CONTAINER`, and having the specified
486 /// `comparator` of the (template parameter) type `COMPARATOR` to order
487 /// elements in the priority queue.
488 priority_queue(const COMPARATOR& comparator, const CONTAINER& container);
489
490 /// Create a priority queue, adapting the specified `container` of the
491 /// (template parameter) type `CONTAINER`, and having the specified
492 /// `comparator` of the (template parameter) type `COMPARATOR` to order
493 /// elements in the priority queue.
494 explicit priority_queue(
495 const COMPARATOR& comparator,
496 BloombergLP::bslmf::MovableRef<CONTAINER> container);
497
498
499 /// Create a priority queue, adapting a default-constructed container of
500 /// the (template parameter) type `CONTAINER`, and inserting into the
501 /// container a sequence of `value_type` elements that starts at the
502 /// specified `first` and ends immediately before the specified `last`.
503 /// Use a default-constructed comparator of the (template parameter)
504 /// type `COMPARATOR` to order elements in the priority queue.
505 template <class INPUT_ITERATOR>
506 priority_queue(INPUT_ITERATOR first, INPUT_ITERATOR last);
507
508 /// Create a priority queue, adapting the specified `container`, having
509 /// the specified `comparator` to order the priorities of elements,
510 /// including those originally existed in `container`, and those
511 /// inserted into the `container` from a sequence of `value_type`
512 /// elements starting at the specified `first`, and ending immediately
513 /// before the specified `last`.
514 template <class INPUT_ITERATOR>
515 priority_queue(INPUT_ITERATOR first,
516 INPUT_ITERATOR last,
517 const COMPARATOR& comparator,
518 const CONTAINER& container);
519
520 /// Create a priority queue, adapting the specified `container`, having
521 /// the specified `comparator` to order elements in the priority queue,
522 /// including those originally existed in `container`, and those
523 /// inserted into the `container` from a sequence of `value_type`
524 /// elements starting at the specified `first`, and ending immediately
525 /// before the specified `last`.
526 template <class INPUT_ITERATOR>
528 INPUT_ITERATOR first,
529 INPUT_ITERATOR last,
530 const COMPARATOR& comparator,
531 BloombergLP::bslmf::MovableRef<CONTAINER> container);
532
533 /// Create a priority queue having the same value as the specified
534 /// `original` object. Use a copy of the comparator from `original` to
535 /// order elements in the priority queue.
536 priority_queue(const priority_queue& original);
537
538 /// Create a priority queue having the same value as the specified
539 /// `original` object. Use a copy of the comparator from `original` to
540 /// order elements in the priority queue.
541 priority_queue(BloombergLP::bslmf::MovableRef<priority_queue> original);
542
543 /// Create an empty priority queue, adapting a default-constructed
544 /// container of the (template parameter) type `CONTAINER` that uses the
545 /// specified `basicAllocator` to supply memory. Use a
546 /// default-constructed object of the (template parameter) type
547 /// `COMPARATOR` to order elements in the priority queue. Note that
548 /// this constructor is only defined if the underlying container uses
549 /// allocator. Otherwise this constructor is disabled.
550 template <class ALLOCATOR>
551 explicit
552 priority_queue(const ALLOCATOR& basicAllocator,
553 typename enable_if<
555 ALLOCATOR>::type * = 0);
556
557 /// Create an empty priority queue, adapting a default-constructed
558 /// container of the (template parameter) type `CONTAINER` that uses the
559 /// specified `basicAllocator` to supply memory, and the specified
560 /// `comparator` to order elements in the priority queue. Note that
561 /// this constructor is only defined if the underlying container uses
562 /// allocator. Otherwise this constructor is disabled.
563 template <class ALLOCATOR>
564 priority_queue(const COMPARATOR& comparator,
565 const ALLOCATOR& basicAllocator,
566 typename enable_if<
568 ALLOCATOR>::type * = 0);
569
570 /// Create a priority queue, adapting the specified `container` that
571 /// uses the specified `basicAllocator` to supply memory, and the
572 /// specified `comparator` to order elements in the priority queue.
573 /// Note that this constructor is only defined if the underlying
574 /// container uses allocator. Otherwise this constructor is disabled.
575 template <class ALLOCATOR>
576 priority_queue(const COMPARATOR& comparator,
577 const CONTAINER& container,
578 const ALLOCATOR& basicAllocator,
579 typename enable_if<
581 ALLOCATOR>::type * = 0);
582
583 /// Create a priority queue, adapting the specified `container` that
584 /// uses the specified `basicAllocator` to supply memory, and the
585 /// specified `comparator` to order elements in the priority queue.
586 /// Note that this constructor is only defined if the underlying
587 /// container uses allocator. Otherwise this constructor is disabled.
588 template <class ALLOCATOR>
589 priority_queue(const COMPARATOR& comparator,
590 BloombergLP::bslmf::MovableRef<CONTAINER> container,
591 const ALLOCATOR& basicAllocator,
592 typename enable_if<
594 ALLOCATOR>::type * = 0);
595
596 /// Create a priority queue having the same value as the specified
597 /// `original` object and using the specified `basicAllocator` to supply
598 /// memory. Use a copy of the comparator from `original` to order
599 /// elements in the priority queue. Note that this constructor is only
600 /// defined if the underlying container uses allocator. Otherwise this
601 /// constructor is disabled.
602 template <class ALLOCATOR>
603 priority_queue(const priority_queue& original,
604 const ALLOCATOR& basicAllocator,
605 typename enable_if<
607 ALLOCATOR>::type * = 0);
608
609 /// Create a priority queue having the same value as the specified
610 /// `original` object and using the specified `basicAllocator` to supply
611 /// memory. Use a copy of the comparator from `original` to order
612 /// elements in the priority queue. Note that this constructor is only
613 /// defined if the underlying container uses allocator. Otherwise this
614 /// constructor is disabled.
615 template <class ALLOCATOR>
617 BloombergLP::bslmf::MovableRef<priority_queue> original,
618 const ALLOCATOR& basicAllocator,
619 typename enable_if<
621 ALLOCATOR>::type * = 0);
622
623 // MANIPULATORS
624
625 /// Assign to this object the value and comparator of the specified
626 /// `rhs` object and return a reference providing modifiable access to
627 /// this object.
629
630 /// Assign to this object the value and comparator of the specified
631 /// `rhs` object and return a reference providing modifiable access to
632 /// this object. `rhs` is left in a valid but unspecified state.
634 BloombergLP::bslmf::MovableRef<priority_queue> rhs)
636
637 /// Insert the specified `value` into this priority queue. In effect,
638 /// performs `c.push_back(value);`.
639 void push(const value_type& value);
640
641 /// Insert the specified `value` into this priority queue. In effect,
642 /// performs `c.push_back(value);`.
643 void push(BloombergLP::bslmf::MovableRef<value_type> value);
644
645#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
646 /// Insert into this priority queue a newly created `value_type` object,
647 /// constructed by forwarding the specified (variable number of) `args`
648 /// to the corresponding constructor of `value_type`. In effect,
649 /// performs `c.emplace_back(FORWARD(Args,args)...);`.
650 template <class... Args>
651 void emplace(Args&&... args);
652#endif
653
654 void pop();
655 /// Remove the top element from this @ref priority_queue object that has
656 /// the highest priority. In effect, performs `c.pop_back();`. The
657 /// behavior is undefined if there is currently no elements in this
658 /// object.
659
661 bsl::is_nothrow_swappable<CONTAINER>::value &&
662 bsl::is_nothrow_swappable<COMPARATOR>::value);
663 // Efficiently exchange the value of this object with the value of the
664 // specified 'other' object. In effect, performs 'using bsl::swap;
665 // swap(c, other.c);'.
666
667 // ACCESSORS
668
669 /// Return `true` if this @ref priority_queue object contains no elements,
670 /// and `false` otherwise. In effect, performs `return c.empty();`.
671 bool empty() const;
672
673 /// Return the number of elements in this @ref priority_queue object. In
674 /// effect, performs `return c.size()`.
675 size_type size() const;
676
677 /// Return a reference providing non-modifiable access to the element
678 /// having the highest priority in this @ref priority_queue object. In
679 /// effect, performs `return c.front()`. The behavior is undefined if
680 /// the priority queue is empty.
681 const_reference top() const;
682};
683
684#ifdef BSLS_COMPILERFEATURES_SUPPORT_CTAD
685// CLASS TEMPLATE DEDUCTION GUIDES
686
687/// Deduce the template parameter `VALUE` and `CONTAINER` from the
688/// parameters supplied to the constructor of @ref priority_queue .
689template <
690 class COMPARATOR,
691 class CONTAINER,
692 class = bsl::enable_if_t<!bsl::IsStdAllocator_v<CONTAINER>>
693 >
694priority_queue(COMPARATOR, CONTAINER)
696
697/// Deduce the template parameters `VALUE`, `CONTAINER` and `COMPARATOR`
698/// from the parameters supplied to the constructor of @ref priority_queue .
699/// This deduction guide does not participate unless the supplied allocator
700/// is convertible to the underlying container's `allocator_type`.
701template <
702 class COMPARATOR,
703 class CONTAINER,
704 class ALLOCATOR,
705 class = bsl::enable_if_t<bsl::uses_allocator_v<CONTAINER, ALLOCATOR>>
706 >
707priority_queue(COMPARATOR, CONTAINER, ALLOCATOR)
709
710/// Deduce the template parameter `VALUE` from the `value_type` of the
711/// iterators supplied to the constructor of @ref priority_queue .
712template <
713 class INPUT_ITERATOR,
714 class VALUE =
715 typename BloombergLP::bslstl::IteratorUtil::IterVal_t<INPUT_ITERATOR>
716 >
717priority_queue(INPUT_ITERATOR, INPUT_ITERATOR)
719
720/// Deduce the template parameter `VALUE` from the `value_type` of the
721/// iterators supplied to the constructor of @ref priority_queue . Deduce the
722/// template parameters `CONTAINER` and `COMPARATOR` from the other
723/// parameters passed to the constructor.
724template <
725 class INPUT_ITERATOR,
726 class COMPARATOR,
727 class CONTAINER,
728 class VALUE =
729 typename BloombergLP::bslstl::IteratorUtil::IterVal_t<INPUT_ITERATOR>
730 >
731priority_queue(INPUT_ITERATOR, INPUT_ITERATOR, COMPARATOR, CONTAINER)
733#endif
734
735// FREE FUNCTIONS
736
737/// Exchange the container and comparator of the specified `a` object with
738/// the container and comparator of the specified `b` object.
739template <class VALUE, class CONTAINER, class COMPARATOR>
743
744// ============================================================================
745// TEMPLATE AND INLINE FUNCTION DEFINITIONS
746// ============================================================================
747
748 // --------------------
749 // class priority_queue
750 // --------------------
751
752// CREATORS
753template <class VALUE, class CONTAINER, class COMPARATOR>
754inline
758
759template <class VALUE, class CONTAINER, class COMPARATOR>
760inline
762 const COMPARATOR& comparator)
763: comp(comparator)
764{
765}
766
767template <class VALUE, class CONTAINER, class COMPARATOR>
768inline
770 const COMPARATOR& comparator,
771 const CONTAINER& container)
772: c(container)
773, comp(comparator)
774{
775 std::make_heap(c.begin(), c.end(), comp);
776}
777
778template <class VALUE, class CONTAINER, class COMPARATOR>
779inline
781 const COMPARATOR& comparator,
782 BloombergLP::bslmf::MovableRef<CONTAINER> container)
783: c(MoveUtil::move(container))
784, comp(comparator)
785{
786 std::make_heap(c.begin(), c.end(), comp);
787}
788
789template <class VALUE, class CONTAINER, class COMPARATOR>
790template <class INPUT_ITERATOR>
791inline
793 INPUT_ITERATOR first,
794 INPUT_ITERATOR last)
795{
796 c.insert(c.end(), first, last);
797 std::make_heap(c.begin(), c.end(), comp);
798}
799
800template <class VALUE, class CONTAINER, class COMPARATOR>
801template <class INPUT_ITERATOR>
802inline
804 INPUT_ITERATOR first,
805 INPUT_ITERATOR last,
806 const COMPARATOR& comparator,
807 const CONTAINER& container)
808: c(container)
809, comp(comparator)
810{
811 c.insert(c.end(), first, last);
812 std::make_heap(c.begin(), c.end(), comp);
813}
814
815template <class VALUE, class CONTAINER, class COMPARATOR>
816template <class INPUT_ITERATOR>
817inline
819 INPUT_ITERATOR first,
820 INPUT_ITERATOR last,
821 const COMPARATOR& comparator,
822 BloombergLP::bslmf::MovableRef<CONTAINER> container)
823: c(MoveUtil::move(container))
824, comp(comparator)
825{
826 c.insert(c.end(), first, last);
827 std::make_heap(c.begin(), c.end(), comp);
828}
829
830template <class VALUE, class CONTAINER, class COMPARATOR>
831inline
833 const priority_queue& original)
834: c(original.c)
835, comp(original.comp)
836{
837}
838
839template <class VALUE, class CONTAINER, class COMPARATOR>
840inline
842 BloombergLP::bslmf::MovableRef<priority_queue> original)
843: c(MoveUtil::move(MoveUtil::access(original).c))
844, comp(MoveUtil::access(original).comp)
845{
846}
847
848template <class VALUE, class CONTAINER, class COMPARATOR>
849template <class ALLOCATOR>
850inline
852 const ALLOCATOR& basicAllocator,
853 typename enable_if<
855 ALLOCATOR>::type *)
856: c(basicAllocator)
857, comp(COMPARATOR())
858{
859}
860
861template <class VALUE, class CONTAINER, class COMPARATOR>
862template <class ALLOCATOR>
863inline
865 const COMPARATOR& comparator,
866 const ALLOCATOR& basicAllocator,
867 typename enable_if<
869 ALLOCATOR>::type *)
870: c(basicAllocator)
871, comp(comparator)
872{
873}
874
875template <class VALUE, class CONTAINER, class COMPARATOR>
876template <class ALLOCATOR>
877inline
879 const COMPARATOR& comparator,
880 const CONTAINER& container,
881 const ALLOCATOR& basicAllocator,
882 typename enable_if<
884 ALLOCATOR>::type *)
885: c(container, basicAllocator)
886, comp(comparator)
887{
888 std::make_heap(c.begin(), c.end(), comp);
889}
890
891template <class VALUE, class CONTAINER, class COMPARATOR>
892template <class ALLOCATOR>
893inline
895 const COMPARATOR& comparator,
896 BloombergLP::bslmf::MovableRef<CONTAINER> container,
897 const ALLOCATOR& basicAllocator,
898 typename enable_if<
900 ALLOCATOR>::type *)
901: c(MoveUtil::move(container), basicAllocator)
902, comp(comparator)
903{
904 std::make_heap(c.begin(), c.end(), comp);
905}
906
907template <class VALUE, class CONTAINER, class COMPARATOR>
908template <class ALLOCATOR>
909inline
911 const priority_queue& original,
912 const ALLOCATOR& basicAllocator,
913 typename enable_if<
915 ALLOCATOR>::type *)
916: c(original.c, basicAllocator)
917, comp(original.comp)
918{
919}
920
921template <class VALUE, class CONTAINER, class COMPARATOR>
922template <class ALLOCATOR>
923inline
925 BloombergLP::bslmf::MovableRef<priority_queue> original,
926 const ALLOCATOR& basicAllocator,
927 typename enable_if<
929 ALLOCATOR>::type *)
930: c(MoveUtil::move(MoveUtil::access(original).c), basicAllocator)
931, comp(MoveUtil::access(original).comp)
932{
933}
934
935// MANIPULATORS
936template <class VALUE, class CONTAINER, class COMPARATOR>
937inline
940 const priority_queue& rhs)
941{
942 c = rhs.c;
943 comp = rhs.comp;
944 return *this;
945}
946
947template <class VALUE, class CONTAINER, class COMPARATOR>
948inline
951 BloombergLP::bslmf::MovableRef<priority_queue> rhs)
953{
954 c = MoveUtil::move(MoveUtil::access(rhs).c);
955 comp = MoveUtil::access(rhs).comp;
956 return *this;
957}
958
959template <class VALUE, class CONTAINER, class COMPARATOR>
960inline
962 const value_type& value)
963{
964 c.push_back(value);
965 std::push_heap(c.begin(), c.end(), comp);
966}
967
968template <class VALUE, class CONTAINER, class COMPARATOR>
969inline
971 BloombergLP::bslmf::MovableRef<value_type> value)
972{
973 c.push_back(MoveUtil::move(value));
974 std::push_heap(c.begin(), c.end(), comp);
975}
976
977#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
978template <class VALUE, class CONTAINER, class COMPARATOR>
979template <class... Args>
980inline
982{
983 c.emplace_back(BSLS_COMPILERFEATURES_FORWARD(Args,args)...);
984 std::push_heap(c.begin(), c.end(), comp);
985}
986#endif
987
988template <class VALUE, class CONTAINER, class COMPARATOR>
989inline
991{
992 std::pop_heap(c.begin(), c.end(), comp);
993 c.pop_back();
994}
995
996template <class VALUE, class CONTAINER, class COMPARATOR>
997inline
1000 bsl::is_nothrow_swappable<CONTAINER>::value &&
1001 bsl::is_nothrow_swappable<COMPARATOR>::value)
1002{
1003 BloombergLP::bslalg::SwapUtil::swap(&c, &other.c);
1004 BloombergLP::bslalg::SwapUtil::swap(&comp, &other.comp);
1005}
1006
1007// ACCESSORS
1008template <class VALUE, class CONTAINER, class COMPARATOR>
1009inline
1011{
1012 return c.empty();
1013}
1014
1015template <class VALUE, class CONTAINER, class COMPARATOR>
1016inline
1022
1023template <class VALUE, class CONTAINER, class COMPARATOR>
1024inline
1027{
1028 return c.front();
1029}
1030
1031// FREE FUNCTIONS
1032template <class VALUE, class CONTAINER, class COMPARATOR>
1039
1040} // close namespace bsl
1041
1042#endif // End C++11 code
1043
1044#endif
1045
1046// ----------------------------------------------------------------------------
1047// Copyright 2016 Bloomberg Finance L.P.
1048//
1049// Licensed under the Apache License, Version 2.0 (the "License");
1050// you may not use this file except in compliance with the License.
1051// You may obtain a copy of the License at
1052//
1053// http://www.apache.org/licenses/LICENSE-2.0
1054//
1055// Unless required by applicable law or agreed to in writing, software
1056// distributed under the License is distributed on an "AS IS" BASIS,
1057// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1058// See the License for the specific language governing permissions and
1059// limitations under the License.
1060// ----------------------------- END-OF-FILE ----------------------------------
1061
1062/** @} */
1063/** @} */
1064/** @} */
Definition bslstl_priorityqueue.h:430
size_type size() const
Definition bslstl_priorityqueue.h:1018
void emplace(Args &&... args)
Definition bslstl_priorityqueue.h:981
void pop()
Definition bslstl_priorityqueue.h:990
void swap(priority_queue &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(bsl bool empty() const
Definition bslstl_priorityqueue.h:671
COMPARATOR value_compare
Definition bslstl_priorityqueue.h:447
CONTAINER::reference reference
Definition bslstl_priorityqueue.h:449
COMPARATOR comp
Definition bslstl_priorityqueue.h:459
CONTAINER::size_type size_type
Definition bslstl_priorityqueue.h:451
priority_queue()
Definition bslstl_priorityqueue.h:755
CONTAINER::const_reference const_reference
Definition bslstl_priorityqueue.h:450
BSLMF_NESTED_TRAIT_DECLARATION_IF(priority_queue, BloombergLP::bslma::UsesBslmaAllocator, BloombergLP::bslma::UsesBslmaAllocator< container_type >::value)
CONTAINER c
Definition bslstl_priorityqueue.h:455
CONTAINER::value_type value_type
Definition bslstl_priorityqueue.h:448
void push(const value_type &value)
Definition bslstl_priorityqueue.h:961
const_reference top() const
Definition bslstl_priorityqueue.h:1026
CONTAINER container_type
Definition bslstl_priorityqueue.h:446
priority_queue & operator=(const priority_queue &rhs)
Definition bslstl_priorityqueue.h:939
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2018
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(...)
Definition bsls_keyword.h:634
Definition bdlb_printmethods.h:283
Definition bslmf_enableif.h:525
Definition bslmf_issame.h:146
Definition bslmf_usesallocator.h:165