aGrUM  0.16.0
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/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 51 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 975 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 973 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 971 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 974 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 111 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 983 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 107 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 972 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 970 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 969 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 1307 of file priorityQueue_tpl.h.

1308  :
1310  // for debugging purposes
1311  GUM_CONSTRUCTOR(PriorityQueue);
1312  }
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 1316 of file priorityQueue_tpl.h.

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

1326  :
1328  // for debugging purposes
1329  GUM_CONS_CPY(PriorityQueue);
1330  }
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 1344 of file priorityQueue_tpl.h.

1345  :
1347  // for debugging purposes
1348  GUM_CONS_MOV(PriorityQueue);
1349  }
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 1353 of file priorityQueue_tpl.h.

1353  {
1354  // for debugging purposes
1355  GUM_DESTRUCTOR(PriorityQueue);
1356  }
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 1335 of file priorityQueue_tpl.h.

1336  :
1338  // for debugging purposes
1339  GUM_CONS_CPY(PriorityQueue);
1340  }
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 424 of file priorityQueue_tpl.h.

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

◆ capacity()

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

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

◆ clear()

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

Removes all the elements from the queue.

Definition at line 331 of file priorityQueue_tpl.h.

331  {
332  __nb_elements = 0;
333  __heap.clear();
334  __indices.clear();
335  }
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  }
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 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 384 of file priorityQueue_tpl.h.

Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().

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

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

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

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

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

◆ insert() [1/2]

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

Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().

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

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

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

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

1362  {
1364  return *this;
1365  }
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 1381 of file priorityQueue_tpl.h.

1381  {
1382  Implementation::operator=(std::move(from));
1383  return *this;
1384  }
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 1372 of file priorityQueue_tpl.h.

1372  {
1374  return *this;
1375  }
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.

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

◆ pop()

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

Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().

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

◆ priority()

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

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

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

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

◆ resize()

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

317  {
318  if (new_size < __nb_elements) return;
319 
320  __heap.reserve(new_size);
321  __indices.resize(new_size / 2);
322  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
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 698 of file priorityQueue_tpl.h.

Referenced by gum::BinaryJoinTreeConverterDefault::__convertClique().

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

◆ setPriority() [2/2]

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

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

◆ setPriorityByPos() [1/2]

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

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

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

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

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

293  {
294  return __nb_elements;
295  }

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

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

◆ topPriority()

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

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

◆ toString()

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

Referenced by gum::operator<<().

578  {
579  bool deja = false;
580  std::stringstream stream;
581  stream << "[";
582 
583  for (Size i = 0; i != __nb_elements; ++i, deja = true) {
584  if (deja) stream << " , ";
585 
586  stream << "(" << __heap[i].first << " , " << *(__heap[i].second) << ")";
587  }
588 
589  stream << "]";
590 
591  return stream.str();
592  }
std::vector< std::pair< Priority, const Val * >, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

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