![]() |
aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
|
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/tools/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 50 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::allocator_type = Alloc |
Types for STL compliance.
Definition at line 958 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::const_pointer = const Val* |
Types for STL compliance.
Definition at line 956 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::const_reference = const Val& |
Types for STL compliance.
Definition at line 954 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::difference_type = std::ptrdiff_t |
Types for STL compliance.
Definition at line 957 of file priorityQueue.h.
|
inherited |
Definition at line 104 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 962 of file priorityQueue.h.
|
inherited |
Definition at line 100 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::pointer = Val* |
Types for STL compliance.
Definition at line 955 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::reference = Val& |
Types for STL compliance.
Definition at line 953 of file priorityQueue.h.
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::value_type = Val |
Types for STL compliance.
Definition at line 952 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 1098 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 1106 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 1115 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 1134 of file priorityQueue_tpl.h.
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::~PriorityQueue | ( | ) |
Class destructor.
Definition at line 1143 of file priorityQueue_tpl.h.
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue | ( | const PriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
Definition at line 1125 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 313 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns the size of the internal structure storing the priority queue.
Definition at line 229 of file priorityQueue_tpl.h.
|
inherited |
Removes all the elements from the queue.
Definition at line 244 of file priorityQueue_tpl.h.
|
inherited |
Indicates whether the priority queue contains a given value.
val | The value to look for. |
Definition at line 410 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 394 of file priorityQueue_tpl.h.
|
noexceptinherited |
Indicates whether the priority queue is empty.
Definition at line 403 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 287 of file priorityQueue_tpl.h.
|
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 252 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 295 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 319 of file priorityQueue_tpl.h.
|
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 356 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 1151 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 1170 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 1161 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 417 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 301 of file priorityQueue_tpl.h.
|
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 550 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 557 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 235 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 533 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 541 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 445 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 489 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns the number of elements in the priority queue.
Definition at line 222 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 205 of file priorityQueue_tpl.h.
|
inherited |
Returns the priority of the top element.
NotFound | Raised if the queue is empty. |
Definition at line 214 of file priorityQueue_tpl.h.
|
inherited |
Displays the content of the queue.
Definition at line 427 of file priorityQueue_tpl.h.