30 #ifndef GUM_PRIORITY_QUEUE_H 31 #define GUM_PRIORITY_QUEUE_H 34 #include <initializer_list> 37 #include <type_traits> 41 #include <agrum/agrum.h> 42 #include <agrum/tools/core/hashTable.h> 46 #define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY 10
49 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
51 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
52 std::ostream& operator<<(std::ostream&,
53 const PriorityQueue< Val, Priority, Cmp, Alloc >&);
79 template <
typename Val,
84 class PriorityQueueImplementation {
86 friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
89 template <
typename V,
typename P,
typename C,
typename A,
bool g >
90 friend class PriorityQueueImplementation;
95 using value_type = Val;
96 using reference = Val&;
97 using const_reference =
const Val&;
99 using const_pointer =
const Val*;
100 using difference_type = std::ptrdiff_t;
101 using allocator_type = Alloc;
105 using IndexAllocator =
106 typename Alloc::
template rebind< std::pair< Val, Size > >::other;
109 using HeapAllocator =
110 typename Alloc::
template rebind< std::pair< Priority,
const Val* > >::other;
127 explicit PriorityQueueImplementation(Cmp compare, Size capacity);
137 explicit PriorityQueueImplementation(
138 std::initializer_list< std::pair< Val, Priority > > list);
144 PriorityQueueImplementation(
145 const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& from);
152 template <
typename OtherAlloc >
153 PriorityQueueImplementation(
154 const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen >&
161 PriorityQueueImplementation(
162 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&& from);
167 ~PriorityQueueImplementation();
188 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& operator=(
189 const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& from);
203 template <
typename OtherAlloc >
204 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& operator=(
205 const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen >&
214 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& operator=(
215 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&& from);
224 const Val& operator[](Size index_elt)
const;
236 Size size()
const noexcept;
242 bool empty()
const noexcept;
249 bool contains(
const Val& val)
const;
253 const Val& top()
const;
260 const Priority& topPriority()
const;
281 Size insert(
const Val& val,
const Priority& priority);
295 Size insert(Val&& val, Priority&& priority);
309 template <
typename... Args >
310 Size emplace(Args&&... args);
338 void eraseByPos(Size index);
352 void erase(
const Val& val);
363 Size setPriorityByPos(Size index,
const Priority& new_priority);
374 Size setPriorityByPos(Size index, Priority&& new_priority);
382 void setPriority(
const Val& elt,
const Priority& new_priority);
390 void setPriority(
const Val& elt, Priority&& new_priority);
401 const Priority& priority(
const Val& elt)
const;
408 const Priority& priorityByPos(Size index)
const;
426 const HashTable< Val, Size >& allValues()
const noexcept;
432 std::string toString()
const;
446 Size capacity()
const noexcept;
453 void resize(Size new_size);
459 std::vector< std::pair< Priority,
const Val* >, HeapAllocator > heap__;
462 HashTable< Val, Size, IndexAllocator > indices__{HashTableConst::default_size,
467 Size nb_elements__{0};
473 #ifndef DOXYGEN_SHOULD_SKIP_THIS 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 >;
505 template <
typename V,
typename P,
typename C,
typename A,
bool g >
506 friend class PriorityQueueImplementation;
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;
521 using IndexAllocator =
522 typename Alloc::
template rebind< std::pair< Val, Size > >::other;
525 using HeapAllocator =
526 typename Alloc::
template rebind< std::pair< Priority, Val > >::other;
543 explicit PriorityQueueImplementation(Cmp compare, Size capacity);
553 explicit PriorityQueueImplementation(
554 std::initializer_list< std::pair< Val, Priority > > list);
560 PriorityQueueImplementation(
561 const PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& from);
568 template <
typename OtherAlloc >
569 PriorityQueueImplementation(
570 const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc,
true >&
577 PriorityQueueImplementation(
578 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&& from);
583 ~PriorityQueueImplementation();
604 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& operator=(
605 const PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& from);
619 template <
typename OtherAlloc >
620 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& operator=(
621 const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc,
true >&
630 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& operator=(
631 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&& from);
640 const Val& operator[](Size index_elt)
const;
652 Size size()
const noexcept;
658 bool empty()
const noexcept;
665 bool contains(Val val)
const;
669 const Val& top()
const;
676 const Priority& topPriority()
const;
697 Size insert(Val val,
const Priority& priority);
711 Size insert(Val val, Priority&& priority);
725 template <
typename... Args >
726 Size emplace(Args&&... args);
754 void eraseByPos(Size index);
779 Size setPriorityByPos(Size index,
const Priority& new_priority);
790 Size setPriorityByPos(Size index, Priority&& new_priority);
798 void setPriority(Val elt,
const Priority& new_priority);
806 void setPriority(Val elt, Priority&& new_priority);
817 const Priority& priority(Val elt)
const;
824 const Priority& priorityByPos(Size index)
const;
842 const HashTable< Val, Size >& allValues()
const noexcept;
848 std::string toString()
const;
862 Size capacity()
const noexcept;
869 void resize(Size new_size);
875 std::vector< std::pair< Priority, Val >, HeapAllocator > heap__;
878 HashTable< Val, Size, IndexAllocator > indices__{HashTableConst::default_size,
883 Size nb_elements__{0};
957 template <
typename Val,
958 typename Priority =
int,
959 typename Cmp = std::less< Priority >,
960 typename Alloc = std::allocator< Val > >
962 public PriorityQueueImplementation< Val,
966 std::is_scalar< Val >::value > {
970 using value_type = Val;
971 using reference = Val&;
972 using const_reference =
const Val&;
973 using pointer = Val*;
974 using const_pointer =
const Val*;
975 using difference_type = std::ptrdiff_t;
976 using allocator_type = Alloc;
980 = PriorityQueueImplementation< Val,
984 std::is_scalar< Val >::value >;
1000 explicit PriorityQueue(Cmp compare = Cmp(),
1011 explicit PriorityQueue(
1012 std::initializer_list< std::pair< Val, Priority > > list);
1018 PriorityQueue(
const PriorityQueue< Val, Priority, Cmp, Alloc >& from);
1024 template <
typename OtherAlloc >
1025 PriorityQueue(
const PriorityQueue< Val, Priority, Cmp, OtherAlloc >& from);
1031 PriorityQueue(PriorityQueue< Val, Priority, Cmp, Alloc >&& from);
1055 PriorityQueue< Val, Priority, Cmp, Alloc >&
1056 operator=(
const PriorityQueue< Val, Priority, Cmp, Alloc >& from);
1069 template <
typename OtherAlloc >
1070 PriorityQueue< Val, Priority, Cmp, Alloc >&
1071 operator=(
const PriorityQueue< Val, Priority, Cmp, OtherAlloc >& from);
1078 PriorityQueue< Val, Priority, Cmp, Alloc >&
1079 operator=(PriorityQueue< Val, Priority, Cmp, Alloc >&& from);
1086 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1087 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1088 extern template class gum::PriorityQueue< std::string >;
1091 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1092 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1093 extern template class gum::PriorityQueue<
int,
int >;
1098 #include <agrum/tools/core/priorityQueue_tpl.h> #define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY