![]() |
aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
|
The internal class for representing priority queues. More...
#include <agrum/tools/core/priorityQueue.h>
Public Member Functions | |
template<typename... Args> | |
INLINE Size | emplace (Args &&... args) |
template<typename Val, typename Priority, typename Cmp, typename Alloc> | |
PriorityQueueImplementation (const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > &from) | |
template<typename OtherAlloc > | |
PriorityQueueImplementation (const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > &from) | |
template<typename Val, typename Priority, typename Cmp, typename Alloc> | |
PriorityQueueImplementation (PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > &&from) | |
template<typename OtherAlloc > | |
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > & | operator= (const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > &from) |
Operators | |
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & | operator= (const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &from) |
Copy operator. More... | |
template<typename OtherAlloc > | |
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & | operator= (const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen > &from) |
Generalized copy operator. More... | |
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & | operator= (PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &&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... | |
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 | 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... | |
Friends | |
class | PriorityQueue< Val, Priority, Cmp, Alloc > |
All gum::PriorityQueue are friends with themselves. More... | |
template<typename V , typename P , typename C , typename A , bool g> | |
class | PriorityQueueImplementation |
All gum::PriorityQueueImplementation are friends with themselves. More... | |
The internal class for representing priority queues.
Priority Queues have different implementations depending on the nature of the values they store. Basically, scalar values can lead to optimization of the code whereas general types like classes cannot. The current class is used for general types (and therefore for classes). The user shall not use directly the implementation but rather use the PriorityQueue class. The latter will be assigned the best implementation at compile time.
Val | The values type. |
Priority | The priorities type. |
Cmp | The priorities comparator. |
Alloc | The values allocator. |
Gen | Used for metaprogramation, for scalar and non-scalar priority queues. |
Definition at line 79 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::allocator_type = Alloc |
Types for STL compliance.
Definition at line 96 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::const_pointer = const Val* |
Types for STL compliance.
Definition at line 94 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::const_reference = const Val& |
Types for STL compliance.
Definition at line 92 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::difference_type = std::ptrdiff_t |
Types for STL compliance.
Definition at line 95 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::HeapAllocator = typename Alloc::template rebind< std::pair< Priority, const Val* > >::other |
Definition at line 104 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::IndexAllocator = typename Alloc::template rebind< std::pair< Val, Size > >::other |
Definition at line 100 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::pointer = Val* |
Types for STL compliance.
Definition at line 93 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::reference = Val& |
Types for STL compliance.
Definition at line 91 of file priorityQueue.h.
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::value_type = Val |
Types for STL compliance.
Definition at line 90 of file priorityQueue.h.
|
explicitprivate |
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 39 of file priorityQueue_tpl.h.
|
explicitprivate |
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 51 of file priorityQueue_tpl.h.
|
private |
Copy constructor.
from | The gum::PriorityQueueImplementation to copy. |
Definition at line 65 of file priorityQueue_tpl.h.
|
private |
Generalized copy constructor.
OtherAlloc | The other gum::PriorityQueueImplementation allocator. |
from | The gum::PriorityQueueImplementation to copy. |
Definition at line 80 of file priorityQueue_tpl.h.
|
private |
Move constructor.
from | The gum::PriorityQueueImplementation to move. |
Definition at line 100 of file priorityQueue_tpl.h.
|
private |
Class destructor.
Definition at line 110 of file priorityQueue_tpl.h.
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation | ( | const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > & | from | ) |
Definition at line 600 of file priorityQueue_tpl.h.
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation | ( | const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > & | from | ) |
Definition at line 611 of file priorityQueue_tpl.h.
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation | ( | PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > && | from | ) |
Definition at line 629 of file priorityQueue_tpl.h.
|
noexcept |
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.
|
noexcept |
Returns the size of the internal structure storing the priority queue.
Definition at line 229 of file priorityQueue_tpl.h.
INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::clear | ( | ) |
Removes all the elements from the queue.
Definition at line 244 of file priorityQueue_tpl.h.
INLINE bool gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::contains | ( | const Val & | val | ) | const |
Indicates whether the priority queue contains a given value.
val | The value to look for. |
Definition at line 410 of file priorityQueue_tpl.h.
Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::emplace | ( | Args &&... | args | ) |
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. |
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::emplace | ( | Args &&... | args | ) |
Definition at line 394 of file priorityQueue_tpl.h.
|
noexcept |
Indicates whether the priority queue is empty.
Definition at line 403 of file priorityQueue_tpl.h.
INLINE void gum::PriorityQueueImplementation< 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 287 of file priorityQueue_tpl.h.
void gum::PriorityQueueImplementation< 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 252 of file priorityQueue_tpl.h.
INLINE void gum::PriorityQueueImplementation< 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 295 of file priorityQueue_tpl.h.
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::insert | ( | const Val & | val, |
const Priority & | priority | ||
) |
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.
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::insert | ( | Val && | val, |
Priority && | priority | ||
) |
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.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= | ( | const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & | 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::PriorityQueueImplementation to copy. |
Definition at line 117 of file priorityQueue_tpl.h.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= | ( | const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen > & | 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.
OtherAlloc | The other gum::PriorityQueueImplementation allocator. |
from | The gum::PriorityQueueImplementation to copy. |
Definition at line 152 of file priorityQueue_tpl.h.
INLINE PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= | ( | PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > && | from | ) |
Move operator.
from | The gum::PriorityQueueImplementation to move. |
Definition at line 188 of file priorityQueue_tpl.h.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true >& gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= | ( | const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > & | from | ) |
Definition at line 679 of file priorityQueue_tpl.h.
INLINE const Val & gum::PriorityQueueImplementation< 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 417 of file priorityQueue_tpl.h.
INLINE Val gum::PriorityQueueImplementation< 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 301 of file priorityQueue_tpl.h.
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::priority | ( | const Val & | elt | ) | const |
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.
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::priorityByPos | ( | Size | index | ) | const |
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.
INLINE void gum::PriorityQueueImplementation< 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 235 of file priorityQueue_tpl.h.
void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::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 533 of file priorityQueue_tpl.h.
void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::setPriority | ( | const Val & | elt, |
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 541 of file priorityQueue_tpl.h.
Size gum::PriorityQueueImplementation< 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 445 of file priorityQueue_tpl.h.
Size gum::PriorityQueueImplementation< 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 489 of file priorityQueue_tpl.h.
|
noexcept |
Returns the number of elements in the priority queue.
Definition at line 222 of file priorityQueue_tpl.h.
INLINE const Val & gum::PriorityQueueImplementation< 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 205 of file priorityQueue_tpl.h.
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::topPriority | ( | ) | const |
Returns the priority of the top element.
NotFound | Raised if the queue is empty. |
Definition at line 214 of file priorityQueue_tpl.h.
std::string gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::toString | ( | ) | const |
Displays the content of the queue.
Definition at line 427 of file priorityQueue_tpl.h.
|
friend |
All gum::PriorityQueueImplementation are friends with themselves.
Definition at line 85 of file priorityQueue.h.
|
friend |
All gum::PriorityQueue are friends with themselves.
Definition at line 81 of file priorityQueue.h.
|
private |
Comparison function.
Definition at line 459 of file priorityQueue.h.
|
private |
An array storing all the elements of the heap as well as their score.
Definition at line 450 of file priorityQueue.h.
|
private |
A hashtable for quickly finding the elements by their value.
Definition at line 453 of file priorityQueue.h.
|
private |
The number of elements in the heap.
Definition at line 456 of file priorityQueue.h.