aGrUM  0.14.2
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 48 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 972 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 970 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 968 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 971 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 108 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 980 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 104 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 969 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 967 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 966 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 1304 of file priorityQueue_tpl.h.

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

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

1323  :
1325  // for debugging purposes
1326  GUM_CONS_CPY(PriorityQueue);
1327  }
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 1341 of file priorityQueue_tpl.h.

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

1350  {
1351  // for debugging purposes
1352  GUM_DESTRUCTOR(PriorityQueue);
1353  }
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 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

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

422  {
423  return reinterpret_cast< const HashTable< Val, Size >& >(__indices);
424  }
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 301 of file priorityQueue_tpl.h.

302  {
303  return Size(__heap.capacity());
304  }
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:45

◆ clear()

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

Removes all the elements from the queue.

Definition at line 328 of file priorityQueue_tpl.h.

328  {
329  __nb_elements = 0;
330  __heap.clear();
331  __indices.clear();
332  }
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 546 of file priorityQueue_tpl.h.

547  {
548  return __indices.exists(val);
549  }
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 520 of file priorityQueue_tpl.h.

521  {
522  std::pair< Val, Priority > new_elt =
523  std::make_pair< Val, Priority >(std::forward< Args >(args)...);
524  return insert(std::move(new_elt.first), std::move(new_elt.second));
525  }
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 534 of file priorityQueue_tpl.h.

535  {
536  return (__nb_elements == 0);
537  }

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

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

382  {
383  try {
384  eraseByPos(__indices[val]);
385  } catch (NotFound&) {}
386  }
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 340 of file priorityQueue_tpl.h.

341  {
342  if (index >= __nb_elements) return;
343 
344  // remove the element from the hashtable
345  __indices.erase(*(__heap[index].second));
346 
347  // put the last element at the "index" location
348  std::pair< Priority, const Val* > last = std::move(__heap[__nb_elements - 1]);
349  __heap.pop_back();
350  --__nb_elements;
351 
352  if (!__nb_elements || (index == __nb_elements)) return;
353 
354  // restore the heap property
355  Size i = index;
356 
357  for (Size j = (index << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
358  // let j be the max child
359  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
360  ++j;
361 
362  // if "last" is lower than heap[j], "last" must be stored at index i
363  if (__cmp(last.first, __heap[j].first)) break;
364 
365  // else pull up the jth node
366  __heap[i] = std::move(__heap[j]);
367  __indices[*(__heap[i].second)] = i;
368  }
369 
370  // put "last" back into the heap
371  __heap[i] = std::move(last);
372  __indices[*(__heap[i].second)] = i;
373  }
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:45

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

395  {
396  eraseByPos(0);
397  }
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 433 of file priorityQueue_tpl.h.

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

434  {
435  // create the entry in the indices hashtable (if the element already exists,
436  // __indices.insert will raise a Duplicateelement exception)
438  __indices.insert(val, 0);
439 
440  try {
441  __heap.push_back(
442  std::pair< Priority, const Val* >(priority, &new_elt.first));
443  } catch (...) {
444  __indices.erase(val);
445  throw;
446  }
447 
448  std::pair< Priority, const Val* > new_heap_val =
449  std::move(__heap[__nb_elements]);
450  ++__nb_elements;
451 
452  // restore the heap property
453  Size i = __nb_elements - 1;
454 
455  for (Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
456  i = j, j = (j - 1) >> 1) {
457  __heap[i] = std::move(__heap[j]);
458  __indices[*(__heap[i].second)] = i;
459  }
460 
461  // put the new bucket into the heap
462  __heap[i].first = std::move(new_heap_val.first);
463  __heap[i].second = &(new_elt.first);
464  new_elt.second = i;
465 
466  return i;
467  }
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:682
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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 476 of file priorityQueue_tpl.h.

477  {
478  // create the entry in the indices hashtable (if the element already exists,
479  // __indices.insert will raise a Duplicateelement exception)
481  __indices.insert(std::move(val), 0);
482 
483  try {
484  __heap.push_back(
485  std::pair< Priority, const Val* >(std::move(priority), &(new_elt.first)));
486  } catch (...) {
487  __indices.erase(new_elt.first);
488  throw;
489  }
490 
491  std::pair< Priority, const Val* > new_heap_val =
492  std::move(__heap[__nb_elements]);
493  ++__nb_elements;
494 
495  // restore the heap property
496  Size i = __nb_elements - 1;
497 
498  for (Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
499  i = j, j = (j - 1) >> 1) {
500  __heap[i] = std::move(__heap[j]);
501  __indices[*(__heap[i].second)] = i;
502  }
503 
504  // put the new bucket into the heap
505  __heap[i].first = std::move(new_heap_val.first);
506  __heap[i].second = &(new_elt.first);
507  new_elt.second = i;
508 
509  return i;
510  }
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:682
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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 1359 of file priorityQueue_tpl.h.

1359  {
1361  return *this;
1362  }
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 1378 of file priorityQueue_tpl.h.

1378  {
1379  Implementation::operator=(std::move(from));
1380  return *this;
1381  }
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 1369 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[]()

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

558  {
559  if (index >= __nb_elements) {
560  GUM_ERROR(NotFound,
561  "not enough elements in the PriorityQueueImplementation");
562  }
563 
564  return *(__heap[index].second);
565  }
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:52

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

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

405  {
406  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
407 
408  Val v = *(__heap[0].second);
409  eraseByPos(0);
410 
411  return v;
412  }
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:52

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

719  {
720  return __heap[__indices[elt]].first;
721  }
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 730 of file priorityQueue_tpl.h.

731  {
732  if (index > __nb_elements) {
733  GUM_ERROR(NotFound,
734  "not enough elements in the PriorityQueueImplementation");
735  }
736  return __heap[index].first;
737  }
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:52

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

314  {
315  if (new_size < __nb_elements) return;
316 
317  __heap.reserve(new_size);
318  __indices.resize(new_size / 2);
319  }
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 695 of file priorityQueue_tpl.h.

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

696  {
697  setPriorityByPos(__indices[elt], new_priority);
698  }
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 706 of file priorityQueue_tpl.h.

707  {
708  setPriorityByPos(__indices[elt], std::move(new_priority));
709  }
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 598 of file priorityQueue_tpl.h.

598  {
599  // check whether the element the priority of which should be changed exists
600  if (index >= __nb_elements) {
601  GUM_ERROR(NotFound,
602  "not enough elements in the PriorityQueueImplementation");
603  }
604 
605  // get the element itself
606  const Val* val = __heap[index].second;
607 
608  // restore the heap property
609  Size i = index;
610 
611  // move val upward if needed
612  for (Size j = (i - 1) >> 1; i && __cmp(new_priority, __heap[j].first);
613  i = j, j = (j - 1) >> 1) {
614  __heap[i] = std::move(__heap[j]);
615  __indices[*(__heap[i].second)] = i;
616  }
617 
618  // move val downward if needed
619  for (Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
620  // let j be the max child
621  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
622  ++j;
623 
624  // if "val" is lower than heap[j], "val" must be stored at index i
625  if (__cmp(new_priority, __heap[j].first)) break;
626 
627  // else pull up the jth node
628  __heap[i] = std::move(__heap[j]);
629  __indices[*(__heap[i].second)] = i;
630  }
631 
632  // update the index of val
633  __heap[i].first = new_priority;
634  __heap[i].second = val;
635  __indices[*val] = i;
636 
637  return i;
638  }
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:45
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

647  {
648  // check whether the element the priority of which should be changed exists
649  if (index >= __nb_elements) {
650  GUM_ERROR(NotFound,
651  "not enough elements in the PriorityQueueImplementation");
652  }
653 
654  // get the element itself
655  const Val* val = __heap[index].second;
656 
657  // restore the heap property
658  Size i = index;
659 
660  // move val upward if needed
661  for (Size j = (i - 1) >> 1; i && __cmp(new_priority, __heap[j].first);
662  i = j, j = (j - 1) >> 1) {
663  __heap[i] = std::move(__heap[j]);
664  __indices[*(__heap[i].second)] = i;
665  }
666 
667  // move val downward if needed
668  for (Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
669  // let j be the max child
670  if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
671  ++j;
672 
673  // if "val" is lower than heap[j], "val" must be stored at index i
674  if (__cmp(new_priority, __heap[j].first)) break;
675 
676  // else pull up the jth node
677  __heap[i] = std::move(__heap[j]);
678  __indices[*(__heap[i].second)] = i;
679  }
680 
681  // update the index of val
682  __heap[i].first = std::move(new_priority);
683  __heap[i].second = val;
684  __indices[*val] = i;
685 
686  return i;
687  }
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:45
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

290  {
291  return __nb_elements;
292  }

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

262  {
263  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
264 
265  return *(__heap[0].second);
266  }
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:52

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

276  {
277  if (!__nb_elements) { GUM_ERROR(NotFound, "empty priority queue"); }
278 
279  return __heap[0].first;
280  }
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:52

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

Referenced by gum::operator<<().

575  {
576  bool deja = false;
577  std::stringstream stream;
578  stream << "[";
579 
580  for (Size i = 0; i != __nb_elements; ++i, deja = true) {
581  if (deja) stream << " , ";
582 
583  stream << "(" << __heap[i].first << " , " << *(__heap[i].second) << ")";
584  }
585 
586  stream << "]";
587 
588  return stream.str();
589  }
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:45

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