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

The internal class for representing priority queues. More...

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

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

Public Member Functions

template<typename... Args>
INLINE Size emplace (Args &&... args)
 
template<typename Val, typename Priority, typename Cmp, typename Alloc>
 PriorityQueueImplementation (const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > &from)
 
template<typename OtherAlloc >
 PriorityQueueImplementation (const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > &from)
 
template<typename Val, typename Priority, typename Cmp, typename Alloc>
 PriorityQueueImplementation (PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > &&from)
 
template<typename OtherAlloc >
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > & operator= (const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > &from)
 
Operators
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator= (const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &from)
 Copy operator. More...
 
template<typename OtherAlloc >
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator= (const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen > &from)
 Generalized copy operator. More...
 
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator= (PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &&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...
 
void setPriority (const Val &elt, 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...
 
const Priority & priorityByPos (Size index) const
 Returns the priority of the value passed in argument. More...
 
void clear ()
 Removes all the elements from the queue. More...
 
const HashTable< Val, Size > & allValues () const noexcept
 Returns a 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
 Returns 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 IndexAllocator = typename Alloc::template rebind< std::pair< Val, Size > >::other
 
using HeapAllocator = typename Alloc::template rebind< std::pair< Priority, const Val *> >::other
 
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

class PriorityQueue< Val, Priority, Cmp, Alloc >
 All gum::PriorityQueue are friends with themselves. More...
 
template<typename V , typename P , typename C , typename A , bool g>
class PriorityQueueImplementation
 All gum::PriorityQueueImplementation are friends with themselves. More...
 

Detailed Description

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
class gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >

The internal class for representing priority queues.

Priority Queues have different implementations depending on the nature of the values they store. Basically, scalar values can lead to optimization of the code whereas general types like classes cannot. The current class is used for general types (and therefore for classes). The user shall not use directly the implementation but rather use the PriorityQueue class. The latter will be assigned the best implementation at compile time.

Template Parameters
ValThe values type.
PriorityThe priorities type.
CmpThe priorities comparator.
AllocThe values allocator.
GenUsed for metaprogramation, for scalar and non-scalar priority queues.

Definition at line 84 of file priorityQueue.h.

Member Typedef Documentation

◆ allocator_type

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::allocator_type = Alloc

Types for STL compliance.

Definition at line 101 of file priorityQueue.h.

◆ const_pointer

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::const_pointer = const Val*

Types for STL compliance.

Definition at line 99 of file priorityQueue.h.

◆ const_reference

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::const_reference = const Val&

Types for STL compliance.

Definition at line 97 of file priorityQueue.h.

◆ difference_type

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 100 of file priorityQueue.h.

◆ HeapAllocator

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::HeapAllocator = typename Alloc::template rebind< std::pair< Priority, const Val* > >::other

Definition at line 110 of file priorityQueue.h.

◆ IndexAllocator

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::IndexAllocator = typename Alloc::template rebind< std::pair< Val, Size > >::other

Definition at line 106 of file priorityQueue.h.

◆ pointer

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::pointer = Val*

Types for STL compliance.

Definition at line 98 of file priorityQueue.h.

◆ reference

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::reference = Val&

Types for STL compliance.

Definition at line 96 of file priorityQueue.h.

◆ value_type

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
using gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::value_type = Val

Types for STL compliance.

Definition at line 95 of file priorityQueue.h.

Constructor & Destructor Documentation

◆ PriorityQueueImplementation() [1/8]

template<typename Val , typename Priority , typename Cmp, typename Alloc >
INLINE gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::PriorityQueueImplementation ( Cmp  compare,
Size  capacity 
)
explicitprivate

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

44  :
45  indices__(capacity >> 1, true, true),
46  cmp__(compare) {
47  heap__.reserve(capacity);
48 
49  // for debugging purposes
50  GUM_CONSTRUCTOR(PriorityQueueImplementation);
51  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.
Cmp cmp__
Comparison function.
Size capacity() const noexcept
Returns the size of the internal structure storing the priority queue.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [2/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc >
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::PriorityQueueImplementation ( std::initializer_list< std::pair< Val, Priority > >  list)
explicitprivate

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

61  :
62  indices__(Size(list.size()) / 2, true, true) {
63  // fill the queue
64  heap__.reserve(list.size());
65  for (const auto& elt: list) {
66  insert(elt.first, elt.second);
67  }
68 
69  // for debugging purposes
70  GUM_CONSTRUCTOR(PriorityQueueImplementation);
71  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [3/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation ( const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &  from)
private

Copy constructor.

Parameters
fromThe gum::PriorityQueueImplementation to copy.

Definition at line 80 of file priorityQueue_tpl.h.

82  :
83  heap__(from.heap__),
84  indices__(from.indices__), nb_elements__(from.nb_elements__),
85  cmp__(from.cmp__) {
86  // fill the heap structure
87  for (const auto& elt: indices__) {
88  heap__[elt.second].second = &(elt.first);
89  }
90 
91  // for debugging purposes
92  GUM_CONS_CPY(PriorityQueueImplementation);
93  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [4/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc , bool Gen>
template<typename OtherAlloc >
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation ( const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen > &  from)
private

Generalized copy constructor.

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

Definition at line 103 of file priorityQueue_tpl.h.

105  :
106  indices__(from.indices__),
107  nb_elements__(from.nb_elements__), cmp__(from.cmp__) {
108  // fill the heap structure
109  if (nb_elements__) {
110  heap__.reserve(from.heap__.size());
111  for (const auto& elt: from.heap__) {
112  heap__.push_back(elt);
113  }
114  for (const auto& elt: indices__) {
115  heap__[elt.second].second = &(elt.first);
116  }
117  }
118 
119  // for debugging purposes
120  GUM_CONS_CPY(PriorityQueueImplementation);
121  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [5/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation ( PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &&  from)
private

Move constructor.

Parameters
fromThe gum::PriorityQueueImplementation to move.

Definition at line 130 of file priorityQueue_tpl.h.

131  :
132  heap__(std::move(from.heap__)),
133  indices__(std::move(from.indices__)),
134  nb_elements__(std::move(from.nb_elements__)), cmp__(std::move(from.cmp__)) {
135  // for debugging purposes
136  GUM_CONS_MOV(PriorityQueueImplementation);
137  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ ~PriorityQueueImplementation()

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

Class destructor.

Definition at line 146 of file priorityQueue_tpl.h.

146  {
147  // for debugging purposes
148  GUM_DESTRUCTOR(PriorityQueueImplementation);
149  }
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [6/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
template<typename Val, typename Priority, typename Cmp, typename Alloc>
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation ( const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > &  from)

Definition at line 780 of file priorityQueue_tpl.h.

782  :
783  heap__(from.heap__),
784  indices__(from.indices__), nb_elements__(from.nb_elements__),
785  cmp__(from.cmp__) {
786  // for debugging purposes
787  GUM_CONS_CPY(PriorityQueueImplementation);
788  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [7/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
template<typename OtherAlloc >
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation ( const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > &  from)

Definition at line 794 of file priorityQueue_tpl.h.

796  :
797  indices__(from.indices__),
798  nb_elements__(from.nb_elements__), cmp__(from.cmp__) {
799  // fill the heap structure
800  if (nb_elements__) {
801  heap__.reserve(from.heap__.size());
802  for (const auto& elt: from.heap__) {
803  heap__.push_back(elt);
804  }
805  }
806 
807  // for debugging purposes
808  GUM_CONS_CPY(PriorityQueueImplementation);
809  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ PriorityQueueImplementation() [8/8]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
template<typename Val, typename Priority, typename Cmp, typename Alloc>
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::PriorityQueueImplementation ( PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > &&  from)

Definition at line 814 of file priorityQueue_tpl.h.

815  :
816  heap__(std::move(from.heap__)),
817  indices__(std::move(from.indices__)),
818  nb_elements__(std::move(from.nb_elements__)), cmp__(std::move(from.cmp__)) {
819  // for debugging purposes
820  GUM_CONS_MOV(PriorityQueueImplementation);
821  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

Member Function Documentation

◆ allValues()

template<typename Val , typename Priority , typename Cmp , typename Alloc >
INLINE const HashTable< Val, Size > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::allValues ( ) const
noexcept

Returns a hashtable the keys of which are the values stored in the queue.

The keys of the hashtable correspond to the values stored in the priority queue and, for each key, the corresponding value is the index in the queue where we can find the key.

Returns
Returns a hashtable the keys of which are the values stored in the queue.

Definition at line 422 of file priorityQueue_tpl.h.

423  {
424  return reinterpret_cast< const HashTable< Val, Size >& >(indices__);
425  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.

◆ capacity()

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

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

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

Definition at line 302 of file priorityQueue_tpl.h.

303  {
304  return Size(heap__.capacity());
305  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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 >
INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::clear ( )

Removes all the elements from the queue.

Definition at line 329 of file priorityQueue_tpl.h.

329  {
330  nb_elements__ = 0;
331  heap__.clear();
332  indices__.clear();
333  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
void clear()
Removes all the elements in the hash table.

◆ contains()

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

Indicates whether the priority queue contains a given value.

Parameters
valThe value to look for.
Returns
Returns true if val is in the priority queue.

Definition at line 549 of file priorityQueue_tpl.h.

550  {
551  return indices__.exists(val);
552  }
HashTable< Val, Size, IndexAllocator > 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, typename Cmp, typename Alloc, bool Gen>
template<typename... Args>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::emplace ( Args &&...  args)

Emplace a new element into the priority queue.

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

Template Parameters
ArgsThe emplace arguments types.
Parameters
argsThe emplace arguments.
Returns
Returns the index of the element inserted into the priority queue.
Exceptions
DuplicateElementRaised if the element already exists.

◆ emplace() [2/2]

template<typename Val , typename Priority , typename Cmp , typename Alloc >
template<typename... Args>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::emplace ( Args &&...  args)

Definition at line 523 of file priorityQueue_tpl.h.

524  {
525  std::pair< Val, Priority > new_elt
526  = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
527  return insert(std::move(new_elt.first), std::move(new_elt.second));
528  }
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::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::empty ( ) const
noexcept

Indicates whether the priority queue is empty.

Returns
Returns true if the priority queue is empty.

Definition at line 537 of file priorityQueue_tpl.h.

538  {
539  return (nb_elements__ == 0);
540  }
Size nb_elements__
The number of elements in the heap.

◆ erase()

template<typename Val, typename Priority , typename Cmp , typename Alloc , bool Gen>
INLINE void gum::PriorityQueueImplementation< 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 382 of file priorityQueue_tpl.h.

383  {
384  try {
385  eraseByPos(indices__[val]);
386  } catch (NotFound&) {}
387  }
HashTable< Val, Size, IndexAllocator > 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::PriorityQueueImplementation< 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 341 of file priorityQueue_tpl.h.

342  {
343  if (index >= nb_elements__) return;
344 
345  // remove the element from the hashtable
346  indices__.erase(*(heap__[index].second));
347 
348  // put the last element at the "index" location
349  std::pair< Priority, const Val* > last = std::move(heap__[nb_elements__ - 1]);
350  heap__.pop_back();
351  --nb_elements__;
352 
353  if (!nb_elements__ || (index == nb_elements__)) return;
354 
355  // restore the heap property
356  Size i = index;
357 
358  for (Size j = (index << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
359  // let j be the max child
360  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
361  ++j;
362 
363  // if "last" is lower than heap[j], "last" must be stored at index i
364  if (cmp__(last.first, heap__[j].first)) break;
365 
366  // else pull up the jth node
367  heap__[i] = std::move(heap__[j]);
368  indices__[*(heap__[i].second)] = i;
369  }
370 
371  // put "last" back into the heap
372  heap__[i] = std::move(last);
373  indices__[*(heap__[i].second)] = i;
374  }
HashTable< Val, Size, IndexAllocator > 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 *>, HeapAllocator > 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::PriorityQueueImplementation< 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 396 of file priorityQueue_tpl.h.

396  {
397  eraseByPos(0);
398  }
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 , bool Gen>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::insert ( const Val &  val,
const Priority &  priority 
)

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

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

Parameters
valThe value to insert.
priorityThe value's priority in the queue.
Returns
Returns the index of the element inserted into the priority queue.
Exceptions
DuplicateElementRaised if the element already exists.

Definition at line 434 of file priorityQueue_tpl.h.

436  {
437  // create the entry in the indices hashtable (if the element already exists,
438  // indices__.insert will raise a Duplicateelement exception)
440  = indices__.insert(val, 0);
441 
442  try {
443  heap__.push_back(
444  std::pair< Priority, const Val* >(priority, &new_elt.first));
445  } catch (...) {
446  indices__.erase(val);
447  throw;
448  }
449 
450  std::pair< Priority, const Val* > new_heap_val
451  = std::move(heap__[nb_elements__]);
452  ++nb_elements__;
453 
454  // restore the heap property
455  Size i = nb_elements__ - 1;
456 
457  for (Size j = (i - 1) >> 1; i && cmp__(new_heap_val.first, heap__[j].first);
458  i = j, j = (j - 1) >> 1) {
459  heap__[i] = std::move(heap__[j]);
460  indices__[*(heap__[i].second)] = i;
461  }
462 
463  // put the new bucket into the heap
464  heap__[i].first = std::move(new_heap_val.first);
465  heap__[i].second = &(new_elt.first);
466  new_elt.second = i;
467 
468  return i;
469  }
HashTable< Val, Size, IndexAllocator > 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.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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::pair< const Val, Size > value_type
Definition: hashTable.h:685
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 , bool Gen>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::insert ( Val &&  val,
Priority &&  priority 
)

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

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

Parameters
valThe value to insert.
priorityThe value's priority in the queue.
Returns
Returns the index of the element inserted into the priority queue.
Exceptions
DuplicateElementRaised if the element already exists.

Definition at line 478 of file priorityQueue_tpl.h.

480  {
481  // create the entry in the indices hashtable (if the element already exists,
482  // indices__.insert will raise a Duplicateelement exception)
484  = indices__.insert(std::move(val), 0);
485 
486  try {
487  heap__.push_back(
488  std::pair< Priority, const Val* >(std::move(priority), &(new_elt.first)));
489  } catch (...) {
490  indices__.erase(new_elt.first);
491  throw;
492  }
493 
494  std::pair< Priority, const Val* > new_heap_val
495  = std::move(heap__[nb_elements__]);
496  ++nb_elements__;
497 
498  // restore the heap property
499  Size i = nb_elements__ - 1;
500 
501  for (Size j = (i - 1) >> 1; i && cmp__(new_heap_val.first, heap__[j].first);
502  i = j, j = (j - 1) >> 1) {
503  heap__[i] = std::move(heap__[j]);
504  indices__[*(heap__[i].second)] = i;
505  }
506 
507  // put the new bucket into the heap
508  heap__[i].first = std::move(new_heap_val.first);
509  heap__[i].second = &(new_elt.first);
510  new_elt.second = i;
511 
512  return i;
513  }
HashTable< Val, Size, IndexAllocator > 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.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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::pair< const Val, Size > value_type
Definition: hashTable.h:685
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/4]

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

Definition at line 158 of file priorityQueue_tpl.h.

160  {
161  // avoid self assignment
162  if (this != &from) {
163  // for debugging purposes
164  GUM_OP_CPY(PriorityQueueImplementation);
165 
166  try {
167  // set the comparison function
168  cmp__ = from.cmp__;
169 
170  // copy the indices and the heap
171  indices__ = from.indices__;
172  heap__ = from.heap__;
173  nb_elements__ = from.nb_elements__;
174 
175  // restore the link between indices__ and heap__
176  for (const auto& elt: indices__) {
177  heap__[elt.second].second = &(elt.first);
178  }
179  } catch (...) {
180  heap__.clear();
181  indices__.clear();
182  nb_elements__ = 0;
183 
184  throw;
185  }
186  }
187 
188  return *this;
189  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ operator=() [2/4]

template<typename Val, typename Priority, typename Cmp, typename Alloc , bool Gen>
template<typename OtherAlloc >
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= ( const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen > &  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.

Template Parameters
OtherAllocThe other gum::PriorityQueueImplementation allocator.
Parameters
fromThe gum::PriorityQueueImplementation to copy.
Returns
Returns this gum::PriorityQueueImplementation.

Definition at line 199 of file priorityQueue_tpl.h.

201  {
202  // for debugging purposes
203  GUM_OP_CPY(PriorityQueueImplementation);
204 
205  try {
206  // set the comprison function
207  cmp__ = from.cmp__;
208 
209  // copy the indices and the heap
210  indices__ = from.indices__;
211  nb_elements__ = from.nb_elements__;
212 
213  heap__.clear();
214  if (nb_elements__) {
215  heap__.reserve(from.heap__.size());
216  for (const auto& elt: from.heap__) {
217  heap__.push_back(elt);
218  }
219  for (const auto& elt: indices__) {
220  heap__[elt.second].second = &(elt.first);
221  }
222  }
223  } catch (...) {
224  heap__.clear();
225  indices__.clear();
226  nb_elements__ = 0;
227 
228  throw;
229  }
230 
231  return *this;
232  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ operator=() [3/4]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
INLINE PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= ( PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &&  from)

Move operator.

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

Definition at line 241 of file priorityQueue_tpl.h.

242  {
243  // avoid self assignment
244  if (this != &from) {
245  // for debugging purposes
246  GUM_OP_MOV(PriorityQueueImplementation);
247 
248  indices__ = std::move(from.indices__);
249  heap__ = std::move(from.heap__);
250  cmp__ = std::move(from.cmp__);
251  nb_elements__ = std::move(from.nb_elements__);
252  }
253 
254  return *this;
255  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ operator=() [4/4]

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
template<typename OtherAlloc >
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true >& gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::operator= ( const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, true > &  from)

Definition at line 866 of file priorityQueue_tpl.h.

868  {
869  // for debugging purposes
870  GUM_OP_CPY(PriorityQueueImplementation);
871 
872  try {
873  // set the comprison function
874  cmp__ = from.cmp__;
875 
876  // copy the indices and the heap
877  indices__ = from.indices__;
878  nb_elements__ = from.nb_elements__;
879 
880  heap__.clear();
881  if (nb_elements__) {
882  heap__.reserve(from.heap__.size());
883  for (const auto& elt: from.heap__) {
884  heap__.push_back(elt);
885  }
886  }
887  } catch (...) {
888  heap__.clear();
889  indices__.clear();
890  nb_elements__ = 0;
891 
892  throw;
893  }
894 
895  return *this;
896  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:90

◆ operator[]()

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

562  {
563  if (index >= nb_elements__) {
564  GUM_ERROR(NotFound,
565  "not enough elements in the PriorityQueueImplementation");
566  }
567 
568  return *(heap__[index].second);
569  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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::PriorityQueueImplementation< 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 406 of file priorityQueue_tpl.h.

406  {
407  if (!nb_elements__) { GUM_ERROR(NotFound, "empty priority queue"); }
408 
409  Val v = *(heap__[0].second);
410  eraseByPos(0);
411 
412  return v;
413  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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 , bool Gen>
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::priority ( const Val &  elt) const

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

Parameters
eltThe element for which the priority is returned.
Returns
Returns the priority of an instance of the value passed in argument.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 724 of file priorityQueue_tpl.h.

725  {
726  return heap__[indices__[elt]].first;
727  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.

◆ priorityByPos()

template<typename Val , typename Priority , typename Cmp , typename Alloc >
INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::priorityByPos ( Size  index) const

Returns the priority of the value passed in argument.

Parameters
indexThe index of the value of which the priority is returned.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 736 of file priorityQueue_tpl.h.

737  {
738  if (index > nb_elements__) {
739  GUM_ERROR(NotFound,
740  "not enough elements in the PriorityQueueImplementation");
741  }
742  return heap__[index].first;
743  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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

◆ resize()

template<typename Val , typename Priority , typename Cmp , typename Alloc >
INLINE void gum::PriorityQueueImplementation< 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.

Definition at line 314 of file priorityQueue_tpl.h.

315  {
316  if (new_size < nb_elements__) return;
317 
318  heap__.reserve(new_size);
319  indices__.resize(new_size / 2);
320  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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() [1/2]

template<typename Val, typename Priority, typename Cmp , typename Alloc , bool Gen>
void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::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 699 of file priorityQueue_tpl.h.

701  {
702  setPriorityByPos(indices__[elt], new_priority);
703  }
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.

◆ setPriority() [2/2]

template<typename Val, typename Priority, typename Cmp , typename Alloc , bool Gen>
void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::setPriority ( const Val &  elt,
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 711 of file priorityQueue_tpl.h.

713  {
714  setPriorityByPos(indices__[elt], std::move(new_priority));
715  }
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
HashTable< Val, Size, IndexAllocator > 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::PriorityQueueImplementation< 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 602 of file priorityQueue_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,
606  "not enough elements in the PriorityQueueImplementation");
607  }
608 
609  // get the element itself
610  const Val* val = heap__[index].second;
611 
612  // restore the heap property
613  Size i = index;
614 
615  // move val upward if needed
616  for (Size j = (i - 1) >> 1; i && cmp__(new_priority, heap__[j].first);
617  i = j, j = (j - 1) >> 1) {
618  heap__[i] = std::move(heap__[j]);
619  indices__[*(heap__[i].second)] = i;
620  }
621 
622  // move val downward if needed
623  for (Size j = (i << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
624  // let j be the max child
625  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
626  ++j;
627 
628  // if "val" is lower than heap[j], "val" must be stored at index i
629  if (cmp__(new_priority, heap__[j].first)) break;
630 
631  // else pull up the jth node
632  heap__[i] = std::move(heap__[j]);
633  indices__[*(heap__[i].second)] = i;
634  }
635 
636  // update the index of val
637  heap__[i].first = new_priority;
638  heap__[i].second = val;
639  indices__[*val] = i;
640 
641  return i;
642  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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:54

◆ setPriorityByPos() [2/2]

template<typename Val , typename Priority, typename Cmp , typename Alloc >
Size gum::PriorityQueueImplementation< 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 651 of file priorityQueue_tpl.h.

651  {
652  // check whether the element the priority of which should be changed exists
653  if (index >= nb_elements__) {
654  GUM_ERROR(NotFound,
655  "not enough elements in the PriorityQueueImplementation");
656  }
657 
658  // get the element itself
659  const Val* val = heap__[index].second;
660 
661  // restore the heap property
662  Size i = index;
663 
664  // move val upward if needed
665  for (Size j = (i - 1) >> 1; i && cmp__(new_priority, heap__[j].first);
666  i = j, j = (j - 1) >> 1) {
667  heap__[i] = std::move(heap__[j]);
668  indices__[*(heap__[i].second)] = i;
669  }
670 
671  // move val downward if needed
672  for (Size j = (i << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
673  // let j be the max child
674  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
675  ++j;
676 
677  // if "val" is lower than heap[j], "val" must be stored at index i
678  if (cmp__(new_priority, heap__[j].first)) break;
679 
680  // else pull up the jth node
681  heap__[i] = std::move(heap__[j]);
682  indices__[*(heap__[i].second)] = i;
683  }
684 
685  // update the index of val
686  heap__[i].first = std::move(new_priority);
687  heap__[i].second = val;
688  indices__[*val] = i;
689 
690  return i;
691  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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:54

◆ size()

template<typename Val , typename Priority , typename Cmp , typename Alloc >
INLINE Size gum::PriorityQueueImplementation< 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 290 of file priorityQueue_tpl.h.

291  {
292  return nb_elements__;
293  }
Size nb_elements__
The number of elements in the heap.

◆ top()

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

returns the element at the top of the priority queue

Exceptions
NotFoundRaised if the queue is empty

Definition at line 264 of file priorityQueue_tpl.h.

264  {
265  if (!nb_elements__) { GUM_ERROR(NotFound, "empty priority queue"); }
266 
267  return *(heap__[0].second);
268  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::topPriority ( ) const

Returns the priority of the top element.

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

Definition at line 277 of file priorityQueue_tpl.h.

278  {
279  if (!nb_elements__) { GUM_ERROR(NotFound, "empty priority queue"); }
280 
281  return heap__[0].first;
282  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::toString ( ) const

Displays the content of the queue.

Returns
Returns the content of the queue.

Definition at line 578 of file priorityQueue_tpl.h.

579  {
580  bool deja = false;
581  std::stringstream stream;
582  stream << "[";
583 
584  for (Size i = 0; i != nb_elements__; ++i, deja = true) {
585  if (deja) stream << " , ";
586 
587  stream << "(" << heap__[i].first << " , " << *(heap__[i].second) << ")";
588  }
589 
590  stream << "]";
591 
592  return stream.str();
593  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > 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

◆ PriorityQueueImplementation

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
template<typename V , typename P , typename C , typename A , bool g>
friend class PriorityQueueImplementation
friend

All gum::PriorityQueueImplementation are friends with themselves.

Definition at line 90 of file priorityQueue.h.

◆ PriorityQueue< Val, Priority, Cmp, Alloc >

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
friend class PriorityQueue< Val, Priority, Cmp, Alloc >
friend

All gum::PriorityQueue are friends with themselves.

Definition at line 86 of file priorityQueue.h.

Member Data Documentation

◆ cmp__

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
Cmp gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::cmp__
private

Comparison function.

Definition at line 470 of file priorityQueue.h.

◆ heap__

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
std::vector< std::pair< Priority, const Val* >, HeapAllocator > gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::heap__
private

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

Definition at line 459 of file priorityQueue.h.

◆ indices__

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
HashTable< Val, Size, IndexAllocator > gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::indices__
private
Initial value:

A hashtable for quickly finding the elements by their value.

Definition at line 462 of file priorityQueue.h.

◆ nb_elements__

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::nb_elements__ {0}
private

The number of elements in the heap.

Definition at line 467 of file priorityQueue.h.


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