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&,
const PriorityQueue< Val, Priority, Cmp, Alloc >&);
78 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc,
bool Gen >
79 class PriorityQueueImplementation {
81 friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
84 template <
typename V,
typename P,
typename C,
typename A,
bool g >
85 friend class PriorityQueueImplementation;
90 using value_type = Val;
91 using reference = Val&;
92 using const_reference =
const Val&;
94 using const_pointer =
const Val*;
95 using difference_type = std::ptrdiff_t;
96 using allocator_type = Alloc;
100 using IndexAllocator =
typename Alloc::
template rebind< std::pair< Val, Size > >::other;
103 using HeapAllocator =
104 typename Alloc::
template rebind< std::pair< Priority,
const Val* > >::other;
121 explicit PriorityQueueImplementation(Cmp compare, Size capacity);
131 explicit PriorityQueueImplementation(std::initializer_list< std::pair< Val, Priority > > list);
137 PriorityQueueImplementation(
138 const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& from);
145 template <
typename OtherAlloc >
146 PriorityQueueImplementation(
147 const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen >& from);
153 PriorityQueueImplementation(
154 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&& from);
159 ~PriorityQueueImplementation();
180 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&
181 operator=(
const PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >& from);
195 template <
typename OtherAlloc >
196 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&
197 operator=(
const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc, Gen >& from);
205 PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&
206 operator=(PriorityQueueImplementation< Val, Priority, Cmp, Alloc, Gen >&& from);
215 const Val& operator[](Size index_elt)
const;
227 Size size()
const noexcept;
233 bool empty()
const noexcept;
240 bool contains(
const Val& val)
const;
244 const Val& top()
const;
251 const Priority& topPriority()
const;
272 Size insert(
const Val& val,
const Priority& priority);
286 Size insert(Val&& val, Priority&& priority);
300 template <
typename... Args >
301 Size emplace(Args&&... args);
329 void eraseByPos(Size index);
343 void erase(
const Val& val);
354 Size setPriorityByPos(Size index,
const Priority& new_priority);
365 Size setPriorityByPos(Size index, Priority&& new_priority);
373 void setPriority(
const Val& elt,
const Priority& new_priority);
381 void setPriority(
const Val& elt, Priority&& new_priority);
392 const Priority& priority(
const Val& elt)
const;
399 const Priority& priorityByPos(Size index)
const;
417 const HashTable< Val, Size >& allValues()
const noexcept;
423 std::string toString()
const;
437 Size capacity()
const noexcept;
444 void resize(Size new_size);
450 std::vector< std::pair< Priority,
const Val* >, HeapAllocator > _heap_;
453 HashTable< Val, Size, IndexAllocator > _indices_{HashTableConst::default_size,
true,
true};
456 Size _nb_elements_{0};
462 #ifndef DOXYGEN_SHOULD_SKIP_THIS 488 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
489 class PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true > {
491 friend class PriorityQueue< Val, Priority, Cmp, Alloc >;
494 template <
typename V,
typename P,
typename C,
typename A,
bool g >
495 friend class PriorityQueueImplementation;
500 using value_type = Val;
501 using reference = Val&;
502 using const_reference =
const Val&;
503 using pointer = Val*;
504 using const_pointer =
const Val*;
505 using difference_type = std::ptrdiff_t;
506 using allocator_type = Alloc;
510 using IndexAllocator =
typename Alloc::
template rebind< std::pair< Val, Size > >::other;
513 using HeapAllocator =
typename Alloc::
template rebind< std::pair< Priority, Val > >::other;
530 explicit PriorityQueueImplementation(Cmp compare, Size capacity);
540 explicit PriorityQueueImplementation(std::initializer_list< std::pair< Val, Priority > > list);
546 PriorityQueueImplementation(
547 const PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& from);
554 template <
typename OtherAlloc >
555 PriorityQueueImplementation(
556 const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc,
true >& from);
562 PriorityQueueImplementation(
563 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&& from);
568 ~PriorityQueueImplementation();
589 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&
590 operator=(
const PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >& from);
604 template <
typename OtherAlloc >
605 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&
606 operator=(
const PriorityQueueImplementation< Val, Priority, Cmp, OtherAlloc,
true >& from);
614 PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&
615 operator=(PriorityQueueImplementation< Val, Priority, Cmp, Alloc,
true >&& from);
624 const Val& operator[](Size index_elt)
const;
636 Size size()
const noexcept;
642 bool empty()
const noexcept;
649 bool contains(Val val)
const;
653 const Val& top()
const;
660 const Priority& topPriority()
const;
681 Size insert(Val val,
const Priority& priority);
695 Size insert(Val val, Priority&& priority);
709 template <
typename... Args >
710 Size emplace(Args&&... args);
738 void eraseByPos(Size index);
763 Size setPriorityByPos(Size index,
const Priority& new_priority);
774 Size setPriorityByPos(Size index, Priority&& new_priority);
782 void setPriority(Val elt,
const Priority& new_priority);
790 void setPriority(Val elt, Priority&& new_priority);
801 const Priority& priority(Val elt)
const;
808 const Priority& priorityByPos(Size index)
const;
826 const HashTable< Val, Size >& allValues()
const noexcept;
832 std::string toString()
const;
846 Size capacity()
const noexcept;
853 void resize(Size new_size);
859 std::vector< std::pair< Priority, Val >, HeapAllocator > _heap_;
862 HashTable< Val, Size, IndexAllocator > _indices_{HashTableConst::default_size,
true,
true};
865 Size _nb_elements_{0};
939 template <
typename Val,
940 typename Priority =
int,
941 typename Cmp = std::less< Priority >,
942 typename Alloc = std::allocator< Val > >
944 public PriorityQueueImplementation< Val,
948 std::is_scalar< Val >::value > {
952 using value_type = Val;
953 using reference = Val&;
954 using const_reference =
const Val&;
955 using pointer = Val*;
956 using const_pointer =
const Val*;
957 using difference_type = std::ptrdiff_t;
958 using allocator_type = Alloc;
962 = PriorityQueueImplementation< Val, Priority, Cmp, Alloc, std::is_scalar< Val >::value >;
978 explicit PriorityQueue(Cmp compare = Cmp(),
989 explicit PriorityQueue(std::initializer_list< std::pair< Val, Priority > > list);
995 PriorityQueue(
const PriorityQueue< Val, Priority, Cmp, Alloc >& from);
1001 template <
typename OtherAlloc >
1002 PriorityQueue(
const PriorityQueue< Val, Priority, Cmp, OtherAlloc >& from);
1008 PriorityQueue(PriorityQueue< Val, Priority, Cmp, Alloc >&& from);
1032 PriorityQueue< Val, Priority, Cmp, Alloc >&
1033 operator=(
const PriorityQueue< Val, Priority, Cmp, Alloc >& from);
1046 template <
typename OtherAlloc >
1047 PriorityQueue< Val, Priority, Cmp, Alloc >&
1048 operator=(
const PriorityQueue< Val, Priority, Cmp, OtherAlloc >& from);
1055 PriorityQueue< Val, Priority, Cmp, Alloc >&
1056 operator=(PriorityQueue< Val, Priority, Cmp, Alloc >&& from);
1063 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1064 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1065 extern template class gum::PriorityQueue< std::string >;
1068 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1069 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1070 extern template class gum::PriorityQueue<
int,
int >;
1075 #include <agrum/tools/core/priorityQueue_tpl.h> #define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY