aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen > Class Template Reference

The internal class for representing priority queues. More...

#include <agrum/base/core/priorityQueue.h>

Collaboration diagram for gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >:

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 difference_type = std::ptrdiff_t
 Types for STL compliance.

Public Member Functions

template<typename... Args>
INLINE Size emplace (Args &&... args)
Operators
PriorityQueueImplementation< Val, Priority, Cmp, Gen > & operator= (const PriorityQueueImplementation< Val, Priority, Cmp, Gen > &from)
 Copy operator.
PriorityQueueImplementation< Val, Priority, Cmp, Gen > & operator= (PriorityQueueImplementation< Val, Priority, Cmp, Gen > &&from)
 Move operator.
const Val & operator[] (Size index_elt) const
 Returns the element at index "index_elt" from the priority queue.
Accessors / Modifiers
Size size () const noexcept
 Returns the number of elements in the priority queue.
bool empty () const noexcept
 Indicates whether the priority queue is empty.
bool contains (const Val &val) const
 Indicates whether the priority queue contains a given value.
const Val & top () const
 returns the element at the top of the priority queue
const Priority & topPriority () const
 Returns the priority of the top element.
Val pop ()
 Removes the top element from the priority queue and return it.
Size insert (const Val &val, const Priority &priority)
 Inserts a new (a copy) element in the priority queue.
Size insert (Val &&val, Priority &&priority)
 Inserts (by move) a new element in the priority queue.
template<typename... Args>
Size emplace (Args &&... args)
 Emplace a new element into the priority queue.
void eraseTop ()
 Removes the top of the priority queue (but does not return it).
void eraseByPos (Size index)
 Removes the element at position "index" from the priority queue.
void erase (const Val &val)
 Removes a given element from the priority queue (but does not return it).
Size setPriorityByPos (Size index, const Priority &new_priority)
 Modifies the priority of the element at position "index" of the queue.
Size setPriorityByPos (Size index, Priority &&new_priority)
 Modifies the priority of the element at position "index" of the queue.
void setPriority (const Val &elt, const Priority &new_priority)
 Modifies the priority of each instance of a given element.
void setPriority (const Val &elt, Priority &&new_priority)
 Modifies the priority of each instance of a given element.
const Priority & priority (const Val &elt) const
 Returns the priority of an instance of the value passed in argument.
const Priority & priorityByPos (Size index) const
 Returns the priority of the value passed in argument.
void clear ()
 Removes all the elements from the queue.
const HashTable< Val, Size > & allValues () const noexcept
 Returns a hashtable the keys of which are the values stored in the queue.
std::string toString () const
 Displays the content of the queue.
Fine tuning
Size capacity () const noexcept
 Returns the size of the internal structure storing the priority queue.
void resize (Size new_size)
 Changes the size of the internal structure storing the priority queue.

Private Member Functions

Constructors / Destructors
 PriorityQueueImplementation (Cmp compare, Size capacity)
 Basic constructor.
 PriorityQueueImplementation (std::initializer_list< std::pair< Val, Priority > > list)
 Initializer list constructor.
 PriorityQueueImplementation (const PriorityQueueImplementation< Val, Priority, Cmp, Gen > &from)
 Copy constructor.
 PriorityQueueImplementation (PriorityQueueImplementation< Val, Priority, Cmp, Gen > &&from)
 Move constructor.
 ~PriorityQueueImplementation ()
 Class destructor.

Private Attributes

std::vector< std::pair< Priority, const Val * > > _heap_
 An array storing all the elements of the heap as well as their score.
HashTable< Val, Size_indices_ {HashTableConst::default_size, true, true}
 A hashtable for quickly finding the elements by their value.
Size _nb_elements_ {0}
 The number of elements in the heap.
Cmp _cmp_
 Comparison function.

Friends

class PriorityQueue< Val, Priority, Cmp >
 All gum::PriorityQueue are friends with themselves.

Detailed Description

template<typename Val, typename Priority, typename Cmp, bool Gen>
class gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >

The internal class for representing priority queues.

Priority Queues have different implementations depending on the nature of the values they store. Basically, scalar values can lead to optimization of the code whereas general types like classes cannot. The current class is used for general types (and therefore for classes). The user shall not use directly the implementation but rather use the PriorityQueue class. The latter will be assigned the best implementation at compile time.

Template Parameters
ValThe values type.
PriorityThe priorities type.
CmpThe priorities comparator.
GenUsed for metaprogramation, for scalar and non-scalar priority queues.

Definition at line 100 of file priorityQueue.h.

Member Typedef Documentation

◆ const_pointer

template<typename Val, typename Priority, typename Cmp, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::const_pointer = const Val*

Types for STL compliance.

Definition at line 111 of file priorityQueue.h.

◆ const_reference

template<typename Val, typename Priority, typename Cmp, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::const_reference = const Val&

Types for STL compliance.

Definition at line 109 of file priorityQueue.h.

◆ difference_type

template<typename Val, typename Priority, typename Cmp, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 112 of file priorityQueue.h.

◆ pointer

template<typename Val, typename Priority, typename Cmp, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::pointer = Val*

Types for STL compliance.

Definition at line 110 of file priorityQueue.h.

◆ reference

template<typename Val, typename Priority, typename Cmp, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::reference = Val&

Types for STL compliance.

Definition at line 108 of file priorityQueue.h.

◆ value_type

template<typename Val, typename Priority, typename Cmp, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::value_type = Val

Types for STL compliance.

Definition at line 107 of file priorityQueue.h.

Constructor & Destructor Documentation

◆ PriorityQueueImplementation() [1/4]

template<typename Val, typename Priority, typename Cmp>
INLINE gum::PriorityQueueImplementation< Val, Priority, Cmp >::PriorityQueueImplementation ( Cmp compare,
Size capacity )
explicitprivate

Basic constructor.

Creates an empty priority queue.

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 60 of file priorityQueue_tpl.h.

62 : _indices_(capacity >> 1, true, true), _cmp_(compare) {
63 _heap_.reserve(capacity);
64
66 }
The internal class for representing priority queues.
Size capacity() const noexcept
Returns the size of the internal structure storing the priority queue.
PriorityQueueImplementation(Cmp compare, Size capacity)
Basic constructor.
HashTable< Val, Size > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val * > > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.

References PriorityQueueImplementation(), _cmp_, _heap_, _indices_, and capacity().

Referenced by PriorityQueueImplementation(), PriorityQueueImplementation(), PriorityQueueImplementation(), PriorityQueueImplementation(), ~PriorityQueueImplementation(), operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PriorityQueueImplementation() [2/4]

template<typename Val, typename Priority, typename Cmp>
gum::PriorityQueueImplementation< Val, Priority, Cmp >::PriorityQueueImplementation ( std::initializer_list< std::pair< Val, Priority > > list)
explicitprivate

Initializer list constructor.

The elements of the initializer list are pairs <Val,Priority>. The comparison function is the default one, i.e., std::less<Priority>.

Parameters
listThe initializer list.

Definition at line 70 of file priorityQueue_tpl.h.

71 :
72 _indices_(Size(list.size()) / 2, true, true) {
73 // fill the queue
74 _heap_.reserve(list.size());
75 for (const auto& elt: list) {
76 insert(elt.first, elt.second);
77 }
78
80 }
Size size() const noexcept
Returns the number of elements in the priority queue.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References PriorityQueueImplementation(), _heap_, _indices_, insert(), and size().

Here is the call graph for this function:

◆ PriorityQueueImplementation() [3/4]

template<typename Val, typename Priority, typename Cmp, bool Gen>
gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::PriorityQueueImplementation ( const PriorityQueueImplementation< Val, Priority, Cmp, Gen > & from)
private

Copy constructor.

Parameters
fromThe gum::PriorityQueueImplementation to copy.

Definition at line 84 of file priorityQueue_tpl.h.

85 :
88 // fill the heap structure
89 for (const auto& elt: _indices_) {
90 _heap_[elt.second].second = &(elt.first);
91 }
92
94 }
Size _nb_elements_
The number of elements in the heap.

References PriorityQueueImplementation(), _cmp_, _heap_, _indices_, and _nb_elements_.

Here is the call graph for this function:

◆ PriorityQueueImplementation() [4/4]

template<typename Val, typename Priority, typename Cmp, bool Gen>
gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::PriorityQueueImplementation ( PriorityQueueImplementation< Val, Priority, Cmp, Gen > && from)
private

Move constructor.

Parameters
fromThe gum::PriorityQueueImplementation to move.

Definition at line 98 of file priorityQueue_tpl.h.

References PriorityQueueImplementation(), _cmp_, _heap_, _indices_, and _nb_elements_.

Here is the call graph for this function:

◆ ~PriorityQueueImplementation()

template<typename Val, typename Priority, typename Cmp>
gum::PriorityQueueImplementation< Val, Priority, Cmp >::~PriorityQueueImplementation ( )
private

Class destructor.

Definition at line 107 of file priorityQueue_tpl.h.

References PriorityQueueImplementation().

Here is the call graph for this function:

Member Function Documentation

◆ allValues()

template<typename Val, typename Priority, typename Cmp>
INLINE const HashTable< Val, Size > & gum::PriorityQueueImplementation< Val, Priority, Cmp >::allValues ( ) const
noexcept

Returns a hashtable the keys of which are the values stored in the queue.

The keys of the hashtable correspond to the values stored in the priority queue and, for each key, the corresponding value is the index in the queue where we can find the key.

Returns
Returns a hashtable the keys of which are the values stored in the queue.

Definition at line 272 of file priorityQueue_tpl.h.

272 {
273 return reinterpret_cast< const HashTable< Val, Size >& >(_indices_);
274 }

References _indices_.

◆ capacity()

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

Returns the size of the internal structure storing the priority queue.

Returns
Returns the size of the internal structure storing the priority queue.

Definition at line 188 of file priorityQueue_tpl.h.

188 {
189 return Size(_heap_.capacity());
190 }

References _heap_.

Referenced by PriorityQueueImplementation().

Here is the caller graph for this function:

◆ clear()

template<typename Val, typename Priority, typename Cmp>
INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp >::clear ( )

Removes all the elements from the queue.

Definition at line 203 of file priorityQueue_tpl.h.

203 {
204 _nb_elements_ = 0;
205 _heap_.clear();
206 _indices_.clear();
207 }

References _heap_, _indices_, and _nb_elements_.

◆ contains()

template<typename Val, typename Priority, typename Cmp, bool Gen>
INLINE bool gum::PriorityQueueImplementation< Val, Priority, Cmp >::contains ( const Val & val) const

Indicates whether the priority queue contains a given value.

Parameters
valThe value to look for.
Returns
Returns true if val is in the priority queue.

Definition at line 365 of file priorityQueue_tpl.h.

365 {
366 return _indices_.exists(val);
367 }

References _indices_.

◆ emplace() [1/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
template<typename... Args>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::emplace ( Args &&... args)

Emplace a new element into the priority queue.

See gum::PriorityQueueImplementation::eraseByPos(Size) for more details about the index.

Template Parameters
ArgsThe emplace arguments types.
Parameters
argsThe emplace arguments.
Returns
Returns the index of the element inserted into the priority queue.
Exceptions
DuplicateElementRaised if the element already exists.

◆ emplace() [2/2]

template<typename Val, typename Priority, typename Cmp>
template<typename... Args>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp >::emplace ( Args &&... args)

◆ empty()

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

Indicates whether the priority queue is empty.

Returns
Returns true if the priority queue is empty.

Definition at line 358 of file priorityQueue_tpl.h.

358 {
359 return (_nb_elements_ == 0);
360 }

References _nb_elements_.

◆ erase()

template<typename Val, typename Priority, typename Cmp, bool Gen>
INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp >::erase ( const Val & val)

Removes a given element from the priority queue (but does not return it).

If the element cannot be found, the function returns without throwing any exception.

If the queue contains several times this element, then the one with the smallest index is removed.

Parameters
valthe element we wish to remove.

Definition at line 246 of file priorityQueue_tpl.h.

246 {
247 try {
249 } catch (NotFound const&) {}
250 }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

References _indices_, and eraseByPos().

Referenced by gum::BinaryJoinTreeConverterDefault::_convertClique_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ eraseByPos()

template<typename Val, typename Priority, typename Cmp>
void gum::PriorityQueueImplementation< Val, Priority, Cmp >::eraseByPos ( Size index)

Removes the element at position "index" from the priority queue.

If the element cannot be found, the function returns without throwing any exception.

The priority is computed as follows: suppose that the queue is a complete binary tree where all levels are completely filled except, eventually, the last one. In this case, the elements of the last level are all on the left of the tree.

We assign 0 to the root, then parsing the tree from top to bottom then from left to right we increment the index and assigned it to the current node. Doing so, we get a unique index for each element. This is precisely what the index passed in argument of the function represents.

Parameters
indexrepresents the position of the element to be removed.

Definition at line 211 of file priorityQueue_tpl.h.

211 {
212 if (index >= _nb_elements_) return;
213
214 // remove the element from the hashtable
215 _indices_.erase(*(_heap_[index].second));
216
217 // put the last element at the "index" location
219 _heap_.pop_back();
221
222 if (!_nb_elements_ || (index == _nb_elements_)) return;
223
224 // restore the heap property
225 Size i = index;
226
227 for (Size j = (index << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
228 // let j be the max child
229 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
230
231 // if "last" is lower than heap[j], "last" must be stored at index i
232 if (_cmp_(last.first, _heap_[j].first)) break;
233
234 // else pull up the jth node
236 _indices_[*(_heap_[i].second)] = i;
237 }
238
239 // put "last" back into the heap
241 _indices_[*(_heap_[i].second)] = i;
242 }

References _nb_elements_.

Referenced by erase(), eraseTop(), and pop().

Here is the caller graph for this function:

◆ eraseTop()

template<typename Val, typename Priority, typename Cmp>
INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp >::eraseTop ( )

Removes the top of the priority queue (but does not return it).

If the heap is empty, it does nothing (in particular, it does not throw any exception).

Definition at line 254 of file priorityQueue_tpl.h.

254 {
255 eraseByPos(0);
256 }

References eraseByPos().

Here is the call graph for this function:

◆ insert() [1/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::insert ( const Val & val,
const Priority & priority )

Inserts a new (a copy) element in the priority queue.

See gum::PriorityQueueImplementation::eraseByPos(Size) for more details about the index.

Parameters
valThe value to insert.
priorityThe value's priority in the queue.
Returns
Returns the index of the element inserted into the priority queue.
Exceptions
DuplicateElementRaised if the element already exists.

Definition at line 279 of file priorityQueue_tpl.h.

280 {
281 // create the entry in the indices hashtable (if the element already exists,
282 // _indices_.insert will raise a Duplicateelement exception)
284
285 try {
287 } catch (...) {
288 _indices_.erase(val);
289 throw;
290 }
291
294
295 // restore the heap property
296 Size i = _nb_elements_ - 1;
297
298 for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
299 i = j, j = (j - 1) >> 1) {
301 _indices_[*(_heap_[i].second)] = i;
302 }
303
304 // put the new bucket into the heap
305 _heap_[i].first = std::move(new_heap_val.first);
306 _heap_[i].second = &(new_elt.first);
307 new_elt.second = i;
308
309 return i;
310 }
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.

References _heap_, _indices_, and priority().

Referenced by PriorityQueueImplementation(), gum::StaticTriangulation::_computeRecursiveThinning_(), and gum::BinaryJoinTreeConverterDefault::_convertClique_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insert() [2/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::insert ( Val && val,
Priority && priority )

Inserts (by move) a new element in the priority queue.

See gum::PriorityQueueImplementation::eraseByPos(Size) for more details about the index.

Parameters
valThe value to insert.
priorityThe value's priority in the queue.
Returns
Returns the index of the element inserted into the priority queue.
Exceptions
DuplicateElementRaised if the element already exists.

Definition at line 314 of file priorityQueue_tpl.h.

315 {
316 // create the entry in the indices hashtable (if the element already exists,
317 // _indices_.insert will raise a Duplicateelement exception)
319
320 try {
322 } catch (...) {
323 _indices_.erase(new_elt.first);
324 throw;
325 }
326
329
330 // restore the heap property
331 Size i = _nb_elements_ - 1;
332
333 for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
334 i = j, j = (j - 1) >> 1) {
336 _indices_[*(_heap_[i].second)] = i;
337 }
338
339 // put the new bucket into the heap
340 _heap_[i].first = std::move(new_heap_val.first);
341 _heap_[i].second = &(new_elt.first);
342 new_elt.second = i;
343
344 return i;
345 }

References _heap_, _indices_, and priority().

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
PriorityQueueImplementation< Val, Priority, Cmp, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::operator= ( const PriorityQueueImplementation< Val, Priority, Cmp, Gen > & 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 priority queue becomes empty. An exception is then thrown.

Parameters
fromThe gum::PriorityQueueImplementation to copy.
Returns
Returns this gum::PriorityQueueImplementation.

Definition at line 114 of file priorityQueue_tpl.h.

115 {
116 // avoid self assignment
117 if (this != &from) {
119
120 try {
121 // set the comparison function
122 _cmp_ = from._cmp_;
123
124 // copy the indices and the heap
128
129 // restore the link between _indices_ and _heap_
130 for (const auto& elt: _indices_) {
131 _heap_[elt.second].second = &(elt.first);
132 }
133 } catch (...) {
134 _heap_.clear();
135 _indices_.clear();
136 _nb_elements_ = 0;
137
138 throw;
139 }
140 }
141
142 return *this;
143 }

References PriorityQueueImplementation(), _cmp_, _heap_, _indices_, and _nb_elements_.

Referenced by gum::PriorityQueue< Val, Priority, Cmp >::operator=(), and gum::PriorityQueue< Val, Priority, Cmp >::operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [2/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
INLINE PriorityQueueImplementation< Val, Priority, Cmp, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::operator= ( PriorityQueueImplementation< Val, Priority, Cmp, Gen > && from)

Move operator.

Parameters
fromThe gum::PriorityQueueImplementation to move.
Returns
Returns this gum::PriorityQueueImplementation.

Definition at line 148 of file priorityQueue_tpl.h.

149 {
150 // avoid self assignment
151 if (this != &from) {
153
158 }
159
160 return *this;
161 }

References PriorityQueueImplementation(), _cmp_, _heap_, and _indices_.

Here is the call graph for this function:

◆ operator[]()

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

Returns the element at index "index_elt" from the priority queue.

Parameters
index_eltThe index of the element to return.
Returns
Returns the element at index "index_elt" from the priority queue.
Exceptions
NotFoundRaised if the element does not exist.

Definition at line 372 of file priorityQueue_tpl.h.

372 {
373 if (index >= _nb_elements_) {
374 GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
375 }
376
377 return *(_heap_[index].second);
378 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _nb_elements_.

◆ pop()

template<typename Val, typename Priority, typename Cmp>
INLINE Val gum::PriorityQueueImplementation< Val, Priority, Cmp >::pop ( )

Removes the top element from the priority queue and return it.

Returns
Returns the top element from the priority queue.
Exceptions
NotFoundRaised if the queue is empty.

Definition at line 260 of file priorityQueue_tpl.h.

260 {
261 if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
262
263 Val v = *(_heap_[0].second);
264 eraseByPos(0);
265
266 return v;
267 }

References _heap_, _nb_elements_, eraseByPos(), and GUM_ERROR.

Referenced by gum::StaticTriangulation::_computeRecursiveThinning_(), and gum::BinaryJoinTreeConverterDefault::_convertClique_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ priority()

template<typename Val, typename Priority, typename Cmp, bool Gen>
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp >::priority ( const Val & elt) const

Returns the priority of an instance of the value passed in argument.

Parameters
eltThe element for which the priority is returned.
Returns
Returns the priority of an instance of the value passed in argument.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 505 of file priorityQueue_tpl.h.

505 {
506 return _heap_[_indices_[elt]].first;
507 }

References _heap_, and _indices_.

Referenced by gum::StaticTriangulation::_computeRecursiveThinning_(), insert(), and insert().

Here is the caller graph for this function:

◆ priorityByPos()

template<typename Val, typename Priority, typename Cmp>
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp >::priorityByPos ( Size index) const

Returns the priority of the value passed in argument.

Parameters
indexThe index of the value of which the priority is returned.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 512 of file priorityQueue_tpl.h.

512 {
513 if (index > _nb_elements_) {
514 GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
515 }
516 return _heap_[index].first;
517 }

References _heap_, _nb_elements_, and GUM_ERROR.

◆ resize()

template<typename Val, typename Priority, typename Cmp>
INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp >::resize ( Size new_size)

Changes the size of the internal structure storing the priority queue.

Parameters
new_sizeThe internal structure new size.

Definition at line 194 of file priorityQueue_tpl.h.

194 {
195 if (new_size < _nb_elements_) return;
196
197 _heap_.reserve(new_size);
198 _indices_.resize(new_size / 2);
199 }

References _nb_elements_.

◆ setPriority() [1/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
void gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::setPriority ( const Val & elt,
const Priority & new_priority )

Modifies the priority of each instance of a given element.

Parameters
eltThe value to update.
new_priorityThe values new priority.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 488 of file priorityQueue_tpl.h.

490 {
492 }
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.

References _indices_, and setPriorityByPos().

Referenced by gum::StaticTriangulation::_computeRecursiveThinning_(), and gum::BinaryJoinTreeConverterDefault::_convertClique_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPriority() [2/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
void gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::setPriority ( const Val & elt,
Priority && new_priority )

Modifies the priority of each instance of a given element.

Parameters
eltThe value to update.
new_priorityThe values new priority.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 497 of file priorityQueue_tpl.h.

References _indices_, and setPriorityByPos().

Here is the call graph for this function:

◆ setPriorityByPos() [1/2]

template<typename Val, typename Priority, typename Cmp>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp >::setPriorityByPos ( Size index,
const Priority & new_priority )

Modifies the priority of the element at position "index" of the queue.

Parameters
indexThe index of the element to update.
new_priorityThe element's new priority.
Returns
Returns the elements new priority.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 400 of file priorityQueue_tpl.h.

402 {
403 // check whether the element the priority of which should be changed exists
404 if (index >= _nb_elements_) {
405 GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
406 }
407
408 // get the element itself
409 const Val* val = _heap_[index].second;
410
411 // restore the heap property
412 Size i = index;
413
414 // move val upward if needed
415 for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
416 i = j, j = (j - 1) >> 1) {
418 _indices_[*(_heap_[i].second)] = i;
419 }
420
421 // move val downward if needed
422 for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
423 // let j be the max child
424 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
425
426 // if "val" is lower than heap[j], "val" must be stored at index i
427 if (_cmp_(new_priority, _heap_[j].first)) break;
428
429 // else pull up the jth node
431 _indices_[*(_heap_[i].second)] = i;
432 }
433
434 // update the index of val
435 _heap_[i].first = new_priority;
436 _heap_[i].second = val;
437 _indices_[*val] = i;
438
439 return i;
440 }

References _nb_elements_.

Referenced by setPriority(), and setPriority().

Here is the caller graph for this function:

◆ setPriorityByPos() [2/2]

template<typename Val, typename Priority, typename Cmp>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp >::setPriorityByPos ( Size index,
Priority && new_priority )

Modifies the priority of the element at position "index" of the queue.

Parameters
indexThe index of the element to update.
new_priorityThe element's new priority.
Returns
Returns the elements new priority.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 444 of file priorityQueue_tpl.h.

446 {
447 // check whether the element the priority of which should be changed exists
448 if (index >= _nb_elements_) {
449 GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
450 }
451
452 // get the element itself
453 const Val* val = _heap_[index].second;
454
455 // restore the heap property
456 Size i = index;
457
458 // move val upward if needed
459 for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
460 i = j, j = (j - 1) >> 1) {
462 _indices_[*(_heap_[i].second)] = i;
463 }
464
465 // move val downward if needed
466 for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
467 // let j be the max child
468 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
469
470 // if "val" is lower than heap[j], "val" must be stored at index i
471 if (_cmp_(new_priority, _heap_[j].first)) break;
472
473 // else pull up the jth node
475 _indices_[*(_heap_[i].second)] = i;
476 }
477
478 // update the index of val
479 _heap_[i].first = std::move(new_priority);
480 _heap_[i].second = val;
481 _indices_[*val] = i;
482
483 return i;
484 }

References _cmp_, _heap_, _indices_, _nb_elements_, and GUM_ERROR.

◆ size()

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

Returns the number of elements in the priority queue.

Returns
Returns the number of elements in the priority queue.

Definition at line 182 of file priorityQueue_tpl.h.

182 {
183 return _nb_elements_;
184 }

References _nb_elements_.

Referenced by PriorityQueueImplementation().

Here is the caller graph for this function:

◆ top()

template<typename Val, typename Priority, typename Cmp>
INLINE const Val & gum::PriorityQueueImplementation< Val, Priority, Cmp >::top ( ) const

returns the element at the top of the priority queue

Exceptions
NotFoundRaised if the queue is empty

Definition at line 165 of file priorityQueue_tpl.h.

165 {
166 if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
167
168 return *(_heap_[0].second);
169 }

References _heap_, _nb_elements_, and GUM_ERROR.

◆ topPriority()

template<typename Val, typename Priority, typename Cmp>
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp >::topPriority ( ) const

Returns the priority of the top element.

Returns
Returns the priority of the top element.
Exceptions
NotFoundRaised if the queue is empty.

Definition at line 174 of file priorityQueue_tpl.h.

174 {
175 if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
176
177 return _heap_[0].first;
178 }

References _heap_, _nb_elements_, and GUM_ERROR.

◆ toString()

template<typename Val, typename Priority, typename Cmp>
std::string gum::PriorityQueueImplementation< Val, Priority, Cmp >::toString ( ) const

Displays the content of the queue.

Returns
Returns the content of the queue.

Definition at line 382 of file priorityQueue_tpl.h.

382 {
383 bool deja = false;
385 stream << "[";
386
387 for (Size i = 0; i != _nb_elements_; ++i, deja = true) {
388 if (deja) stream << " , ";
389
390 stream << "(" << _heap_[i].first << " , " << *(_heap_[i].second) << ")";
391 }
392
393 stream << "]";
394
395 return stream.str();
396 }

Referenced by gum::operator<<().

Here is the caller graph for this function:

◆ PriorityQueue< Val, Priority, Cmp >

template<typename Val, typename Priority, typename Cmp, bool Gen>
friend class PriorityQueue< Val, Priority, Cmp >
friend

All gum::PriorityQueue are friends with themselves.

Definition at line 74 of file priorityQueue.h.

Member Data Documentation

◆ _cmp_

template<typename Val, typename Priority, typename Cmp, bool Gen>
Cmp gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::_cmp_
private

◆ _heap_

template<typename Val, typename Priority, typename Cmp, bool Gen>
std::vector< std::pair< Priority, const Val* > > gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::_heap_
private

◆ _indices_

template<typename Val, typename Priority, typename Cmp, bool Gen>
HashTable< Val, Size > gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::_indices_ {HashTableConst::default_size, true, true}
private

◆ _nb_elements_

template<typename Val, typename Priority, typename Cmp, bool Gen>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::_nb_elements_ {0}
private

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