50#ifndef GUM_SORTED_PRIORITY_QUEUE_H
51#define GUM_SORTED_PRIORITY_QUEUE_H
65#include <initializer_list>
70#ifndef DOXYGEN_SHOULD_SKIP_THIS
72 template <
typename Val,
typename Priority,
typename Cmp >
74 template <
typename Val,
typename Priority,
typename Cmp >
77 template <
typename Val,
typename Priority,
typename Cmp >
79 template <
typename Val,
typename Priority,
typename Cmp >
145 template <
typename Val,
typename Priority =
int,
typename Cmp = std::less< Priority > >
348 template < typename... Args >
381 void erase(const Val& val,
bool internal_val = false);
394 void setPriority(const Val& elt, const Priority& new_priority,
bool internal_val = false);
407 void setPriority(const Val& elt, Priority&& new_priority,
bool internal_val = false);
423 const Priority&
priority(const Val& elt,
bool internal_val = false) const;
491#ifndef DOXYGEN_SHOULD_SKIP_THIS
493 using AVLNode = AVLTreeNode< Val >;
496 using HashElt =
typename std::pair< const AVLTreeNode< Val >, Priority >;
502 TreeCmp(
const Cmp& cmp);
511 const Priority& getPriority(
const Val& v)
const;
516 AVLTreeNode< Val >* getNode(
const Val& v)
const;
520 bool operator()(
const Val& x,
const Val& y)
const;
526 static constexpr std::size_t offset_to_priority = offsetof(HashElt, second);
527 static constexpr std::size_t offset_to_value = offsetof(AVLTreeNode< Val >, value);
528 static constexpr std::size_t offset_from_value_to_priority
529 = offset_to_priority - offset_to_value;
578#ifndef DOXYGEN_SHOULD_SKIP_THIS
584 template <
typename T >
585 struct is_basic_string: std::false_type {};
587 template <
typename T1,
typename T2,
typename T3 >
588 struct is_basic_string<
std::basic_string< T1, T2, T3 > >: std::true_type {};
608 template <
typename Val,
typename Priority =
int,
typename Cmp = std::less< Priority > >
610 protected SharedAVLTreeReverseIterator<
612 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > {
638 const bool begin =
true) noexcept;
640#ifndef DOXYGEN_SHOULD_SKIP_THIS
644 SharedAVLTreeReverseIterator< Val, TreeCmp >(init) {}
745 template < typename Val, typename Priority =
int, typename
Cmp =
std::less< Val > >
747 protected SharedAVLTreeReverseIteratorSafe<
775 const bool rbegin =
true);
777#ifndef DOXYGEN_SHOULD_SKIP_THIS
781 SharedAVLTreeReverseIteratorSafe< Val, TreeCmp >(init) {}
882 template < typename Val, typename Priority =
int, typename
Cmp =
std::less< Val > >
884 protected SharedAVLTreeIterator<
913 const bool rbegin =
true) noexcept;
915#ifndef DOXYGEN_SHOULD_SKIP_THIS
919 SharedAVLTreeIterator< Val, TreeCmp >(init) {}
1020 template < typename Val, typename Priority =
int, typename
Cmp =
std::less< Val > >
1022 protected SharedAVLTreeIteratorSafe<
1051 const bool rbegin =
true);
1053#ifndef DOXYGEN_SHOULD_SKIP_THIS
1057 SharedAVLTreeIteratorSafe< Val, TreeCmp >(init) {}
1146 = SharedAVLTreeIteratorSafe< Val,
1151 template < typename Val, typename Priority, typename
Cmp >
1152 std::ostream& operator<<(
std::ostream& stream,
1156#ifndef DOXYGEN_SHOULD_SKIP_THIS
1167 _static_SortedPriorityQueue_end_;
1169 _static_SortedPriorityQueue_rend_;
1171 _static_SortedPriorityQueue_end_safe_;
1173 _static_SortedPriorityQueue_rend_safe_;
1175 inline constexpr void*
const _SortedPriorityQueue_end_
1176 = (
void*
const)&_static_SortedPriorityQueue_end_;
1177 inline constexpr void*
const _SortedPriorityQueue_rend_
1178 = (
void*
const)&_static_SortedPriorityQueue_rend_;
1179 inline constexpr void*
const _SortedPriorityQueue_end_safe_
1180 = (
void*
const)&_static_SortedPriorityQueue_end_safe_;
1181 inline constexpr void*
const _SortedPriorityQueue_rend_safe_
1182 = (
void*
const)&_static_SortedPriorityQueue_rend_safe_;
The class for generic Hash Tables.
Sorted priority queues safe (w.r.t.
~SortedPriorityQueueIteratorSafe() noexcept
destructor
SharedAVLTreeReverseIteratorSafe< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > TreeIterator
const value_type * const_pointer
SortedPriorityQueueIteratorSafe(SortedPriorityQueueIteratorSafe< Val, Priority, Cmp > &&from)
move constructor
SortedPriorityQueueIteratorSafe(const SortedPriorityQueueIteratorSafe< Val, Priority, Cmp > &from)
copy constructor
std::bidirectional_iterator_tag iterator_category
typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp TreeCmp
Types for STL compliance.
SortedPriorityQueueIteratorSafe(SortedPriorityQueue< Val, Priority, Cmp > &queue, const bool rbegin=true)
constructor for begin safe iterators
const_reference value() const
const Priority & priority() const
const value_type & const_reference
Sorted priority queue iterator.
std::bidirectional_iterator_tag iterator_category
friend SortedPriorityQueue< Val, Priority, Cmp >
typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp TreeCmp
const Priority & priority() const
SharedAVLTreeReverseIterator< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > TreeIterator
~SortedPriorityQueueIterator() noexcept
destructor
const value_type & const_reference
SortedPriorityQueueIterator(SortedPriorityQueueIterator< Val, Priority, Cmp > &&from) noexcept
move constructor
const value_type * const_pointer
const_reference value() const
SortedPriorityQueueIterator(const SortedPriorityQueueIterator< Val, Priority, Cmp > &from) noexcept
copy constructor
SortedPriorityQueueIterator(const SortedPriorityQueue< Val, Priority, Cmp > &queue, const bool begin=true) noexcept
constructor for begin iterators
Sorted priority queue safe (w.r.t.
typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp TreeCmp
Types for STL compliance.
SortedPriorityQueueReverseIteratorSafe(SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp > &&from)
move constructor
SortedPriorityQueueReverseIteratorSafe(SortedPriorityQueue< Val, Priority, Cmp > &queue, const bool rbegin=true)
constructor for rbegin safe iterators
SharedAVLTreeIteratorSafe< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > TreeIterator
const Priority & priority() const
const value_type & const_reference
SortedPriorityQueueReverseIteratorSafe(const SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp > &from)
copy constructor
std::bidirectional_iterator_tag iterator_category
~SortedPriorityQueueReverseIteratorSafe() noexcept
destructor
const value_type * const_pointer
const_reference value() const
Sorted priority queue reverse iterator.
const Priority & priority() const
std::bidirectional_iterator_tag iterator_category
friend SortedPriorityQueue< Val, Priority, Cmp >
SortedPriorityQueueReverseIterator(SortedPriorityQueueReverseIterator< Val, Priority, Cmp > &&from) noexcept
move constructor
typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp TreeCmp
Types for STL compliance.
~SortedPriorityQueueReverseIterator() noexcept
destructor
SharedAVLTreeIterator< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > TreeIterator
const value_type * const_pointer
SortedPriorityQueueReverseIterator(const SortedPriorityQueueReverseIterator< Val, Priority, Cmp > &from) noexcept
copy constructor
SortedPriorityQueueReverseIterator(const SortedPriorityQueue< Val, Priority, Cmp > &queue, const bool rbegin=true) noexcept
constructor for rbegin iterators
const_reference value() const
const value_type & const_reference
A priority queue in which we can iterate over the elements from the top to bottom or conversely.
iterator_safe beginSafe()
returns a new safe iterator pointing to the minimal element of the tree
AVLNode & getNodeFromExternalValue_(const Val &val) const
returns the node of the hash table corresponding to a given external value
Val * pointer
Types for STL compliance.
optional_ref< const value_type > tryGet(const Val &key) const
Returns a pointer to the "internal" value stored into the queue corresponding to val if it exists,...
SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp > reverse_iterator_safe
Types for STL compliance.
Size capacity() const noexcept
Returns the size of the internal structure storing the priority queue.
SortedPriorityQueueIteratorSafe< Val, Priority, Cmp > iterator_safe
Types for STL compliance.
const_reference emplace(Args &&... args)
Emplace a new element into the priority queue.
void eraseTop()
Removes the top of the priority queue (but does not return it).
value_type popTop()
Alias of pop.
bool empty() const noexcept
Indicates whether the priority queue is empty.
HashTable< AVLTreeNode< Val >, Priority > _nodes_
A hashtable for quickly finding the elements by their value.
SharedAVLTree< Val, TreeCmp > _tree_
A binary search tree storing all the values of the queue.
void erase(const Val &val, bool internal_val=false)
Removes a given element from the priority queue (but does not return it).
const Val & top() const
returns the element at the top of the sorted priority queue
void clear()
Removes all the elements from the queue.
~SortedPriorityQueue()
Class destructor.
constexpr const iterator_safe & endSafe() const
returns a safe iterator pointing just after the maximal element
constexpr const reverse_iterator & rend() const
returns an iterator pointing just before the minimal element
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.
std::ptrdiff_t difference_type
Types for STL compliance.
void eraseBottom()
Removes the bottom of the priority queue (but does not return it).
TreeCmp _tree_cmp_
Comparison function.
SortedPriorityQueue< Val, Priority, Cmp > & operator=(const SortedPriorityQueue< Val, Priority, Cmp > &from)
Copy operator.
SortedPriorityQueueIterator< Val, Priority, Cmp > iterator
Types for STL compliance.
Val value_type
Types for STL compliance.
SortedPriorityQueue(std::initializer_list< std::pair< Val, Priority > > list)
Initializer list constructor.
SortedPriorityQueue(SortedPriorityQueue< Val, Priority, Cmp > &&from) noexcept
Move constructor.
const Val & const_reference
Types for STL compliance.
reverse_iterator_safe rbeginSafe()
returns a safe iterator pointing to the maximal element of the tree
value_type pop()
Removes the top element from the priority queue and return it.
Val & reference
Types for STL compliance.
void setPriority(const Val &elt, const Priority &new_priority, bool internal_val=false)
Modifies the priority of each instance of a given element.
SortedPriorityQueue< Val, Priority, Cmp > & operator=(SortedPriorityQueue< Val, Priority, Cmp > &&from) noexcept
Move operator.
SortedPriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
std::string toString() const
Displays the content of the queue.
SortedPriorityQueue(const SortedPriorityQueue< Val, Priority, Cmp > &from)
Copy constructor.
reverse_iterator rbegin() const
returns a new iterator pointing to the maximal element of the tree
optional_ref< AVLNode > tryGetNodeFromExternalValue_(const Val &val) const
returns an optional reference on the node of the hash table corresponding to a given external value
constexpr const iterator & end() const
returns an iterator pointing just after the maximal element
const Val * const_pointer
Types for STL compliance.
value_type popBottom()
Removes the bottom element from the priority queue and return it.
Size size() const noexcept
Returns the number of elements in the priority queue.
const_reference operator[](const Val &val) const
returns the "internal" value stored into the queue corresponding to val
const_reference insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
const Priority & bottomPriority() const
Returns the priority of the bottom element.
AVLNode & getNodeFromInternalValue_(const Val &val) const
returns the node in the hash table corresponding to a given internal value
bool contains(const Val &val) const noexcept
Indicates whether the priority queue contains a given value.
SortedPriorityQueueReverseIterator< Val, Priority, Cmp > reverse_iterator
Types for STL compliance.
constexpr const reverse_iterator_safe & rendSafe() const
returns a safe iterator pointing just before the minimal element
const Val & bottom() const
returns the element at the bottom of the sorted priority queue
iterator begin() const
returns a new iterator pointing to the minimal element of the tree
const Priority & topPriority() const
Returns the priority of the top element.
const Priority & priority(const Val &elt, bool internal_val=false) const
Returns the priority of an instance of the value passed in argument.
A lightweight wrapper around a pointer providing an optional-like API for references (not supported b...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
gum is the global namespace for all aGrUM entities
priority queues (in which an element cannot appear more than once)
#define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY
AVL binary search trees that do not possess their own nodes.
static constexpr Size default_size
The default number of slots in hashtables.