aGrUM  0.16.0
priorityQueue.h
Go to the documentation of this file.
1 
31 #ifndef GUM_PRIORITY_QUEUE_H
32 #define GUM_PRIORITY_QUEUE_H
33 
34 #include <functional>
35 #include <initializer_list>
36 #include <sstream>
37 #include <string>
38 #include <type_traits>
39 #include <utility>
40 #include <vector>
41 
42 #include <agrum/agrum.h>
43 #include <agrum/core/hashTable.h>
44 
45 namespace gum {
46 
47 #define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY 10
48 
49  // templates provided by this file
50  template < typename Val, typename Priority, typename Cmp, typename Alloc >
52  template < typename Val, typename Priority, typename Cmp, typename Alloc >
53  std::ostream& operator<<(std::ostream&,
55 
56  // ===========================================================================
57  // === GENERAL IMPLEMENTATION OF PRIORITY QUEUES ===
58  // ===========================================================================
59 
80  template < typename Val,
81  typename Priority,
82  typename Cmp,
83  typename Alloc,
84  bool Gen >
87  friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
88 
90  template < typename V, typename P, typename C, typename A, bool g >
92 
93  public:
96  using value_type = Val;
97  using reference = Val&;
98  using const_reference = const Val&;
99  using pointer = Val*;
100  using const_pointer = const Val*;
101  using difference_type = std::ptrdiff_t;
102  using allocator_type = Alloc;
104 
105  // The allocator for the indices.
106  using IndexAllocator =
107  typename Alloc::template rebind< std::pair< Val, Size > >::other;
108 
109  // The allocator for the heap.
110  using HeapAllocator =
111  typename Alloc::template rebind< std::pair< Priority, const Val* > >::other;
112 
113  private:
114  // ============================================================================
116  // ============================================================================
118 
128  explicit PriorityQueueImplementation(Cmp compare, Size capacity);
129 
139  std::initializer_list< std::pair< Val, Priority > > list);
140 
147 
153  template < typename OtherAlloc >
156  from);
157 
164 
169 
171 
172  public:
173  // ============================================================================
175  // ============================================================================
177 
191 
204  template < typename OtherAlloc >
207  from);
208 
217 
225  const Val& operator[](Size index_elt) const;
226 
228  // ============================================================================
230  // ============================================================================
232 
237  Size size() const noexcept;
238 
243  bool empty() const noexcept;
244 
250  bool contains(const Val& val) const;
251 
253 
254  const Val& top() const;
255 
261  const Priority& topPriority() const;
262 
268  Val pop();
269 
282  Size insert(const Val& val, const Priority& priority);
283 
296  Size insert(Val&& val, Priority&& priority);
297 
310  template < typename... Args >
311  Size emplace(Args&&... args);
312 
319  void eraseTop();
320 
339  void eraseByPos(Size index);
340 
353  void erase(const Val& val);
354 
364  Size setPriorityByPos(Size index, const Priority& new_priority);
365 
375  Size setPriorityByPos(Size index, Priority&& new_priority);
376 
383  void setPriority(const Val& elt, const Priority& new_priority);
384 
391  void setPriority(const Val& elt, Priority&& new_priority);
392 
402  const Priority& priority(const Val& elt) const;
403 
409  const Priority& priorityByPos(Size index) const;
410 
414  void clear();
415 
427  const HashTable< Val, Size >& allValues() const noexcept;
428 
433  std::string toString() const;
434 
436  // ============================================================================
438  // ============================================================================
440 
447  Size capacity() const noexcept;
448 
454  void resize(Size new_size);
455 
457 
458  private:
460  std::vector< std::pair< Priority, const Val* >, HeapAllocator > __heap;
461 
464  HashTableConst::default_size, true, true};
465 
468 
470  Cmp __cmp;
471  };
472 
473 #ifndef DOXYGEN_SHOULD_SKIP_THIS
474 
475  // ===========================================================================
476  // === SCALAR IMPLEMENTATION OF PRIORITY QUEUES ===
477  // ===========================================================================
478 
499  template < typename Val, typename Priority, typename Cmp, typename Alloc >
500  class PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > {
502  friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
503 
505  template < typename V, typename P, typename C, typename A, bool g >
506  friend class PriorityQueueImplementation;
507 
508  public:
511  using value_type = Val;
512  using reference = Val&;
513  using const_reference = const Val&;
514  using pointer = Val*;
515  using const_pointer = const Val*;
516  using difference_type = std::ptrdiff_t;
517  using allocator_type = Alloc;
519 
520  // The allocator for the indices.
521  using IndexAllocator =
522  typename Alloc::template rebind< std::pair< Val, Size > >::other;
523 
524  // The allocator for the heap.
525  using HeapAllocator =
526  typename Alloc::template rebind< std::pair< Priority, Val > >::other;
527 
528  private:
529  // ============================================================================
531  // ============================================================================
533 
543  explicit PriorityQueueImplementation(Cmp compare, Size capacity);
544 
554  std::initializer_list< std::pair< Val, Priority > > list);
555 
562 
568  template < typename OtherAlloc >
571  from);
572 
579 
584 
586 
587  public:
588  // ============================================================================
590  // ============================================================================
592 
606 
619  template < typename OtherAlloc >
622  from);
623 
632 
640  const Val& operator[](Size index_elt) const;
641 
643  // ============================================================================
645  // ============================================================================
647 
652  Size size() const noexcept;
653 
658  bool empty() const noexcept;
659 
665  bool contains(Val val) const;
666 
668 
669  const Val& top() const;
670 
676  const Priority& topPriority() const;
677 
683  Val pop();
684 
697  Size insert(Val val, const Priority& priority);
698 
711  Size insert(Val val, Priority&& priority);
712 
725  template < typename... Args >
726  Size emplace(Args&&... args);
727 
734  void eraseTop();
735 
754  void eraseByPos(Size index);
755 
768  void erase(Val val);
769 
779  Size setPriorityByPos(Size index, const Priority& new_priority);
780 
790  Size setPriorityByPos(Size index, Priority&& new_priority);
791 
798  void setPriority(Val elt, const Priority& new_priority);
799 
806  void setPriority(Val elt, Priority&& new_priority);
807 
817  const Priority& priority(Val elt) const;
818 
824  const Priority& priorityByPos(Size index) const;
825 
829  void clear();
830 
842  const HashTable< Val, Size >& allValues() const noexcept;
843 
848  std::string toString() const;
849 
851  // ============================================================================
853  // ============================================================================
855 
862  Size capacity() const noexcept;
863 
869  void resize(Size new_size);
870 
872 
873  private:
875  std::vector< std::pair< Priority, Val >, HeapAllocator > __heap;
876 
879  HashTableConst::default_size, true, true};
880 
882  Size __nb_elements{0};
883 
885  Cmp __cmp;
886  };
887 
888 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
889 
890  // ===========================================================================
891  // === PRIORITY QUEUES ===
892  // ===========================================================================
956  template < typename Val,
957  typename Priority = int,
958  typename Cmp = std::less< Priority >,
959  typename Alloc = std::allocator< Val > >
960  class PriorityQueue
961  : public PriorityQueueImplementation< Val,
962  Priority,
963  Cmp,
964  Alloc,
965  std::is_scalar< Val >::value > {
966  public:
969  using value_type = Val;
970  using reference = Val&;
971  using const_reference = const Val&;
972  using pointer = Val*;
973  using const_pointer = const Val*;
974  using difference_type = std::ptrdiff_t;
975  using allocator_type = Alloc;
977 
978  using Implementation =
980  Priority,
981  Cmp,
982  Alloc,
983  std::is_scalar< Val >::value >;
984 
985  // ============================================================================
987  // ============================================================================
989 
999  explicit PriorityQueue(Cmp compare = Cmp(),
1001 
1010  explicit PriorityQueue(
1011  std::initializer_list< std::pair< Val, Priority > > list);
1012 
1018 
1023  template < typename OtherAlloc >
1025 
1031 
1035  ~PriorityQueue();
1036 
1038  // ============================================================================
1040  // ============================================================================
1042 
1056 
1068  template < typename OtherAlloc >
1071 
1079 
1081  };
1082 
1083 } /* namespace gum */
1084 
1085 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1086 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1087 extern template class gum::PriorityQueue< std::string >;
1088 # endif
1089 #endif
1090 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1091 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1092 extern template class gum::PriorityQueue< int, int >;
1093 # endif
1094 #endif
1095 
1096 // always include the implementation of the templates
1098 
1099 #endif /* GUM_PRIORITY_QUEUE_H */
Val pop()
Removes the top element from the priority queue and return it.
bool empty() const noexcept
Indicates whether the priority queue is empty.
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
const Val & operator[](Size index_elt) const
Returns the element at index "index_elt" from the priority queue.
The internal class for representing priority queues.
Definition: priorityQueue.h:85
const HashTable< Val, Size > & allValues() const noexcept
Returns a hashtable the keys of which are the values stored in the queue.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
STL namespace.
static constexpr Size default_size
The default number of slots in hashtables.
Definition: hashTable.h:80
const Val & top() const
returns the element at the top of the priority queue
Size emplace(Args &&... args)
Emplace a new element into the priority queue.
Size __nb_elements
The number of elements in the heap.
const Priority & priorityByPos(Size index) const
Returns the priority of the value passed in argument.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > & operator=(const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen > &from)
Copy operator.
The class for generic Hash Tables.
Definition: hashTable.h:679
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
HashTable< Val, Size, IndexAllocator > __indices
A hashtable for quickly finding the elements by their value.
typename std::allocator< NodeId > ::template rebind< std::pair< double, const NodeId * > >::other HeapAllocator
A priorityQueue is a heap in which each element has a mutable priorityA priority queue is quite simil...
Definition: priorityQueue.h:51
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
void eraseTop()
Removes the top of the priority queue (but does not return it).
std::string toString() const
Displays the content of the queue.
std::vector< std::pair< Priority, const Val *>, HeapAllocator > __heap
An array storing all the elements of the heap as well as their score.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
std::allocator< gum::Edge > allocator_type
Size capacity() const noexcept
Returns the size of the internal structure storing the priority queue.
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.
The base class for all undirected edges.
void clear()
Removes all the elements from the queue.
#define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY
Definition: priorityQueue.h:47
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
~PriorityQueueImplementation()
Class destructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:91
typename std::allocator< NodeId > ::template rebind< std::pair< NodeId, Size > >::other IndexAllocator
const Priority & topPriority() const
Returns the priority of the top element.
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.
Cmp __cmp
Comparison function.
Size size() const noexcept
Returns the number of elements in the priority queue.