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

A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowed. More...

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

Collaboration diagram for gum::MultiPriorityQueue< Val, Priority, Cmp >:

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)
Constructors / Destructors
 MultiPriorityQueue (Cmp compare=Cmp(), Size capacity=GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY)
 Basic constructor.
 MultiPriorityQueue (std::initializer_list< std::pair< Val, Priority > > list)
 Initializer list constructor.
 MultiPriorityQueue (const MultiPriorityQueue< Val, Priority, Cmp > &from)
 Copy constructor.
 MultiPriorityQueue (MultiPriorityQueue< Val, Priority, Cmp > &&from)
 Move constructor.
 ~MultiPriorityQueue ()
 Class destructor.
Operators
MultiPriorityQueue< Val, Priority, Cmp > & operator= (const MultiPriorityQueue< Val, Priority, Cmp > &from)
 Copy operator.
MultiPriorityQueue< Val, Priority, Cmp > & operator= (MultiPriorityQueue< Val, Priority, Cmp > &&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.
const Priority & priority (const Val &elt) const
 Returns the priority of an instance of the value passed in argument.
void clear ()
 Removes all the elements from the queue.
const HashTable< Val, std::vector< Size > > & allValues () const
 Returns a gum::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
 Return 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 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, std::vector< Size > > _indices_
 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.

Detailed Description

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
class gum::MultiPriorityQueue< Val, Priority, Cmp >

A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowed.

A priority queue is quite similar to a heap except that a priority (a score) is assigned to each element in the structure. The elements are sorted according to a weak order on the scores. The priority of any element can be changed at any moment by the user. The priority queue then restores a heap property accordingly.

Usage example:
// create a priority queue of strings, the priorities of which are
// integers the element at the top of the queue has the smallest priority
// insert elements into the queue
queue1.insert (8, "AAA");
queue1.insert (10, "BBB");
queue1.insert (2, "CCC");
queue1.insert (23, "DDD");
queue1.insert (24, "EEE");
queue1.insert (10, "AAA");
// copy the queue
// initializer list constructor
queue3 { std::pair<std::string,int> ( "aa", 3 ),
std::pair<std::string,int> ( "bb", 2 ) };
// create a priority queue of strings, the priorities of which are
// pairs of ints
// get the top element, then remove it
std::cerr << queue2.top() << std::endl;
queue2.eraseTop();
// get the top element, then remove it
std::cerr << queue2.pop() << std::endl;
// output the content of the queue
std::cerr << queue1 << std::endl;
// change the priority of the element at position 3
Size new_pos=queue1.setPriorityByPos (3,100);
// change the priority of all instances of element "AAA"
queue1.setPriority ("AAA",100);
A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowe...
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
const Val & top() const
Returns the element at the top of the priority queue.
Val pop()
Removes the top element from the priority queue and return it.
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
void eraseTop()
Removes the top of the priority queue (but does not return it).
MultiPriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Template Parameters
ValThe values type stored in the gum::MultiPriorityQueue.
PriorityThe priorities type.
CmpThe priorities comparator.

Definition at line 143 of file multiPriorityQueue.h.

Member Typedef Documentation

◆ const_pointer

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::MultiPriorityQueue< Val, Priority, Cmp >::const_pointer = const Val*

types for STL compliance

Definition at line 151 of file multiPriorityQueue.h.

◆ const_reference

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::MultiPriorityQueue< Val, Priority, Cmp >::const_reference = const Val&

types for STL compliance

Definition at line 149 of file multiPriorityQueue.h.

◆ difference_type

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::MultiPriorityQueue< Val, Priority, Cmp >::difference_type = std::ptrdiff_t

types for STL compliance

Definition at line 152 of file multiPriorityQueue.h.

◆ pointer

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::MultiPriorityQueue< Val, Priority, Cmp >::pointer = Val*

types for STL compliance

Definition at line 150 of file multiPriorityQueue.h.

◆ reference

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::MultiPriorityQueue< Val, Priority, Cmp >::reference = Val&

types for STL compliance

Definition at line 148 of file multiPriorityQueue.h.

◆ value_type

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::MultiPriorityQueue< Val, Priority, Cmp >::value_type = Val

types for STL compliance

Definition at line 147 of file multiPriorityQueue.h.

Constructor & Destructor Documentation

◆ MultiPriorityQueue() [1/4]

template<typename Val, typename Priority, typename Cmp>
gum::MultiPriorityQueue< Val, Priority, Cmp >::MultiPriorityQueue ( Cmp compare = Cmp(),
Size capacity = GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY )
explicit

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

57 :
58 _indices_(capacity >> 1, true, false), _cmp_(compare) {
59 _heap_.reserve(capacity);
60
61 // for debugging purposes
63 }
Cmp _cmp_
Comparison function.
Size capacity() const noexcept
Return the size of the internal structure storing the priority queue.
std::vector< std::pair< Priority, const Val * > > _heap_
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size > > _indices_
A hashtable for quickly finding the elements by their value.

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

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

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

◆ MultiPriorityQueue() [2/4]

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

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 67 of file multiPriorityQueue_tpl.h.

68 :
69 _indices_(Size(list.size()) / 2, true, false) {
70 // fill the queue
71 _heap_.reserve(list.size());
72 for (const auto& elt: list) {
73 insert(elt.first, elt.second);
74 }
75
76 // for debugging purposes
78 }
Size size() const noexcept
Returns the number of elements in the priority queue.

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

Here is the call graph for this function:

◆ MultiPriorityQueue() [3/4]

template<typename Val, typename Priority, typename Cmp>
gum::MultiPriorityQueue< Val, Priority, Cmp >::MultiPriorityQueue ( const MultiPriorityQueue< Val, Priority, Cmp > & from)

Copy constructor.

Parameters
fromThe gum::MultiPriorityQueue to copy.

Definition at line 82 of file multiPriorityQueue_tpl.h.

83 :
86 // for debugging purposes
88
89 // fill the heap structure
90 for (const auto& val_and_index: _indices_) {
91 const Val* val = &(val_and_index.first);
93 for (auto index: vect) {
94 _heap_[index].second = val;
95 }
96 }
97 }
Size _nb_elements_
The number of elements in the heap.

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

Here is the call graph for this function:

◆ MultiPriorityQueue() [4/4]

template<typename Val, typename Priority, typename Cmp>
gum::MultiPriorityQueue< Val, Priority, Cmp >::MultiPriorityQueue ( MultiPriorityQueue< Val, Priority, Cmp > && from)

Move constructor.

Parameters
fromThe gum::MultiPriorityQueue to move.

Definition at line 101 of file multiPriorityQueue_tpl.h.

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

Here is the call graph for this function:

◆ ~MultiPriorityQueue()

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

Class destructor.

Definition at line 111 of file multiPriorityQueue_tpl.h.

111 {
112 // for debugging purposes
114 }

References MultiPriorityQueue().

Here is the call graph for this function:

Member Function Documentation

◆ allValues()

template<typename Val, typename Priority, typename Cmp>
INLINE const HashTable< Val, std::vector< Size > > & gum::MultiPriorityQueue< Val, Priority, Cmp >::allValues ( ) const

Returns a gum::HashTable the keys of which are the values stored in the queue.

The keys of the gum::HashTable correspond to the values stored in the priority queue and, for each key, the corresponding value is the list of indices in the queue where we can find the key.

Returns
Returns a gum::HashTable the keys of which are the values stored in the queue.

Definition at line 300 of file multiPriorityQueue_tpl.h.

300 {
301 return reinterpret_cast< const HashTable< Val, std::vector< Size > >& >(_indices_);
302 }

References _indices_.

Referenced by emplace().

Here is the caller graph for this function:

◆ capacity()

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

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

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

Definition at line 193 of file multiPriorityQueue_tpl.h.

193 {
194 return Size(_heap_.capacity());
195 }

References _heap_.

Referenced by MultiPriorityQueue(), and emplace().

Here is the caller graph for this function:

◆ clear()

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

Removes all the elements from the queue.

Definition at line 208 of file multiPriorityQueue_tpl.h.

208 {
209 _nb_elements_ = 0;
210 _heap_.clear();
211 _indices_.clear();
212 }

References _heap_, _indices_, and _nb_elements_.

Referenced by emplace().

Here is the caller graph for this function:

◆ contains()

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

Indicates whether the priority queue contains a given value.

Parameters
valThe value to check if it is in the priority queue.
Returns
Returns true if the priority queue cotains the given value.

Definition at line 431 of file multiPriorityQueue_tpl.h.

431 {
432 return _indices_.exists(val);
433 }

References _indices_.

◆ emplace() [1/2]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
template<typename... Args>
Size gum::MultiPriorityQueue< Val, Priority, Cmp >::emplace ( Args &&... args)

Emplace a new element into the priority queue.

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

Template Parameters
ArgsThe emplace arguments types.
Parameters
argsThe emplace arguments.
Returns
the index of the element inserted into the priority queue.

References allValues(), capacity(), clear(), emplace(), erase(), eraseByPos(), eraseTop(), priority(), resize(), setPriority(), setPriorityByPos(), and toString().

Referenced by emplace().

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

◆ emplace() [2/2]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
template<typename... Args>
INLINE Size gum::MultiPriorityQueue< Val, Priority, Cmp >::emplace ( Args &&... args)

◆ empty()

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

Indicates whether the priority queue is empty.

Returns
Indicates whether the priority queue is empty.

Definition at line 425 of file multiPriorityQueue_tpl.h.

425 {
426 return (_nb_elements_ == 0);
427 }

References _nb_elements_.

◆ erase()

template<typename Val, typename Priority, typename Cmp>
INLINE void gum::MultiPriorityQueue< 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 274 of file multiPriorityQueue_tpl.h.

274 {
275 try {
277 } catch (NotFound const&) {}
278 }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

References _indices_, and eraseByPos().

Referenced by emplace().

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::MultiPriorityQueue< 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 216 of file multiPriorityQueue_tpl.h.

216 {
217 if (index >= _nb_elements_) return;
218
219 // remove the element from the hashtable
220 const Val& del_val = *(_heap_[index].second);
222 if (vect_index.size() == 1) _indices_.erase(del_val);
223 else {
224 for (auto& v_index: vect_index) {
225 if (v_index == index) {
226 v_index = vect_index.back();
227 vect_index.pop_back();
228 break;
229 }
230 }
231 }
232
233 // put the last element at the "index" location
235 _heap_.pop_back();
237
238 if (!_nb_elements_ || (index == _nb_elements_)) return;
239
240 // restore the heap property
241 Size i = index;
242
243 for (Size j = (index << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
244 // let j be the max child
245 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
246
247 // if "last" is lower than heap[j], "last" must be stored at index i
248 if (_cmp_(last.first, _heap_[j].first)) break;
249
250 // else pull up the jth node
253 for (auto& v_index: vect_index) {
254 if (v_index == j) {
255 v_index = i;
256 break;
257 }
258 }
259 }
260
261 // put "last" back into the heap
264 for (auto& v_index: last_indices) {
265 if (v_index == _nb_elements_) {
266 v_index = i;
267 break;
268 }
269 }
270 }

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

Here is the caller graph for this function:

◆ eraseTop()

template<typename Val, typename Priority, typename Cmp>
INLINE void gum::MultiPriorityQueue< 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 282 of file multiPriorityQueue_tpl.h.

282 {
283 eraseByPos(0);
284 }

References eraseByPos().

Referenced by emplace().

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

◆ insert() [1/2]

template<typename Val, typename Priority, typename Cmp>
Size gum::MultiPriorityQueue< Val, Priority, Cmp >::insert ( const Val & val,
const Priority & priority )

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

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

Parameters
valThe value to insert.
priorityThe value priority.
Returns
Returns the index of the element inserted into the priority queue.

Definition at line 306 of file multiPriorityQueue_tpl.h.

306 {
307 // create the entry in the indices hashtable
308 const Val* new_val;
310 if (!_indices_.exists(val)) {
311 auto& new_elt = _indices_.insert(val, std::vector< Size >());
312 new_val = &(new_elt.first);
313 new_vect = &(new_elt.second);
314 } else {
315 new_val = &(_indices_.key(val));
316 new_vect = &(_indices_[val]);
317 }
318
319 try {
320 new_vect->push_back(0);
321 } catch (...) {
322 if (new_vect->empty()) { _indices_.erase(val); }
323 throw;
324 }
325
326 try {
328 } catch (...) {
329 if (new_vect->size() == 1) { _indices_.erase(val); }
330 throw;
331 }
332
335
336 // restore the heap property
337 Size i = _nb_elements_ - 1;
338
339 for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
340 i = j, j = (j - 1) >> 1) {
343 for (auto& index: vect_index) {
344 if (index == j) {
345 index = i;
346 break;
347 }
348 }
349 }
350
351 // put the new bucket into the heap
352 _heap_[i].first = std::move(new_heap_val.first);
353 _heap_[i].second = new_val;
354 new_vect->back() = i;
355
356 return i;
357 }
bool empty() const noexcept
Indicates whether the priority queue is empty.
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 MultiPriorityQueue().

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>
Size gum::MultiPriorityQueue< Val, Priority, Cmp >::insert ( Val && val,
Priority && priority )

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

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

Parameters
valThe value to insert.
priorityThe value priority.
Returns
Returns the index of the element inserted into the priority queue.

Definition at line 361 of file multiPriorityQueue_tpl.h.

361 {
362 // create the entry in the indices hashtable
363 const Val* new_val;
365 if (!_indices_.exists(val)) {
367 new_val = &(new_elt.first);
368 new_vect = &(new_elt.second);
369 } else {
370 new_val = &(_indices_.key(val));
371 new_vect = &(_indices_[val]);
372 }
373
374 try {
375 new_vect->push_back(0);
376 } catch (...) {
377 if (new_vect->empty()) { _indices_.erase(*new_val); }
378 throw;
379 }
380
381 try {
383 } catch (...) {
384 if (new_vect->size() == 1) { _indices_.erase(*new_val); }
385 throw;
386 }
387
390
391 // restore the heap property
392 Size i = _nb_elements_ - 1;
393
394 for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
395 i = j, j = (j - 1) >> 1) {
398 for (auto& index: vect_index) {
399 if (index == j) {
400 index = i;
401 break;
402 }
403 }
404 }
405
406 // put the new bucket into the heap
407 _heap_[i].first = std::move(new_heap_val.first);
408 _heap_[i].second = new_val;
409 new_vect->back() = i;
410
411 return i;
412 }

References _indices_, and priority().

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Val, typename Priority, typename Cmp>
MultiPriorityQueue< Val, Priority, Cmp > & gum::MultiPriorityQueue< Val, Priority, Cmp >::operator= ( const MultiPriorityQueue< Val, Priority, Cmp > & 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::MultiPriorityQueue to copy.

Definition at line 118 of file multiPriorityQueue_tpl.h.

119 {
120 // for debugging purposes
122
123 try {
124 // set the comprison function
125 _cmp_ = from._cmp_;
126
127 // copy the indices and the heap
131
132 // restore the link between _indices_ and _heap_
133 for (const auto& val_and_index: _indices_) {
134 const Val* val = &(val_and_index.first);
135 const std::vector< Size >& vect = val_and_index.second;
136 for (auto index: vect) {
137 _heap_[index].second = val;
138 }
139 }
140 } catch (...) {
141 _heap_.clear();
142 _indices_.clear();
143 _nb_elements_ = 0;
144
145 throw;
146 }
147
148 return *this;
149 }

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

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename Val, typename Priority, typename Cmp>
MultiPriorityQueue< Val, Priority, Cmp > & gum::MultiPriorityQueue< Val, Priority, Cmp >::operator= ( MultiPriorityQueue< Val, Priority, Cmp > && from)

Move operator.

Parameters
fromThe gum::MultiPriorityQueue to copy.

Definition at line 153 of file multiPriorityQueue_tpl.h.

154 {
155 // avoid self assignment
156 if (this != &from) {
157 // for debugging purposes
159
164 }
165
166 return *this;
167 }

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

Here is the call graph for this function:

◆ operator[]()

template<typename Val, typename Priority, typename Cmp>
INLINE const Val & gum::MultiPriorityQueue< 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 437 of file multiPriorityQueue_tpl.h.

437 {
438 if (index >= _nb_elements_) {
439 GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue")
440 }
441
442 return *(_heap_[index].second);
443 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _nb_elements_, and GUM_ERROR.

◆ pop()

template<typename Val, typename Priority, typename Cmp>
INLINE Val gum::MultiPriorityQueue< 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 288 of file multiPriorityQueue_tpl.h.

288 {
289 if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
290
291 Val v = *(_heap_[0].second);
292 eraseByPos(0);
293
294 return v;
295 }

References _nb_elements_, and GUM_ERROR.

◆ priority()

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

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

Of course, this method is really meaningful only when there is only one instance of the given element within the PriorityQueue.

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 598 of file multiPriorityQueue_tpl.h.

598 {
599 return _heap_[_indices_[elt][0]].first;
600 }

References _heap_, and _indices_.

Referenced by emplace(), insert(), and insert().

Here is the caller graph for this function:

◆ resize()

template<typename Val, typename Priority, typename Cmp>
INLINE void gum::MultiPriorityQueue< 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.
Returns
Changes the size of the internal structure storing the priority queue.

Definition at line 199 of file multiPriorityQueue_tpl.h.

199 {
200 if (new_size < _nb_elements_) return;
201
202 _heap_.reserve(new_size);
203 _indices_.resize(new_size / 2);
204 }

References _heap_, _indices_, and _nb_elements_.

Referenced by emplace().

Here is the caller graph for this function:

◆ setPriority()

template<typename Val, typename Priority, typename Cmp>
void gum::MultiPriorityQueue< Val, Priority, Cmp >::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 587 of file multiPriorityQueue_tpl.h.

588 {
590
591 for (auto index: vect_index) {
593 }
594 }

References _indices_, and setPriorityByPos().

Referenced by emplace().

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

◆ setPriorityByPos() [1/2]

template<typename Val, typename Priority, typename Cmp>
Size gum::MultiPriorityQueue< 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 465 of file multiPriorityQueue_tpl.h.

466 {
467 // check whether the element the priority of which should be changed exists
468 if (index >= _nb_elements_) {
469 GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue")
470 }
471
472 // get the element itself
473 const Val* val = _heap_[index].second;
474
475 // restore the heap property
476 Size i = index;
477
478 // move val upward if needed
479 for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
480 i = j, j = (j - 1) >> 1) {
483 for (auto& idx: vect_index) {
484 if (idx == j) {
485 idx = i;
486 break;
487 }
488 }
489 }
490
491 // move val downward if needed
492 for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
493 // let j be the max child
494 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
495
496 // if "val" is lower than heap[j], "val" must be stored at index i
497 if (_cmp_(new_priority, _heap_[j].first)) break;
498
499 // else pull up the jth node
502 for (auto& idx: vect_index) {
503 if (idx == j) {
504 idx = i;
505 break;
506 }
507 }
508 }
509
510 // update the index of val
511 _heap_[i].first = new_priority;
512 _heap_[i].second = val;
514 for (auto& idx: vect_index) {
515 if (idx == index) {
516 idx = i;
517 break;
518 }
519 }
520
521 return i;
522 }

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

Referenced by emplace(), and setPriority().

Here is the caller graph for this function:

◆ setPriorityByPos() [2/2]

template<typename Val, typename Priority, typename Cmp>
Size gum::MultiPriorityQueue< 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 526 of file multiPriorityQueue_tpl.h.

527 {
528 // check whether the element the priority of which should be changed exists
529 if (index >= _nb_elements_) {
530 GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue")
531 }
532
533 // get the element itself
534 const Val* val = _heap_[index].second;
535
536 // restore the heap property
537 Size i = index;
538
539 // move val upward if needed
540 for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
541 i = j, j = (j - 1) >> 1) {
544 for (auto& idx: vect_index) {
545 if (idx == j) {
546 idx = i;
547 break;
548 }
549 }
550 }
551
552 // move val downward if needed
553 for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
554 // let j be the max child
555 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
556
557 // if "val" is lower than heap[j], "val" must be stored at index i
558 if (_cmp_(new_priority, _heap_[j].first)) break;
559
560 // else pull up the jth node
563 for (auto& idx: vect_index) {
564 if (idx == j) {
565 idx = i;
566 break;
567 }
568 }
569 }
570
571 // update the index of val
572 _heap_[i].first = std::move(new_priority);
573 _heap_[i].second = val;
575 for (auto& idx: vect_index) {
576 if (idx == index) {
577 idx = i;
578 break;
579 }
580 }
581
582 return i;
583 }

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

◆ size()

template<typename Val, typename Priority, typename Cmp>
INLINE Size gum::MultiPriorityQueue< 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 187 of file multiPriorityQueue_tpl.h.

187 {
188 return _nb_elements_;
189 }

References _nb_elements_.

Referenced by MultiPriorityQueue().

Here is the caller graph for this function:

◆ top()

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

Returns the element at the top of the priority queue.

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

Definition at line 171 of file multiPriorityQueue_tpl.h.

171 {
172 if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
173
174 return *(_heap_[0].second);
175 }

References _heap_, _nb_elements_, and GUM_ERROR.

◆ topPriority()

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

Returns the priority of the top element.

Exceptions
NotFoundRaised if the queue is empty.

Definition at line 179 of file multiPriorityQueue_tpl.h.

179 {
180 if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
181
182 return _heap_[0].first;
183 }

References _heap_, _nb_elements_, and GUM_ERROR.

◆ toString()

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

Displays the content of the queue.

Returns
Returns the content of the queue.

Definition at line 447 of file multiPriorityQueue_tpl.h.

447 {
448 bool deja = false;
450 stream << "[";
451
452 for (Size i = 0; i != _nb_elements_; ++i, deja = true) {
453 if (deja) stream << " , ";
454
455 stream << "(" << _heap_[i].first << " , " << *(_heap_[i].second) << ")";
456 }
457
458 stream << "]";
459
460 return stream.str();
461 }

Referenced by emplace(), and gum::operator<<().

Here is the caller graph for this function:

Member Data Documentation

◆ _cmp_

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
Cmp gum::MultiPriorityQueue< Val, Priority, Cmp >::_cmp_
private

◆ _heap_

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
std::vector< std::pair< Priority, const Val* > > gum::MultiPriorityQueue< Val, Priority, Cmp >::_heap_
private

An array storing all the elements of the heap as well as their score.

Definition at line 456 of file multiPriorityQueue.h.

Referenced by MultiPriorityQueue(), MultiPriorityQueue(), MultiPriorityQueue(), MultiPriorityQueue(), capacity(), clear(), operator=(), operator=(), priority(), resize(), setPriorityByPos(), setPriorityByPos(), top(), and topPriority().

◆ _indices_

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
HashTable< Val, std::vector< Size > > gum::MultiPriorityQueue< Val, Priority, Cmp >::_indices_
private

◆ _nb_elements_

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
Size gum::MultiPriorityQueue< Val, Priority, Cmp >::_nb_elements_ {0}
private

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