aGrUM  0.16.0
multiPriorityQueue.h
Go to the documentation of this file.
1 
30 #ifndef GUM_MULTI_PRIORITY_QUEUE_H
31 #define GUM_MULTI_PRIORITY_QUEUE_H
32 
33 #include <functional>
34 #include <initializer_list>
35 #include <sstream>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 #include <agrum/agrum.h>
41 #include <agrum/core/hashTable.h>
42 
43 namespace gum {
44 
45 #define GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY 10
46 
47 #ifndef DOXYGEN_SHOULD_SKIP_THIS
48  // templates provided by this file
49  template < typename Val, typename Priority, typename Cmp, typename Alloc >
50  class MultiPriorityQueue;
51 
52  template < typename Val, typename Priority, typename Cmp, typename Alloc >
53  std::ostream& operator<<(std::ostream&,
54  const MultiPriorityQueue< Val, Priority, Cmp, Alloc >&);
55 
56 #endif // DOXYGEN_SHOULD_SKIP_THIS
57 
58  // ===========================================================================
59  // === PRIORITY QUEUES ===
60  // ===========================================================================
123  template < typename Val,
124  typename Priority = int,
125  typename Cmp = std::less< Priority >,
126  typename Alloc = std::allocator< Val > >
129  template < typename V, typename P, typename C, typename A >
130  friend class MultiPriorityQueue;
131 
132  public:
135  using value_type = Val;
136  using reference = Val&;
137  using const_reference = const Val&;
138  using pointer = Val*;
139  using const_pointer = const Val*;
140  using difference_type = std::ptrdiff_t;
141  using allocator_type = Alloc;
143 
145  using IndexAlloc = typename Alloc::template rebind<
146  std::pair< Val, std::vector< Size > > >::other;
147 
149  using HeapAlloc =
150  typename Alloc::template rebind< std::pair< Priority, const Val* > >::other;
151 
152  // ============================================================================
154  // ============================================================================
156 
166  explicit MultiPriorityQueue(
167  Cmp compare = Cmp(),
169 
178  explicit MultiPriorityQueue(
179  std::initializer_list< std::pair< Val, Priority > > list);
180 
187 
189 
194  template < typename OtherAlloc >
197 
203 
208 
210  // ============================================================================
212  // ============================================================================
214 
227 
239  template < typename OtherAlloc >
242 
249 
257  const Val& operator[](Size index_elt) const;
258 
260  // ============================================================================
262  // ============================================================================
264 
269  Size size() const noexcept;
270 
275  bool empty() const noexcept;
276 
282  bool contains(const Val& val) const;
283 
289  const Val& top() const;
290 
295  const Priority& topPriority() const;
296 
302  Val pop();
303 
315  Size insert(const Val& val, const Priority& priority);
316 
328  Size insert(Val&& val, Priority&& priority);
329 
340  template < typename... Args >
341  Size emplace(Args&&... args);
342 
349  void eraseTop();
350 
369  void eraseByPos(Size index);
370 
383  void erase(const Val& val);
384 
394  Size setPriorityByPos(Size index, const Priority& new_priority);
395 
405  Size setPriorityByPos(Size index, Priority&& new_priority);
406 
413  void setPriority(const Val& elt, const Priority& new_priority);
414 
427  const Priority& priority(const Val& elt) const;
428 
432  void clear();
433 
445  const HashTable< Val, std::vector< Size > >& allValues() const;
446 
451  std::string toString() const;
452 
454  // ============================================================================
456  // ============================================================================
458 
465  Size capacity() const noexcept;
466 
474  void resize(Size new_size);
475 
477 
478  private:
480  std::vector< std::pair< Priority, const Val* >, HeapAlloc > __heap;
481 
483  HashTable< Val, std::vector< Size >, IndexAlloc > __indices;
484 
486  Size __nb_elements{0};
487 
489  Cmp __cmp;
490  };
491 
492 } /* namespace gum */
493 
494 
495 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
496 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
497 extern template class gum::MultiPriorityQueue< std::string >;
498 # endif
499 #endif
500 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
501 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
502 extern template class gum::MultiPriorityQueue< int, int >;
503 # endif
504 #endif
505 
506 
507 // always include the implementation of the templates
509 
510 #endif /* GUM_MULTI_PRIORITY_QUEUE_H */
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
std::vector< std::pair< Priority, const Val *>, HeapAlloc > __heap
An array storing all the elements of the heap as well as their score.
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
const HashTable< Val, std::vector< Size > > & allValues() const
Returns a gum::HashTable the keys of which are the values stored in the queue.
const Val & operator[](Size index_elt) const
Returns the element at index "index_elt" from the priority queue.
Size __nb_elements
The number of elements in the heap.
std::string toString() const
Displays the content of the queue.
Size emplace(Args &&... args)
Emplace a new element into the priority queue.
Cmp __cmp
Comparison function.
void clear()
Removes all the elements from the queue.
#define GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY
typename std::allocator< gum::LeafPair * > ::template rebind< std::pair< double, const gum::LeafPair * * > >::other HeapAlloc
The allocator for the heap.
const Val & top() const
Returns the element at the top of the priority queue.
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
Size size() const noexcept
Returns the number of elements in the priority queue.
STL namespace.
typename std::allocator< gum::LeafPair * > ::template rebind< std::pair< gum::LeafPair *, std::vector< Size > > >::other IndexAlloc
The allocator for the indices.
<agrum/FMDP/learning/datastructure/leaves/leafPair.h>
Definition: leafPair.h:51
Val pop()
Removes the top element from the priority queue and return it.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
bool empty() const noexcept
Indicates whether the priority queue is empty.
The class for generic Hash Tables.
Definition: hashTable.h:679
Size capacity() const noexcept
Return the size of the internal structure storing the priority queue.
const Priority & topPriority() const
Returns the priority of the top element.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
MultiPriorityQueue< Val, Priority, Cmp, Alloc > & operator=(const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &from)
Copy operator.
~MultiPriorityQueue()
Class destructor.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void eraseTop()
Removes the top of the priority queue (but does not return it).
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool contains(const Val &val) const
Indicates whether the priority queue contains a given value.
A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowe...
HashTable< Val, std::vector< Size >, IndexAlloc > __indices
A hashtable for quickly finding the elements by their value.