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

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

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

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

Public Member Functions

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

Public Types

using IndexAllocator = typename Alloc::template rebind< std::pair< Val, Size > >::other
 
using HeapAllocator = typename Alloc::template rebind< std::pair< Priority, const Val *> >::other
 
using value_type = Val
 Types for STL compliance. More...
 
using reference = Val &
 Types for STL compliance. More...
 
using const_reference = const Val &
 Types for STL compliance. More...
 
using pointer = Val *
 Types for STL compliance. More...
 
using const_pointer = const Val *
 Types for STL compliance. More...
 
using difference_type = std::ptrdiff_t
 Types for STL compliance. More...
 
using allocator_type = Alloc
 Types for STL compliance. More...
 

Friends

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

Detailed Description

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

The internal class for representing priority queues.

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

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

Definition at line 79 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 96 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 94 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 92 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 95 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 104 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 100 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 93 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 91 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 90 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 39 of file priorityQueue_tpl.h.

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

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

52  :
53  _indices_(Size(list.size()) / 2, true, true) {
54  // fill the queue
55  _heap_.reserve(list.size());
56  for (const auto& elt: list) {
57  insert(elt.first, elt.second);
58  }
59 
60  GUM_CONSTRUCTOR(PriorityQueueImplementation);
61  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

66  :
67  _heap_(from._heap_),
68  _indices_(from._indices_), _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
69  // fill the heap structure
70  for (const auto& elt: _indices_) {
71  _heap_[elt.second].second = &(elt.first);
72  }
73 
74  GUM_CONS_CPY(PriorityQueueImplementation);
75  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

81  :
82  _indices_(from._indices_),
83  _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
84  // fill the heap structure
85  if (_nb_elements_) {
86  _heap_.reserve(from._heap_.size());
87  for (const auto& elt: from._heap_) {
88  _heap_.push_back(elt);
89  }
90  for (const auto& elt: _indices_) {
91  _heap_[elt.second].second = &(elt.first);
92  }
93  }
94 
95  GUM_CONS_CPY(PriorityQueueImplementation);
96  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

101  :
102  _heap_(std::move(from._heap_)),
103  _indices_(std::move(from._indices_)), _nb_elements_(std::move(from._nb_elements_)),
104  _cmp_(std::move(from._cmp_)) {
105  GUM_CONS_MOV(PriorityQueueImplementation)
106  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

◆ ~PriorityQueueImplementation()

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

Class destructor.

Definition at line 110 of file priorityQueue_tpl.h.

110  {
111  GUM_DESTRUCTOR(PriorityQueueImplementation);
112  }
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

601  :
602  _heap_(from._heap_),
603  _indices_(from._indices_), _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
604  // for debugging purposes
605  GUM_CONS_CPY(PriorityQueueImplementation);
606  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

612  :
613  _indices_(from._indices_),
614  _nb_elements_(from._nb_elements_), _cmp_(from._cmp_) {
615  // fill the heap structure
616  if (_nb_elements_) {
617  _heap_.reserve(from._heap_.size());
618  for (const auto& elt: from._heap_) {
619  _heap_.push_back(elt);
620  }
621  }
622 
623  // for debugging purposes
624  GUM_CONS_CPY(PriorityQueueImplementation);
625  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

630  :
631  _heap_(std::move(from._heap_)),
632  _indices_(std::move(from._indices_)), _nb_elements_(std::move(from._nb_elements_)),
633  _cmp_(std::move(from._cmp_)) {
634  // for debugging purposes
635  GUM_CONS_MOV(PriorityQueueImplementation);
636  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

313  {
314  return reinterpret_cast< const HashTable< Val, Size >& >(_indices_);
315  }
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 229 of file priorityQueue_tpl.h.

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

◆ clear()

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

Removes all the elements from the queue.

Definition at line 244 of file priorityQueue_tpl.h.

244  {
245  _nb_elements_ = 0;
246  _heap_.clear();
247  _indices_.clear();
248  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
void clear()
Removes all the elements in the hash table.

◆ contains()

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

Indicates whether the priority queue contains a given value.

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

Definition at line 410 of file priorityQueue_tpl.h.

410  {
411  return _indices_.exists(val);
412  }
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 394 of file priorityQueue_tpl.h.

394  {
395  std::pair< Val, Priority > new_elt
396  = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
397  return insert(std::move(new_elt.first), std::move(new_elt.second));
398  }
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 403 of file priorityQueue_tpl.h.

403  {
404  return (_nb_elements_ == 0);
405  }
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 287 of file priorityQueue_tpl.h.

287  {
288  try {
289  eraseByPos(_indices_[val]);
290  } catch (NotFound&) {}
291  }
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.

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

252  {
253  if (index >= _nb_elements_) return;
254 
255  // remove the element from the hashtable
256  _indices_.erase(*(_heap_[index].second));
257 
258  // put the last element at the "index" location
259  std::pair< Priority, const Val* > last = std::move(_heap_[_nb_elements_ - 1]);
260  _heap_.pop_back();
261  --_nb_elements_;
262 
263  if (!_nb_elements_ || (index == _nb_elements_)) return;
264 
265  // restore the heap property
266  Size i = index;
267 
268  for (Size j = (index << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
269  // let j be the max child
270  if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
271 
272  // if "last" is lower than heap[j], "last" must be stored at index i
273  if (_cmp_(last.first, _heap_[j].first)) break;
274 
275  // else pull up the jth node
276  _heap_[i] = std::move(_heap_[j]);
277  _indices_[*(_heap_[i].second)] = i;
278  }
279 
280  // put "last" back into the heap
281  _heap_[i] = std::move(last);
282  _indices_[*(_heap_[i].second)] = i;
283  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
void erase(const Key &key)
Removes a given element from the hash table.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ eraseTop()

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

295  {
296  eraseByPos(0);
297  }
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 319 of file priorityQueue_tpl.h.

321  {
322  // create the entry in the indices hashtable (if the element already exists,
323  // _indices_.insert will raise a Duplicateelement exception)
325 
326  try {
327  _heap_.push_back(std::pair< Priority, const Val* >(priority, &new_elt.first));
328  } catch (...) {
329  _indices_.erase(val);
330  throw;
331  }
332 
333  std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
334  ++_nb_elements_;
335 
336  // restore the heap property
337  Size i = _nb_elements_ - 1;
338 
339  for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
340  i = j, j = (j - 1) >> 1) {
341  _heap_[i] = std::move(_heap_[j]);
342  _indices_[*(_heap_[i].second)] = i;
343  }
344 
345  // put the new bucket into the heap
346  _heap_[i].first = std::move(new_heap_val.first);
347  _heap_[i].second = &(new_elt.first);
348  new_elt.second = i;
349 
350  return i;
351  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
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.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
std::pair< const Val, Size > value_type
Definition: hashTable.h:672
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ insert() [2/2]

template<typename Val, typename Priority, typename Cmp , typename Alloc , bool Gen>
INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::insert ( Val &&  val,
Priority &&  priority 
)

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

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

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

Definition at line 356 of file priorityQueue_tpl.h.

357  {
358  // create the entry in the indices hashtable (if the element already exists,
359  // _indices_.insert will raise a Duplicateelement exception)
361  = _indices_.insert(std::move(val), 0);
362 
363  try {
364  _heap_.push_back(std::pair< Priority, const Val* >(std::move(priority), &(new_elt.first)));
365  } catch (...) {
366  _indices_.erase(new_elt.first);
367  throw;
368  }
369 
370  std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
371  ++_nb_elements_;
372 
373  // restore the heap property
374  Size i = _nb_elements_ - 1;
375 
376  for (Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
377  i = j, j = (j - 1) >> 1) {
378  _heap_[i] = std::move(_heap_[j]);
379  _indices_[*(_heap_[i].second)] = i;
380  }
381 
382  // put the new bucket into the heap
383  _heap_[i].first = std::move(new_heap_val.first);
384  _heap_[i].second = &(new_elt.first);
385  new_elt.second = i;
386 
387  return i;
388  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
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.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
std::pair< const Val, Size > value_type
Definition: hashTable.h:672
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ operator=() [1/4]

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

Copy operator.

When a problem occurs during the copy (for instance when not enough memory is available), the operator guarantees that the heap stays in a coherent state. Actually, the priority queue becomes empty. An exception is then thrown.

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

Definition at line 117 of file priorityQueue_tpl.h.

118  {
119  // avoid self assignment
120  if (this != &from) {
121  GUM_OP_CPY(PriorityQueueImplementation)
122 
123  try {
124  // set the comparison function
125  _cmp_ = from._cmp_;
126 
127  // copy the indices and the heap
128  _indices_ = from._indices_;
129  _heap_ = from._heap_;
130  _nb_elements_ = from._nb_elements_;
131 
132  // restore the link between _indices_ and _heap_
133  for (const auto& elt: _indices_) {
134  _heap_[elt.second].second = &(elt.first);
135  }
136  } catch (...) {
137  _heap_.clear();
138  _indices_.clear();
139  _nb_elements_ = 0;
140 
141  throw;
142  }
143  }
144 
145  return *this;
146  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

153  {
154  GUM_OP_CPY(PriorityQueueImplementation);
155 
156  try {
157  // set the comprison function
158  _cmp_ = from._cmp_;
159 
160  // copy the indices and the heap
161  _indices_ = from._indices_;
162  _nb_elements_ = from._nb_elements_;
163 
164  _heap_.clear();
165  if (_nb_elements_) {
166  _heap_.reserve(from._heap_.size());
167  for (const auto& elt: from._heap_) {
168  _heap_.push_back(elt);
169  }
170  for (const auto& elt: _indices_) {
171  _heap_[elt.second].second = &(elt.first);
172  }
173  }
174  } catch (...) {
175  _heap_.clear();
176  _indices_.clear();
177  _nb_elements_ = 0;
178 
179  throw;
180  }
181 
182  return *this;
183  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

189  {
190  // avoid self assignment
191  if (this != &from) {
192  GUM_OP_MOV(PriorityQueueImplementation)
193 
194  _indices_ = std::move(from._indices_);
195  _heap_ = std::move(from._heap_);
196  _cmp_ = std::move(from._cmp_);
197  _nb_elements_ = std::move(from._nb_elements_);
198  }
199 
200  return *this;
201  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

680  {
681  // for debugging purposes
682  GUM_OP_CPY(PriorityQueueImplementation);
683 
684  try {
685  // set the comprison function
686  _cmp_ = from._cmp_;
687 
688  // copy the indices and the heap
689  _indices_ = from._indices_;
690  _nb_elements_ = from._nb_elements_;
691 
692  _heap_.clear();
693  if (_nb_elements_) {
694  _heap_.reserve(from._heap_.size());
695  for (const auto& elt: from._heap_) {
696  _heap_.push_back(elt);
697  }
698  }
699  } catch (...) {
700  _heap_.clear();
701  _indices_.clear();
702  _nb_elements_ = 0;
703 
704  throw;
705  }
706 
707  return *this;
708  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
void clear()
Removes all the elements in the hash table.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:85

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

417  {
418  if (index >= _nb_elements_) {
419  GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
420  }
421 
422  return *(_heap_[index].second);
423  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

301  {
302  if (!_nb_elements_) { GUM_ERROR(NotFound, "empty priority queue") }
303 
304  Val v = *(_heap_[0].second);
305  eraseByPos(0);
306 
307  return v;
308  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

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

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

558  {
559  if (index > _nb_elements_) {
560  GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
561  }
562  return _heap_[index].first;
563  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

235  {
236  if (new_size < _nb_elements_) return;
237 
238  _heap_.reserve(new_size);
239  _indices_.resize(new_size / 2);
240  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.

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

535  {
536  setPriorityByPos(_indices_[elt], new_priority);
537  }
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 541 of file priorityQueue_tpl.h.

543  {
544  setPriorityByPos(_indices_[elt], std::move(new_priority));
545  }
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 445 of file priorityQueue_tpl.h.

447  {
448  // check whether the element the priority of which should be changed exists
449  if (index >= _nb_elements_) {
450  GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
451  }
452 
453  // get the element itself
454  const Val* val = _heap_[index].second;
455 
456  // restore the heap property
457  Size i = index;
458 
459  // move val upward if needed
460  for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
461  i = j, j = (j - 1) >> 1) {
462  _heap_[i] = std::move(_heap_[j]);
463  _indices_[*(_heap_[i].second)] = i;
464  }
465 
466  // move val downward if needed
467  for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
468  // let j be the max child
469  if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
470 
471  // if "val" is lower than heap[j], "val" must be stored at index i
472  if (_cmp_(new_priority, _heap_[j].first)) break;
473 
474  // else pull up the jth node
475  _heap_[i] = std::move(_heap_[j]);
476  _indices_[*(_heap_[i].second)] = i;
477  }
478 
479  // update the index of val
480  _heap_[i].first = new_priority;
481  _heap_[i].second = val;
482  _indices_[*val] = i;
483 
484  return i;
485  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

491  {
492  // check whether the element the priority of which should be changed exists
493  if (index >= _nb_elements_) {
494  GUM_ERROR(NotFound, "not enough elements in the PriorityQueueImplementation")
495  }
496 
497  // get the element itself
498  const Val* val = _heap_[index].second;
499 
500  // restore the heap property
501  Size i = index;
502 
503  // move val upward if needed
504  for (Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
505  i = j, j = (j - 1) >> 1) {
506  _heap_[i] = std::move(_heap_[j]);
507  _indices_[*(_heap_[i].second)] = i;
508  }
509 
510  // move val downward if needed
511  for (Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
512  // let j be the max child
513  if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
514 
515  // if "val" is lower than heap[j], "val" must be stored at index i
516  if (_cmp_(new_priority, _heap_[j].first)) break;
517 
518  // else pull up the jth node
519  _heap_[i] = std::move(_heap_[j]);
520  _indices_[*(_heap_[i].second)] = i;
521  }
522 
523  // update the index of val
524  _heap_[i].first = std::move(new_priority);
525  _heap_[i].second = val;
526  _indices_[*val] = i;
527 
528  return i;
529  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
Cmp _cmp_
Comparison function.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

222  {
223  return _nb_elements_;
224  }
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 205 of file priorityQueue_tpl.h.

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

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

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

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

427  {
428  bool deja = false;
429  std::stringstream stream;
430  stream << "[";
431 
432  for (Size i = 0; i != _nb_elements_; ++i, deja = true) {
433  if (deja) stream << " , ";
434 
435  stream << "(" << _heap_[i].first << " , " << *(_heap_[i].second) << ")";
436  }
437 
438  stream << "]";
439 
440  return stream.str();
441  }
std::vector< std::pair< Priority, const Val *>, HeapAllocator > _heap_
An array storing all the elements of the heap as well as their score.
Size _nb_elements_
The number of elements in the heap.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

Friends And Related Function Documentation

◆ PriorityQueueImplementation

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

All gum::PriorityQueueImplementation are friends with themselves.

Definition at line 85 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 81 of file priorityQueue.h.

Member Data Documentation

◆ _cmp_

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

Comparison function.

Definition at line 459 of file priorityQueue.h.

◆ _heap_

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

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

Definition at line 450 of file priorityQueue.h.

◆ _indices_

template<typename Val, typename Priority, typename Cmp, typename Alloc, bool Gen>
HashTable< Val, Size, IndexAllocator > gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::_indices_ {HashTableConst::default_size, true, true}
private

A hashtable for quickly finding the elements by their value.

Definition at line 453 of file priorityQueue.h.

◆ _nb_elements_

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

The number of elements in the heap.

Definition at line 456 of file priorityQueue.h.


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