aGrUM  0.20.2
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 976 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 974 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 972 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 975 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 110 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 984 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 106 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 973 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 971 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 970 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 1314 of file priorityQueue_tpl.h.

1315  :
1317  // for debugging purposes
1318  GUM_CONSTRUCTOR(PriorityQueue);
1319  }
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 1323 of file priorityQueue_tpl.h.

1324  :
1326  // for debugging purposes
1327  GUM_CONSTRUCTOR(PriorityQueue);
1328  }
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 1332 of file priorityQueue_tpl.h.

1333  :
1335  // for debugging purposes
1336  GUM_CONS_CPY(PriorityQueue);
1337  }
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 1351 of file priorityQueue_tpl.h.

1352  :
1354  // for debugging purposes
1355  GUM_CONS_MOV(PriorityQueue);
1356  }
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 1360 of file priorityQueue_tpl.h.

1360  {
1361  // for debugging purposes
1362  GUM_DESTRUCTOR(PriorityQueue);
1363  }
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 1342 of file priorityQueue_tpl.h.

1343  :
1345  // for debugging purposes
1346  GUM_CONS_CPY(PriorityQueue);
1347  }
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 422 of file priorityQueue_tpl.h.

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

◆ capacity()

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

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

◆ clear()

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

Removes all the elements from the queue.

Definition at line 329 of file priorityQueue_tpl.h.

329  {
330  nb_elements__ = 0;
331  heap__.clear();
332  indices__.clear();
333  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val * >, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.
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 549 of file priorityQueue_tpl.h.

550  {
551  return indices__.exists(val);
552  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.

◆ emplace() [1/2]

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

524  {
525  std::pair< Val, Priority > new_elt
526  = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
527  return insert(std::move(new_elt.first), std::move(new_elt.second));
528  }
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.

◆ empty()

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

538  {
539  return (nb_elements__ == 0);
540  }

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

383  {
384  try {
385  eraseByPos(indices__[val]);
386  } catch (NotFound&) {}
387  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

◆ eraseByPos()

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

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

396  {
397  eraseByPos(0);
398  }
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.

◆ insert() [1/2]

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

436  {
437  // create the entry in the indices hashtable (if the element already exists,
438  // indices__.insert will raise a Duplicateelement exception)
440  = indices__.insert(val, 0);
441 
442  try {
443  heap__.push_back(
444  std::pair< Priority, const Val* >(priority, &new_elt.first));
445  } catch (...) {
446  indices__.erase(val);
447  throw;
448  }
449 
450  std::pair< Priority, const Val* > new_heap_val
451  = std::move(heap__[nb_elements__]);
452  ++nb_elements__;
453 
454  // restore the heap property
455  Size i = nb_elements__ - 1;
456 
457  for (Size j = (i - 1) >> 1; i && cmp__(new_heap_val.first, heap__[j].first);
458  i = j, j = (j - 1) >> 1) {
459  heap__[i] = std::move(heap__[j]);
460  indices__[*(heap__[i].second)] = i;
461  }
462 
463  // put the new bucket into the heap
464  heap__[i].first = std::move(new_heap_val.first);
465  heap__[i].second = &(new_elt.first);
466  new_elt.second = i;
467 
468  return i;
469  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val * >, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.
std::pair< const Val, Size > value_type
Definition: hashTable.h:685
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ insert() [2/2]

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

480  {
481  // create the entry in the indices hashtable (if the element already exists,
482  // indices__.insert will raise a Duplicateelement exception)
484  = indices__.insert(std::move(val), 0);
485 
486  try {
487  heap__.push_back(
488  std::pair< Priority, const Val* >(std::move(priority), &(new_elt.first)));
489  } catch (...) {
490  indices__.erase(new_elt.first);
491  throw;
492  }
493 
494  std::pair< Priority, const Val* > new_heap_val
495  = std::move(heap__[nb_elements__]);
496  ++nb_elements__;
497 
498  // restore the heap property
499  Size i = nb_elements__ - 1;
500 
501  for (Size j = (i - 1) >> 1; i && cmp__(new_heap_val.first, heap__[j].first);
502  i = j, j = (j - 1) >> 1) {
503  heap__[i] = std::move(heap__[j]);
504  indices__[*(heap__[i].second)] = i;
505  }
506 
507  // put the new bucket into the heap
508  heap__[i].first = std::move(new_heap_val.first);
509  heap__[i].second = &(new_elt.first);
510  new_elt.second = i;
511 
512  return i;
513  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
void erase(const Key &key)
Removes a given element from the hash table.
std::vector< std::pair< Priority, const Val * >, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.
std::pair< const Val, Size > value_type
Definition: hashTable.h:685
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ operator=() [1/4]

template<typename Val, typename Priority, typename Cmp, typename Alloc>
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 1368 of file priorityQueue_tpl.h.

1369  {
1371  return *this;
1372  }
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 1387 of file priorityQueue_tpl.h.

1388  {
1389  Implementation::operator=(std::move(from));
1390  return *this;
1391  }
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 1378 of file priorityQueue_tpl.h.

1379  {
1381  return *this;
1382  }
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 561 of file priorityQueue_tpl.h.

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

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

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

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

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

◆ priorityByPos()

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

737  {
738  if (index > nb_elements__) {
739  GUM_ERROR(NotFound,
740  "not enough elements in the PriorityQueueImplementation");
741  }
742  return heap__[index].first;
743  }
std::vector< std::pair< Priority, const Val * >, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

315  {
316  if (new_size < nb_elements__) return;
317 
318  heap__.reserve(new_size);
319  indices__.resize(new_size / 2);
320  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val * >, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.

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

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

◆ setPriority() [2/2]

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

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

◆ setPriorityByPos() [1/2]

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

602  {
603  // check whether the element the priority of which should be changed exists
604  if (index >= nb_elements__) {
605  GUM_ERROR(NotFound,
606  "not enough elements in the PriorityQueueImplementation");
607  }
608 
609  // get the element itself
610  const Val* val = heap__[index].second;
611 
612  // restore the heap property
613  Size i = index;
614 
615  // move val upward if needed
616  for (Size j = (i - 1) >> 1; i && cmp__(new_priority, heap__[j].first);
617  i = j, j = (j - 1) >> 1) {
618  heap__[i] = std::move(heap__[j]);
619  indices__[*(heap__[i].second)] = i;
620  }
621 
622  // move val downward if needed
623  for (Size j = (i << 1) + 1; j < nb_elements__; i = j, j = (j << 1) + 1) {
624  // let j be the max child
625  if ((j + 1 < nb_elements__) && cmp__(heap__[j + 1].first, heap__[j].first))
626  ++j;
627 
628  // if "val" is lower than heap[j], "val" must be stored at index i
629  if (cmp__(new_priority, heap__[j].first)) break;
630 
631  // else pull up the jth node
632  heap__[i] = std::move(heap__[j]);
633  indices__[*(heap__[i].second)] = i;
634  }
635 
636  // update the index of val
637  heap__[i].first = new_priority;
638  heap__[i].second = val;
639  indices__[*val] = i;
640 
641  return i;
642  }
HashTable< Val, Size, IndexAllocator > indices__
A hashtable for quickly finding the elements by their value.
std::vector< std::pair< Priority, const Val * >, HeapAllocator > heap__
An array storing all the elements of the heap as well as their score.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ setPriorityByPos() [2/2]

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

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

◆ size()

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

291  {
292  return nb_elements__;
293  }

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

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

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

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

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

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