aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::Heap< Val, Cmp, Alloc > Class Template Reference

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...
 

Detailed Description

template<typename Val, typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
class gum::Heap< Val, Cmp, Alloc >

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.

Usage example:
// create a heap of integers, the top element is the smallest element
Heap<int> heap1;
// create a heap of floats, the top element is the greatest
Heap<float> heap2 (std::greater<float>());
// insert elements into the heap
heap1.insert (8);
heap1.insert (10);
heap1.insert (2);
heap1.insert (23);
heap1.insert (24);
heap1.insert (10);
heap1.insert (10);
// copy a heap into another
Heap<int> heap2 = heap1;
// get the number of elements of the heap
std::cerr << heap2.size() << std::endl;
// get the top element but do not remove it
std::cerr << heap2.top() << std::endl;
// get and remove the top element of the heap
std::cerr << heap2.pop() << std::endl;
// remove the top element but do not return it (faster than above)
heap2.eraseTop();
// erase in heap1 value 8
heap1.eraseByVal (8);
// erase element at position 3 (see function erase for the
// meaning of position 3)
heap1.erase (3);
// get the element at position 3
heap1[3];
// check whether element 24 belongs to the heap
heap1.contains (24);
Template Parameters
ValThe gum::Heap values type.
CmpThe comperator used to sort elements in the gum::Heap.
AllocThe allocator of elements stored in the gum::Heap.

Definition at line 47 of file heap.h.

Member Typedef Documentation

◆ allocator_type

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::allocator_type = Alloc

Types for STL compliance.

Definition at line 134 of file heap.h.

◆ const_pointer

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::const_pointer = const Val*

Types for STL compliance.

Definition at line 131 of file heap.h.

◆ const_reference

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::const_reference = const Val&

Types for STL compliance.

Definition at line 129 of file heap.h.

◆ difference_type

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 133 of file heap.h.

◆ pointer

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::pointer = Val*

Types for STL compliance.

Definition at line 130 of file heap.h.

◆ reference

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::reference = Val&

Types for STL compliance.

Definition at line 128 of file heap.h.

◆ size_type

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::size_type = std::size_t

Types for STL compliance.

Definition at line 132 of file heap.h.

◆ value_type

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
using gum::Heap< Val, Cmp, Alloc >::value_type = Val

Types for STL compliance.

Definition at line 127 of file heap.h.

Constructor & Destructor Documentation

◆ Heap() [1/5]

template<typename Val , typename Cmp , typename Alloc >
gum::Heap< Val, Cmp, Alloc >::Heap ( Cmp  compare = Cmp(),
Size  capacity = GUM_HEAP_DEFAULT_CAPACITY 
)
explicit

Basic constructor: creates an empty heap.

Parameters
comparea 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.
capacitythe 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.

38  : cmp__(compare) {
39  heap__.reserve(capacity);
40 
41  // for debugging purposes
42  GUM_CONSTRUCTOR(Heap);
43  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size capacity() const noexcept
Returns the size of the internal structure storing the heap.
Definition: heap_tpl.h:184

◆ Heap() [2/5]

template<typename Val , typename Cmp , typename Alloc >
gum::Heap< Val, Cmp, Alloc >::Heap ( std::initializer_list< Val >  list)
explicit

Initializer list constructor.

Parameters
listThe initializer list.

Definition at line 47 of file heap_tpl.h.

47  {
48  heap__.reserve(list.size());
49  for (const auto& elt: list) {
50  insert(elt);
51  }
52 
53  // for debugging purposes
54  GUM_CONSTRUCTOR(Heap);
55  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size insert(const Val &val)
inserts a new element (actually a copy) in the heap and returns its index
Definition: heap_tpl.h:269

◆ Heap() [3/5]

template<typename Val , typename Cmp , typename Alloc >
gum::Heap< Val, Cmp, Alloc >::Heap ( const Heap< Val, Cmp, Alloc > &  from)

Copy constructor.

Parameters
fromThe gum::Heap to copy.

Definition at line 59 of file heap_tpl.h.

59  :
60  heap__(from.heap__), nb_elements__(from.nb_elements__), cmp__(from.cmp__) {
61  // for debugging purposes
62  GUM_CONS_CPY(Heap);
63  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ Heap() [4/5]

template<typename Val , typename Cmp , typename Alloc >
template<typename OtherAlloc >
gum::Heap< Val, Cmp, Alloc >::Heap ( const Heap< Val, Cmp, OtherAlloc > &  from)

Generalized copy constructor.

Template Parameters
OtherAllocThe other gum::Heap allocator.
Parameters
fromThe gum::Heap to copy.

Definition at line 68 of file heap_tpl.h.

68  :
69  nb_elements__(from.nb_elements__), cmp__(from.cmp__) {
70  heap__.reserve(nb_elements__);
71 
72  // copy the elements of from.heap__
73  for (unsigned i = 0; i < nb_elements__; ++i)
74  heap__.push_back(from.heap__[i]);
75 
76  // for debugging purposes
77  GUM_CONS_CPY(Heap);
78  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ Heap() [5/5]

template<typename Val , typename Cmp , typename Alloc >
gum::Heap< Val, Cmp, Alloc >::Heap ( Heap< Val, Cmp, Alloc > &&  from)
noexcept

Move constructor.

Parameters
fromThe gum::Heap to move.

Definition at line 82 of file heap_tpl.h.

82  :
83  heap__(std::move(from.heap__)), nb_elements__(std::move(from.nb_elements__)),
84  cmp__(std::move(from.cmp__)) {
85  // for debugging purposes
86  GUM_CONS_MOV(Heap);
87  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ ~Heap()

template<typename Val , typename Cmp , typename Alloc >
gum::Heap< Val, Cmp, Alloc >::~Heap ( )

Class destructor.

Definition at line 91 of file heap_tpl.h.

91  {
92  // for debugging purposes
93  GUM_DESTRUCTOR(Heap);
94  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38

Member Function Documentation

◆ capacity()

template<typename Val , typename Cmp , typename Alloc >
INLINE Size gum::Heap< Val, Cmp, Alloc >::capacity ( ) const
noexcept

Returns the size of the internal structure storing the heap.

Returns
Returns the size of the internal structure storing the heap.

Definition at line 184 of file heap_tpl.h.

184  {
185  return Size(heap__.size());
186  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ contains()

template<typename Val , typename Cmp , typename Alloc >
INLINE bool gum::Heap< Val, Cmp, Alloc >::contains ( const Val &  val) const

Indicates whether the heap contains a given value.

Returns
Indicates whether the heap contains a given value.

Definition at line 303 of file heap_tpl.h.

303  {
304  for (Size i = 0; i < nb_elements__; ++i)
305  if (heap__[i] == val) return true;
306 
307  return false;
308  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ emplace()

template<typename Val , typename Cmp , typename Alloc >
template<typename... Args>
Size gum::Heap< Val, Cmp, Alloc >::emplace ( Args &&...  args)

Emplace a new element in the heap and returns its index.

Template Parameters
Argsthe type of the new element to emplace.
Parameters
argsThe new element to emplace.
Returns
The emplaced element position in the gum::Heap.

Definition at line 288 of file heap_tpl.h.

288  {
289  // create a new element at the end of the heap
290  heap__.emplace_back(std::forward< Args >(args)...);
291  ++nb_elements__;
292  return restoreHeap__();
293  }
Size restoreHeap__()
After inserting an element at the end of the heap, restore heap property.
Definition: heap_tpl.h:253
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ empty()

template<typename Val , typename Cmp , typename Alloc >
INLINE bool gum::Heap< Val, Cmp, Alloc >::empty ( ) const
noexcept

Indicates whether the heap is empty.

Returns
Indicates whether the heap is empty.

Definition at line 297 of file heap_tpl.h.

297  {
298  return (nb_elements__ == 0);
299  }
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ erase()

template<typename Val , typename Cmp , typename Alloc >
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.

Parameters
valThe 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 224 of file heap_tpl.h.

224  {
225  // find val in the heap
226  for (Size i = 0; i < nb_elements__; ++i)
227  if (heap__[i] == val) {
228  eraseByPos(i);
229  break;
230  }
231  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
void eraseByPos(Size index)
Removes the element positioned at "index" from the heap.
Definition: heap_tpl.h:196

◆ eraseByPos()

template<typename Val , typename Cmp , typename Alloc >
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.

Parameters
indexrepresents 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.
indexThe index of the element to remove

Definition at line 196 of file heap_tpl.h.

196  {
197  if (index >= nb_elements__) return;
198 
199  // remove the element and put the last element in its place
200  Val last = std::move(heap__[nb_elements__ - 1]);
201  heap__.pop_back();
202  --nb_elements__;
203 
204  if (!nb_elements__ || (index == nb_elements__)) return;
205 
206  // restore the heap property
207  Size i = index;
208 
209  for (Size j = (index << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
210  // let j be the max child
211  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1], heap__[j])) ++j;
212 
213  // if "last" is smaller than heap__[j], "last" must be stored at index i
214  if (cmp__(last, heap__[j])) break;
215 
216  heap__[i] = std::move(heap__[j]);
217  }
218 
219  heap__[i] = std::move(last);
220  }
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ eraseTop()

template<typename Val , typename Cmp , typename Alloc >
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 235 of file heap_tpl.h.

235  {
236  // if the heap is empty, do nothing
237  if (!nb_elements__) return;
238  eraseByPos(0);
239  }
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
void eraseByPos(Size index)
Removes the element positioned at "index" from the heap.
Definition: heap_tpl.h:196

◆ insert() [1/2]

template<typename Val , typename Cmp , typename Alloc >
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

Parameters
valThe element to insert.
Returns
The inserted element position in the gum::Heap.

Definition at line 269 of file heap_tpl.h.

269  {
270  // create a new element at the end of the heap
271  heap__.push_back(val);
272  ++nb_elements__;
273  return restoreHeap__();
274  }
Size restoreHeap__()
After inserting an element at the end of the heap, restore heap property.
Definition: heap_tpl.h:253
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ insert() [2/2]

template<typename Val , typename Cmp , typename Alloc >
Size gum::Heap< Val, Cmp, Alloc >::insert ( Val &&  val)

Inserts a new element (by moving it) in the heap and returns its index.

Parameters
valThe element to insert.
Returns
The inserted element position in the gum::Heap.

Definition at line 278 of file heap_tpl.h.

278  {
279  // create a new element at the end of the heap
280  heap__.push_back(std::move(val));
281  ++nb_elements__;
282  return restoreHeap__();
283  }
Size restoreHeap__()
After inserting an element at the end of the heap, restore heap property.
Definition: heap_tpl.h:253
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ operator=() [1/3]

template<typename Val , typename Cmp , typename Alloc >
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.

Parameters
fromThe gum::Heap to copy.

Definition at line 99 of file heap_tpl.h.

99  {
100  // avoid self assignment
101  if (this != &from) {
102  try {
103  heap__ = from.heap__;
104  } catch (...) {
105  heap__.clear();
106  nb_elements__ = 0;
107 
108  throw;
109  }
110 
111  // set the comparison function
112  cmp__ = from.cmp__;
113  nb_elements__ = from.nb_elements__;
114 
115  // for debugging purposes
116  GUM_OP_CPY(Heap);
117  }
118 
119  return *this;
120  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ operator=() [2/3]

template<typename Val , typename Cmp , typename Alloc >
template<typename OtherAlloc >
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.

Template Parameters
OtherAllocThe other gum::Heap allocator.
Parameters
fromThe gum::Heap to copy.

Definition at line 126 of file heap_tpl.h.

126  {
127  // avoid self assignment
128  if (this != &from) {
129  try {
130  heap__.clear();
131 
132  if (heap__.capacity() < from.nb_elements__)
133  heap__.reserve(from.nb_elements__);
134 
135  for (unsigned int i = 0; i < from.nb_elements__; ++i) {
136  heap__.push_back(from.heap__[i]);
137  }
138  } catch (...) {
139  heap__.clear();
140  nb_elements__ = 0;
141  throw;
142  }
143 
144  cmp__ = from.cmp__;
145  nb_elements__ = from.nb_elements__;
146 
147  // for debugging purposes
148  GUM_OP_CPY(Heap);
149  }
150 
151  return *this;
152  }
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition: heap_tpl.h:38
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ operator=() [3/3]

template<typename Val , typename Cmp , typename Alloc >
Heap< Val, Cmp, Alloc > & gum::Heap< Val, Cmp, Alloc >::operator= ( Heap< Val, Cmp, Alloc > &&  from)
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.

Parameters
fromThe gum::Heap to move.

Definition at line 157 of file heap_tpl.h.

157  {
158  // avoid self assignment
159  if (this != &from) {
160  heap__ = std::move(from.heap__);
161  nb_elements__ = std::move(from.nb_elements__);
162  cmp__ = std::move(from.cmp__);
163  }
164 
165  return *this;
166  }
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ operator[]()

template<typename Val , typename Cmp , typename Alloc >
INLINE const Val & gum::Heap< Val, Cmp, Alloc >::operator[] ( Size  index_elt) const

Returns the element at index index_elt from the heap.

Parameters
index_eltThe index of the element to return.
Returns
Returns the element at index index_elt from the heap.
Exceptions
NotFoundexception is thrown if there is no element at position "index_elt".

Definition at line 312 of file heap_tpl.h.

312  {
313  // check if the element exists
314  if (index >= nb_elements__) {
315  GUM_ERROR(NotFound, "not enough elements in the heap");
316  }
317 
318  return heap__[index];
319  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ pop()

template<typename Val , typename Cmp , typename Alloc >
INLINE Val gum::Heap< Val, Cmp, Alloc >::pop ( )

Removes the top element from the heap and return it.

Returns
Returns the top element from the heap.
Exceptions
NotFoundexception is thrown if the heap is empty.

Definition at line 243 of file heap_tpl.h.

243  {
244  if (!nb_elements__) { GUM_ERROR(NotFound, "empty heap"); }
245 
246  Val v = heap__[0];
247  eraseByPos(0);
248  return v;
249  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
void eraseByPos(Size index)
Removes the element positioned at "index" from the heap.
Definition: heap_tpl.h:196
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ resize()

template<typename Val , typename Cmp , typename Alloc >
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.

Parameters
new_sizeThe gum::Heap new size.

Definition at line 190 of file heap_tpl.h.

190  {
191  if (new_size > nb_elements__) heap__.reserve(new_size);
192  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ restoreHeap__()

template<typename Val , typename Cmp , typename Alloc >
Size gum::Heap< Val, Cmp, Alloc >::restoreHeap__ ( )
private

After inserting an element at the end of the heap, restore heap property.

Definition at line 253 of file heap_tpl.h.

253  {
254  // get the element at the end of the heap
255  Size i = nb_elements__ - 1;
256  Val v = std::move(heap__[i]);
257 
258  // restore the heap property
259  for (Size j = (i - 1) >> 1; i && cmp__(v, heap__[j]); i = j, j = (j - 1) >> 1)
260  heap__[i] = std::move(heap__[j]);
261 
262  heap__[i] = std::move(v);
263 
264  return i;
265  }
Cmp cmp__
Comparison function.
Definition: heap.h:379
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ size()

template<typename Val , typename Cmp , typename Alloc >
INLINE Size gum::Heap< Val, Cmp, Alloc >::size ( ) const
noexcept

Returns the number of elements in the heap.

Returns
Returns the number of elements in the heap.

Definition at line 178 of file heap_tpl.h.

178  {
179  return nb_elements__;
180  }
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376

◆ top()

template<typename Val , typename Cmp , typename Alloc >
INLINE const Val & gum::Heap< Val, Cmp, Alloc >::top ( ) const

Returns the element at the top of the heap.

Returns
Returns the element at the top of the heap.
Exceptions
NotFoundexception is thrown if the heap is empty.

Definition at line 170 of file heap_tpl.h.

170  {
171  if (!nb_elements__) { GUM_ERROR(NotFound, "empty heap"); }
172 
173  return heap__[0];
174  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ toString()

template<typename Val , typename Cmp , typename Alloc >
std::string gum::Heap< Val, Cmp, Alloc >::toString ( ) const
Returns
Displays the content of the heap.
Returns a std::string representing the content of the heap.

Definition at line 323 of file heap_tpl.h.

323  {
324  bool deja = false;
325  std::stringstream stream;
326  stream << "[";
327 
328  for (Size i = 0; i != nb_elements__; ++i, deja = true) {
329  if (deja) stream << " , ";
330 
331  stream << heap__[i];
332  }
333 
334  stream << "]";
335 
336  return stream.str();
337  }
std::vector< Val, Alloc > heap__
An array storing all the elements of the heap.
Definition: heap.h:373
Size nb_elements__
The number of elements in the heap.
Definition: heap.h:376
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

Member Data Documentation

◆ cmp__

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
Cmp gum::Heap< Val, Cmp, Alloc >::cmp__
private

Comparison function.

Definition at line 379 of file heap.h.

◆ heap__

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
std::vector< Val, Alloc > gum::Heap< Val, Cmp, Alloc >::heap__
private

An array storing all the elements of the heap.

Definition at line 373 of file heap.h.

◆ nb_elements__

template<typename Val , typename Cmp = std::less< Val >, typename Alloc = std::allocator< Val >>
Size gum::Heap< Val, Cmp, Alloc >::nb_elements__ {0}
private

The number of elements in the heap.

Definition at line 376 of file heap.h.


The documentation for this class was generated from the following files: