aGrUM  0.14.2
priorityQueue.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 #ifndef GUM_PRIORITY_QUEUE_H
29 #define GUM_PRIORITY_QUEUE_H
30 
31 #include <functional>
32 #include <initializer_list>
33 #include <sstream>
34 #include <string>
35 #include <type_traits>
36 #include <utility>
37 #include <vector>
38 
39 #include <agrum/agrum.h>
40 #include <agrum/core/hashTable.h>
41 
42 namespace gum {
43 
44 #define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY 10
45 
46  // templates provided by this file
47  template < typename Val, typename Priority, typename Cmp, typename Alloc >
49  template < typename Val, typename Priority, typename Cmp, typename Alloc >
50  std::ostream& operator<<(std::ostream&,
52 
53  // ===========================================================================
54  // === GENERAL IMPLEMENTATION OF PRIORITY QUEUES ===
55  // ===========================================================================
56 
77  template < typename Val,
78  typename Priority,
79  typename Cmp,
80  typename Alloc,
81  bool Gen >
84  friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
85 
87  template < typename V, typename P, typename C, typename A, bool g >
89 
90  public:
93  using value_type = Val;
94  using reference = Val&;
95  using const_reference = const Val&;
96  using pointer = Val*;
97  using const_pointer = const Val*;
98  using difference_type = std::ptrdiff_t;
99  using allocator_type = Alloc;
101 
102  // The allocator for the indices.
103  using IndexAllocator =
104  typename Alloc::template rebind< std::pair< Val, Size > >::other;
105 
106  // The allocator for the heap.
107  using HeapAllocator =
108  typename Alloc::template rebind< std::pair< Priority, const Val* > >::other;
109 
110  private:
111  // ============================================================================
113  // ============================================================================
115 
125  explicit PriorityQueueImplementation(Cmp compare, Size capacity);
126 
136  std::initializer_list< std::pair< Val, Priority > > list);
137 
144 
150  template < typename OtherAlloc >
153  from);
154 
161 
166 
168 
169  public:
170  // ============================================================================
172  // ============================================================================
174 
188 
201  template < typename OtherAlloc >
204  from);
205 
214 
222  const Val& operator[](Size index_elt) const;
223 
225  // ============================================================================
227  // ============================================================================
229 
234  Size size() const noexcept;
235 
240  bool empty() const noexcept;
241 
247  bool contains(const Val& val) const;
248 
250 
251  const Val& top() const;
252 
258  const Priority& topPriority() const;
259 
265  Val pop();
266 
279  Size insert(const Val& val, const Priority& priority);
280 
293  Size insert(Val&& val, Priority&& priority);
294 
307  template < typename... Args >
308  Size emplace(Args&&... args);
309 
316  void eraseTop();
317 
336  void eraseByPos(Size index);
337 
350  void erase(const Val& val);
351 
361  Size setPriorityByPos(Size index, const Priority& new_priority);
362 
372  Size setPriorityByPos(Size index, Priority&& new_priority);
373 
380  void setPriority(const Val& elt, const Priority& new_priority);
381 
388  void setPriority(const Val& elt, Priority&& new_priority);
389 
399  const Priority& priority(const Val& elt) const;
400 
406  const Priority& priorityByPos(Size index) const;
407 
411  void clear();
412 
424  const HashTable< Val, Size >& allValues() const noexcept;
425 
430  std::string toString() const;
431 
433  // ============================================================================
435  // ============================================================================
437 
444  Size capacity() const noexcept;
445 
451  void resize(Size new_size);
452 
454 
455  private:
457  std::vector< std::pair< Priority, const Val* >, HeapAllocator > __heap;
458 
461  HashTableConst::default_size, true, true};
462 
465 
467  Cmp __cmp;
468  };
469 
470 #ifndef DOXYGEN_SHOULD_SKIP_THIS
471 
472  // ===========================================================================
473  // === SCALAR IMPLEMENTATION OF PRIORITY QUEUES ===
474  // ===========================================================================
475 
496  template < typename Val, typename Priority, typename Cmp, typename Alloc >
497  class PriorityQueueImplementation< Val, Priority, Cmp, Alloc, true > {
499  friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
500 
502  template < typename V, typename P, typename C, typename A, bool g >
503  friend class PriorityQueueImplementation;
504 
505  public:
508  using value_type = Val;
509  using reference = Val&;
510  using const_reference = const Val&;
511  using pointer = Val*;
512  using const_pointer = const Val*;
513  using difference_type = std::ptrdiff_t;
514  using allocator_type = Alloc;
516 
517  // The allocator for the indices.
518  using IndexAllocator =
519  typename Alloc::template rebind< std::pair< Val, Size > >::other;
520 
521  // The allocator for the heap.
522  using HeapAllocator =
523  typename Alloc::template rebind< std::pair< Priority, Val > >::other;
524 
525  private:
526  // ============================================================================
528  // ============================================================================
530 
540  explicit PriorityQueueImplementation(Cmp compare, Size capacity);
541 
551  std::initializer_list< std::pair< Val, Priority > > list);
552 
559 
565  template < typename OtherAlloc >
568  from);
569 
576 
581 
583 
584  public:
585  // ============================================================================
587  // ============================================================================
589 
603 
616  template < typename OtherAlloc >
619  from);
620 
629 
637  const Val& operator[](Size index_elt) const;
638 
640  // ============================================================================
642  // ============================================================================
644 
649  Size size() const noexcept;
650 
655  bool empty() const noexcept;
656 
662  bool contains(Val val) const;
663 
665 
666  const Val& top() const;
667 
673  const Priority& topPriority() const;
674 
680  Val pop();
681 
694  Size insert(Val val, const Priority& priority);
695 
708  Size insert(Val val, Priority&& priority);
709 
722  template < typename... Args >
723  Size emplace(Args&&... args);
724 
731  void eraseTop();
732 
751  void eraseByPos(Size index);
752 
765  void erase(Val val);
766 
776  Size setPriorityByPos(Size index, const Priority& new_priority);
777 
787  Size setPriorityByPos(Size index, Priority&& new_priority);
788 
795  void setPriority(Val elt, const Priority& new_priority);
796 
803  void setPriority(Val elt, Priority&& new_priority);
804 
814  const Priority& priority(Val elt) const;
815 
821  const Priority& priorityByPos(Size index) const;
822 
826  void clear();
827 
839  const HashTable< Val, Size >& allValues() const noexcept;
840 
845  std::string toString() const;
846 
848  // ============================================================================
850  // ============================================================================
852 
859  Size capacity() const noexcept;
860 
866  void resize(Size new_size);
867 
869 
870  private:
872  std::vector< std::pair< Priority, Val >, HeapAllocator > __heap;
873 
876  HashTableConst::default_size, true, true};
877 
879  Size __nb_elements{0};
880 
882  Cmp __cmp;
883  };
884 
885 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
886 
887  // ===========================================================================
888  // === PRIORITY QUEUES ===
889  // ===========================================================================
953  template < typename Val,
954  typename Priority = int,
955  typename Cmp = std::less< Priority >,
956  typename Alloc = std::allocator< Val > >
957  class PriorityQueue
958  : public PriorityQueueImplementation< Val,
959  Priority,
960  Cmp,
961  Alloc,
962  std::is_scalar< Val >::value > {
963  public:
966  using value_type = Val;
967  using reference = Val&;
968  using const_reference = const Val&;
969  using pointer = Val*;
970  using const_pointer = const Val*;
971  using difference_type = std::ptrdiff_t;
972  using allocator_type = Alloc;
974 
975  using Implementation =
977  Priority,
978  Cmp,
979  Alloc,
980  std::is_scalar< Val >::value >;
981 
982  // ============================================================================
984  // ============================================================================
986 
996  explicit PriorityQueue(Cmp compare = Cmp(),
998 
1007  explicit PriorityQueue(
1008  std::initializer_list< std::pair< Val, Priority > > list);
1009 
1015 
1020  template < typename OtherAlloc >
1022 
1028 
1032  ~PriorityQueue();
1033 
1035  // ============================================================================
1037  // ============================================================================
1039 
1053 
1065  template < typename OtherAlloc >
1068 
1076 
1078  };
1079 
1080 } /* namespace gum */
1081 
1082 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1083 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1084 extern template class gum::PriorityQueue< std::string >;
1085 # endif
1086 #endif
1087 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1088 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1089 extern template class gum::PriorityQueue< int, int >;
1090 # endif
1091 #endif
1092 
1093 // always include the implementation of the templates
1095 
1096 #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:82
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:77
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.
gum is the global namespace for all aGrUM entities
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:676
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:48
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:583
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:44
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
~PriorityQueueImplementation()
Class destructor.
template implementations of priority queues
friend class PriorityQueueImplementation
All gum::PriorityQueueImplementation are friends with themselves.
Definition: priorityQueue.h:88
typename std::allocator< NodeId > ::template rebind< std::pair< NodeId, Size > >::other IndexAllocator
const Priority & topPriority() const
Returns the priority of the top element.
Class hash tables iterators.
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.