![]() |
MySQL 9.1.0
Source Code Documentation
|
Implements a priority queue using a vector-based max-heap. More...
#include <priority_queue.h>
Public Types | |
typedef Container | container_type |
typedef Less | less_type |
typedef container_type::value_type | value_type |
typedef container_type::size_type | size_type |
typedef container_type::iterator | iterator |
typedef container_type::const_iterator | const_iterator |
typedef container_type::allocator_type | allocator_type |
Public Member Functions | |
Priority_queue (Less const &less=Less(), const allocator_type &alloc=allocator_type(), const Marker &marker=Marker()) | |
Constructs an empty priority queue. More... | |
template<typename Input_iterator > | |
Priority_queue (Input_iterator first, Input_iterator beyond, Less const &less=Less(), const allocator_type &alloc=allocator_type(), const Marker &marker=Marker()) | |
Constructs a heap of the objects between first and beyond. More... | |
void | assign (const container_type &container) |
Constructs a heap based on input argument. More... | |
void | build_heap () |
Constructs a heap based on container contents. More... | |
value_type const & | top () const |
Returns a const reference to the top element of the priority queue. More... | |
value_type & | top () |
Returns a reference to the top element of the priority queue. More... | |
bool | push (value_type const &x) |
Inserts an element in the priority queue. More... | |
void | pop () |
Pops the top-most element in the priority queue. More... | |
void | remove (size_type i) |
Removes the element at position i from the priority queue. More... | |
void | decrease (size_type i, value_type const &x) |
Decreases the priority of the element at position i, where the new priority is x. More... | |
void | increase (size_type i, value_type const &x) |
Increases the priority of the element at position i, where the new priority is x. More... | |
void | update (size_type i, value_type const &x) |
Changes the priority of the element at position i, where the new priority is x. More... | |
void | increase (size_type i) |
Assumes that the i-th element's value has increased and rebuilds the priority queue. More... | |
void | decrease (size_type i) |
Assumes that the i-th element's value has decreased and rebuilds the priority queue. More... | |
void | update (size_type i) |
Assumes that the i-th element's value has changed and rebuilds the priority queue. More... | |
void | update_top () |
Assumes that the top element's value has changed and rebuilds the priority queue. More... | |
size_type | size () const |
Returns the number of elements of the priority queue. More... | |
bool | empty () const |
Returns true if the priority queue is empty. More... | |
value_type const & | operator[] (size_type i) const |
Returns a const reference to the i-th element in the underlying container. More... | |
value_type & | operator[] (size_type i) |
Returns a reference to the i-th element in the underlying container. More... | |
const_iterator | begin () const |
Returns a const iterator to the first element of the underlying container. More... | |
const_iterator | end () const |
Returns a const iterator to the end element of the underlying container. More... | |
iterator | begin () |
Returns an iterator to the first element of the underlying container. More... | |
iterator | end () |
Returns an iterator to the end element of the underlying container. More... | |
void | swap (Priority_queue &other) |
Swaps the contents of two priority queues. More... | |
bool | is_valid () const |
Returns true if the priority queue has the heap property. More... | |
void | sort () |
Sorts the elements of the priority queue according to the strict partial ordering defined by the object of type Less passed to the priority queue. More... | |
void | clear () |
Clears the priority queue. More... | |
void | delete_elements () |
Clears the priority queue, but deletes all elements first. More... | |
size_type | capacity () const |
Returns the capacity of the internal container. More... | |
bool | reserve (size_type n) |
Reserves space for array elements. More... | |
Private Types | |
typedef Less | Base |
Private Member Functions | |
void | heapify (size_type i, size_type last) |
void | heapify (size_type i) |
void | reverse_heapify (size_type i) |
void | decrease_key (size_type i, value_type const &x) |
void | increase_key (size_type i, value_type const &x) |
Static Private Member Functions | |
static size_type | parent (size_type i) |
static size_type | left (size_type i) |
static size_type | right (size_type i) |
Private Attributes | |
container_type | m_container |
Marker | m_marker |
Friends | |
class | priority_queue_unittest::PriorityQueueTest |
Implements a priority queue using a vector-based max-heap.
A priority queue is a container specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.
For object locality, the implementation is vector-based, rather than node-based.
The priority queue is mutable, which means that the priority of an element can be changed. See increase/decrease/update member functions. The typical use case is to change the value/priority of the root node.
We provide iterators, which can be used to visit all elements. Iterators do not visit queue elements in priority order. Iterators should not be used to change the priority of elements.
The underlying container must be constructible from an iterator range, should provide const and non-const random access iterators to access its elements, as well as the following operations:
T | Type of the elements of the priority queue. |
Container | Type of the underlying container object where elements are stored. Its value_type shall be T. |
Less | A binary predicate that takes two elements (of type T) and returns a bool. The expression less(a,b), where less is an object of this type and a and b are elements in the container, shall return true if a is considered to go before b in the strict weak ordering the function defines. |
Marker | A functor, with signature void operator()(size_t, T *), that gets called whenever an element gets a new position in the queue (including initial insert, but excluding removals). The marker can then store the element's position somewhere, for later calls to update() as needed. |
typedef container_type::allocator_type Priority_queue< T, Container, Less, Marker >::allocator_type |
|
private |
typedef container_type::const_iterator Priority_queue< T, Container, Less, Marker >::const_iterator |
typedef Container Priority_queue< T, Container, Less, Marker >::container_type |
typedef container_type::iterator Priority_queue< T, Container, Less, Marker >::iterator |
typedef Less Priority_queue< T, Container, Less, Marker >::less_type |
typedef container_type::size_type Priority_queue< T, Container, Less, Marker >::size_type |
typedef container_type::value_type Priority_queue< T, Container, Less, Marker >::value_type |
|
inline |
Constructs an empty priority queue.
|
inline |
Constructs a heap of the objects between first and beyond.
|
inline |
Constructs a heap based on input argument.
|
inline |
Returns an iterator to the first element of the underlying container.
|
inline |
Returns a const iterator to the first element of the underlying container.
|
inline |
Constructs a heap based on container contents.
Can also be used when many elements have changed.
|
inline |
Returns the capacity of the internal container.
|
inline |
Clears the priority queue.
|
inline |
Assumes that the i-th element's value has decreased and rebuilds the priority queue.
|
inline |
Decreases the priority of the element at position i, where the new priority is x.
|
inlineprivate |
|
inline |
Clears the priority queue, but deletes all elements first.
|
inline |
Returns true if the priority queue is empty.
|
inline |
Returns an iterator to the end element of the underlying container.
|
inline |
Returns a const iterator to the end element of the underlying container.
|
inlineprivate |
|
inlineprivate |
|
inline |
Assumes that the i-th element's value has increased and rebuilds the priority queue.
|
inline |
Increases the priority of the element at position i, where the new priority is x.
|
inlineprivate |
|
inline |
Returns true if the priority queue has the heap property.
|
inlinestaticprivate |
|
inline |
Returns a reference to the i-th element in the underlying container.
|
inline |
Returns a const reference to the i-th element in the underlying container.
|
inlinestaticprivate |
|
inline |
Pops the top-most element in the priority queue.
|
inline |
Inserts an element in the priority queue.
x | value to be pushed. |
true | if out-of-memory, false otherwise. |
|
inline |
Removes the element at position i from the priority queue.
|
inline |
Reserves space for array elements.
n | number of elements. |
true | if out-of-memory, false otherwise. |
|
inlineprivate |
|
inlinestaticprivate |
|
inline |
Returns the number of elements of the priority queue.
|
inline |
Sorts the elements of the priority queue according to the strict partial ordering defined by the object of type Less passed to the priority queue.
The heap property of the priority queue is invalidated by this operation.
|
inline |
Swaps the contents of two priority queues.
|
inline |
Returns a reference to the top element of the priority queue.
|
inline |
Returns a const reference to the top element of the priority queue.
|
inline |
Assumes that the i-th element's value has changed and rebuilds the priority queue.
|
inline |
Changes the priority of the element at position i, where the new priority is x.
|
inline |
Assumes that the top element's value has changed and rebuilds the priority queue.
|
friend |
|
private |
|
private |