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

A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowedA priority queue is quite similar to a heap except that a priority (a score) is assigned to each element in the structure. More...

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

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

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. More...
 
 MultiPriorityQueue (std::initializer_list< std::pair< Val, Priority > > list)
 Initializer list constructor. More...
 
 MultiPriorityQueue (const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &from)
 Copy constructor. More...
 
template<typename OtherAlloc >
 MultiPriorityQueue (const MultiPriorityQueue< Val, Priority, Cmp, OtherAlloc > &from)
 generalized copy constructor More...
 
 MultiPriorityQueue (MultiPriorityQueue< Val, Priority, Cmp, Alloc > &&from)
 Move constructor. More...
 
 ~MultiPriorityQueue ()
 Class destructor. More...
 
Operators
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & operator= (const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &from)
 Copy operator. More...
 
template<typename OtherAlloc >
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & operator= (const MultiPriorityQueue< Val, Priority, Cmp, OtherAlloc > &from)
 Generalized copy operator. More...
 
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & operator= (MultiPriorityQueue< Val, Priority, Cmp, Alloc > &&from)
 Move operator. More...
 
const Val & operator[] (Size index_elt) const
 Returns the element at index "index_elt" from the priority queue. More...
 
Accessors / Modifiers
Size size () const noexcept
 Returns the number of elements in the priority queue. More...
 
bool empty () const noexcept
 Indicates whether the priority queue is empty. More...
 
bool contains (const Val &val) const
 Indicates whether the priority queue contains a given value. More...
 
const Val & top () const
 Returns the element at the top of the priority queue. More...
 
const Priority & topPriority () const
 Returns the priority of the top element. More...
 
Val pop ()
 Removes the top element from the priority queue and return it. More...
 
Size insert (const Val &val, const Priority &priority)
 Inserts a new (a copy) element in the priority queue. More...
 
Size insert (Val &&val, Priority &&priority)
 Inserts (by move) a new element in the priority queue. More...
 
template<typename... Args>
Size emplace (Args &&... args)
 Emplace a new element into the priority queue. More...
 
void eraseTop ()
 Removes the top of the priority queue (but does not return it). More...
 
void eraseByPos (Size index)
 Removes the element at position "index" from the priority queue. More...
 
void erase (const Val &val)
 Removes a given element from the priority queue (but does not return it). More...
 
Size setPriorityByPos (Size index, const Priority &new_priority)
 Modifies the priority of the element at position "index" of the queue. More...
 
Size setPriorityByPos (Size index, Priority &&new_priority)
 Modifies the priority of the element at position "index" of the queue. More...
 
void setPriority (const Val &elt, const Priority &new_priority)
 Modifies the priority of each instance of a given element. More...
 
const Priority & priority (const Val &elt) const
 Returns the priority of an instance of the value passed in argument. More...
 
void clear ()
 Removes all the elements from the queue. More...
 
const HashTable< Val, std::vector< Size > > & allValues () const
 Returns a gum::HashTable the keys of which are the values stored in the queue. More...
 
std::string toString () const
 Displays the content of the queue. More...
 
Fine tuning
Size capacity () const noexcept
 Return the size of the internal structure storing the priority queue. More...
 
void resize (Size new_size)
 Changes the size of the internal structure storing the priority queue. More...
 

Public Types

using IndexAlloc = typename Alloc::template rebind< std::pair< Val, std::vector< Size > > >::other
 The allocator for the indices. More...
 
using HeapAlloc = typename Alloc::template rebind< std::pair< Priority, const Val *> >::other
 The allocator for the heap. More...
 
using value_type = Val
 types for STL compliance More...
 
using reference = Val &
 types for STL compliance More...
 
using const_reference = const Val &
 types for STL compliance More...
 
using pointer = Val *
 types for STL compliance More...
 
using const_pointer = const Val *
 types for STL compliance More...
 
using difference_type = std::ptrdiff_t
 types for STL compliance More...
 
using allocator_type = Alloc
 types for STL compliance More...
 

Friends

template<typename V , typename P , typename C , typename A >
class MultiPriorityQueue
 Making all MultiPriorityQueue friend with themselves. More...
 

Detailed Description

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

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
MultiPriorityQueue<std::string> queue1;
// 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
MultiPriorityQueue<std::string> queue2 = queue1;
// initializer list constructor
MultiPriorityQueue<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
MultiPriorityQueue< std::string, std::pair<int,int> > queue3;
// 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);
Template Parameters
ValThe values type stored in the gum::MultiPriorityQueue.
PriorityThe priorities type.
CmpThe priorities comparator.
AllocThe values allocator.

Definition at line 125 of file multiPriorityQueue.h.

Member Typedef Documentation

◆ allocator_type

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

types for STL compliance

Definition at line 139 of file multiPriorityQueue.h.

◆ const_pointer

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

types for STL compliance

Definition at line 137 of file multiPriorityQueue.h.

◆ const_reference

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

types for STL compliance

Definition at line 135 of file multiPriorityQueue.h.

◆ difference_type

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

types for STL compliance

Definition at line 138 of file multiPriorityQueue.h.

◆ HeapAlloc

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::HeapAlloc = typename Alloc::template rebind< std::pair< Priority, const Val* > >::other

The allocator for the heap.

Definition at line 147 of file multiPriorityQueue.h.

◆ IndexAlloc

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::IndexAlloc = typename Alloc::template rebind< std::pair< Val, std::vector< Size > > >::other

The allocator for the indices.

Definition at line 144 of file multiPriorityQueue.h.

◆ pointer

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

types for STL compliance

Definition at line 136 of file multiPriorityQueue.h.

◆ reference

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

types for STL compliance

Definition at line 134 of file multiPriorityQueue.h.

◆ value_type

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

types for STL compliance

Definition at line 133 of file multiPriorityQueue.h.

Constructor & Destructor Documentation

◆ MultiPriorityQueue() [1/5]

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

36  :
37  _indices_(capacity >> 1, true, false), _cmp_(compare) {
38  _heap_.reserve(capacity);
39 
40  // for debugging purposes
41  GUM_CONSTRUCTOR(MultiPriorityQueue);
42  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size capacity() const noexcept
Return the size of the internal structure storing the priority queue.
Cmp _cmp_
Comparison function.

◆ MultiPriorityQueue() [2/5]

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

47  :
48  _indices_(Size(list.size()) / 2, true, false) {
49  // fill the queue
50  _heap_.reserve(list.size());
51  for (const auto& elt: list) {
52  insert(elt.first, elt.second);
53  }
54 
55  // for debugging purposes
56  GUM_CONSTRUCTOR(MultiPriorityQueue);
57  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ MultiPriorityQueue() [3/5]

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

Copy constructor.

Parameters
fromThe gum::MultiPriorityQueue to copy.

Definition at line 61 of file multiPriorityQueue_tpl.h.

62  :
63  _heap_(from._heap_),
64  _indices_(from._indices_), _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
65  // for debugging purposes
66  GUM_CONS_CPY(MultiPriorityQueue);
67 
68  // fill the heap structure
69  for (const auto& val_and_index: _indices_) {
70  const Val* val = &(val_and_index.first);
71  const std::vector< Size >& vect = val_and_index.second;
72  for (auto index: vect) {
73  _heap_[index].second = val;
74  }
75  }
76  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.

◆ MultiPriorityQueue() [4/5]

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

generalized copy constructor

Generalized Copy constructor.

Parameters
fromThe gum::MultiPriorityQueue to copy.
Template Parameters
OtherAllocThe other gum::MultiPriorityQueue allocator.

Definition at line 81 of file multiPriorityQueue_tpl.h.

82  :
83  _indices_(from._indices_),
84  _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
85  // for debugging purposes
86  GUM_CONS_CPY(MultiPriorityQueue);
87 
88  // fill the heap structure
89  if (_nb_elements_) {
90  _heap_.reserve(from._heap_.size());
91  for (const auto& elt: from._heap_) {
92  _heap_.push_back(elt);
93  }
94  for (const auto& val_and_index: _indices_) {
95  const Val* val = &(val_and_index.first);
96  const std::vector< Size >& vect = val_and_index.second;
97  for (auto index: vect) {
98  _heap_[index].second = val;
99  }
100  }
101  }
102  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.

◆ MultiPriorityQueue() [5/5]

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

Move constructor.

Parameters
fromThe gum::MultiPriorityQueue to move.

Definition at line 106 of file multiPriorityQueue_tpl.h.

107  :
108  _heap_(std::move(from._heap_)),
109  _indices_(std::move(from._indices_)), _nb_elements_(std::move(from._nb_elements_)),
110  _cmp_(std::move(from._cmp_)) {
111  // for debugging purposes
112  GUM_CONS_MOV(MultiPriorityQueue);
113  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.

◆ ~MultiPriorityQueue()

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

Class destructor.

Definition at line 117 of file multiPriorityQueue_tpl.h.

117  {
118  // for debugging purposes
119  GUM_DESTRUCTOR(MultiPriorityQueue);
120  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.

Member Function Documentation

◆ allValues()

template<typename Val , typename Priority , typename Cmp , typename Alloc >
INLINE const HashTable< Val, std::vector< Size > > & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::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 350 of file multiPriorityQueue_tpl.h.

350  {
351  return reinterpret_cast< const HashTable< Val, std::vector< Size > >& >(_indices_);
352  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.

◆ capacity()

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

242  {
243  return Size(_heap_.capacity());
244  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ clear()

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

Removes all the elements from the queue.

Definition at line 257 of file multiPriorityQueue_tpl.h.

257  {
258  _nb_elements_ = 0;
259  _heap_.clear();
260  _indices_.clear();
261  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
void clear()
Removes all the elements in the hash table.
Size _nb_elements_
The number of elements in the heap.

◆ contains()

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

482  {
483  return _indices_.exists(val);
484  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.

◆ emplace() [1/2]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
template<typename... Args>
Size gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::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.

◆ emplace() [2/2]

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

Definition at line 468 of file multiPriorityQueue_tpl.h.

468  {
469  std::pair< Val, Priority > new_elt
470  = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
471  return insert(std::move(new_elt.first), std::move(new_elt.second));
472  }
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.

◆ empty()

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

Indicates whether the priority queue is empty.

Returns
Indicates whether the priority queue is empty.

Definition at line 476 of file multiPriorityQueue_tpl.h.

476  {
477  return (_nb_elements_ == 0);
478  }
Size _nb_elements_
The number of elements in the heap.

◆ erase()

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

324  {
325  try {
326  eraseByPos(_indices_[val][0]);
327  } catch (NotFound&) {}
328  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

◆ eraseByPos()

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

265  {
266  if (index >= _nb_elements_) return;
267 
268  // remove the element from the hashtable
269  const Val& del_val = *(_heap_[index].second);
270  std::vector< Size >& vect_index = _indices_[del_val];
271  if (vect_index.size() == 1)
272  _indices_.erase(del_val);
273  else {
274  for (auto& v_index: vect_index) {
275  if (v_index == index) {
276  v_index = vect_index.back();
277  vect_index.pop_back();
278  break;
279  }
280  }
281  }
282 
283  // put the last element at the "index" location
284  std::pair< Priority, const Val* > last = std::move(_heap_.back());
285  _heap_.pop_back();
286  --_nb_elements_;
287 
288  if (!_nb_elements_ || (index == _nb_elements_)) return;
289 
290  // restore the heap property
291  Size i = index;
292 
293  for (Size j = (index << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
294  // let j be the max child
295  if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
296 
297  // if "last" is lower than heap[j], "last" must be stored at index i
298  if (_cmp_(last.first, _heap_[j].first)) break;
299 
300  // else pull up the jth node
301  _heap_[i] = std::move(_heap_[j]);
302  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
303  for (auto& v_index: vect_index) {
304  if (v_index == j) {
305  v_index = i;
306  break;
307  }
308  }
309  }
310 
311  // put "last" back into the heap
312  _heap_[i] = std::move(last);
313  std::vector< Size >& last_indices = _indices_[*(_heap_[i].second)];
314  for (auto& v_index: last_indices) {
315  if (v_index == _nb_elements_) {
316  v_index = i;
317  break;
318  }
319  }
320  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ eraseTop()

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

332  {
333  eraseByPos(0);
334  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

◆ insert() [1/2]

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

357  {
358  // create the entry in the indices hashtable
359  const Val* new_val;
360  std::vector< Size >* new_vect;
361  if (!_indices_.exists(val)) {
362  auto& new_elt = _indices_.insert(val, std::vector< Size >());
363  new_val = &(new_elt.first);
364  new_vect = &(new_elt.second);
365  } else {
366  new_val = &(_indices_.key(val));
367  new_vect = &(_indices_[val]);
368  }
369 
370  try {
371  new_vect->push_back(0);
372  } catch (...) {
373  if (new_vect->empty()) { _indices_.erase(val); }
374  throw;
375  }
376 
377  try {
378  _heap_.push_back(std::pair< Priority, const Val* >(priority, new_val));
379  } catch (...) {
380  if (new_vect->size() == 1) { _indices_.erase(val); }
381  throw;
382  }
383 
384  std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
385  ++_nb_elements_;
386 
387  // restore the heap property
388  Size i = _nb_elements_ - 1;
389 
390  for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
391  i = j, j = (j - 1) >> 1) {
392  _heap_[i] = std::move(_heap_[j]);
393  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
394  for (auto& index: vect_index) {
395  if (index == j) {
396  index = i;
397  break;
398  }
399  }
400  }
401 
402  // put the new bucket into the heap
403  _heap_[i].first = std::move(new_heap_val.first);
404  _heap_[i].second = new_val;
405  new_vect->back() = i;
406 
407  return i;
408  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const Key & key(const Key &key) const
Returns a reference on a given key.
Cmp _cmp_
Comparison function.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ insert() [2/2]

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

412  {
413  // create the entry in the indices hashtable
414  const Val* new_val;
415  std::vector< Size >* new_vect;
416  if (!_indices_.exists(val)) {
417  auto& new_elt = _indices_.insert(std::move(val), std::vector< Size >());
418  new_val = &(new_elt.first);
419  new_vect = &(new_elt.second);
420  } else {
421  new_val = &(_indices_.key(val));
422  new_vect = &(_indices_[val]);
423  }
424 
425  try {
426  new_vect->push_back(0);
427  } catch (...) {
428  if (new_vect->empty()) { _indices_.erase(*new_val); }
429  throw;
430  }
431 
432  try {
433  _heap_.push_back(std::pair< Priority, const Val* >(std::move(priority), new_val));
434  } catch (...) {
435  if (new_vect->size() == 1) { _indices_.erase(*new_val); }
436  throw;
437  }
438 
439  std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
440  ++_nb_elements_;
441 
442  // restore the heap property
443  Size i = _nb_elements_ - 1;
444 
445  for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
446  i = j, j = (j - 1) >> 1) {
447  _heap_[i] = std::move(_heap_[j]);
448  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
449  for (auto& index: vect_index) {
450  if (index == j) {
451  index = i;
452  break;
453  }
454  }
455  }
456 
457  // put the new bucket into the heap
458  _heap_[i].first = std::move(new_heap_val.first);
459  _heap_[i].second = new_val;
460  new_vect->back() = i;
461 
462  return i;
463  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const Key & key(const Key &key) const
Returns a reference on a given key.
Cmp _cmp_
Comparison function.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ operator=() [1/3]

template<typename Val, typename Priority, typename Cmp, typename Alloc>
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & gum::MultiPriorityQueue< Val, Priority, Cmp, Alloc >::operator= ( const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &  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 125 of file multiPriorityQueue_tpl.h.

126  {
127  // for debugging purposes
128  GUM_OP_CPY(MultiPriorityQueue);
129 
130  try {
131  // set the comprison function
132  _cmp_ = from._cmp_;
133 
134  // copy the indices and the heap
135  _indices_ = from._indices_;
136  _heap_ = from._heap_;
137  _nb_elements_ = from._nb_elements_;
138 
139  // restore the link between _indices_ and _heap_
140  for (const auto& val_and_index: _indices_) {
141  const Val* val = &(val_and_index.first);
142  const std::vector< Size >& vect = val_and_index.second;
143  for (auto index: vect) {
144  _heap_[index].second = val;
145  }
146  }
147  } catch (...) {
148  _heap_.clear();
149  _indices_.clear();
150  _nb_elements_ = 0;
151 
152  throw;
153  }
154 
155  return *this;
156  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.

◆ operator=() [2/3]

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

Generalized 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.
Template Parameters
OtherAllocThe other gum::MultiPriorityQueue allocator.

Definition at line 162 of file multiPriorityQueue_tpl.h.

163  {
164  // for debugging purposes
165  GUM_OP_CPY(MultiPriorityQueue);
166 
167  try {
168  // set the comprison function
169  _cmp_ = from._cmp_;
170 
171  // copy the indices and the heap
172  _indices_ = from._indices_;
173  _nb_elements_ = from._nb_elements_;
174 
175  // restore the link between _indices_ and _heap_
176  _heap_.clear();
177  _heap_.reserve(from._heap_.size());
178  for (const auto& elt: from._heap_) {
179  _heap_.push_back(elt);
180  }
181  for (const auto& val_and_index: _indices_) {
182  const Val* val = &(val_and_index.first);
183  const std::vector< Size >& vect = val_and_index.second;
184  for (auto index: vect) {
185  _heap_[index].second = val;
186  }
187  }
188  } catch (...) {
189  _heap_.clear();
190  _indices_.clear();
191  _nb_elements_ = 0;
192 
193  throw;
194  }
195 
196  return *this;
197  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.

◆ operator=() [3/3]

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

Move operator.

Parameters
fromThe gum::MultiPriorityQueue to copy.

Definition at line 202 of file multiPriorityQueue_tpl.h.

203  {
204  // avoid self assignment
205  if (this != &from) {
206  // for debugging purposes
207  GUM_OP_MOV(MultiPriorityQueue);
208 
209  _cmp_ = std::move(from._cmp_);
210  _indices_ = std::move(from._indices_);
211  _heap_ = std::move(from._heap_);
212  _nb_elements_ = std::move(from._nb_elements_);
213  }
214 
215  return *this;
216  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.

◆ operator[]()

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

488  {
489  if (index >= _nb_elements_) {
490  GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue")
491  }
492 
493  return *(_heap_[index].second);
494  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ pop()

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

338  {
339  if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
340 
341  Val v = *(_heap_[0].second);
342  eraseByPos(0);
343 
344  return v;
345  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
Size _nb_elements_
The number of elements in the heap.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ priority()

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

651  {
652  return _heap_[_indices_[elt][0]].first;
653  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.

◆ resize()

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

248  {
249  if (new_size < _nb_elements_) return;
250 
251  _heap_.reserve(new_size);
252  _indices_.resize(new_size / 2);
253  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.

◆ setPriority()

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

640  {
641  std::vector< Size >& vect_index = _indices_[elt];
642 
643  for (auto index: vect_index) {
644  setPriorityByPos(index, new_priority);
645  }
646  }
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.

◆ setPriorityByPos() [1/2]

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

518  {
519  // check whether the element the priority of which should be changed exists
520  if (index >= _nb_elements_) {
521  GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue")
522  }
523 
524  // get the element itself
525  const Val* val = _heap_[index].second;
526 
527  // restore the heap property
528  Size i = index;
529 
530  // move val upward if needed
531  for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
532  i = j, j = (j - 1) >> 1) {
533  _heap_[i] = std::move(_heap_[j]);
534  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
535  for (auto& idx: vect_index) {
536  if (idx == j) {
537  idx = i;
538  break;
539  }
540  }
541  }
542 
543  // move val downward if needed
544  for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
545  // let j be the max child
546  if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
547 
548  // if "val" is lower than heap[j], "val" must be stored at index i
549  if (_cmp_(new_priority, _heap_[j].first)) break;
550 
551  // else pull up the jth node
552  _heap_[i] = std::move(_heap_[j]);
553  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
554  for (auto& idx: vect_index) {
555  if (idx == j) {
556  idx = i;
557  break;
558  }
559  }
560  }
561 
562  // update the index of val
563  _heap_[i].first = new_priority;
564  _heap_[i].second = val;
565  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
566  for (auto& idx: vect_index) {
567  if (idx == index) {
568  idx = i;
569  break;
570  }
571  }
572 
573  return i;
574  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setPriorityByPos() [2/2]

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

579  {
580  // check whether the element the priority of which should be changed exists
581  if (index >= _nb_elements_) {
582  GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue")
583  }
584 
585  // get the element itself
586  const Val* val = _heap_[index].second;
587 
588  // restore the heap property
589  Size i = index;
590 
591  // move val upward if needed
592  for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
593  i = j, j = (j - 1) >> 1) {
594  _heap_[i] = std::move(_heap_[j]);
595  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
596  for (auto& idx: vect_index) {
597  if (idx == j) {
598  idx = i;
599  break;
600  }
601  }
602  }
603 
604  // move val downward if needed
605  for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
606  // let j be the max child
607  if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
608 
609  // if "val" is lower than heap[j], "val" must be stored at index i
610  if (_cmp_(new_priority, _heap_[j].first)) break;
611 
612  // else pull up the jth node
613  _heap_[i] = std::move(_heap_[j]);
614  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
615  for (auto& idx: vect_index) {
616  if (idx == j) {
617  idx = i;
618  break;
619  }
620  }
621  }
622 
623  // update the index of val
624  _heap_[i].first = std::move(new_priority);
625  _heap_[i].second = val;
626  std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
627  for (auto& idx: vect_index) {
628  if (idx == index) {
629  idx = i;
630  break;
631  }
632  }
633 
634  return i;
635  }
HashTable< Val, std::vector< Size >, IndexAlloc > _indices_
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Cmp _cmp_
Comparison function.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ size()

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

236  {
237  return _nb_elements_;
238  }
Size _nb_elements_
The number of elements in the heap.

◆ top()

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

220  {
221  if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
222 
223  return *(_heap_[0].second);
224  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ topPriority()

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

Returns the priority of the top element.

Exceptions
NotFoundRaised if the queue is empty.

Definition at line 228 of file multiPriorityQueue_tpl.h.

228  {
229  if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
230 
231  return _heap_[0].first;
232  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ toString()

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

Displays the content of the queue.

Returns
Returns the content of the queue.

Definition at line 498 of file multiPriorityQueue_tpl.h.

498  {
499  bool deja = false;
500  std::stringstream stream;
501  stream << "[";
502 
503  for (Size i = 0; i != _nb_elements_; ++i, deja = true) {
504  if (deja) stream << " , ";
505 
506  stream << "(" << _heap_[i].first << " , " << *(_heap_[i].second) << ")";
507  }
508 
509  stream << "]";
510 
511  return stream.str();
512  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

Friends And Related Function Documentation

◆ MultiPriorityQueue

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
template<typename V , typename P , typename C , typename A >
friend class MultiPriorityQueue
friend

Making all MultiPriorityQueue friend with themselves.

Definition at line 128 of file multiPriorityQueue.h.

Member Data Documentation

◆ _cmp_

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

Comparison function.

Definition at line 482 of file multiPriorityQueue.h.

◆ _heap_

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

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

Definition at line 473 of file multiPriorityQueue.h.

◆ _indices_

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

A hashtable for quickly finding the elements by their value.

Definition at line 476 of file multiPriorityQueue.h.

◆ _nb_elements_

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

The number of elements in the heap.

Definition at line 479 of file multiPriorityQueue.h.


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