Quick Links: |
Provide container adapter class template priority_queue
.
More...
Classes | |
class | bsl::priority_queue< VALUE, CONTAINER, COMPARATOR > |
Typedefs | |
typedef CONTAINER | bsl::priority_queue::container_type |
typedef COMPARATOR | bsl::priority_queue::value_compare |
typedef CONTAINER::value_type | bsl::priority_queue::value_type |
typedef CONTAINER::reference | bsl::priority_queue::reference |
typedef CONTAINER::const_reference | bsl::priority_queue::const_reference |
typedef CONTAINER::size_type | bsl::priority_queue::size_type |
Functions | |
bsl::priority_queue::BSLMF_NESTED_TRAIT_DECLARATION_IF (priority_queue, BloombergLP::bslma::UsesBslmaAllocator, BloombergLP::bslma::UsesBslmaAllocator< container_type >::value) | |
bsl::priority_queue::priority_queue () | |
bsl::priority_queue::priority_queue (const COMPARATOR &comparator) | |
bsl::priority_queue::priority_queue (const COMPARATOR &comparator, const CONTAINER &container) | |
bsl::priority_queue::priority_queue (const COMPARATOR &comparator, BloombergLP::bslmf::MovableRef< CONTAINER > container) | |
template<class INPUT_ITERATOR > | |
bsl::priority_queue::priority_queue (INPUT_ITERATOR first, INPUT_ITERATOR last) | |
template<class INPUT_ITERATOR > | |
bsl::priority_queue::priority_queue (INPUT_ITERATOR first, INPUT_ITERATOR last, const COMPARATOR &comparator, const CONTAINER &container) | |
template<class INPUT_ITERATOR > | |
bsl::priority_queue::priority_queue (INPUT_ITERATOR first, INPUT_ITERATOR last, const COMPARATOR &comparator, BloombergLP::bslmf::MovableRef< CONTAINER > container) | |
bsl::priority_queue::priority_queue (const priority_queue &original) | |
bsl::priority_queue::priority_queue (BloombergLP::bslmf::MovableRef< priority_queue > original) | |
template<class ALLOCATOR > | |
bsl::priority_queue::priority_queue (const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::priority_queue::priority_queue (const COMPARATOR &comparator, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::priority_queue::priority_queue (const COMPARATOR &comparator, const CONTAINER &container, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::priority_queue::priority_queue (const COMPARATOR &comparator, BloombergLP::bslmf::MovableRef< CONTAINER > container, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::priority_queue::priority_queue (const priority_queue &original, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::priority_queue::priority_queue (BloombergLP::bslmf::MovableRef< priority_queue > original, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
priority_queue & | bsl::priority_queue::operator= (const priority_queue &rhs) |
priority_queue & | bsl::priority_queue::operator= (BloombergLP::bslmf::MovableRef< priority_queue > rhs) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(false) |
void | bsl::priority_queue::push (const value_type &value) |
void | bsl::priority_queue::push (BloombergLP::bslmf::MovableRef< value_type > value) |
template<class... Args> | |
void | bsl::priority_queue::emplace (Args &&...args) |
void | bsl::priority_queue::pop () |
size_type | bsl::priority_queue::size () const |
const_reference | bsl::priority_queue::top () const |
template<class VALUE , class CONTAINER , class COMPARATOR > | |
void | bsl::swap (priority_queue< VALUE, CONTAINER, COMPARATOR > &a, priority_queue< VALUE, CONTAINER, COMPARATOR > &b) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(false) |
Variables | |
CONTAINER | bsl::priority_queue::c |
COMPARATOR | bsl::priority_queue::comp |
void swap(priority_queue &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(bsl bool | bsl::priority_queue::empty () const |
priority_queue
. bsl::priority_queue | template of highest-priority-first data structure |
bsl::priority_queue
, holding a container (of a template parameter type, CONTAINER
, containing elements of another template parameter type, VALUE
), and adapting it to provide highest-priority-first priority queue data structure. The component takes a third template parameter type, COMPARATOR
, for customized priorities comparison between two elements. priority_queue
is an allocator-aware, value-semantic type whose salient attributes are its size (number of held elements) and the sorted sequence of values (of held elements). If priority_queue
is instantiated with a value type, that is not itself value-semantic, then it will not retain all of its value-semantic qualities. A priority_queue
cannot be tested for equality, but its value type must be able to be tested for comparing less by its comparator type. priority_queue
implemented here adheres to the C++11 standard when compiled with a C++11 compiler, and makes the best approximation when compiled with a C++03 compiler. In particular, for C++03 we emulate move semantics, but limit forwarding (in emplace
) to const
lvalues, and make no effort to emulate noexcept
or initializer-lists. ALLOCATOR
template parameter in some of priority_queue
constructors determines how the held container (of the (template parameter) type CONTAINER
) will allocate memory. A priority_queue
supports allocators meeting the requirements of the C++11 standard [17.6.3.5] as long as the held container does. In addition it supports scoped-allocators derived from the bslma::Allocator
memory allocation protocol. Clients intending to use bslma
-style allocators should use bsl::allocator
as the ALLOCATOR
template parameter, providing a C++11 standard-compatible adapter for a bslma::Allocator
object. CONTAINER
template parameter is omitted the VALUE
template parameter specifies the value_type
of bsl::vector
, the default container type. The VALUE
template has no other role. CONTAINER::value_type
determines the contained value type and VALUE
is simply ignored. The resulting code may work with instances of VALUE
(e.g., VALUE
is convertible to CONTAINER::value_type
) or not (compiler errors). front
, push_back
, and pop_back
can be used to instantiate the (template parameter) type CONTAINER
. Below is a list of public methods of priority_queue
class that are effectively implemented as calling the corresponding operations in the held container (referenced as c
). +--------------------------------------+--------------------------+ | Public methods in 'priority_queue' | Operation in 'CONTAINER' | +======================================+==========================+ | void push(const value_type& value); | c.push_back(value); | | void pop(); | c.pop_back(); | | void emplace(Args&&... args) | c.emplace_back(...) | +--------------------------------------+--------------------------+ | bool empty() const; | c.empty(); | | size_type size() const; | c.size(); | | const_reference top() const; | c.front(); | +--------------------------------------+--------------------------+
bsl::priority_queue
class to implement a task scheduler that schedules a group of tasks based on their designated priorities. bsl::priority_queue
in the task scheduler to buffer received, but as yet unprocessed, tasks. The task scheduler adds newly received tasks into the priority queue in the receiving thread, and extracts tasks from the priority queue for execution according to their priorities in the processing thread. TaskFunction
type: typedef void (*TaskFunction)(int, int, int);
Task
class, which contains a task id, a TaskFunction
object and an associated task priority: class Task // This class represents a task that has an integer task id, a task // function, and an integer priority. The smaller the numerical value // of a priority, the higher the priority. { private: // DATA int d_taskId; // task id TaskFunction d_taskFunction_p; // task function int d_priority; // priority of the task public: // CREATORS explicit Task(int taskId, TaskFunction taskFunction, int priority); // Create a 'Task' object having the specified 'taskId', the // specified 'd_taskFunction_p', and the specified 'priority'. // ACCESSORS int getId() const; // Return the contained task id. int getPriority() const; // Return the priority of the task. TaskFunction getFunction() const; // Return the contained task function object. }; // CREATORS Task::Task(int taskId, TaskFunction taskFunction, int priority) : d_taskId(taskId) , d_taskFunction_p(taskFunction) , d_priority(priority) { } // ACCESSORS inline int Task::getId() const { return d_taskId; } inline int Task::getPriority() const { return d_priority; } inline TaskFunction Task::getFunction() const { return d_taskFunction_p; }
Task
objects: struct TaskComparator { // This 'struct' defines an ordering on 'Task' objects, allowing them // to be included in sorted data structures such as // 'bsl::priority_queue'. bool operator()(const Task& lhs, const Task& rhs) const // Return 'true' if the priority of the specified 'lhs' is // numerically less than that of the specified 'rhs', and 'false' // otherwise. Note that the smaller the value returned by the // 'Task::getPriority' method, the higher the priority. { return lhs.getPriority() > rhs.getPriority(); } };
TaskScheduler
class that provides methods to hold and schedule unprocessed tasks: class TaskScheduler { // This class holds and schedules tasks to execute.
bsl::priority_queue
, which uses Task
for its VALUE
(template parameter) type, bsl::vector<Task>
for its CONTAINER
(template parameter) type, and TaskComparator
for its COMPARATOR
(template parameter) type: // DATA bsl::priority_queue<Task, bsl::vector<Task>, TaskComparator> d_taskPriorityQueue; // priority queue holding unprocessed tasks // ... public: // CREATORS explicit TaskScheduler(bslma::Allocator *basicAllocator = 0); // Create a 'TaskScheduler' object. Optionally specify a // 'basicAllocator' used to supply memory. If 'basicAllocator' is // 0, the currently installed default allocator is used. // MANIPULATORS void addTask(int taskId, TaskFunction taskFunction, int priority); // Enqueue the specified 'task' having the specified 'priority' // onto this scheduler. void processTasks(int verbose); // Dequeue the task having the highest priority in this scheduler, // and call its task function by passing in the specified 'verbose' // flag. };
TaskScheduler
constructor: TaskScheduler::TaskScheduler(bslma::Allocator *basicAllocator) : d_taskPriorityQueue(basicAllocator) { }
d_taskPriorityQueue
object the bslma::Allocator
supplied to the TaskScheduler
at construction. addTask
method, which constructs a Task
object and adds it into the priority queue: void TaskScheduler::addTask(int taskId, TaskFunction taskFunction, int priority) { // ... (some synchronization) d_taskPriorityQueue.push(Task(taskId, taskFunction, priority)); // ... }
processTasks
method, which extracts tasks from the priority queue in order of descending priorities, and executes them: void TaskScheduler::processTasks(int verbose) { // ... (some synchronization) while (!d_taskPriorityQueue.empty()) { const Task& task = d_taskPriorityQueue.top(); TaskFunction taskFunction = task.getFunction(); if (taskFunction) { taskFunction(task.getId(), task.getPriority(), verbose); } d_taskPriorityQueue.pop(); } // ... }
top
method always returns the Task
object having the highest priority in the priority queue. void taskFunction1(int taskId, int priority, int verbose) { if (verbose) { printf("Executing task %d (priority = %d) in 'taskFunction1'.\n", taskId, priority); } } void taskFunction2(int taskId, int priority, int verbose) { if (verbose) { printf("Executing task %d (priority = %d) in 'taskFunction2'.\n", taskId, priority); } }
TaskScheduler
object: TaskScheduler taskScheduler;
addTask
method of taskScheduler
in the receiving thread: // (in receiving thread) // ... taskScheduler.addTask(1, taskFunction1, 50); // ... taskScheduler.addTask(2, taskFunction1, 99); // ... taskScheduler.addTask(3, taskFunction2, 4); // ...
processTasks
method of taskScheduler
in the processing thread: // (in processing thread) // ... taskScheduler.processTasks(veryVerbose); // ...
typedef CONTAINER bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::container_type [inherited] |
typedef COMPARATOR bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::value_compare [inherited] |
typedef CONTAINER::value_type bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::value_type [inherited] |
typedef CONTAINER::reference bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::reference [inherited] |
typedef CONTAINER::const_reference bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::const_reference [inherited] |
typedef CONTAINER::size_type bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::size_type [inherited] |
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::BSLMF_NESTED_TRAIT_DECLARATION_IF | ( | priority_queue< VALUE, CONTAINER, COMPARATOR > | , | |
BloombergLP::bslma::UsesBslmaAllocator | , | |||
BloombergLP::bslma::UsesBslmaAllocator< container_type >::value | ||||
) | [inherited] |
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | ) | [inherited] |
Create an empty priority queue, adapting a default-constructed container of the (template parameter) type CONTAINER
. Use a default-constructed comparator of the (template parameter) type COMPARATOR
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const COMPARATOR & | comparator | ) | [explicit, inherited] |
Create an empty priority queue, adapting a default-constructed container of the (template parameter) type CONTAINER
, and having the specified comparator
of the (template parameter) type COMPARATOR
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const COMPARATOR & | comparator, | |
const CONTAINER & | container | |||
) | [inherited] |
Create a priority queue, adapting the specified container
of the (template parameter) type CONTAINER
, and having the specified comparator
of the (template parameter) type COMPARATOR
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const COMPARATOR & | comparator, | |
BloombergLP::bslmf::MovableRef< CONTAINER > | container | |||
) | [explicit, inherited] |
Create a priority queue, adapting the specified container
of the (template parameter) type CONTAINER
, and having the specified comparator
of the (template parameter) type COMPARATOR
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | INPUT_ITERATOR | first, | |
INPUT_ITERATOR | last | |||
) | [inherited] |
Create a priority queue, adapting a default-constructed container of the (template parameter) type CONTAINER
, and inserting into the container a sequence of value_type
elements that starts at the specified first
and ends immediately before the specified last
. Use a default-constructed comparator of the (template parameter) type COMPARATOR
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | INPUT_ITERATOR | first, | |
INPUT_ITERATOR | last, | |||
const COMPARATOR & | comparator, | |||
const CONTAINER & | container | |||
) | [inherited] |
Create a priority queue, adapting the specified container
, having the specified comparator
to order the priorities of elements, including those originally existed in container
, and those inserted into the container
from a sequence of value_type
elements starting at the specified first
, and ending immediately before the specified last
.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | INPUT_ITERATOR | first, | |
INPUT_ITERATOR | last, | |||
const COMPARATOR & | comparator, | |||
BloombergLP::bslmf::MovableRef< CONTAINER > | container | |||
) | [inherited] |
Create a priority queue, adapting the specified container
, having the specified comparator
to order elements in the priority queue, including those originally existed in container
, and those inserted into the container
from a sequence of value_type
elements starting at the specified first
, and ending immediately before the specified last
.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const priority_queue< VALUE, CONTAINER, COMPARATOR > & | original | ) | [inherited] |
Create a priority queue having the same value as the specified original
object. Use a copy of the comparator from original
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | BloombergLP::bslmf::MovableRef< priority_queue< VALUE, CONTAINER, COMPARATOR > > | original | ) | [inherited] |
Create a priority queue having the same value as the specified original
object. Use a copy of the comparator from original
to order elements in the priority queue.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const ALLOCATOR & | basicAllocator, | |
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [explicit, inherited] |
Create an empty priority queue, adapting a default-constructed container of the (template parameter) type CONTAINER
that uses the specified basicAllocator
to supply memory. Use a default-constructed object of the (template parameter) type COMPARATOR
to order elements in the priority queue. Note that this constructor is only defined if the underlying container uses allocator. Otherwise this constructor is disabled.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const COMPARATOR & | comparator, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create an empty priority queue, adapting a default-constructed container of the (template parameter) type CONTAINER
that uses the specified basicAllocator
to supply memory, and the specified comparator
to order elements in the priority queue. Note that this constructor is only defined if the underlying container uses allocator. Otherwise this constructor is disabled.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const COMPARATOR & | comparator, | |
const CONTAINER & | container, | |||
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a priority queue, adapting the specified container
that uses the specified basicAllocator
to supply memory, and the specified comparator
to order elements in the priority queue. Note that this constructor is only defined if the underlying container uses allocator. Otherwise this constructor is disabled.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const COMPARATOR & | comparator, | |
BloombergLP::bslmf::MovableRef< CONTAINER > | container, | |||
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a priority queue, adapting the specified container
that uses the specified basicAllocator
to supply memory, and the specified comparator
to order elements in the priority queue. Note that this constructor is only defined if the underlying container uses allocator. Otherwise this constructor is disabled.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | const priority_queue< VALUE, CONTAINER, COMPARATOR > & | original, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a priority queue having the same value as the specified original
object and using the specified basicAllocator
to supply memory. Use a copy of the comparator from original
to order elements in the priority queue. Note that this constructor is only defined if the underlying container uses allocator. Otherwise this constructor is disabled.
bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::priority_queue | ( | BloombergLP::bslmf::MovableRef< priority_queue< VALUE, CONTAINER, COMPARATOR > > | original, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a priority queue having the same value as the specified original
object and using the specified basicAllocator
to supply memory. Use a copy of the comparator from original
to order elements in the priority queue. Note that this constructor is only defined if the underlying container uses allocator. Otherwise this constructor is disabled.
priority_queue& bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::operator= | ( | const priority_queue< VALUE, CONTAINER, COMPARATOR > & | rhs | ) | [inherited] |
Assign to this object the value and comparator of the specified rhs
object and return a reference providing modifiable access to this object.
priority_queue& bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::operator= | ( | BloombergLP::bslmf::MovableRef< priority_queue< VALUE, CONTAINER, COMPARATOR > > | rhs | ) | [inherited] |
Assign to this object the value and comparator of the specified rhs
object and return a reference providing modifiable access to this object. rhs
is left in a valid but unspecified state.
void bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::push | ( | const value_type & | value | ) | [inherited] |
Insert the specified value
into this priority queue. In effect, performs c.push_back(value);
.
void bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::push | ( | BloombergLP::bslmf::MovableRef< value_type > | value | ) | [inherited] |
Insert the specified value
into this priority queue. In effect, performs c.push_back(value);
.
void bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::emplace | ( | Args &&... | args | ) | [inherited] |
Insert into this priority queue a newly created value_type
object, constructed by forwarding the specified (variable number of) args
to the corresponding constructor of value_type
. In effect, performs c.emplace_back(FORWARD(Args,args)...);
.
void bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::pop | ( | ) | [inherited] |
Remove the top element from this priority_queue
object that has the highest priority. In effect, performs c.pop_back();
. The behavior is undefined if there is currently no elements in this object.
size_type bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::size | ( | ) | const [inherited] |
Return the number of elements in this priority_queue
object. In effect, performs return c.size()
.
const_reference bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::top | ( | ) | const [inherited] |
Return a reference providing non-modifiable access to the element having the highest priority in this priority_queue
object. In effect, performs return c.front()
. The behavior is undefined if the priority queue is empty.
void bsl::swap | ( | priority_queue< VALUE, CONTAINER, COMPARATOR > & | a, | |
priority_queue< VALUE, CONTAINER, COMPARATOR > & | b | |||
) |
Exchange the container and comparator of the specified a
object with the container and comparator of the specified b
object.
CONTAINER bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::c [protected, inherited] |
container for elements in the priority_queue
. This
COMPARATOR bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::comp [protected, inherited] |
comparator that defines the priority order of elements
void swap (priority_queue& other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION( bsl bool bsl::priority_queue< VALUE, CONTAINER, COMPARATOR >::empty() const [inherited] |
< Efficiently exchange the value of this object with the value of the specified other
object. In effect, performs 'using bsl::swap; swap(c, other.c);'. Return true
if this priority_queue
object contains no elements, and false
otherwise. In effect, performs return c.empty();
.