![]() |
aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
|
A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowedA 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/multiPriorityQueue.h>
Public Member Functions | |
template<typename... Args> | |
INLINE Size | emplace (Args &&... args) |
Constructors / Destructors | |
MultiPriorityQueue (Cmp compare=Cmp(), Size capacity=GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY) | |
Basic constructor. More... | |
MultiPriorityQueue (std::initializer_list< std::pair< Val, Priority > > list) | |
Initializer list constructor. More... | |
MultiPriorityQueue (const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &from) | |
Copy constructor. More... | |
template<typename OtherAlloc > | |
MultiPriorityQueue (const MultiPriorityQueue< Val, Priority, Cmp, OtherAlloc > &from) | |
generalized copy constructor More... | |
MultiPriorityQueue (MultiPriorityQueue< Val, Priority, Cmp, Alloc > &&from) | |
Move constructor. More... | |
~MultiPriorityQueue () | |
Class destructor. More... | |
Operators | |
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &from) |
Copy operator. More... | |
template<typename OtherAlloc > | |
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (const MultiPriorityQueue< Val, Priority, Cmp, OtherAlloc > &from) |
Generalized copy operator. More... | |
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & | operator= (MultiPriorityQueue< Val, Priority, Cmp, Alloc > &&from) |
Move operator. More... | |
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... | |
template<typename... Args> | |
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... | |
const Priority & | priority (const Val &elt) const |
Returns the priority of an instance of the value passed in argument. More... | |
void | clear () |
Removes all the elements from the queue. More... | |
const HashTable< Val, std::vector< Size > > & | allValues () const |
Returns a gum::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 |
Return 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 | IndexAlloc = typename Alloc::template rebind< std::pair< Val, std::vector< Size > > >::other |
The allocator for the indices. More... | |
using | HeapAlloc = typename Alloc::template rebind< std::pair< Priority, const Val *> >::other |
The allocator for the heap. More... | |
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... | |
Friends | |
template<typename V , typename P , typename C , typename A > | |
class | MultiPriorityQueue |
Making all MultiPriorityQueue friend with themselves. More... | |
A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowed
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.
Val | The values type stored in the gum::MultiPriorityQueue. |
Priority | The priorities type. |
Cmp | The priorities comparator. |
Alloc | The values allocator. |
Definition at line 125 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::allocator_type = Alloc |
types for STL compliance
Definition at line 139 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::const_pointer = const Val* |
types for STL compliance
Definition at line 137 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::const_reference = const Val& |
types for STL compliance
Definition at line 135 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::difference_type = std::ptrdiff_t |
types for STL compliance
Definition at line 138 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::HeapAlloc = typename Alloc::template rebind< std::pair< Priority, const Val* > >::other |
The allocator for the heap.
Definition at line 147 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::IndexAlloc = typename Alloc::template rebind< std::pair< Val, std::vector< Size > > >::other |
The allocator for the indices.
Definition at line 144 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::pointer = Val* |
types for STL compliance
Definition at line 136 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::reference = Val& |
types for STL compliance
Definition at line 134 of file multiPriorityQueue.h.
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::value_type = Val |
types for STL compliance
Definition at line 133 of file multiPriorityQueue.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 36 of file multiPriorityQueue_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 46 of file multiPriorityQueue_tpl.h.
gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::MultiPriorityQueue | ( | const MultiPriorityQueue< Val, Priority, Cmp, Alloc > & | from | ) |
Copy constructor.
from | The gum::MultiPriorityQueue to copy. |
Definition at line 61 of file multiPriorityQueue_tpl.h.
gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::MultiPriorityQueue | ( | const MultiPriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
generalized copy constructor
Generalized Copy constructor.
from | The gum::MultiPriorityQueue to copy. |
OtherAlloc | The other gum::MultiPriorityQueue allocator. |
Definition at line 81 of file multiPriorityQueue_tpl.h.
gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::MultiPriorityQueue | ( | MultiPriorityQueue< Val, Priority, Cmp, Alloc > && | from | ) |
Move constructor.
from | The gum::MultiPriorityQueue to move. |
Definition at line 106 of file multiPriorityQueue_tpl.h.
gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::~MultiPriorityQueue | ( | ) |
Class destructor.
Definition at line 117 of file multiPriorityQueue_tpl.h.
INLINE const HashTable< Val, std::vector< Size > > & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::allValues | ( | ) | const |
Returns a gum::HashTable the keys of which are the values stored in the queue.
The keys of the gum::HashTable correspond to the values stored in the priority queue and, for each key, the corresponding value is the list of indices in the queue where we can find the key.
Definition at line 350 of file multiPriorityQueue_tpl.h.
|
noexcept |
Return the size of the internal structure storing the priority queue.
Definition at line 242 of file multiPriorityQueue_tpl.h.
void gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::clear | ( | ) |
Removes all the elements from the queue.
Definition at line 257 of file multiPriorityQueue_tpl.h.
INLINE bool gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::contains | ( | const Val & | val | ) | const |
Indicates whether the priority queue contains a given value.
val | The value to check if it is in the priority queue. |
Definition at line 482 of file multiPriorityQueue_tpl.h.
Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::emplace | ( | Args &&... | args | ) |
Emplace a new element into the priority queue.
See method gum::MultiPriorityQueue::eraseByPos(Size) for more details about the index.
Args | The emplace arguments types. |
args | The emplace arguments. |
INLINE Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::emplace | ( | Args &&... | args | ) |
Definition at line 468 of file multiPriorityQueue_tpl.h.
|
noexcept |
Indicates whether the priority queue is empty.
Definition at line 476 of file multiPriorityQueue_tpl.h.
INLINE void gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::erase | ( | const Val & | val | ) |
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 324 of file multiPriorityQueue_tpl.h.
void gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::eraseByPos | ( | Size | index | ) |
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 265 of file multiPriorityQueue_tpl.h.
INLINE void gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::eraseTop | ( | ) |
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 332 of file multiPriorityQueue_tpl.h.
Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::insert | ( | const Val & | val, |
const Priority & | priority | ||
) |
Inserts a new (a copy) element in the priority queue.
See method gum::MultiPriorityQueue::eraseByPos(Size) for more details about the index.
val | The value to insert. |
priority | The value priority. |
Definition at line 356 of file multiPriorityQueue_tpl.h.
Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::insert | ( | Val && | val, |
Priority && | priority | ||
) |
Inserts (by move) a new element in the priority queue.
See method gum::MultiPriorityQueue::eraseByPos(Size) for more details about the index.
val | The value to insert. |
priority | The value priority. |
Definition at line 412 of file multiPriorityQueue_tpl.h.
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | const MultiPriorityQueue< 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::MultiPriorityQueue to copy. |
Definition at line 125 of file multiPriorityQueue_tpl.h.
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | const MultiPriorityQueue< Val, Priority, Cmp, OtherAlloc > & | from | ) |
Generalized 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::MultiPriorityQueue to copy. |
OtherAlloc | The other gum::MultiPriorityQueue allocator. |
Definition at line 162 of file multiPriorityQueue_tpl.h.
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::operator= | ( | MultiPriorityQueue< Val, Priority, Cmp, Alloc > && | from | ) |
Move operator.
from | The gum::MultiPriorityQueue to copy. |
Definition at line 202 of file multiPriorityQueue_tpl.h.
INLINE const Val & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::operator[] | ( | Size | index_elt | ) | const |
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 488 of file multiPriorityQueue_tpl.h.
INLINE Val gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::pop | ( | ) |
Removes the top element from the priority queue and return it.
NotFound | Raised if the queue is empty. |
Definition at line 338 of file multiPriorityQueue_tpl.h.
INLINE const Priority & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::priority | ( | const Val & | elt | ) | const |
Returns the priority of an instance of the value passed in argument.
Of course, this method is really meaningful only when there is only one instance of the given element within the PriorityQueue.
elt | The element for which the priority is returned. |
NotFound | Raised if the element cannot be found. |
Definition at line 651 of file multiPriorityQueue_tpl.h.
INLINE void gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::resize | ( | Size | new_size | ) |
Changes the size of the internal structure storing the priority queue.
new_size | The internal structure new size. |
Definition at line 248 of file multiPriorityQueue_tpl.h.
void gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::setPriority | ( | const Val & | elt, |
const Priority & | new_priority | ||
) |
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 639 of file multiPriorityQueue_tpl.h.
Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::setPriorityByPos | ( | Size | index, |
const Priority & | new_priority | ||
) |
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 516 of file multiPriorityQueue_tpl.h.
Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::setPriorityByPos | ( | Size | index, |
Priority && | new_priority | ||
) |
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 578 of file multiPriorityQueue_tpl.h.
|
noexcept |
Returns the number of elements in the priority queue.
Definition at line 236 of file multiPriorityQueue_tpl.h.
INLINE const Val & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::top | ( | ) | const |
Returns the element at the top of the priority queue.
NotFound | Raised if the queue is empty. |
Definition at line 220 of file multiPriorityQueue_tpl.h.
INLINE const Priority & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::topPriority | ( | ) | const |
Returns the priority of the top element.
NotFound | Raised if the queue is empty. |
Definition at line 228 of file multiPriorityQueue_tpl.h.
std::string gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::toString | ( | ) | const |
Displays the content of the queue.
Definition at line 498 of file multiPriorityQueue_tpl.h.
|
friend |
Making all MultiPriorityQueue friend with themselves.
Definition at line 128 of file multiPriorityQueue.h.
|
private |
Comparison function.
Definition at line 482 of file multiPriorityQueue.h.
|
private |
An array storing all the elements of the heap as well as their score.
Definition at line 473 of file multiPriorityQueue.h.
|
private |
A hashtable for quickly finding the elements by their value.
Definition at line 476 of file multiPriorityQueue.h.
|
private |
The number of elements in the heap.
Definition at line 479 of file multiPriorityQueue.h.