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

A priorityQueue is a heap in which each element has a mutable priorityA priority queue is quite similar to a heap except that a priority (a score) is assigned to each element in the structure. More...

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

+ Inheritance diagram for gum::PriorityQueue< Val, Priority, Cmp, Alloc >:
+ Collaboration diagram for gum::PriorityQueue< Val, Priority, Cmp, Alloc >:

Public Member Functions

template<typename OtherAlloc >
INLINE PriorityQueue (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from)
 
template<typename OtherAlloc >
INLINE PriorityQueue< Val, Priority, Cmp, Alloc > & operator= (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from)
 
INLINE Size emplace (Args &&... args)
 
Constructors / Destructors
 PriorityQueue (Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
 Basic constructor. More...
 
 PriorityQueue (std::initializer_list< std::pair< Val, Priority > > list)
 Initializer list constructor. More...
 
 PriorityQueue (const PriorityQueue< Val, Priority, Cmp, Alloc > &from)
 Copy constructor. More...
 
template<typename OtherAlloc >
 PriorityQueue (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from)
 Generalized copy constructor. More...
 
 PriorityQueue (PriorityQueue< Val, Priority, Cmp, Alloc > &&from)
 Move constructor. More...
 
 ~PriorityQueue ()
 Class destructor. More...
 
Operators
PriorityQueue< Val, Priority, Cmp, Alloc > & operator= (const PriorityQueue< Val, Priority, Cmp, Alloc > &from)
 Copy operator. More...
 
template<typename OtherAlloc >
PriorityQueue< Val, Priority, Cmp, Alloc > & operator= (const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &from)
 Generalized opy operator. More...
 
PriorityQueue< Val, Priority, Cmp, Alloc > & operator= (PriorityQueue< Val, Priority, Cmp, Alloc > &&from)
 Move operator. More...
 
Operators
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...
 
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 Implementation = PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value >
 
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...
 

Detailed Description

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
class gum::PriorityQueue< Val, Priority, Cmp, Alloc >

A priorityQueue is a heap in which each element has a mutable priority

A priority queue is quite similar to a heap except that a priority (a score) is assigned to each element in the structure.

The elements are sorted according to a weak order on the scores. The priority of any element can be changed at any moment by the user. The priority queue then restores a heap property accordingly. Duplicate elements are not allowed in priority queues; if you wish an element to appear several times with different priorities, prefer using class MultiplePriorityQueue.

Usage example:
// create a priority queue of strings, the priorities of which are integers
// the element at the top of the queue has the smallest priority
PriorityQueue<std::string> queue1;
// insert elements into the queue
queue1.insert ("AAA", 8);
queue1.insert ("BBB", 10);
queue1.insert ("CCC", 2);
queue1.insert ("DDD", 23);
queue1.insert ("EEE", 24);
// copy the queue
PriorityQueue<std::string> queue2 = queue1;
// initializer list constructor
PriorityQueue<std::string,int> queue3 { std::pair<std::string,int>("aa",3),
std::pair<std::string,int>("bb",2)
};
// create a priority queue of strings, the priorities of which are
// pairs of ints
PriorityQueue< std::string, std::pair<int,int> > queue3;
// get the top element, then remove it
std::cerr << queue2.top() << std::endl;
queue2.eraseTop();
// get the top element, then remove it
std::cerr << queue2.pop() << std::endl;
// output the content of the queue
std::cerr << queue1 << std::endl;
// change the priority of the element at position 3
Size new_pos=queue1.setPriorityByPos (3,100);
// change the priority of all instances of element "AAA"
queue1.setPriority ("AAA",100);
Template Parameters
ValThe values type.
PriorityThe priorities type.
CmpThe priorities comparator.
AllocThe values allocator.

Definition at line 50 of file priorityQueue.h.

Member Typedef Documentation

◆ allocator_type

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::allocator_type = Alloc

Types for STL compliance.

Definition at line 958 of file priorityQueue.h.

◆ const_pointer

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::const_pointer = const Val*

Types for STL compliance.

Definition at line 956 of file priorityQueue.h.

◆ const_reference

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::const_reference = const Val&

Types for STL compliance.

Definition at line 954 of file priorityQueue.h.

◆ difference_type

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 957 of file priorityQueue.h.

◆ HeapAllocator

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

Definition at line 104 of file priorityQueue.h.

◆ Implementation

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::Implementation = PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value >

Definition at line 962 of file priorityQueue.h.

◆ IndexAllocator

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

Definition at line 100 of file priorityQueue.h.

◆ pointer

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::pointer = Val*

Types for STL compliance.

Definition at line 955 of file priorityQueue.h.

◆ reference

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::reference = Val&

Types for STL compliance.

Definition at line 953 of file priorityQueue.h.

◆ value_type

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
using gum::PriorityQueue< Val, Priority, Cmp, Alloc >::value_type = Val

Types for STL compliance.

Definition at line 952 of file priorityQueue.h.

Constructor & Destructor Documentation

◆ PriorityQueue() [1/6]

template<typename Val , typename Priority , typename Cmp, typename Alloc >
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue ( Cmp  compare = Cmp(),
Size  capacity = GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY 
)
explicit

Basic constructor.

Creates an empty priority queue.

Parameters
comparea function taking two elements in argument, say e1 and e2, and returning a Boolean indicating wether e1 < e2, i.e., whether e1 should be nearer than e2 to the top of the heap.
capacitythe size of the internal data structures containing the elements (could be for instance vectors or hashtables).

Definition at line 1098 of file priorityQueue_tpl.h.

1098  :
1100  // for debugging purposes
1101  GUM_CONSTRUCTOR(PriorityQueue);
1102  }
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > Implementation
Size capacity() const noexcept
Returns the size of the internal structure storing the priority queue.

◆ PriorityQueue() [2/6]

template<typename Val, typename Priority, typename Cmp, typename Alloc >
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue ( std::initializer_list< std::pair< Val, Priority > >  list)
explicit

Initializer list constructor.

The elements of the initializer list are pairs <Val,Priority>. The comparison function is the default one, i.e., std::less<Priority>.

Parameters
listThe initializer list.

Definition at line 1106 of file priorityQueue_tpl.h.

1107  :
1109  // for debugging purposes
1110  GUM_CONSTRUCTOR(PriorityQueue);
1111  }
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > Implementation

◆ PriorityQueue() [3/6]

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

Copy constructor.

Parameters
fromThe gum::PriorityQueue to copy.

Definition at line 1115 of file priorityQueue_tpl.h.

1116  :
1118  // for debugging purposes
1119  GUM_CONS_CPY(PriorityQueue);
1120  }
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > Implementation

◆ PriorityQueue() [4/6]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
template<typename OtherAlloc >
gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue ( const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &  from)

Generalized copy constructor.

Parameters
fromThe gum::PriorityQueue to copy.

◆ PriorityQueue() [5/6]

template<typename Val, typename Priority, typename Cmp, typename Alloc>
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue ( PriorityQueue< Val, Priority, Cmp, Alloc > &&  from)

Move constructor.

Parameters
fromThe gum::PriorityQueue to move.

Definition at line 1134 of file priorityQueue_tpl.h.

1135  :
1137  // for debugging purposes
1138  GUM_CONS_MOV(PriorityQueue);
1139  }
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > Implementation

◆ ~PriorityQueue()

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

Class destructor.

Definition at line 1143 of file priorityQueue_tpl.h.

1143  {
1144  // for debugging purposes
1145  GUM_DESTRUCTOR(PriorityQueue);
1146  }
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.

◆ PriorityQueue() [6/6]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
template<typename OtherAlloc >
INLINE gum::PriorityQueue< Val, Priority, Cmp, Alloc >::PriorityQueue ( const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &  from)

Definition at line 1125 of file priorityQueue_tpl.h.

1126  :
1128  // for debugging purposes
1129  GUM_CONS_CPY(PriorityQueue);
1130  }
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value > Implementation

Member Function Documentation

◆ allValues()

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

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()

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

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()

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

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

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

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]

Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::emplace ( Args &&...  args)
inherited

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]

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

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()

INLINE bool gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::empty ( ) const
noexceptinherited

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  }

◆ erase()

INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::erase ( const Val &  val)
inherited

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()

void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::eraseByPos ( Size  index)
inherited

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.
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ eraseTop()

INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::eraseTop ( )
inherited

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]

INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::insert ( const Val &  val,
const Priority &  priority 
)
inherited

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

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

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.
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.
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>
INLINE PriorityQueue< Val, Priority, Cmp, Alloc > & gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= ( const PriorityQueue< Val, Priority, Cmp, Alloc > &  from)

Copy operator.

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

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

Definition at line 1151 of file priorityQueue_tpl.h.

1152  {
1154  return *this;
1155  }
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator=(const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &from)
Copy operator.

◆ operator=() [2/4]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
template<typename OtherAlloc >
PriorityQueue< Val, Priority, Cmp, Alloc >& gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= ( const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &  from)

Generalized opy 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::PriorityQueue to copy.
Returns
Returns this gum::PriorityQueue.

◆ operator=() [3/4]

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

Move operator.

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

Definition at line 1170 of file priorityQueue_tpl.h.

1171  {
1172  Implementation::operator=(std::move(from));
1173  return *this;
1174  }
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator=(const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &from)
Copy operator.

◆ operator=() [4/4]

template<typename Val, typename Priority = int, typename Cmp = std::less< Priority >, typename Alloc = std::allocator< Val >>
template<typename OtherAlloc >
INLINE PriorityQueue< Val, Priority, Cmp, Alloc >& gum::PriorityQueue< Val, Priority, Cmp, Alloc >::operator= ( const PriorityQueue< Val, Priority, Cmp, OtherAlloc > &  from)

Definition at line 1161 of file priorityQueue_tpl.h.

1162  {
1164  return *this;
1165  }
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator=(const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &from)
Copy operator.

◆ operator[]()

INLINE const Val & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::operator[] ( Size  index_elt) const
inherited

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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ pop()

INLINE Val gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::pop ( )
inherited

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.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ priority()

INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::priority ( const Val &  elt) const
inherited

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()

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

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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ resize()

INLINE void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::resize ( Size  new_size)
inherited

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.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.

◆ setPriority() [1/2]

void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::setPriority ( const Val &  elt,
const Priority &  new_priority 
)
inherited

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]

void gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >::setPriority ( const Val &  elt,
Priority &&  new_priority 
)
inherited

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]

Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::setPriorityByPos ( Size  index,
const Priority &  new_priority 
)
inherited

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.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
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]

Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::setPriorityByPos ( Size  index,
Priority &&  new_priority 
)
inherited

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.
HashTable< Val, Size, IndexAllocator > _indices_
A hashtable for quickly finding the elements by their value.
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()

INLINE Size gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::size ( ) const
noexceptinherited

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  }

◆ top()

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

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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ topPriority()

INLINE const Priority & gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::topPriority ( ) const
inherited

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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ toString()

std::string gum::PriorityQueueImplementation< Val, Priority, Cmp, Alloc >::toString ( ) const
inherited

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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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