aGrUM 3.1.1
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

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

63 : _indices_(capacity >> 1, true, true), _cmp_(compare) {
64 _heap_.reserve(capacity);
65
67 }
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 71 of file priorityQueue_tpl.h.

72 :
73 _indices_(Size(list.size()) / 2, true, true) {
74 // fill the queue
75 _heap_.reserve(list.size());
76 for (const auto& elt: list) {
77 insert(elt.first, elt.second);
78 }
79
81 }
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
Size size() const noexcept
Returns the number of elements 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 85 of file priorityQueue_tpl.h.

86 :
89 // fill the heap structure
90 for (const auto& elt: _indices_) {
91 _heap_[elt.second].second = &(elt.first);
92 }
93
95 }
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 99 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 108 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>
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 270 of file priorityQueue_tpl.h.

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

References _indices_.

◆ capacity()

template<typename Val, typename Priority, typename Cmp>
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>
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>
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 361 of file priorityQueue_tpl.h.

361 {
362 return _indices_.exists(val);
363 }

References _indices_.

Referenced by gum::StaticTriangulation::_computeRecursiveThinning_().

Here is the caller graph for this function:

◆ emplace()

template<typename Val, typename Priority, typename Cmp>
template<typename... Args>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp >::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.

Definition at line 347 of file priorityQueue_tpl.h.

References insert().

Here is the call graph for this function:

◆ empty()

template<typename Val, typename Priority, typename Cmp>
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 355 of file priorityQueue_tpl.h.

355 {
356 return (_nb_elements_ == 0);
357 }

References _nb_elements_.

◆ erase()

template<typename Val, typename Priority, typename Cmp, bool Gen>
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 if (auto ptr = _indices_.tryGet(val)) eraseByPos(*ptr);
248 }
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>
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 252 of file priorityQueue_tpl.h.

252 {
253 eraseByPos(0);
254 }

References eraseByPos().

Here is the call graph for this function:

◆ insert() [1/2]

template<typename Val, typename Priority, typename Cmp, bool Gen>
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 276 of file priorityQueue_tpl.h.

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

References _indices_, and priority().

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

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

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

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

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

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

150 {
151 // avoid self assignment
152 if (this != &from) {
154
159 }
160
161 return *this;
162 }

References PriorityQueueImplementation(), _heap_, and _indices_.

Here is the call graph for this function:

◆ operator[]()

template<typename Val, typename Priority, typename Cmp>
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 367 of file priorityQueue_tpl.h.

367 {
368 if (index >= _nb_elements_) {
369 GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
370 }
371
372 return *(_heap_[index].second);
373 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _heap_, _nb_elements_, and GUM_ERROR.

◆ pop()

template<typename Val, typename Priority, typename Cmp>
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 258 of file priorityQueue_tpl.h.

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

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

500 {
501 return _heap_[_indices_[elt]].first;
502 }

References _heap_, and _indices_.

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

Here is the caller graph for this function:

◆ priorityByPos()

template<typename Val, typename Priority, typename Cmp>
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 507 of file priorityQueue_tpl.h.

507 {
508 if (index > _nb_elements_) {
509 GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
510 }
511 return _heap_[index].first;
512 }

References _heap_, _nb_elements_, and GUM_ERROR.

◆ resize()

template<typename Val, typename Priority, typename Cmp>
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 483 of file priorityQueue_tpl.h.

485 {
487 }
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 492 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 395 of file priorityQueue_tpl.h.

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

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

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

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

◆ size()

template<typename Val, typename Priority, typename Cmp>
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>
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 166 of file priorityQueue_tpl.h.

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

References _heap_, _nb_elements_, and GUM_ERROR.

◆ topPriority()

template<typename Val, typename Priority, typename Cmp>
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 377 of file priorityQueue_tpl.h.

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

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: