![]() |
aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
|
Heap data structureThis structure is a basic heap data structure, i.e., it is a container in which elements are sorted according to a weak ordering. More...
#include <agrum/tools/core/heap.h>
Public Member Functions | |
Constructors / Destructors | |
Heap (Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY) | |
Basic constructor: creates an empty heap. More... | |
Heap (std::initializer_list< Val > list) | |
Initializer list constructor. More... | |
Heap (const Heap< Val, Cmp, Alloc > &from) | |
Copy constructor. More... | |
template<typename OtherAlloc > | |
Heap (const Heap< Val, Cmp, OtherAlloc > &from) | |
Generalized copy constructor. More... | |
Heap (Heap< Val, Cmp, Alloc > &&from) noexcept | |
Move constructor. More... | |
~Heap () | |
Class destructor. More... | |
Operators | |
Heap< Val, Cmp, Alloc > & | operator= (const Heap< Val, Cmp, Alloc > &from) |
Copy operator. More... | |
template<typename OtherAlloc > | |
Heap< Val, Cmp, Alloc > & | operator= (const Heap< Val, Cmp, OtherAlloc > &from) |
Generalized copy operator. More... | |
Heap< Val, Cmp, Alloc > & | operator= (Heap< Val, Cmp, Alloc > &&from) noexcept |
Move operator. More... | |
const Val & | operator[] (Size index_elt) const |
Returns the element at index index_elt from the heap. More... | |
Accessors / Modifiers | |
const Val & | top () const |
Returns the element at the top of the heap. More... | |
Val | pop () |
Removes the top element from the heap and return it. More... | |
void | eraseTop () |
Removes the top of the heap (but does not return it). More... | |
void | eraseByPos (Size index) |
Removes the element positioned at "index" from the heap. More... | |
void | erase (const Val &val) |
Removes a given element from the heap (but does not return it). More... | |
Size | insert (const Val &val) |
inserts a new element (actually a copy) in the heap and returns its index More... | |
Size | insert (Val &&val) |
Inserts a new element (by moving it) in the heap and returns its index. More... | |
template<typename... Args> | |
Size | emplace (Args &&... args) |
Emplace a new element in the heap and returns its index. More... | |
Size | size () const noexcept |
Returns the number of elements in the heap. More... | |
bool | empty () const noexcept |
Indicates whether the heap is empty. More... | |
bool | contains (const Val &) const |
Indicates whether the heap contains a given value. More... | |
std::string | toString () const |
Fine tuning | |
Size | capacity () const noexcept |
Returns the size of the internal structure storing the heap. More... | |
void | resize (Size new_size) |
Changes the size of the the internal structure storing the heap. More... | |
Public Types | |
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 | size_type = std::size_t |
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... | |
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.
using gum::Heap< Val, Cmp, Alloc >::allocator_type = Alloc |
using gum::Heap< Val, Cmp, Alloc >::const_pointer = const Val* |
using gum::Heap< Val, Cmp, Alloc >::const_reference = const Val& |
using gum::Heap< Val, Cmp, Alloc >::difference_type = std::ptrdiff_t |
using gum::Heap< Val, Cmp, Alloc >::value_type = Val |
|
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 38 of file heap_tpl.h.
|
explicit |
Initializer list constructor.
list | The initializer list. |
Definition at line 46 of file heap_tpl.h.
gum::Heap< Val, Cmp, Alloc >::Heap | ( | const Heap< Val, Cmp, Alloc > & | from | ) |
Copy constructor.
from | The gum::Heap to copy. |
Definition at line 57 of file heap_tpl.h.
gum::Heap< Val, Cmp, Alloc >::Heap | ( | const Heap< Val, Cmp, OtherAlloc > & | from | ) |
Generalized copy constructor.
OtherAlloc | The other gum::Heap allocator. |
from | The gum::Heap to copy. |
Definition at line 66 of file heap_tpl.h.
|
noexcept |
Move constructor.
from | The gum::Heap to move. |
Definition at line 80 of file heap_tpl.h.
Class destructor.
Definition at line 89 of file heap_tpl.h.
|
private |
After inserting an element at the end of the heap, restore heap property.
Definition at line 249 of file heap_tpl.h.
|
noexcept |
Returns the size of the internal structure storing the heap.
Definition at line 180 of file heap_tpl.h.
INLINE bool gum::Heap< Val, Cmp, Alloc >::contains | ( | const Val & | val | ) | const |
Indicates whether the heap contains a given value.
Definition at line 299 of file heap_tpl.h.
Size gum::Heap< Val, Cmp, Alloc >::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 284 of file heap_tpl.h.
|
noexcept |
Indicates whether the heap is empty.
Definition at line 293 of file heap_tpl.h.
INLINE void gum::Heap< Val, Cmp, Alloc >::erase | ( | const Val & | val | ) |
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 220 of file heap_tpl.h.
void gum::Heap< Val, Cmp, Alloc >::eraseByPos | ( | Size | index | ) |
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 192 of file heap_tpl.h.
INLINE void gum::Heap< Val, Cmp, Alloc >::eraseTop | ( | ) |
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 231 of file heap_tpl.h.
INLINE Size gum::Heap< Val, Cmp, Alloc >::insert | ( | const Val & | val | ) |
inserts a new element (actually a copy) in the heap and returns its index
val | The element to insert. |
Definition at line 265 of file heap_tpl.h.
Size gum::Heap< Val, Cmp, Alloc >::insert | ( | Val && | val | ) |
Inserts a new element (by moving it) in the heap and returns its index.
val | The element to insert. |
Definition at line 274 of file heap_tpl.h.
Heap< Val, Cmp, Alloc > & gum::Heap< Val, Cmp, Alloc >::operator= | ( | const Heap< Val, 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 heap becomes empty. An exception is then thrown.
from | The gum::Heap to copy. |
Definition at line 96 of file heap_tpl.h.
Heap< Val, Cmp, Alloc > & gum::Heap< Val, Cmp, Alloc >::operator= | ( | const Heap< Val, 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 heap becomes empty. An exception is then thrown.
OtherAlloc | The other gum::Heap allocator. |
from | The gum::Heap to copy. |
Definition at line 123 of file heap_tpl.h.
|
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 153 of file heap_tpl.h.
INLINE const Val & gum::Heap< Val, Cmp, Alloc >::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 308 of file heap_tpl.h.
INLINE Val gum::Heap< Val, Cmp, Alloc >::pop | ( | ) |
Removes the top element from the heap and return it.
NotFound | exception is thrown if the heap is empty. |
Definition at line 239 of file heap_tpl.h.
INLINE void gum::Heap< Val, Cmp, Alloc >::resize | ( | Size | new_size | ) |
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 186 of file heap_tpl.h.
|
noexcept |
Returns the number of elements in the heap.
Definition at line 174 of file heap_tpl.h.
INLINE const Val & gum::Heap< Val, Cmp, Alloc >::top | ( | ) | const |
Returns the element at the top of the heap.
NotFound | exception is thrown if the heap is empty. |
Definition at line 166 of file heap_tpl.h.
std::string gum::Heap< Val, Cmp, Alloc >::toString | ( | ) | const |
Definition at line 317 of file heap_tpl.h.
|
private |
|
private |