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

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.
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.
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
void eraseTop()
Removes the top of 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.
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 58 of file multiPriorityQueue_tpl.h.

58 :
59 _indices_(capacity >> 1, true, false), _cmp_(compare) {
60 _heap_.reserve(capacity);
61
62 // for debugging purposes
64 }
Cmp _cmp_
Comparison function.
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 capacity() const noexcept
Return the size of the internal structure storing the priority queue.

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

69 :
70 _indices_(Size(list.size()) / 2, true, false) {
71 // fill the queue
72 _heap_.reserve(list.size());
73 for (const auto& elt: list) {
74 insert(elt.first, elt.second);
75 }
76
77 // for debugging purposes
79 }
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 83 of file multiPriorityQueue_tpl.h.

84 :
87 // for debugging purposes
89
90 // fill the heap structure
91 for (const auto& val_and_index: _indices_) {
92 const Val* val = &(val_and_index.first);
94 for (auto index: vect) {
95 _heap_[index].second = val;
96 }
97 }
98 }
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 102 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 112 of file multiPriorityQueue_tpl.h.

112 {
113 // for debugging purposes
115 }

References MultiPriorityQueue().

Here is the call graph for this function:

Member Function Documentation

◆ allValues()

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

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

References _indices_.

◆ capacity()

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

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

References _heap_.

Referenced by MultiPriorityQueue().

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

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

References _heap_, _indices_, and _nb_elements_.

◆ contains()

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

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

References _indices_.

◆ emplace()

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

Definition at line 416 of file multiPriorityQueue_tpl.h.

References insert().

Here is the call graph for this function:

◆ empty()

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

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

References _nb_elements_.

◆ erase()

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

275 {
276 if (auto p = _indices_.tryGet(val)) eraseByPos((*p)[0]);
277 }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

References _indices_, and eraseByPos().

Here is the call 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 217 of file multiPriorityQueue_tpl.h.

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

References _heap_, _indices_, and _nb_elements_.

Referenced by erase(), and eraseTop().

Here is the caller graph for this function:

◆ eraseTop()

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

281 {
282 eraseByPos(0);
283 }

References eraseByPos().

Here is the call 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 305 of file multiPriorityQueue_tpl.h.

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

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

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

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

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

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

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

Here is the call graph for this function:

◆ operator[]()

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

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

References _nb_elements_, and GUM_ERROR.

◆ pop()

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

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

References _heap_, _nb_elements_, and GUM_ERROR.

◆ priority()

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

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

References _heap_, and _indices_.

Referenced by insert(), and insert().

Here is the caller graph for this function:

◆ resize()

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

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

References _heap_, _indices_, and _nb_elements_.

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

587 {
589
590 for (auto index: vect_index) {
592 }
593 }

References _indices_, and setPriorityByPos().

Here is the call 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 464 of file multiPriorityQueue_tpl.h.

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

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

Referenced by 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 525 of file multiPriorityQueue_tpl.h.

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

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

◆ size()

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

188 {
189 return _nb_elements_;
190 }

References _nb_elements_.

Referenced by MultiPriorityQueue().

Here is the caller graph for this function:

◆ top()

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

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

References _heap_, _nb_elements_, and GUM_ERROR.

◆ topPriority()

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

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

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

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

Referenced by 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

◆ _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: