aGrUM  0.20.2
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 126 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 140 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 138 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 136 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 139 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 149 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 145 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 137 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 135 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 134 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.

38  :
39  indices__(capacity >> 1, true, false),
40  cmp__(compare) {
41  heap__.reserve(capacity);
42 
43  // for debugging purposes
44  GUM_CONSTRUCTOR(MultiPriorityQueue);
45  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
Size capacity() const noexcept
Return the size of the internal structure storing the priority queue.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
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 49 of file multiPriorityQueue_tpl.h.

50  :
51  indices__(Size(list.size()) / 2, true, false) {
52  // fill the queue
53  heap__.reserve(list.size());
54  for (const auto& elt: list) {
55  insert(elt.first, elt.second);
56  }
57 
58  // for debugging purposes
59  GUM_CONSTRUCTOR(MultiPriorityQueue);
60  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
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 64 of file multiPriorityQueue_tpl.h.

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

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

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

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

111  :
112  heap__(std::move(from.heap__)),
113  indices__(std::move(from.indices__)),
114  nb_elements__(std::move(from.nb_elements__)), cmp__(std::move(from.cmp__)) {
115  // for debugging purposes
116  GUM_CONS_MOV(MultiPriorityQueue);
117  }
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
Size nb_elements__
The number of elements in the heap.
Cmp cmp__
Comparison function.

◆ ~MultiPriorityQueue()

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

Class destructor.

Definition at line 121 of file multiPriorityQueue_tpl.h.

121  {
122  // for debugging purposes
123  GUM_DESTRUCTOR(MultiPriorityQueue);
124  }
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 360 of file multiPriorityQueue_tpl.h.

360  {
361  return reinterpret_cast< const HashTable< Val, std::vector< Size > >& >(
362  indices__);
363  }
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 249 of file multiPriorityQueue_tpl.h.

249  {
250  return Size(heap__.capacity());
251  }
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 265 of file multiPriorityQueue_tpl.h.

265  {
266  nb_elements__ = 0;
267  heap__.clear();
268  indices__.clear();
269  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
Size nb_elements__
The number of elements in the heap.
void clear()
Removes all the elements in the hash table.

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

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

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

486  {
487  std::pair< Val, Priority > new_elt
488  = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
489  return insert(std::move(new_elt.first), std::move(new_elt.second));
490  }
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 495 of file multiPriorityQueue_tpl.h.

495  {
496  return (nb_elements__ == 0);
497  }
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 334 of file multiPriorityQueue_tpl.h.

334  {
335  try {
336  eraseByPos(indices__[val][0]);
337  } catch (NotFound&) {}
338  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.

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

273  {
274  if (index >= nb_elements__) return;
275 
276  // remove the element from the hashtable
277  const Val& del_val = *(heap__[index].second);
278  std::vector< Size >& vect_index = indices__[del_val];
279  if (vect_index.size() == 1)
280  indices__.erase(del_val);
281  else {
282  for (auto& v_index: vect_index) {
283  if (v_index == index) {
284  v_index = vect_index.back();
285  vect_index.pop_back();
286  break;
287  }
288  }
289  }
290 
291  // put the last element at the "index" location
292  std::pair< Priority, const Val* > last = std::move(heap__.back());
293  heap__.pop_back();
294  --nb_elements__;
295 
296  if (!nb_elements__ || (index == nb_elements__)) return;
297 
298  // restore the heap property
299  Size i = index;
300 
301  for (Size j = (index << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
302  // let j be the max child
303  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
304  ++j;
305 
306  // if "last" is lower than heap[j], "last" must be stored at index i
307  if (cmp__(last.first, heap__[j].first)) break;
308 
309  // else pull up the jth node
310  heap__[i] = std::move(heap__[j]);
311  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
312  for (auto& v_index: vect_index) {
313  if (v_index == j) {
314  v_index = i;
315  break;
316  }
317  }
318  }
319 
320  // put "last" back into the heap
321  heap__[i] = std::move(last);
322  std::vector< Size >& last_indices = indices__[*(heap__[i].second)];
323  for (auto& v_index: last_indices) {
324  if (v_index == nb_elements__) {
325  v_index = i;
326  break;
327  }
328  }
329  }
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.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
Size nb_elements__
The number of elements in the heap.
Cmp cmp__
Comparison function.
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 342 of file multiPriorityQueue_tpl.h.

342  {
343  eraseByPos(0);
344  }
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 367 of file multiPriorityQueue_tpl.h.

369  {
370  // create the entry in the indices hashtable
371  const Val* new_val;
372  std::vector< Size >* new_vect;
373  if (!indices__.exists(val)) {
374  auto& new_elt = indices__.insert(val, std::vector< Size >());
375  new_val = &(new_elt.first);
376  new_vect = &(new_elt.second);
377  } else {
378  new_val = &(indices__.key(val));
379  new_vect = &(indices__[val]);
380  }
381 
382  try {
383  new_vect->push_back(0);
384  } catch (...) {
385  if (new_vect->empty()) { indices__.erase(val); }
386  throw;
387  }
388 
389  try {
390  heap__.push_back(std::pair< Priority, const Val* >(priority, new_val));
391  } catch (...) {
392  if (new_vect->size() == 1) { indices__.erase(val); }
393  throw;
394  }
395 
396  std::pair< Priority, const Val* > new_heap_val
397  = std::move(heap__[nb_elements__]);
398  ++nb_elements__;
399 
400  // restore the heap property
401  Size i = nb_elements__ - 1;
402 
403  for (Size j = (i - 1) >> 1; i && cmp__(new_heap_val.first, heap__[j].first);
404  i = j, j = (j - 1) >> 1) {
405  heap__[i] = std::move(heap__[j]);
406  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
407  for (auto& index: vect_index) {
408  if (index == j) {
409  index = i;
410  break;
411  }
412  }
413  }
414 
415  // put the new bucket into the heap
416  heap__[i].first = std::move(new_heap_val.first);
417  heap__[i].second = new_val;
418  new_vect->back() = i;
419 
420  return i;
421  }
void erase(const Key &key)
Removes a given element from the hash table.
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.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
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.
Cmp cmp__
Comparison function.
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 426 of file multiPriorityQueue_tpl.h.

427  {
428  // create the entry in the indices hashtable
429  const Val* new_val;
430  std::vector< Size >* new_vect;
431  if (!indices__.exists(val)) {
432  auto& new_elt = indices__.insert(std::move(val), std::vector< Size >());
433  new_val = &(new_elt.first);
434  new_vect = &(new_elt.second);
435  } else {
436  new_val = &(indices__.key(val));
437  new_vect = &(indices__[val]);
438  }
439 
440  try {
441  new_vect->push_back(0);
442  } catch (...) {
443  if (new_vect->empty()) { indices__.erase(*new_val); }
444  throw;
445  }
446 
447  try {
448  heap__.push_back(
449  std::pair< Priority, const Val* >(std::move(priority), new_val));
450  } catch (...) {
451  if (new_vect->size() == 1) { indices__.erase(*new_val); }
452  throw;
453  }
454 
455  std::pair< Priority, const Val* > new_heap_val
456  = std::move(heap__[nb_elements__]);
457  ++nb_elements__;
458 
459  // restore the heap property
460  Size i = nb_elements__ - 1;
461 
462  for (Size j = (i - 1) >> 1; i && cmp__(new_heap_val.first, heap__[j].first);
463  i = j, j = (j - 1) >> 1) {
464  heap__[i] = std::move(heap__[j]);
465  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
466  for (auto& index: vect_index) {
467  if (index == j) {
468  index = i;
469  break;
470  }
471  }
472  }
473 
474  // put the new bucket into the heap
475  heap__[i].first = std::move(new_heap_val.first);
476  heap__[i].second = new_val;
477  new_vect->back() = i;
478 
479  return i;
480  }
void erase(const Key &key)
Removes a given element from the hash table.
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.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
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.
Cmp cmp__
Comparison function.
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 129 of file multiPriorityQueue_tpl.h.

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

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

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

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

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

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

509  {
510  if (index >= nb_elements__) {
511  GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue");
512  }
513 
514  return *(heap__[index].second);
515  }
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:54

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

348  {
349  if (!nb_elements__) { GUM_ERROR(NotFound, "empty priority queue"); }
350 
351  Val v = *(heap__[0].second);
352  eraseByPos(0);
353 
354  return v;
355  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
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:54

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

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

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

256  {
257  if (new_size < nb_elements__) return;
258 
259  heap__.reserve(new_size);
260  indices__.resize(new_size / 2);
261  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of 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.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
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 663 of file multiPriorityQueue_tpl.h.

665  {
666  std::vector< Size >& vect_index = indices__[elt];
667 
668  for (auto index: vect_index) {
669  setPriorityByPos(index, new_priority);
670  }
671  }
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 537 of file multiPriorityQueue_tpl.h.

539  {
540  // check whether the element the priority of which should be changed exists
541  if (index >= nb_elements__) {
542  GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue");
543  }
544 
545  // get the element itself
546  const Val* val = heap__[index].second;
547 
548  // restore the heap property
549  Size i = index;
550 
551  // move val upward if needed
552  for (Size j = (i - 1) >> 1; i && cmp__(new_priority, heap__[j].first);
553  i = j, j = (j - 1) >> 1) {
554  heap__[i] = std::move(heap__[j]);
555  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
556  for (auto& idx: vect_index) {
557  if (idx == j) {
558  idx = i;
559  break;
560  }
561  }
562  }
563 
564  // move val downward if needed
565  for (Size j = (i << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
566  // let j be the max child
567  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
568  ++j;
569 
570  // if "val" is lower than heap[j], "val" must be stored at index i
571  if (cmp__(new_priority, heap__[j].first)) break;
572 
573  // else pull up the jth node
574  heap__[i] = std::move(heap__[j]);
575  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
576  for (auto& idx: vect_index) {
577  if (idx == j) {
578  idx = i;
579  break;
580  }
581  }
582  }
583 
584  // update the index of val
585  heap__[i].first = new_priority;
586  heap__[i].second = val;
587  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
588  for (auto& idx: vect_index) {
589  if (idx == index) {
590  idx = i;
591  break;
592  }
593  }
594 
595  return i;
596  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
Size nb_elements__
The number of elements in the heap.
Cmp cmp__
Comparison function.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

602  {
603  // check whether the element the priority of which should be changed exists
604  if (index >= nb_elements__) {
605  GUM_ERROR(NotFound, "not enough elements in the MultiPriorityQueue");
606  }
607 
608  // get the element itself
609  const Val* val = heap__[index].second;
610 
611  // restore the heap property
612  Size i = index;
613 
614  // move val upward if needed
615  for (Size j = (i - 1) >> 1; i && cmp__(new_priority, heap__[j].first);
616  i = j, j = (j - 1) >> 1) {
617  heap__[i] = std::move(heap__[j]);
618  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
619  for (auto& idx: vect_index) {
620  if (idx == j) {
621  idx = i;
622  break;
623  }
624  }
625  }
626 
627  // move val downward if needed
628  for (Size j = (i << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
629  // let j be the max child
630  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
631  ++j;
632 
633  // if "val" is lower than heap[j], "val" must be stored at index i
634  if (cmp__(new_priority, heap__[j].first)) break;
635 
636  // else pull up the jth node
637  heap__[i] = std::move(heap__[j]);
638  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
639  for (auto& idx: vect_index) {
640  if (idx == j) {
641  idx = i;
642  break;
643  }
644  }
645  }
646 
647  // update the index of val
648  heap__[i].first = std::move(new_priority);
649  heap__[i].second = val;
650  std::vector< Size >& vect_index = indices__[*(heap__[i].second)];
651  for (auto& idx: vect_index) {
652  if (idx == index) {
653  idx = i;
654  break;
655  }
656  }
657 
658  return i;
659  }
std::vector< std::pair< Priority, const Val *>, HeapAlloc > heap__
An array storing all the elements of the heap as well as their score.
HashTable< Val, std::vector< Size >, IndexAlloc > indices__
A hashtable for quickly finding the elements by their value.
Size nb_elements__
The number of elements in the heap.
Cmp cmp__
Comparison function.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

242  {
243  return nb_elements__;
244  }
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 224 of file multiPriorityQueue_tpl.h.

224  {
225  if (!nb_elements__) { GUM_ERROR(NotFound, "empty priority queue"); }
226 
227  return *(heap__[0].second);
228  }
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:54

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

233  {
234  if (!nb_elements__) { GUM_ERROR(NotFound, "empty priority queue"); }
235 
236  return heap__[0].first;
237  }
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:54

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

519  {
520  bool deja = false;
521  std::stringstream stream;
522  stream << "[";
523 
524  for (Size i = 0; i != nb_elements__; ++i, deja = true) {
525  if (deja) stream << " , ";
526 
527  stream << "(" << heap__[i].first << " , " << *(heap__[i].second) << ")";
528  }
529 
530  stream << "]";
531 
532  return stream.str();
533  }
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 129 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 488 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 479 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 482 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 485 of file multiPriorityQueue.h.


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