aGrUM  0.17.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 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 1308 of file priorityQueue_tpl.h.

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

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

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

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

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

1337  :
1339  // for debugging purposes
1340  GUM_CONS_CPY(PriorityQueue);
1341  }
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.

1363  {
1365  return *this;
1366  }
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.

1382  {
1383  Implementation::operator=(std::move(from));
1384  return *this;
1385  }
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.

1373  {
1375  return *this;
1376  }
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: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 722 of file priorityQueue_tpl.h.

723  {
724  return __heap[__indices[elt]].first;
725  }
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 734 of file priorityQueue_tpl.h.

735  {
736  if (index > __nb_elements) {
737  GUM_ERROR(NotFound,
738  "not enough elements in the PriorityQueueImplementation");
739  }
740  return __heap[index].first;
741  }
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 699 of file priorityQueue_tpl.h.

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

700  {
701  setPriorityByPos(__indices[elt], new_priority);
702  }
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 710 of file priorityQueue_tpl.h.

711  {
712  setPriorityByPos(__indices[elt], std::move(new_priority));
713  }
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: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 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: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 578 of file priorityQueue_tpl.h.

Referenced by gum::operator<<().

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:48

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