![]() |
aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
|
#include <agrum/base/core/heap.h>
Public Types | |
| 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 | size_type = std::size_t |
| Types for STL compliance. | |
| using | difference_type = std::ptrdiff_t |
| Types for STL compliance. | |
Public Member Functions | |
Constructors / Destructors | |
| Heap (Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY) | |
| Basic constructor: creates an empty heap. | |
| Heap (std::initializer_list< Val > list) | |
| Initializer list constructor. | |
| Heap (const Heap< Val, Cmp > &from) | |
| Copy constructor. | |
| Heap (Heap< Val, Cmp > &&from) noexcept | |
| Move constructor. | |
| ~Heap () | |
| Class destructor. | |
Operators | |
| Heap< Val, Cmp > & | operator= (const Heap< Val, Cmp > &from) |
| Copy operator. | |
| Heap< Val, Cmp > & | operator= (Heap< Val, Cmp > &&from) noexcept |
| Move operator. | |
| const Val & | operator[] (Size index_elt) const |
| Returns the element at index index_elt from the heap. | |
Accessors / Modifiers | |
| const Val & | top () const |
| Returns the element at the top of the heap. | |
| Val | pop () |
| Removes the top element from the heap and return it. | |
| void | eraseTop () |
| Removes the top of the heap (but does not return it). | |
| void | eraseByPos (Size index) |
| Removes the element positioned at "index" from the heap. | |
| void | erase (const Val &val) |
| Removes a given element from the heap (but does not return it). | |
| Size | insert (const Val &val) |
| inserts a new element (actually a copy) in the heap and returns its index | |
| Size | insert (Val &&val) |
| Inserts a new element (by moving it) in the heap and returns its index. | |
| template<typename... Args> | |
| Size | emplace (Args &&... args) |
| Emplace a new element in the heap and returns its index. | |
| Size | size () const noexcept |
| Returns the number of elements in the heap. | |
| bool | empty () const noexcept |
| Indicates whether the heap is empty. | |
| bool | contains (const Val &) const |
| Indicates whether the heap contains a given value. | |
| std::string | toString () const |
Fine tuning | |
| Size | capacity () const noexcept |
| Returns the size of the internal structure storing the heap. | |
| void | resize (Size new_size) |
| Changes the size of the the internal structure storing the heap. | |
Private Member Functions | |
| Size | _restoreHeap_ () |
| After inserting an element at the end of the heap, restore heap property. | |
Private Attributes | |
| std::vector< Val > | _heap_ |
| An array storing all the elements of the heap. | |
| Size | _nb_elements_ {0} |
| The number of elements in the heap. | |
| Cmp | _cmp_ |
| Comparison function. | |
Heap data structure.
This structure is a basic heap data structure, i.e., it is a container in which elements are sorted according to a weak ordering. Given this ordering, we do have access in O(1) to the "smallest" element contained by the structure. Insertions and deletions of elements are processed in O(ln n), where n is the number of elements in the heap. In addition to the classical pop, top and push functions, Heap provide direct accessors to the elements in the heap (see operator[]). However, these can only be accessed in read-only mode.
|
explicit |
Basic constructor: creates an empty heap.
| 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 59 of file heap_tpl.h.
References Heap(), _cmp_, _heap_, and capacity().
Referenced by Heap(), Heap(), Heap(), Heap(), ~Heap(), operator=(), and operator=().
|
explicit |
Initializer list constructor.
| list | The initializer list. |
Definition at line 67 of file heap_tpl.h.
References Heap(), _heap_, and insert().
Copy constructor.
| from | The gum::Heap to copy. |
Definition at line 78 of file heap_tpl.h.
References Heap(), _cmp_, _heap_, and _nb_elements_.
|
noexcept |
Move constructor.
| from | The gum::Heap to move. |
Definition at line 86 of file heap_tpl.h.
References Heap(), _cmp_, _heap_, and _nb_elements_.
Class destructor.
Definition at line 95 of file heap_tpl.h.
References Heap().
After inserting an element at the end of the heap, restore heap property.
Definition at line 223 of file heap_tpl.h.
References _cmp_, _heap_, and _nb_elements_.
Referenced by emplace(), insert(), and insert().
Returns the size of the internal structure storing the heap.
Definition at line 154 of file heap_tpl.h.
References _heap_.
Referenced by Heap().
| INLINE bool gum::Heap< Val, Cmp >::contains | ( | const Val & | val | ) | const |
Indicates whether the heap contains a given value.
Definition at line 273 of file heap_tpl.h.
References _heap_, and _nb_elements_.
| Size gum::Heap< Val, Cmp >::emplace | ( | Args &&... | args | ) |
Emplace a new element in the heap and returns its index.
| Args | the type of the new element to emplace. |
| args | The new element to emplace. |
Definition at line 258 of file heap_tpl.h.
References _heap_, _nb_elements_, and _restoreHeap_().
Indicates whether the heap is empty.
Definition at line 267 of file heap_tpl.h.
References _nb_elements_.
Removes a given element from the heap (but does not return it).
If the element cannot be found, the function returns without throwing any exception.
| val | The element we wish to remove. If the heap contains several times this element, then the one with the smallest index is removed. |
Definition at line 194 of file heap_tpl.h.
References _heap_, _nb_elements_, and eraseByPos().
Removes the element positioned at "index" from the heap.
If the element cannot be found, the function returns without throwing any exception.
| index | represents the position of the element to be removed. This is computed as follows: suppose that the heap is a complete binary tree, that is, a binary tree where all levels are completely filled except, maybe, the last one and, in this case, the elements of this level are all to the left of the tree. Then parsing the tree from top to bottom and, for each level, from left to right, and assigning index 0 to the root of the tree and, incrementing the index by 1 each time we jump to another node, we get a unique index for each element. This is precisely what the index passed in argument of the function represents. |
| index | The index of the element to remove |
Definition at line 166 of file heap_tpl.h.
References _cmp_, _heap_, and _nb_elements_.
Referenced by erase(), eraseTop(), and pop().
Removes the top of the heap (but does not return it).
If the heap is empty, it does nothing (in particular, it does not throw any exception).
Definition at line 205 of file heap_tpl.h.
References _nb_elements_, and eraseByPos().
inserts a new element (actually a copy) in the heap and returns its index
| val | The element to insert. |
Definition at line 239 of file heap_tpl.h.
References _heap_, _nb_elements_, and _restoreHeap_().
Referenced by Heap(), gum::learning::Miic::findBestContributor_(), and gum::learning::SimpleMiic::findBestContributor_().
Inserts a new element (by moving it) in the heap and returns its index.
| val | The element to insert. |
Definition at line 248 of file heap_tpl.h.
References _heap_, _nb_elements_, and _restoreHeap_().
| Heap< Val, Cmp > & gum::Heap< Val, Cmp >::operator= | ( | const Heap< Val, 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 heap becomes empty. An exception is then thrown.
| from | The gum::Heap to copy. |
Definition at line 102 of file heap_tpl.h.
References Heap(), _cmp_, _heap_, and _nb_elements_.
|
noexcept |
Move 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 heap becomes empty. An exception is then thrown.
| from | The gum::Heap to move. |
Definition at line 127 of file heap_tpl.h.
References Heap(), _cmp_, _heap_, and _nb_elements_.
| INLINE const Val & gum::Heap< Val, Cmp >::operator[] | ( | Size | index_elt | ) | const |
Returns the element at index index_elt from the heap.
| index_elt | The index of the element to return. |
| NotFound | exception is thrown if there is no element at position "index_elt". |
Definition at line 282 of file heap_tpl.h.
References _heap_, _nb_elements_, and GUM_ERROR.
Removes the top element from the heap and return it.
| NotFound | exception is thrown if the heap is empty. |
Definition at line 213 of file heap_tpl.h.
References _heap_, _nb_elements_, eraseByPos(), and GUM_ERROR.
Referenced by gum::learning::Miic::iteration_(), and gum::learning::SimpleMiic::iteration_().
Changes the size of the the internal structure storing the heap.
If the new size does not enable the heap to contain the elements it currently contains, then the resizing does not occur.
| new_size | The gum::Heap new size. |
Definition at line 160 of file heap_tpl.h.
References _heap_, and _nb_elements_.
Returns the number of elements in the heap.
Definition at line 148 of file heap_tpl.h.
References _nb_elements_.
Referenced by gum::learning::Miic::iteration_(), and gum::learning::SimpleMiic::iteration_().
Returns the element at the top of the heap.
| NotFound | exception is thrown if the heap is empty. |
Definition at line 140 of file heap_tpl.h.
References _heap_, _nb_elements_, and GUM_ERROR.
Referenced by gum::learning::Miic::iteration_(), and gum::learning::SimpleMiic::iteration_().
Definition at line 291 of file heap_tpl.h.
References _heap_, and _nb_elements_.
Referenced by gum::operator<<().
Comparison function.
Definition at line 374 of file heap.h.
Referenced by Heap(), Heap(), Heap(), _restoreHeap_(), eraseByPos(), operator=(), and operator=().
|
private |
An array storing all the elements of the heap.
Definition at line 368 of file heap.h.
Referenced by Heap(), Heap(), Heap(), Heap(), _restoreHeap_(), capacity(), contains(), emplace(), erase(), eraseByPos(), insert(), insert(), operator=(), operator=(), operator[](), pop(), resize(), top(), and toString().
|
private |
The number of elements in the heap.
Definition at line 371 of file heap.h.
Referenced by Heap(), Heap(), _restoreHeap_(), contains(), emplace(), empty(), erase(), eraseByPos(), eraseTop(), insert(), insert(), operator=(), operator=(), operator[](), pop(), resize(), size(), top(), and toString().