![]() |
aGrUM
0.16.0
|
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/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 39 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap.
|
explicit |
Initializer list constructor.
list | The initializer list. |
Definition at line 48 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::insert().
gum::Heap< Val, Cmp, Alloc >::Heap | ( | const Heap< Val, Cmp, Alloc > & | from | ) |
Copy constructor.
from | The gum::Heap to copy. |
Definition at line 60 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 69 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
|
noexcept |
Move constructor.
from | The gum::Heap to move. |
Definition at line 83 of file heap_tpl.h.
Class destructor.
Definition at line 92 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::operator=().
|
private |
After inserting an element at the end of the heap, restore heap property.
Definition at line 254 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__cmp, gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
Referenced by gum::Heap< Val, Cmp, Alloc >::emplace(), and gum::Heap< Val, Cmp, Alloc >::insert().
|
noexcept |
Returns the size of the internal structure storing the heap.
Definition at line 185 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap.
INLINE bool gum::Heap< Val, Cmp, Alloc >::contains | ( | const Val & | val | ) | const |
Indicates whether the heap contains a given value.
Definition at line 304 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
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 289 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and gum::Heap< Val, Cmp, Alloc >::__restoreHeap().
|
noexcept |
Indicates whether the heap is empty.
Definition at line 298 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__nb_elements.
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 225 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and gum::Heap< Val, Cmp, Alloc >::eraseByPos().
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 197 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__cmp, gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
Referenced by gum::Heap< Val, Cmp, Alloc >::erase(), gum::Heap< Val, Cmp, Alloc >::eraseTop(), and gum::Heap< Val, Cmp, Alloc >::pop().
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 236 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__nb_elements, and gum::Heap< Val, Cmp, Alloc >::eraseByPos().
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 270 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and gum::Heap< Val, Cmp, Alloc >::__restoreHeap().
Referenced by gum::Heap< Val, Cmp, Alloc >::Heap().
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 279 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and gum::Heap< Val, Cmp, Alloc >::__restoreHeap().
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 100 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__cmp, gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
Referenced by gum::Heap< Val, Cmp, Alloc >::operator=(), and gum::Heap< Val, Cmp, Alloc >::~Heap().
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 127 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__cmp, gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and gum::Heap< Val, Cmp, Alloc >::operator=().
|
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 158 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__cmp, gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
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 313 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and GUM_ERROR.
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 244 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, gum::Heap< Val, Cmp, Alloc >::eraseByPos(), and GUM_ERROR.
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 191 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
|
noexcept |
Returns the number of elements in the heap.
Definition at line 179 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__nb_elements.
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 171 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, gum::Heap< Val, Cmp, Alloc >::__nb_elements, and GUM_ERROR.
std::string gum::Heap< Val, Cmp, Alloc >::toString | ( | ) | const |
Definition at line 324 of file heap_tpl.h.
References gum::Heap< Val, Cmp, Alloc >::__heap, and gum::Heap< Val, Cmp, Alloc >::__nb_elements.
Referenced by gum::operator<<().
|
private |
Comparison function.
Definition at line 380 of file heap.h.
Referenced by gum::Heap< Val, Cmp, Alloc >::__restoreHeap(), gum::Heap< Val, Cmp, Alloc >::eraseByPos(), and gum::Heap< Val, Cmp, Alloc >::operator=().
|
private |
An array storing all the elements of the heap.
Definition at line 374 of file heap.h.
Referenced by gum::Heap< Val, Cmp, Alloc >::__restoreHeap(), gum::Heap< Val, Cmp, Alloc >::capacity(), gum::Heap< Val, Cmp, Alloc >::contains(), gum::Heap< Val, Cmp, Alloc >::emplace(), gum::Heap< Val, Cmp, Alloc >::erase(), gum::Heap< Val, Cmp, Alloc >::eraseByPos(), gum::Heap< Val, Cmp, Alloc >::Heap(), gum::Heap< Val, Cmp, Alloc >::insert(), gum::Heap< Val, Cmp, Alloc >::operator=(), gum::Heap< Val, Cmp, Alloc >::operator[](), gum::Heap< Val, Cmp, Alloc >::pop(), gum::Heap< Val, Cmp, Alloc >::resize(), gum::Heap< Val, Cmp, Alloc >::top(), and gum::Heap< Val, Cmp, Alloc >::toString().
|
private |
The number of elements in the heap.
Definition at line 377 of file heap.h.
Referenced by gum::Heap< Val, Cmp, Alloc >::__restoreHeap(), gum::Heap< Val, Cmp, Alloc >::contains(), gum::Heap< Val, Cmp, Alloc >::emplace(), gum::Heap< Val, Cmp, Alloc >::empty(), gum::Heap< Val, Cmp, Alloc >::erase(), gum::Heap< Val, Cmp, Alloc >::eraseByPos(), gum::Heap< Val, Cmp, Alloc >::eraseTop(), gum::Heap< Val, Cmp, Alloc >::Heap(), gum::Heap< Val, Cmp, Alloc >::insert(), gum::Heap< Val, Cmp, Alloc >::operator=(), gum::Heap< Val, Cmp, Alloc >::operator[](), gum::Heap< Val, Cmp, Alloc >::pop(), gum::Heap< Val, Cmp, Alloc >::resize(), gum::Heap< Val, Cmp, Alloc >::size(), gum::Heap< Val, Cmp, Alloc >::top(), and gum::Heap< Val, Cmp, Alloc >::toString().