![]() |
aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
|
A priorityQueue is a heap in which each element has a mutable priority. More...
#include <agrum/base/core/priorityQueue.h>
Public Types | |
| using | Implementation = PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value > |
| using | value_type = Val |
| Types for STL compliance. | |
| using | reference = Val& |
| Types for STL compliance. | |
| using | const_reference = const Val& |
| Types for STL compliance. | |
| using | pointer = Val* |
| Types for STL compliance. | |
| using | const_pointer = const Val* |
| Types for STL compliance. | |
| using | difference_type = std::ptrdiff_t |
| Types for STL compliance. | |
Public Member Functions | |
| INLINE Size | emplace (Args &&... args) |
Constructors / Destructors | |
| PriorityQueue (Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY) | |
| Basic constructor. | |
| PriorityQueue (std::initializer_list< std::pair< Val, Priority > > list) | |
| Initializer list constructor. | |
| PriorityQueue (const PriorityQueue< Val, Priority, Cmp > &from) | |
| Copy constructor. | |
| PriorityQueue (PriorityQueue< Val, Priority, Cmp > &&from) | |
| Move constructor. | |
| ~PriorityQueue () | |
| Class destructor. | |
Operators | |
| PriorityQueue< Val, Priority, Cmp > & | operator= (const PriorityQueue< Val, Priority, Cmp > &from) |
| Copy operator. | |
| PriorityQueue< Val, Priority, Cmp > & | operator= (PriorityQueue< Val, Priority, Cmp > &&from) |
| Move operator. | |
Operators | |
| const Val & | operator[] (Size index_elt) const |
| Returns the element at index "index_elt" from the priority queue. | |
Accessors / Modifiers | |
| Size | size () const noexcept |
| Returns the number of elements in the priority queue. | |
| bool | empty () const noexcept |
| Indicates whether the priority queue is empty. | |
| bool | contains (const Val &val) const |
| Indicates whether the priority queue contains a given value. | |
| const Val & | top () const |
| returns the element at the top of the priority queue | |
| const int & | topPriority () const |
| Returns the priority of the top element. | |
| Val | pop () |
| Removes the top element from the priority queue and return it. | |
| Size | insert (const Val &val, const int &priority) |
| Inserts a new (a copy) element in the priority queue. | |
| void | eraseTop () |
| Removes the top of the priority queue (but does not return it). | |
| void | eraseByPos (Size index) |
| Removes the element at position "index" from the priority queue. | |
| void | erase (const Val &val) |
| Removes a given element from the priority queue (but does not return it). | |
| Size | setPriorityByPos (Size index, const int &new_priority) |
| Modifies the priority of the element at position "index" of the queue. | |
| void | setPriority (const Val &elt, const int &new_priority) |
| Modifies the priority of each instance of a given element. | |
| const int & | priority (const Val &elt) const |
| Returns the priority of an instance of the value passed in argument. | |
| const int & | priorityByPos (Size index) const |
| Returns the priority of the value passed in argument. | |
| void | clear () |
| Removes all the elements from the queue. | |
| const HashTable< Val, Size > & | allValues () const noexcept |
| Returns a hashtable the keys of which are the values stored in the queue. | |
| std::string | toString () const |
| Displays the content of the queue. | |
Fine tuning | |
| Size | capacity () const noexcept |
| Returns the size of the internal structure storing the priority queue. | |
| void | resize (Size new_size) |
| Changes the size of the internal structure storing the priority queue. | |
Private Attributes | |
| std::vector< std::pair< int, const Val * > > | _heap_ |
| An array storing all the elements of the heap as well as their score. | |
| HashTable< Val, Size > | _indices_ |
| A hashtable for quickly finding the elements by their value. | |
| Size | _nb_elements_ |
| The number of elements in the heap. | |
| std::less< int > | _cmp_ |
| Comparison function. | |
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. |
Definition at line 883 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::const_pointer = const Val* |
Types for STL compliance.
Definition at line 892 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::const_reference = const Val& |
Types for STL compliance.
Definition at line 890 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::difference_type = std::ptrdiff_t |
Types for STL compliance.
Definition at line 893 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::Implementation = PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value > |
Definition at line 896 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::pointer = Val* |
Types for STL compliance.
Definition at line 891 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::reference = Val& |
Types for STL compliance.
Definition at line 889 of file priorityQueue.h.
| using gum::PriorityQueue< Val, Priority, Cmp >::value_type = Val |
Types for STL compliance.
Definition at line 888 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 whether 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 986 of file priorityQueue_tpl.h.
References PriorityQueue(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::capacity().
Referenced by PriorityQueue(), PriorityQueue(), PriorityQueue(), PriorityQueue(), and ~PriorityQueue().
|
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 994 of file priorityQueue_tpl.h.
References PriorityQueue().
| INLINE gum::PriorityQueue< Val, Priority, Cmp >::PriorityQueue | ( | const PriorityQueue< Val, Priority, Cmp > & | from | ) |
Copy constructor.
| from | The gum::PriorityQueue to copy. |
Definition at line 1003 of file priorityQueue_tpl.h.
References PriorityQueue(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.
| INLINE gum::PriorityQueue< Val, Priority, Cmp >::PriorityQueue | ( | PriorityQueue< Val, Priority, Cmp > && | from | ) |
Move constructor.
| from | The gum::PriorityQueue to move. |
Definition at line 1012 of file priorityQueue_tpl.h.
References PriorityQueue(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.
| INLINE gum::PriorityQueue< Val, Priority, Cmp >::~PriorityQueue | ( | ) |
Class destructor.
Definition at line 1021 of file priorityQueue_tpl.h.
References PriorityQueue().
|
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 399 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns the size of the internal structure storing the priority queue.
Definition at line 419 of file priorityQueue_tpl.h.
Referenced by gum::PriorityQueue< Val, Priority, Cmp >::PriorityQueue().
|
inherited |
Removes all the elements from the queue.
Definition at line 386 of file priorityQueue_tpl.h.
References _heap_, and _nb_elements_.
|
inherited |
Indicates whether the priority queue contains a given value.
| val | The value to look for. |
Definition at line 222 of file priorityQueue_tpl.h.
References _nb_elements_.
|
inherited |
Definition at line 350 of file priorityQueue_tpl.h.
|
noexceptinherited |
Indicates whether the priority queue is empty.
Definition at line 215 of file priorityQueue_tpl.h.
References _heap_, _indices_, and _nb_elements_.
|
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 325 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 311 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 291 of file priorityQueue_tpl.h.
References _cmp_, _heap_, _indices_, and _nb_elements_.
|
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 254 of file priorityQueue_tpl.h.
| INLINE PriorityQueue< Val, Priority, Cmp > & gum::PriorityQueue< Val, Priority, Cmp >::operator= | ( | const PriorityQueue< Val, Priority, Cmp > & | 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 1028 of file priorityQueue_tpl.h.
References gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::operator=(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.
| INLINE PriorityQueue< Val, Priority, Cmp > & gum::PriorityQueue< Val, Priority, Cmp >::operator= | ( | PriorityQueue< Val, Priority, Cmp > && | from | ) |
Move operator.
| from | The gum::PriorityQueue to move. |
Definition at line 1037 of file priorityQueue_tpl.h.
References gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::operator=(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.
|
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 197 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 240 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 374 of file priorityQueue_tpl.h.
Referenced by gum::DefaultPartialOrderedEliminationSequenceStrategy::_nodeToEliminate_().
|
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 381 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 426 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 355 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 336 of file priorityQueue_tpl.h.
|
noexceptinherited |
Returns the number of elements in the priority queue.
Definition at line 209 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 226 of file priorityQueue_tpl.h.
References _cmp_, _heap_, and _nb_elements_.
|
inherited |
Returns the priority of the top element.
| NotFound | Raised if the queue is empty. |
Definition at line 233 of file priorityQueue_tpl.h.
|
inherited |
Displays the content of the queue.
Definition at line 405 of file priorityQueue_tpl.h.
|
privateinherited |
Comparison function.
Definition at line 441 of file priorityQueue.h.
Referenced by eraseTop(), resize(), and top().
|
privateinherited |
An array storing all the elements of the heap as well as their score.
Definition at line 432 of file priorityQueue.h.
Referenced by PriorityQueueImplementation(), clear(), empty(), eraseTop(), operator[](), pop(), resize(), setPriorityByPos(), top(), and topPriority().
|
privateinherited |
A hashtable for quickly finding the elements by their value.
Definition at line 435 of file priorityQueue.h.
Referenced by PriorityQueueImplementation(), empty(), eraseTop(), operator[](), pop(), resize(), setPriorityByPos(), and topPriority().
|
privateinherited |
The number of elements in the heap.
Definition at line 438 of file priorityQueue.h.
Referenced by ~PriorityQueueImplementation(), clear(), contains(), empty(), eraseTop(), and top().