aGrUM  0.20.3
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 132 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 129 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 127 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 131 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 128 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 126 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 130 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 125 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  GUM_CONSTRUCTOR(Heap);
42  }
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:371
Size capacity() const noexcept
Returns the size of the internal structure storing the heap.
Definition: heap_tpl.h:180
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 46 of file heap_tpl.h.

46  {
47  _heap_.reserve(list.size());
48  for (const auto& elt: list) {
49  insert(elt);
50  }
51 
52  GUM_CONSTRUCTOR(Heap);
53  }
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:371
Size insert(const Val &val)
inserts a new element (actually a copy) in the heap and returns its index
Definition: heap_tpl.h:265

◆ 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 57 of file heap_tpl.h.

57  :
58  _heap_(from._heap_), _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
59  // for debugging purposes
60  GUM_CONS_CPY(Heap);
61  }
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:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 66 of file heap_tpl.h.

66  :
67  _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
68  _heap_.reserve(_nb_elements_);
69 
70  // copy the elements of from. _heap_
71  for (unsigned i = 0; i < _nb_elements_; ++i)
72  _heap_.push_back(from._heap_[i]);
73 
74  // for debugging purposes
75  GUM_CONS_CPY(Heap);
76  }
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:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 80 of file heap_tpl.h.

80  :
81  _heap_(std::move(from._heap_)), _nb_elements_(std::move(from._nb_elements_)),
82  _cmp_(std::move(from._cmp_)) {
83  // for debugging purposes
84  GUM_CONS_MOV(Heap);
85  }
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:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ ~Heap()

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

Class destructor.

Definition at line 89 of file heap_tpl.h.

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

Member Function Documentation

◆ _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 249 of file heap_tpl.h.

249  {
250  // get the element at the end of the heap
251  Size i = _nb_elements_ - 1;
252  Val v = std::move(_heap_[i]);
253 
254  // restore the heap property
255  for (Size j = (i - 1) >> 1; i && _cmp_(v, _heap_[j]); i = j, j = (j - 1) >> 1)
256  _heap_[i] = std::move(_heap_[j]);
257 
258  _heap_[i] = std::move(v);
259 
260  return i;
261  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 180 of file heap_tpl.h.

180  {
181  return Size(_heap_.size());
182  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
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 299 of file heap_tpl.h.

299  {
300  for (Size i = 0; i < _nb_elements_; ++i)
301  if (_heap_[i] == val) return true;
302 
303  return false;
304  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
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 284 of file heap_tpl.h.

284  {
285  // create a new element at the end of the heap
286  _heap_.emplace_back(std::forward< Args >(args)...);
287  ++_nb_elements_;
288  return _restoreHeap_();
289  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Size _restoreHeap_()
After inserting an element at the end of the heap, restore heap property.
Definition: heap_tpl.h:249

◆ 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 293 of file heap_tpl.h.

293  {
294  return (_nb_elements_ == 0);
295  }
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374

◆ 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 220 of file heap_tpl.h.

220  {
221  // find val in the heap
222  for (Size i = 0; i < _nb_elements_; ++i)
223  if (_heap_[i] == val) {
224  eraseByPos(i);
225  break;
226  }
227  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
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:192

◆ 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 192 of file heap_tpl.h.

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

◆ 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 231 of file heap_tpl.h.

231  {
232  // if the heap is empty, do nothing
233  if (!_nb_elements_) return;
234  eraseByPos(0);
235  }
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
void eraseByPos(Size index)
Removes the element positioned at "index" from the heap.
Definition: heap_tpl.h:192

◆ 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 265 of file heap_tpl.h.

265  {
266  // create a new element at the end of the heap
267  _heap_.push_back(val);
268  ++_nb_elements_;
269  return _restoreHeap_();
270  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Size _restoreHeap_()
After inserting an element at the end of the heap, restore heap property.
Definition: heap_tpl.h:249

◆ 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 274 of file heap_tpl.h.

274  {
275  // create a new element at the end of the heap
276  _heap_.push_back(std::move(val));
277  ++_nb_elements_;
278  return _restoreHeap_();
279  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Size _restoreHeap_()
After inserting an element at the end of the heap, restore heap property.
Definition: heap_tpl.h:249

◆ 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 96 of file heap_tpl.h.

96  {
97  // avoid self assignment
98  if (this != &from) {
99  try {
100  _heap_ = from._heap_;
101  } catch (...) {
102  _heap_.clear();
103  _nb_elements_ = 0;
104 
105  throw;
106  }
107 
108  // set the comparison function
109  _cmp_ = from._cmp_;
110  _nb_elements_ = from._nb_elements_;
111 
112  // for debugging purposes
113  GUM_OP_CPY(Heap);
114  }
115 
116  return *this;
117  }
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:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 123 of file heap_tpl.h.

123  {
124  // avoid self assignment
125  if (this != &from) {
126  try {
127  _heap_.clear();
128 
129  if (_heap_.capacity() < from._nb_elements_) _heap_.reserve(from._nb_elements_);
130 
131  for (unsigned int i = 0; i < from._nb_elements_; ++i) {
132  _heap_.push_back(from._heap_[i]);
133  }
134  } catch (...) {
135  _heap_.clear();
136  _nb_elements_ = 0;
137  throw;
138  }
139 
140  _cmp_ = from._cmp_;
141  _nb_elements_ = from._nb_elements_;
142 
143  // for debugging purposes
144  GUM_OP_CPY(Heap);
145  }
146 
147  return *this;
148  }
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:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 153 of file heap_tpl.h.

153  {
154  // avoid self assignment
155  if (this != &from) {
156  _heap_ = std::move(from._heap_);
157  _nb_elements_ = std::move(from._nb_elements_);
158  _cmp_ = std::move(from._cmp_);
159  }
160 
161  return *this;
162  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
Cmp _cmp_
Comparison function.
Definition: heap.h:377

◆ 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 308 of file heap_tpl.h.

308  {
309  // check if the element exists
310  if (index >= _nb_elements_) { GUM_ERROR(NotFound, "not enough elements in the heap") }
311 
312  return _heap_[index];
313  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 239 of file heap_tpl.h.

239  {
240  if (!_nb_elements_) { GUM_ERROR(NotFound, "empty heap") }
241 
242  Val v = _heap_[0];
243  eraseByPos(0);
244  return v;
245  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
void eraseByPos(Size index)
Removes the element positioned at "index" from the heap.
Definition: heap_tpl.h:192
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 186 of file heap_tpl.h.

186  {
187  if (new_size > _nb_elements_) _heap_.reserve(new_size);
188  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374

◆ 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 174 of file heap_tpl.h.

174  {
175  return _nb_elements_;
176  }
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374

◆ 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 166 of file heap_tpl.h.

166  {
167  if (!_nb_elements_) { GUM_ERROR(NotFound, "empty heap") }
168 
169  return _heap_[0];
170  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 317 of file heap_tpl.h.

317  {
318  bool deja = false;
319  std::stringstream stream;
320  stream << "[";
321 
322  for (Size i = 0; i != _nb_elements_; ++i, deja = true) {
323  if (deja) stream << " , ";
324 
325  stream << _heap_[i];
326  }
327 
328  stream << "]";
329 
330  return stream.str();
331  }
std::vector< Val, Alloc > _heap_
An array storing all the elements of the heap.
Definition: heap.h:371
Size _nb_elements_
The number of elements in the heap.
Definition: heap.h:374
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 377 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 371 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 374 of file heap.h.


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