aGrUM  0.16.0
gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > Class Template Reference

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

#include <agrum/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 85 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 102 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 100 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 98 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 101 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 111 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 107 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 99 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 97 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 96 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 45 of file priorityQueue_tpl.h.

45  :
46  __indices(capacity >> 1, true, true),
47  __cmp(compare) {
48  __heap.reserve(capacity);
49 
50  // for debugging purposes
51  GUM_CONSTRUCTOR(PriorityQueueImplementation);
52  }
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 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:91
Cmp __cmp
Comparison function.

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

62  :
63  __indices(Size(list.size()) / 2, true, true) {
64  // fill the queue
65  __heap.reserve(list.size());
66  for (const auto& elt : list) {
67  insert(elt.first, elt.second);
68  }
69 
70  // for debugging purposes
71  GUM_CONSTRUCTOR(PriorityQueueImplementation);
72  }
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:48
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91

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

83  :
84  __heap(from.__heap),
85  __indices(from.__indices), __nb_elements(from.__nb_elements),
86  __cmp(from.__cmp) {
87  // fill the heap structure
88  for (const auto& elt : __indices) {
89  __heap[elt.second].second = &(elt.first);
90  }
91 
92  // for debugging purposes
93  GUM_CONS_CPY(PriorityQueueImplementation);
94  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

106  :
107  __indices(from.__indices),
108  __nb_elements(from.__nb_elements), __cmp(from.__cmp) {
109  // fill the heap structure
110  if (__nb_elements) {
111  __heap.reserve(from.__heap.size());
112  for (const auto& elt : from.__heap) {
113  __heap.push_back(elt);
114  }
115  for (const auto& elt : __indices) {
116  __heap[elt.second].second = &(elt.first);
117  }
118  }
119 
120  // for debugging purposes
121  GUM_CONS_CPY(PriorityQueueImplementation);
122  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

132  :
133  __heap(std::move(from.__heap)),
134  __indices(std::move(from.__indices)),
135  __nb_elements(std::move(from.__nb_elements)), __cmp(std::move(from.__cmp)) {
136  // for debugging purposes
137  GUM_CONS_MOV(PriorityQueueImplementation);
138  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

◆ ~PriorityQueueImplementation()

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

Class destructor.

Definition at line 147 of file priorityQueue_tpl.h.

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

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

779  :
780  __heap(from.__heap),
781  __indices(from.__indices), __nb_elements(from.__nb_elements),
782  __cmp(from.__cmp) {
783  // for debugging purposes
784  GUM_CONS_CPY(PriorityQueueImplementation);
785  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

793  :
794  __indices(from.__indices),
795  __nb_elements(from.__nb_elements), __cmp(from.__cmp) {
796  // fill the heap structure
797  if (__nb_elements) {
798  __heap.reserve(from.__heap.size());
799  for (const auto& elt : from.__heap) {
800  __heap.push_back(elt);
801  }
802  }
803 
804  // for debugging purposes
805  GUM_CONS_CPY(PriorityQueueImplementation);
806  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

812  :
813  __heap(std::move(from.__heap)),
814  __indices(std::move(from.__indices)),
815  __nb_elements(std::move(from.__nb_elements)), __cmp(std::move(from.__cmp)) {
816  // for debugging purposes
817  GUM_CONS_MOV(PriorityQueueImplementation);
818  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

425  {
426  return reinterpret_cast< const HashTable< Val, Size >& >(__indices);
427  }
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 304 of file priorityQueue_tpl.h.

305  {
306  return Size(__heap.capacity());
307  }
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:48

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

Referenced by gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::clear(), and gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::operator=().

331  {
332  __nb_elements = 0;
333  __heap.clear();
334  __indices.clear();
335  }
Size __nb_elements
The number of elements in the heap.
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.
void clear()
Removes all the elements in the hash table.
+ Here is the caller graph for this function:

◆ 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  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< Val, Size, IndexAllocator > __indices
A hashtable for quickly finding the elements by their value.

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

Referenced by gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::eraseByPos(), and gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::operator=().

385  {
386  try {
387  eraseByPos(__indices[val]);
388  } catch (NotFound&) {}
389  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
HashTable< Val, Size, IndexAllocator > __indices
A hashtable for quickly finding the elements by their value.
+ Here is the caller graph for this function:

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

344  {
345  if (index >= __nb_elements) return;
346 
347  // remove the element from the hashtable
348  __indices.erase(*(__heap[index].second));
349 
350  // put the last element at the "index" location
351  std::pair< Priority, const Val* > last = std::move(__heap[__nb_elements - 1]);
352  __heap.pop_back();
353  --__nb_elements;
354 
355  if (!__nb_elements || (index == __nb_elements)) return;
356 
357  // restore the heap property
358  Size i = index;
359 
360  for (Size j = (index << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
361  // let j be the max child
362  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
363  ++j;
364 
365  // if "last" is lower than heap[j], "last" must be stored at index i
366  if (__cmp(last.first, __heap[j].first)) break;
367 
368  // else pull up the jth node
369  __heap[i] = std::move(__heap[j]);
370  __indices[*(__heap[i].second)] = i;
371  }
372 
373  // put "last" back into the heap
374  __heap[i] = std::move(last);
375  __indices[*(__heap[i].second)] = i;
376  }
void erase(const Key &key)
Removes a given element from the hash table.
Size __nb_elements
The number of elements in the heap.
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Cmp __cmp
Comparison function.

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

398  {
399  eraseByPos(0);
400  }
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 436 of file priorityQueue_tpl.h.

Referenced by gum::StaticTriangulation::__computeRecursiveThinning().

437  {
438  // create the entry in the indices hashtable (if the element already exists,
439  // __indices.insert will raise a Duplicateelement exception)
441  __indices.insert(val, 0);
442 
443  try {
444  __heap.push_back(
445  std::pair< Priority, const Val* >(priority, &new_elt.first));
446  } catch (...) {
447  __indices.erase(val);
448  throw;
449  }
450 
451  std::pair< Priority, const Val* > new_heap_val =
452  std::move(__heap[__nb_elements]);
453  ++__nb_elements;
454 
455  // restore the heap property
456  Size i = __nb_elements - 1;
457 
458  for (Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
459  i = j, j = (j - 1) >> 1) {
460  __heap[i] = std::move(__heap[j]);
461  __indices[*(__heap[i].second)] = i;
462  }
463 
464  // put the new bucket into the heap
465  __heap[i].first = std::move(new_heap_val.first);
466  __heap[i].second = &(new_elt.first);
467  new_elt.second = i;
468 
469  return i;
470  }
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.
Size __nb_elements
The number of elements in the heap.
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.
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:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Cmp __cmp
Comparison function.
+ Here is the caller graph for this function:

◆ 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 479 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  }
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.
Size __nb_elements
The number of elements in the heap.
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.
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:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Cmp __cmp
Comparison function.

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

161  {
162  // avoid self assignment
163  if (this != &from) {
164  // for debugging purposes
165  GUM_OP_CPY(PriorityQueueImplementation);
166 
167  try {
168  // set the comparison function
169  __cmp = from.__cmp;
170 
171  // copy the indices and the heap
172  __indices = from.__indices;
173  __heap = from.__heap;
174  __nb_elements = from.__nb_elements;
175 
176  // restore the link between __indices and __heap
177  for (const auto& elt : __indices) {
178  __heap[elt.second].second = &(elt.first);
179  }
180  } catch (...) {
181  __heap.clear();
182  __indices.clear();
183  __nb_elements = 0;
184 
185  throw;
186  }
187  }
188 
189  return *this;
190  }
Size __nb_elements
The number of elements in the heap.
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.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

202  {
203  // for debugging purposes
204  GUM_OP_CPY(PriorityQueueImplementation);
205 
206  try {
207  // set the comprison function
208  __cmp = from.__cmp;
209 
210  // copy the indices and the heap
211  __indices = from.__indices;
212  __nb_elements = from.__nb_elements;
213 
214  __heap.clear();
215  if (__nb_elements) {
216  __heap.reserve(from.__heap.size());
217  for (const auto& elt : from.__heap) {
218  __heap.push_back(elt);
219  }
220  for (const auto& elt : __indices) {
221  __heap[elt.second].second = &(elt.first);
222  }
223  }
224  } catch (...) {
225  __heap.clear();
226  __indices.clear();
227  __nb_elements = 0;
228 
229  throw;
230  }
231 
232  return *this;
233  }
Size __nb_elements
The number of elements in the heap.
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.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

243  {
244  // avoid self assignment
245  if (this != &from) {
246  // for debugging purposes
247  GUM_OP_MOV(PriorityQueueImplementation);
248 
249  __indices = std::move(from.__indices);
250  __heap = std::move(from.__heap);
251  __cmp = std::move(from.__cmp);
252  __nb_elements = std::move(from.__nb_elements);
253  }
254 
255  return *this;
256  }
Size __nb_elements
The number of elements in the heap.
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.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

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

865  {
866  // for debugging purposes
867  GUM_OP_CPY(PriorityQueueImplementation);
868 
869  try {
870  // set the comprison function
871  __cmp = from.__cmp;
872 
873  // copy the indices and the heap
874  __indices = from.__indices;
875  __nb_elements = from.__nb_elements;
876 
877  __heap.clear();
878  if (__nb_elements) {
879  __heap.reserve(from.__heap.size());
880  for (const auto& elt : from.__heap) {
881  __heap.push_back(elt);
882  }
883  }
884  } catch (...) {
885  __heap.clear();
886  __indices.clear();
887  __nb_elements = 0;
888 
889  throw;
890  }
891 
892  return *this;
893  }
Size __nb_elements
The number of elements in the heap.
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.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
Cmp __cmp
Comparison function.

◆ 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.

561  {
562  if (index >= __nb_elements) {
563  GUM_ERROR(NotFound,
564  "not enough elements in the PriorityQueueImplementation");
565  }
566 
567  return *(__heap[index].second);
568  }
Size __nb_elements
The number of elements in the heap.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

408  {
409  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
410 
411  Val v = *(__heap[0].second);
412  eraseByPos(0);
413 
414  return v;
415  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
Size __nb_elements
The number of elements in the heap.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

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

722  {
723  return __heap[__indices[elt]].first;
724  }
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.
+ Here is the caller graph for this function:

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

734  {
735  if (index > __nb_elements) {
736  GUM_ERROR(NotFound,
737  "not enough elements in the PriorityQueueImplementation");
738  }
739  return __heap[index].first;
740  }
Size __nb_elements
The number of elements in the heap.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

Referenced by gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::operator=(), and gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::resize().

317  {
318  if (new_size < __nb_elements) return;
319 
320  __heap.reserve(new_size);
321  __indices.resize(new_size / 2);
322  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
Size __nb_elements
The number of elements in the heap.
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.
+ Here is the caller graph for this function:

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

699  {
700  setPriorityByPos(__indices[elt], new_priority);
701  }
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 709 of file priorityQueue_tpl.h.

710  {
711  setPriorityByPos(__indices[elt], std::move(new_priority));
712  }
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 601 of file priorityQueue_tpl.h.

601  {
602  // check whether the element the priority of which should be changed exists
603  if (index >= __nb_elements) {
604  GUM_ERROR(NotFound,
605  "not enough elements in the PriorityQueueImplementation");
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  __indices[*(__heap[i].second)] = i;
619  }
620 
621  // move val downward if needed
622  for (Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
623  // let j be the max child
624  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
625  ++j;
626 
627  // if "val" is lower than heap[j], "val" must be stored at index i
628  if (__cmp(new_priority, __heap[j].first)) break;
629 
630  // else pull up the jth node
631  __heap[i] = std::move(__heap[j]);
632  __indices[*(__heap[i].second)] = i;
633  }
634 
635  // update the index of val
636  __heap[i].first = new_priority;
637  __heap[i].second = val;
638  __indices[*val] = i;
639 
640  return i;
641  }
Size __nb_elements
The number of elements in the heap.
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Cmp __cmp
Comparison function.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

650  {
651  // check whether the element the priority of which should be changed exists
652  if (index >= __nb_elements) {
653  GUM_ERROR(NotFound,
654  "not enough elements in the PriorityQueueImplementation");
655  }
656 
657  // get the element itself
658  const Val* val = __heap[index].second;
659 
660  // restore the heap property
661  Size i = index;
662 
663  // move val upward if needed
664  for (Size j = (i - 1) >> 1; i && __cmp(new_priority, __heap[j].first);
665  i = j, j = (j - 1) >> 1) {
666  __heap[i] = std::move(__heap[j]);
667  __indices[*(__heap[i].second)] = i;
668  }
669 
670  // move val downward if needed
671  for (Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
672  // let j be the max child
673  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
674  ++j;
675 
676  // if "val" is lower than heap[j], "val" must be stored at index i
677  if (__cmp(new_priority, __heap[j].first)) break;
678 
679  // else pull up the jth node
680  __heap[i] = std::move(__heap[j]);
681  __indices[*(__heap[i].second)] = i;
682  }
683 
684  // update the index of val
685  __heap[i].first = std::move(new_priority);
686  __heap[i].second = val;
687  __indices[*val] = i;
688 
689  return i;
690  }
Size __nb_elements
The number of elements in the heap.
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Cmp __cmp
Comparison function.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

293  {
294  return __nb_elements;
295  }
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 265 of file priorityQueue_tpl.h.

265  {
266  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
267 
268  return *(__heap[0].second);
269  }
Size __nb_elements
The number of elements in the heap.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

279  {
280  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
281 
282  return __heap[0].first;
283  }
Size __nb_elements
The number of elements in the heap.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

578  {
579  bool deja = false;
580  std::stringstream stream;
581  stream << "[";
582 
583  for (Size i = 0; i != __nb_elements; ++i, deja = true) {
584  if (deja) stream << " , ";
585 
586  stream << "(" << __heap[i].first << " , " << *(__heap[i].second) << ")";
587  }
588 
589  stream << "]";
590 
591  return stream.str();
592  }
Size __nb_elements
The number of elements in the heap.
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:48

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 91 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 87 of file priorityQueue.h.

Member Data Documentation

◆ __cmp

◆ __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

◆ __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

◆ __nb_elements


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