![]() |
aGrUM
0.16.0
|
A priorityQueue is a heap in which each element has a mutable priorityA priority queue is quite similar to a heap except that a priority (a score) is assigned to each element in the structure. More...
#include <agrum/core/priorityQueue.h>
Public Member Functions | |
template<typename OtherAlloc > | |
INLINE | PriorityQueue (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from) |
template<typename OtherAlloc > | |
INLINE PriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from) |
INLINE Size | emplace (Args &&... args) |
Constructors / Destructors | |
PriorityQueue (Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY) | |
Basic constructor. More... | |
PriorityQueue (std::initializer_list< std::pair< Val, Priority > > list) | |
Initializer list constructor. More... | |
PriorityQueue (const PriorityQueue< Val, Priority, Cmp, Alloc > &from) | |
Copy constructor. More... | |
template<typename OtherAlloc > | |
PriorityQueue (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from) | |
Generalized copy constructor. More... | |
PriorityQueue (PriorityQueue< Val, Priority, Cmp, Alloc > &&from) | |
Move constructor. More... | |
~PriorityQueue () | |
Class destructor. More... | |
Operators | |
PriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (const PriorityQueue< Val, Priority, Cmp, Alloc > &from) |
Copy operator. More... | |
template<typename OtherAlloc > | |
PriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from) |
Generalized opy operator. More... | |
PriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (PriorityQueue< Val, Priority, Cmp, Alloc > &&from) |
Move operator. More... | |
Operators | |
const Val & | operator[] (Size index_elt) const |
Returns the element at index "index_elt" from the priority queue. More... | |
Accessors / Modifiers | |
Size | size () const noexcept |
Returns the number of elements in the priority queue. More... | |
bool | empty () const noexcept |
Indicates whether the priority queue is empty. More... | |
bool | contains (const Val &val) const |
Indicates whether the priority queue contains a given value. More... | |
const Val & | top () const |
returns the element at the top of the priority queue More... | |
const Priority & | topPriority () const |
Returns the priority of the top element. More... | |
Val | pop () |
Removes the top element from the priority queue and return it. More... | |
Size | insert (const Val &val, const Priority &priority) |
Inserts a new (a copy) element in the priority queue. More... | |
Size | insert (Val &&val, Priority &&priority) |
Inserts (by move) a new element in the priority queue. More... | |
Size | emplace (Args &&... args) |
Emplace a new element into the priority queue. More... | |
void | eraseTop () |
Removes the top of the priority queue (but does not return it). More... | |
void | eraseByPos (Size index) |
Removes the element at position "index" from the priority queue. More... | |
void | erase (const Val &val) |
Removes a given element from the priority queue (but does not return it). More... | |
Size | setPriorityByPos (Size index, const Priority &new_priority) |
Modifies the priority of the element at position "index" of the queue. More... | |
Size | setPriorityByPos (Size index, Priority &&new_priority) |
Modifies the priority of the element at position "index" of the queue. More... | |
void | setPriority (const Val &elt, const Priority &new_priority) |
Modifies the priority of each instance of a given element. More... | |
void | setPriority (const Val &elt, Priority &&new_priority) |
Modifies the priority of each instance of a given element. More... | |
const Priority & | priority (const Val &elt) const |
Returns the priority of an instance of the value passed in argument. More... | |
const Priority & | priorityByPos (Size index) const |
Returns the priority of the value passed in argument. More... | |
void | clear () |
Removes all the elements from the queue. More... | |
const HashTable< Val, Size > & | allValues () const noexcept |
Returns a hashtable the keys of which are the values stored in the queue. More... | |
std::string | toString () const |
Displays the content of the queue. More... | |
Fine tuning | |
Size | capacity () const noexcept |
Returns the size of the internal structure storing the priority queue. More... | |
void | resize (Size new_size) |
Changes the size of the internal structure storing the priority queue. More... | |
Public Types | |
using | Implementation = PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > |
using | IndexAllocator = typename Alloc::template rebind< std::pair< Val, Size > >::other |
using | HeapAllocator = typename Alloc::template rebind< std::pair< Priority, const Val * > >::other |
using | value_type = Val |
Types for STL compliance. More... | |
using | reference = Val & |
Types for STL compliance. More... | |
using | const_reference = const Val & |
Types for STL compliance. More... | |
using | pointer = Val * |
Types for STL compliance. More... | |
using | const_pointer = const Val * |
Types for STL compliance. More... | |
using | difference_type = std::ptrdiff_t |
Types for STL compliance. More... | |
using | allocator_type = Alloc |
Types for STL compliance. More... | |
A priorityQueue is a heap in which each element has a mutable priority
A priority queue is quite similar to a heap except that a priority (a score) is assigned to each element in the structure.
The elements are sorted according to a weak order on the scores. The priority of any element can be changed at any moment by the user. The priority queue then restores a heap property accordingly. Duplicate elements are not allowed in priority queues; if you wish an element to appear several times with different priorities, prefer using class MultiplePriorityQueue.
Val | The values type. |
Priority | The priorities type. |
Cmp | The priorities comparator. |
Alloc | The values allocator. |
Definition at line 51 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::allocator_type = Alloc |
Types for STL compliance.
Definition at line 975 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::const_pointer = const Val* |
Types for STL compliance.
Definition at line 973 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::const_reference = const Val& |
Types for STL compliance.
Definition at line 971 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::difference_type = std::ptrdiff_t |
Types for STL compliance.
Definition at line 974 of file priorityQueue.h.
|
inherited |
Definition at line 111 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::Implementation = PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > |
Definition at line 983 of file priorityQueue.h.
|
inherited |
Definition at line 107 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::pointer = Val* |
Types for STL compliance.
Definition at line 972 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::reference = Val& |
Types for STL compliance.
Definition at line 970 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::value_type = Val |
Types for STL compliance.
Definition at line 969 of file priorityQueue.h.
|
explicit |
Basic constructor.
Creates an empty priority queue.
compare | a function taking two elements in argument, say e1 and e2, and returning a Boolean indicating wether e1 < e2, i.e., whether e1 should be nearer than e2 to the top of the heap. |
capacity | the size of the internal data structures containing the elements (could be for instance vectors or hashtables). |
Definition at line 1307 of file priorityQueue_tpl.h.
|
explicit |
Initializer list constructor.
The elements of the initializer list are pairs <Val,Priority>. The comparison function is the default one, i.e., std::less<Priority>.
list | The initializer list. |
Definition at line 1316 of file priorityQueue_tpl.h.
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue | ( | const PriorityQueue< Val, Priority, Cmp, Alloc > & | from | ) |
Copy constructor.
from | The gum::PriorityQueue to copy. |
Definition at line 1325 of file priorityQueue_tpl.h.
gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue | ( | const PriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
Generalized copy constructor.
from | The gum::PriorityQueue to copy. |
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue | ( | PriorityQueue< Val, Priority, Cmp, Alloc > && | from | ) |
Move constructor.
from | The gum::PriorityQueue to move. |
Definition at line 1344 of file priorityQueue_tpl.h.
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::~PriorityQueue | ( | ) |
Class destructor.
Definition at line 1353 of file priorityQueue_tpl.h.
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue | ( | const PriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
Definition at line 1335 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns a hashtable the keys of which are the values stored in the queue.
The keys of the hashtable correspond to the values stored in the priority queue and, for each key, the corresponding value is the index in the queue where we can find the key.
Definition at line 424 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns the size of the internal structure storing the priority queue.
Definition at line 304 of file priorityQueue_tpl.h.
|
inherited |
Removes all the elements from the queue.
Definition at line 331 of file priorityQueue_tpl.h.
|
inherited |
Indicates whether the priority queue contains a given value.
val | The value to look for. |
Definition at line 549 of file priorityQueue_tpl.h.
|
inherited |
Emplace a new element into the priority queue.
See gum::PriorityQueueImplementation::eraseByPos(Size) for more details about the index.
Args | The emplace arguments types. |
args | The emplace arguments. |
DuplicateElement | Raised if the element already exists. |
|
inherited |
Definition at line 523 of file priorityQueue_tpl.h.
|
noexceptinherited |
Indicates whether the priority queue is empty.
Definition at line 537 of file priorityQueue_tpl.h.
|
inherited |
Removes a given element from the priority queue (but does not return it).
If the element cannot be found, the function returns without throwing any exception.
If the queue contains several times this element, then the one with the smallest index is removed.
val | the element we wish to remove. |
Definition at line 384 of file priorityQueue_tpl.h.
Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().
|
inherited |
Removes the element at position "index" from the priority queue.
If the element cannot be found, the function returns without throwing any exception.
The priority is computed as follows: suppose that the queue is a complete binary tree where all levels are completely filled except, eventually, the last one. In this case, the elements of the last level are all on the left of the tree.
We assign 0 to the root, then parsing the tree from top to bottom then from left to right we increment the index and assigned it to the current node. Doing so, we get a unique index for each element. This is precisely what the index passed in argument of the function represents.
index | represents the position of the element to be removed. |
Definition at line 343 of file priorityQueue_tpl.h.
|
inherited |
Removes the top of the priority queue (but does not return it).
If the heap is empty, it does nothing (in particular, it does not throw any exception).
Definition at line 398 of file priorityQueue_tpl.h.
|
inherited |
Inserts a new (a copy) element in the priority queue.
See gum::PriorityQueueImplementation::eraseByPos(Size) for more details about the index.
val | The value to insert. |
priority | The value's priority in the queue. |
DuplicateElement | Raised if the element already exists. |
Definition at line 436 of file priorityQueue_tpl.h.
Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().
|
inherited |
Inserts (by move) a new element in the priority queue.
See gum::PriorityQueueImplementation::eraseByPos(Size) for more details about the index.
val | The value to insert. |
priority | The value's priority in the queue. |
DuplicateElement | Raised if the element already exists. |
Definition at line 479 of file priorityQueue_tpl.h.
INLINE PriorityQueue< Val, Priority, Cmp, Alloc > & gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | const PriorityQueue< Val, Priority, Cmp, Alloc > & | from | ) |
Copy operator.
When a problem occurs during the copy (for instance when not enough memory is available), the operator guarantees that the heap stays in a coherent state. Actually, the priority queue becomes empty. An exception is then thrown.
from | The gum::PriorityQueue to copy. |
Definition at line 1362 of file priorityQueue_tpl.h.
PriorityQueue< Val, Priority, Cmp, Alloc >& gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | const PriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
Generalized opy operator.
When a problem occurs during the copy (for instance when not enough memory is available), the operator guarantees that the heap stays in a coherent state. Actually, the priority queue becomes empty. An exception is then thrown.
from | The gum::PriorityQueue to copy. |
INLINE PriorityQueue< Val, Priority, Cmp, Alloc > & gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | PriorityQueue< Val, Priority, Cmp, Alloc > && | from | ) |
Move operator.
from | The gum::PriorityQueue to move. |
Definition at line 1381 of file priorityQueue_tpl.h.
INLINE PriorityQueue< Val, Priority, Cmp, Alloc >& gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | const PriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
Definition at line 1372 of file priorityQueue_tpl.h.
|
inherited |
Returns the element at index "index_elt" from the priority queue.
index_elt | The index of the element to return. |
NotFound | Raised if the element does not exist. |
Definition at line 561 of file priorityQueue_tpl.h.
|
inherited |
Removes the top element from the priority queue and return it.
NotFound | Raised if the queue is empty. |
Definition at line 408 of file priorityQueue_tpl.h.
Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().
|
inherited |
Returns the priority of an instance of the value passed in argument.
elt | The element for which the priority is returned. |
NotFound | Raised if the element cannot be found. |
Definition at line 721 of file priorityQueue_tpl.h.
|
inherited |
Returns the priority of the value passed in argument.
index | The index of the value of which the priority is returned. |
NotFound | Raised if the element cannot be found. |
Definition at line 733 of file priorityQueue_tpl.h.
|
inherited |
Changes the size of the internal structure storing the priority queue.
new_size | The internal structure new size. |
Definition at line 316 of file priorityQueue_tpl.h.
|
inherited |
Modifies the priority of each instance of a given element.
elt | The value to update. |
new_priority | The values new priority. |
NotFound | Raised if the element cannot be found. |
Definition at line 698 of file priorityQueue_tpl.h.
Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().
|
inherited |
Modifies the priority of each instance of a given element.
elt | The value to update. |
new_priority | The values new priority. |
NotFound | Raised if the element cannot be found. |
Definition at line 709 of file priorityQueue_tpl.h.
|
inherited |
Modifies the priority of the element at position "index" of the queue.
index | The index of the element to update. |
new_priority | The element's new priority. |
NotFound | Raised if the element cannot be found. |
Definition at line 601 of file priorityQueue_tpl.h.
|
inherited |
Modifies the priority of the element at position "index" of the queue.
index | The index of the element to update. |
new_priority | The element's new priority. |
NotFound | Raised if the element cannot be found. |
Definition at line 650 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns the number of elements in the priority queue.
Definition at line 292 of file priorityQueue_tpl.h.
|
inherited |
returns the element at the top of the priority queue
NotFound | Raised if the queue is empty |
Definition at line 265 of file priorityQueue_tpl.h.
|
inherited |
Returns the priority of the top element.
NotFound | Raised if the queue is empty. |
Definition at line 278 of file priorityQueue_tpl.h.
|
inherited |
Displays the content of the queue.
Definition at line 577 of file priorityQueue_tpl.h.
Referenced by gum::operator<<().