aGrUM  0.14.2
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 82 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 99 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 97 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 95 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 98 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 108 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 104 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 96 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 94 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 93 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 42 of file priorityQueue_tpl.h.

42  :
43  __indices(capacity >> 1, true, true),
44  __cmp(compare) {
45  __heap.reserve(capacity);
46 
47  // for debugging purposes
48  GUM_CONSTRUCTOR(PriorityQueueImplementation);
49  }
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:88
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 58 of file priorityQueue_tpl.h.

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

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

80  :
81  __heap(from.__heap),
82  __indices(from.__indices), __nb_elements(from.__nb_elements),
83  __cmp(from.__cmp) {
84  // fill the heap structure
85  for (const auto& elt : __indices) {
86  __heap[elt.second].second = &(elt.first);
87  }
88 
89  // for debugging purposes
90  GUM_CONS_CPY(PriorityQueueImplementation);
91  }
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:88
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 101 of file priorityQueue_tpl.h.

103  :
104  __indices(from.__indices),
105  __nb_elements(from.__nb_elements), __cmp(from.__cmp) {
106  // fill the heap structure
107  if (__nb_elements) {
108  __heap.reserve(from.__heap.size());
109  for (const auto& elt : from.__heap) {
110  __heap.push_back(elt);
111  }
112  for (const auto& elt : __indices) {
113  __heap[elt.second].second = &(elt.first);
114  }
115  }
116 
117  // for debugging purposes
118  GUM_CONS_CPY(PriorityQueueImplementation);
119  }
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:88
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 128 of file priorityQueue_tpl.h.

129  :
130  __heap(std::move(from.__heap)),
131  __indices(std::move(from.__indices)),
132  __nb_elements(std::move(from.__nb_elements)), __cmp(std::move(from.__cmp)) {
133  // for debugging purposes
134  GUM_CONS_MOV(PriorityQueueImplementation);
135  }
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:88
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 144 of file priorityQueue_tpl.h.

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

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

776  :
777  __heap(from.__heap),
778  __indices(from.__indices), __nb_elements(from.__nb_elements),
779  __cmp(from.__cmp) {
780  // for debugging purposes
781  GUM_CONS_CPY(PriorityQueueImplementation);
782  }
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:88
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 788 of file priorityQueue_tpl.h.

790  :
791  __indices(from.__indices),
792  __nb_elements(from.__nb_elements), __cmp(from.__cmp) {
793  // fill the heap structure
794  if (__nb_elements) {
795  __heap.reserve(from.__heap.size());
796  for (const auto& elt : from.__heap) {
797  __heap.push_back(elt);
798  }
799  }
800 
801  // for debugging purposes
802  GUM_CONS_CPY(PriorityQueueImplementation);
803  }
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:88
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 808 of file priorityQueue_tpl.h.

809  :
810  __heap(std::move(from.__heap)),
811  __indices(std::move(from.__indices)),
812  __nb_elements(std::move(from.__nb_elements)), __cmp(std::move(from.__cmp)) {
813  // for debugging purposes
814  GUM_CONS_MOV(PriorityQueueImplementation);
815  }
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:88
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 421 of file priorityQueue_tpl.h.

422  {
423  return reinterpret_cast< const HashTable< Val, Size >& >(__indices);
424  }
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 301 of file priorityQueue_tpl.h.

302  {
303  return Size(__heap.capacity());
304  }
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:45

◆ 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 328 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=().

328  {
329  __nb_elements = 0;
330  __heap.clear();
331  __indices.clear();
332  }
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 546 of file priorityQueue_tpl.h.

547  {
548  return __indices.exists(val);
549  }
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 520 of file priorityQueue_tpl.h.

521  {
522  std::pair< Val, Priority > new_elt =
523  std::make_pair< Val, Priority >(std::forward< Args >(args)...);
524  return insert(std::move(new_elt.first), std::move(new_elt.second));
525  }
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 534 of file priorityQueue_tpl.h.

535  {
536  return (__nb_elements == 0);
537  }
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 381 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=().

382  {
383  try {
384  eraseByPos(__indices[val]);
385  } catch (NotFound&) {}
386  }
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 340 of file priorityQueue_tpl.h.

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

395  {
396  eraseByPos(0);
397  }
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 433 of file priorityQueue_tpl.h.

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

434  {
435  // create the entry in the indices hashtable (if the element already exists,
436  // __indices.insert will raise a Duplicateelement exception)
438  __indices.insert(val, 0);
439 
440  try {
441  __heap.push_back(
442  std::pair< Priority, const Val* >(priority, &new_elt.first));
443  } catch (...) {
444  __indices.erase(val);
445  throw;
446  }
447 
448  std::pair< Priority, const Val* > new_heap_val =
449  std::move(__heap[__nb_elements]);
450  ++__nb_elements;
451 
452  // restore the heap property
453  Size i = __nb_elements - 1;
454 
455  for (Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
456  i = j, j = (j - 1) >> 1) {
457  __heap[i] = std::move(__heap[j]);
458  __indices[*(__heap[i].second)] = i;
459  }
460 
461  // put the new bucket into the heap
462  __heap[i].first = std::move(new_heap_val.first);
463  __heap[i].second = &(new_elt.first);
464  new_elt.second = i;
465 
466  return i;
467  }
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:682
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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 476 of file priorityQueue_tpl.h.

477  {
478  // create the entry in the indices hashtable (if the element already exists,
479  // __indices.insert will raise a Duplicateelement exception)
481  __indices.insert(std::move(val), 0);
482 
483  try {
484  __heap.push_back(
485  std::pair< Priority, const Val* >(std::move(priority), &(new_elt.first)));
486  } catch (...) {
487  __indices.erase(new_elt.first);
488  throw;
489  }
490 
491  std::pair< Priority, const Val* > new_heap_val =
492  std::move(__heap[__nb_elements]);
493  ++__nb_elements;
494 
495  // restore the heap property
496  Size i = __nb_elements - 1;
497 
498  for (Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
499  i = j, j = (j - 1) >> 1) {
500  __heap[i] = std::move(__heap[j]);
501  __indices[*(__heap[i].second)] = i;
502  }
503 
504  // put the new bucket into the heap
505  __heap[i].first = std::move(new_heap_val.first);
506  __heap[i].second = &(new_elt.first);
507  new_elt.second = i;
508 
509  return i;
510  }
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:682
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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 156 of file priorityQueue_tpl.h.

158  {
159  // avoid self assignment
160  if (this != &from) {
161  // for debugging purposes
162  GUM_OP_CPY(PriorityQueueImplementation);
163 
164  try {
165  // set the comparison function
166  __cmp = from.__cmp;
167 
168  // copy the indices and the heap
169  __indices = from.__indices;
170  __heap = from.__heap;
171  __nb_elements = from.__nb_elements;
172 
173  // restore the link between __indices and __heap
174  for (const auto& elt : __indices) {
175  __heap[elt.second].second = &(elt.first);
176  }
177  } catch (...) {
178  __heap.clear();
179  __indices.clear();
180  __nb_elements = 0;
181 
182  throw;
183  }
184  }
185 
186  return *this;
187  }
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:88
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 197 of file priorityQueue_tpl.h.

199  {
200  // for debugging purposes
201  GUM_OP_CPY(PriorityQueueImplementation);
202 
203  try {
204  // set the comprison function
205  __cmp = from.__cmp;
206 
207  // copy the indices and the heap
208  __indices = from.__indices;
209  __nb_elements = from.__nb_elements;
210 
211  __heap.clear();
212  if (__nb_elements) {
213  __heap.reserve(from.__heap.size());
214  for (const auto& elt : from.__heap) {
215  __heap.push_back(elt);
216  }
217  for (const auto& elt : __indices) {
218  __heap[elt.second].second = &(elt.first);
219  }
220  }
221  } catch (...) {
222  __heap.clear();
223  __indices.clear();
224  __nb_elements = 0;
225 
226  throw;
227  }
228 
229  return *this;
230  }
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:88
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 239 of file priorityQueue_tpl.h.

240  {
241  // avoid self assignment
242  if (this != &from) {
243  // for debugging purposes
244  GUM_OP_MOV(PriorityQueueImplementation);
245 
246  __indices = std::move(from.__indices);
247  __heap = std::move(from.__heap);
248  __cmp = std::move(from.__cmp);
249  __nb_elements = std::move(from.__nb_elements);
250  }
251 
252  return *this;
253  }
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:88
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 860 of file priorityQueue_tpl.h.

862  {
863  // for debugging purposes
864  GUM_OP_CPY(PriorityQueueImplementation);
865 
866  try {
867  // set the comprison function
868  __cmp = from.__cmp;
869 
870  // copy the indices and the heap
871  __indices = from.__indices;
872  __nb_elements = from.__nb_elements;
873 
874  __heap.clear();
875  if (__nb_elements) {
876  __heap.reserve(from.__heap.size());
877  for (const auto& elt : from.__heap) {
878  __heap.push_back(elt);
879  }
880  }
881  } catch (...) {
882  __heap.clear();
883  __indices.clear();
884  __nb_elements = 0;
885 
886  throw;
887  }
888 
889  return *this;
890  }
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:88
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 558 of file priorityQueue_tpl.h.

558  {
559  if (index >= __nb_elements) {
560  GUM_ERROR(NotFound,
561  "not enough elements in the PriorityQueueImplementation");
562  }
563 
564  return *(__heap[index].second);
565  }
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:52

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

405  {
406  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
407 
408  Val v = *(__heap[0].second);
409  eraseByPos(0);
410 
411  return v;
412  }
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:52

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

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

719  {
720  return __heap[__indices[elt]].first;
721  }
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 730 of file priorityQueue_tpl.h.

731  {
732  if (index > __nb_elements) {
733  GUM_ERROR(NotFound,
734  "not enough elements in the PriorityQueueImplementation");
735  }
736  return __heap[index].first;
737  }
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:52

◆ 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 313 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().

314  {
315  if (new_size < __nb_elements) return;
316 
317  __heap.reserve(new_size);
318  __indices.resize(new_size / 2);
319  }
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 695 of file priorityQueue_tpl.h.

696  {
697  setPriorityByPos(__indices[elt], new_priority);
698  }
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 706 of file priorityQueue_tpl.h.

707  {
708  setPriorityByPos(__indices[elt], std::move(new_priority));
709  }
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 598 of file priorityQueue_tpl.h.

598  {
599  // check whether the element the priority of which should be changed exists
600  if (index >= __nb_elements) {
601  GUM_ERROR(NotFound,
602  "not enough elements in the PriorityQueueImplementation");
603  }
604 
605  // get the element itself
606  const Val* val = __heap[index].second;
607 
608  // restore the heap property
609  Size i = index;
610 
611  // move val upward if needed
612  for (Size j = (i - 1) >> 1; i && __cmp(new_priority, __heap[j].first);
613  i = j, j = (j - 1) >> 1) {
614  __heap[i] = std::move(__heap[j]);
615  __indices[*(__heap[i].second)] = i;
616  }
617 
618  // move val downward if needed
619  for (Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
620  // let j be the max child
621  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
622  ++j;
623 
624  // if "val" is lower than heap[j], "val" must be stored at index i
625  if (__cmp(new_priority, __heap[j].first)) break;
626 
627  // else pull up the jth node
628  __heap[i] = std::move(__heap[j]);
629  __indices[*(__heap[i].second)] = i;
630  }
631 
632  // update the index of val
633  __heap[i].first = new_priority;
634  __heap[i].second = val;
635  __indices[*val] = i;
636 
637  return i;
638  }
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:45
Cmp __cmp
Comparison function.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

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

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

290  {
291  return __nb_elements;
292  }
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 262 of file priorityQueue_tpl.h.

262  {
263  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
264 
265  return *(__heap[0].second);
266  }
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:52

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

276  {
277  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
278 
279  return __heap[0].first;
280  }
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:52

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

575  {
576  bool deja = false;
577  std::stringstream stream;
578  stream << "[";
579 
580  for (Size i = 0; i != __nb_elements; ++i, deja = true) {
581  if (deja) stream << " , ";
582 
583  stream << "(" << __heap[i].first << " , " << *(__heap[i].second) << ")";
584  }
585 
586  stream << "]";
587 
588  return stream.str();
589  }
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:45

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 88 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 84 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: