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

A priorityQueue is a heap in which each element has a mutable priority. More...

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

Inheritance diagram for gum::PriorityQueue< Val, Priority, Cmp >:
Collaboration diagram for gum::PriorityQueue< Val, Priority, Cmp >:

Public Types

using Implementation = PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value >
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

INLINE Size emplace (Args &&... args)
Constructors / Destructors
 PriorityQueue (Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
 Basic constructor.
 PriorityQueue (std::initializer_list< std::pair< Val, Priority > > list)
 Initializer list constructor.
 PriorityQueue (const PriorityQueue< Val, Priority, Cmp > &from)
 Copy constructor.
 PriorityQueue (PriorityQueue< Val, Priority, Cmp > &&from)
 Move constructor.
 ~PriorityQueue ()
 Class destructor.
Operators
PriorityQueue< Val, Priority, Cmp > & operator= (const PriorityQueue< Val, Priority, Cmp > &from)
 Copy operator.
PriorityQueue< Val, Priority, Cmp > & operator= (PriorityQueue< Val, Priority, Cmp > &&from)
 Move operator.
Operators
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 int & 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 int &priority)
 Inserts a new (a copy) element in 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 int &new_priority)
 Modifies the priority of the element at position "index" of the queue.
void setPriority (const Val &elt, const int &new_priority)
 Modifies the priority of each instance of a given element.
const int & priority (const Val &elt) const
 Returns the priority of an instance of the value passed in argument.
const int & 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 Attributes

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

Detailed Description

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

A priorityQueue is a heap in which each element has a mutable priority.

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. Duplicate elements are not allowed in priority queues; if you wish an element to appear several times with different priorities, prefer using class MultiplePriorityQueue.

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 ("AAA", 8);
queue1.insert ("BBB", 10);
queue1.insert ("CCC", 2);
queue1.insert ("DDD", 23);
queue1.insert ("EEE", 24);
// copy the queue
// initializer list constructor
PriorityQueue<std::string,int> 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);
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).
Val pop()
Removes the top element from the priority queue and return it.
const Val & top() const
returns the element at the top of the priority queue
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
A priorityQueue is a heap in which each element has a mutable priority.
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_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.
PriorityThe priorities type.
CmpThe priorities comparator.

Definition at line 883 of file priorityQueue.h.

Member Typedef Documentation

◆ const_pointer

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

Types for STL compliance.

Definition at line 892 of file priorityQueue.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 890 of file priorityQueue.h.

◆ difference_type

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

Types for STL compliance.

Definition at line 893 of file priorityQueue.h.

◆ Implementation

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >>
using gum::PriorityQueue< Val, Priority, Cmp >::Implementation = PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value >

Definition at line 896 of file priorityQueue.h.

◆ pointer

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

Types for STL compliance.

Definition at line 891 of file priorityQueue.h.

◆ reference

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

Types for STL compliance.

Definition at line 889 of file priorityQueue.h.

◆ value_type

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

Types for STL compliance.

Definition at line 888 of file priorityQueue.h.

Constructor & Destructor Documentation

◆ PriorityQueue() [1/4]

template<typename Val, typename Priority, typename Cmp>
INLINE gum::PriorityQueue< Val, Priority, Cmp >::PriorityQueue ( Cmp compare = Cmp(),
Size capacity = GUM_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 whether 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 986 of file priorityQueue_tpl.h.

986 :
988 // for debugging purposes
990 }
PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value > Implementation

References PriorityQueue(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::capacity().

Referenced by PriorityQueue(), PriorityQueue(), PriorityQueue(), PriorityQueue(), and ~PriorityQueue().

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

◆ PriorityQueue() [2/4]

template<typename Val, typename Priority, typename Cmp>
INLINE gum::PriorityQueue< Val, Priority, Cmp >::PriorityQueue ( 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 994 of file priorityQueue_tpl.h.

995 :
997 // for debugging purposes
999 }

References PriorityQueue().

Here is the call graph for this function:

◆ PriorityQueue() [3/4]

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

Copy constructor.

Parameters
fromThe gum::PriorityQueue to copy.

Definition at line 1003 of file priorityQueue_tpl.h.

1004 :
1006 // for debugging purposes
1008 }

References PriorityQueue(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.

Here is the call graph for this function:

◆ PriorityQueue() [4/4]

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

Move constructor.

Parameters
fromThe gum::PriorityQueue to move.

Definition at line 1012 of file priorityQueue_tpl.h.

1013 :
1015 // for debugging purposes
1017 }

References PriorityQueue(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.

Here is the call graph for this function:

◆ ~PriorityQueue()

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

Class destructor.

Definition at line 1021 of file priorityQueue_tpl.h.

1021 {
1022 // for debugging purposes
1024 }

References PriorityQueue().

Here is the call graph for this function:

Member Function Documentation

◆ allValues()

INLINE const HashTable< Val, Size > & gum::PriorityQueueImplementation< Val, int, std::less< int > >::allValues ( ) const
noexceptinherited

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

272 {
273 return reinterpret_cast< const HashTable< Val, Size >& >(_indices_);
274 }
The internal class for representing priority queues.

◆ capacity()

INLINE Size gum::PriorityQueueImplementation< Val, int, std::less< int > >::capacity ( ) const
noexceptinherited

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

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

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

Here is the caller graph for this function:

◆ clear()

INLINE void gum::PriorityQueueImplementation< Val, int, std::less< int > >::clear ( )
inherited

Removes all the elements from the queue.

Definition at line 386 of file priorityQueue_tpl.h.

203 {
204 _nb_elements_ = 0;
205 _heap_.clear();
207 }
void clear()
Removes all the elements from the queue.

References _heap_, and _nb_elements_.

◆ contains()

INLINE bool gum::PriorityQueueImplementation< Val, int, std::less< int > >::contains ( const Val & val) const
inherited

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

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

References _nb_elements_.

◆ emplace()

INLINE Size gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::emplace ( Args &&... args)
inherited

◆ empty()

INLINE bool gum::PriorityQueueImplementation< Val, int, std::less< int > >::empty ( ) const
noexceptinherited

Indicates whether the priority queue is empty.

Returns
Returns true if the priority queue is empty.

Definition at line 215 of file priorityQueue_tpl.h.

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

References _heap_, _indices_, and _nb_elements_.

◆ erase()

INLINE void gum::PriorityQueueImplementation< Val, int, std::less< int > >::erase ( const Val & val)
inherited

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

246 {
247 try {
249 } catch (NotFound const&) {}
250 }

◆ eraseByPos()

void gum::PriorityQueueImplementation< Val, int, std::less< int > >::eraseByPos ( Size index)
inherited

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

211 {
212 if (index >= _nb_elements_) return;
213
214 // remove the element from the hashtable
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 }
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).

◆ eraseTop()

INLINE void gum::PriorityQueueImplementation< Val, int, std::less< int > >::eraseTop ( )
inherited

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

254 {
255 eraseByPos(0);
256 }

References _cmp_, _heap_, _indices_, and _nb_elements_.

◆ insert()

INLINE Size gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::insert ( const Val & val,
const int & priority )
inherited

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

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

◆ operator=() [1/2]

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

Definition at line 1028 of file priorityQueue_tpl.h.

1029 {
1031 return *this;
1032 }
PriorityQueueImplementation< Val, Priority, Cmp, Gen > & operator=(const PriorityQueueImplementation< Val, Priority, Cmp, Gen > &from)
Copy operator.

References gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::operator=(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.

Here is the call graph for this function:

◆ operator=() [2/2]

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

Move operator.

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

Definition at line 1037 of file priorityQueue_tpl.h.

1037 {
1039 return *this;
1040 }

References gum::PriorityQueueImplementation< Val, Priority, Cmp, Gen >::operator=(), and gum::PriorityQueueImplementation< Val, int, std::less< int >, std::is_scalar< Val >::value >::PriorityQueue< Val, Priority, Cmp >.

Here is the call graph for this function:

◆ operator[]()

INLINE const Val & gum::PriorityQueueImplementation< Val, int, std::less< int > >::operator[] ( Size index_elt) const
inherited

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

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

References _heap_, and _indices_.

◆ pop()

INLINE Val gum::PriorityQueueImplementation< Val, int, std::less< int > >::pop ( )
inherited

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

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

References _heap_, and _indices_.

◆ priority()

INLINE const int & gum::PriorityQueueImplementation< Val, int, std::less< int > >::priority ( const Val & elt) const
inherited

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

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

Referenced by gum::DefaultPartialOrderedEliminationSequenceStrategy::_nodeToEliminate_().

Here is the caller graph for this function:

◆ priorityByPos()

INLINE const int & gum::PriorityQueueImplementation< Val, int, std::less< int > >::priorityByPos ( Size index) const
inherited

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

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

◆ resize()

INLINE void gum::PriorityQueueImplementation< Val, int, std::less< int > >::resize ( Size new_size)
inherited

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

Parameters
new_sizeThe internal structure new size.

Definition at line 426 of file priorityQueue_tpl.h.

194 {
195 if (new_size < _nb_elements_) return;
196
197 _heap_.reserve(new_size);
199 }
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.

References _cmp_, _heap_, and _indices_.

◆ setPriority()

void gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::setPriority ( const Val & elt,
const int & new_priority )
inherited

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

◆ setPriorityByPos()

Size gum::PriorityQueueImplementation< Val, int, std::less< int > >::setPriorityByPos ( Size index,
const int & new_priority )
inherited

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

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

References _heap_, and _indices_.

◆ size()

INLINE Size gum::PriorityQueueImplementation< Val, int, std::less< int > >::size ( ) const
noexceptinherited

Returns the number of elements in the priority queue.

Returns
Returns the number of elements in the priority queue.

Definition at line 209 of file priorityQueue_tpl.h.

182 {
183 return _nb_elements_;
184 }

◆ top()

INLINE const Val & gum::PriorityQueueImplementation< Val, int, std::less< int > >::top ( ) const
inherited

returns the element at the top of the priority queue

Exceptions
NotFoundRaised if the queue is empty

Definition at line 226 of file priorityQueue_tpl.h.

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

References _cmp_, _heap_, and _nb_elements_.

◆ topPriority()

INLINE const int & gum::PriorityQueueImplementation< Val, int, std::less< int > >::topPriority ( ) const
inherited

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 233 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_, and _indices_.

◆ toString()

std::string gum::PriorityQueueImplementation< Val, int, std::less< int > >::toString ( ) const
inherited

Displays the content of the queue.

Returns
Returns the content of the queue.

Definition at line 405 of file priorityQueue_tpl.h.

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

Member Data Documentation

◆ _cmp_

std::less< int > gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::_cmp_
privateinherited

Comparison function.

Definition at line 441 of file priorityQueue.h.

Referenced by eraseTop(), resize(), and top().

◆ _heap_

std::vector< std::pair< int, const Val* > > gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::_heap_
privateinherited

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

Definition at line 432 of file priorityQueue.h.

Referenced by PriorityQueueImplementation(), clear(), empty(), eraseTop(), operator[](), pop(), resize(), setPriorityByPos(), top(), and topPriority().

◆ _indices_

HashTable< Val, Size > gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::_indices_
privateinherited

A hashtable for quickly finding the elements by their value.

Definition at line 435 of file priorityQueue.h.

435{HashTableConst::default_size, true, true};

Referenced by PriorityQueueImplementation(), empty(), eraseTop(), operator[](), pop(), resize(), setPriorityByPos(), and topPriority().

◆ _nb_elements_

Size gum::PriorityQueueImplementation< Val, int, std::less< int >, Gen >::_nb_elements_
privateinherited

The number of elements in the heap.

Definition at line 438 of file priorityQueue.h.

438{0};

Referenced by ~PriorityQueueImplementation(), clear(), contains(), empty(), eraseTop(), and top().


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