50#ifndef GUM_SORTED_PRIORITY_QUEUE_H
51#define GUM_SORTED_PRIORITY_QUEUE_H
65#include <initializer_list>
70 template <
typename Val,
typename Priority,
typename Cmp >
72 template <
typename Val,
typename Priority,
typename Cmp >
75 template <
typename Val,
typename Priority,
typename Cmp >
77 template <
typename Val,
typename Priority,
typename Cmp >
138 template <
typename Val,
typename Priority =
int,
typename Cmp = std::less< Priority > >
327 template < typename... Args >
450#ifndef DOXYGEN_SHOULD_SKIP_THIS
452 using AVLNode = AVLTreeNode< Val >;
455 using HashElt =
typename std::pair< const AVLTreeNode< Val >, Priority >;
461 TreeCmp(
const Cmp& cmp) : _cmp_(cmp) {}
463 TreeCmp(
Cmp&& cmp) : _cmp_(std::move(cmp)) {}
470 inline const Priority& getPriority(
const Val& v)
const {
471 return *((Priority*)((
char*)&v + offset_from_value_to_priority));
477 inline AVLTreeNode< Val >* getNode(
const Val& v)
const {
478 return (AVLTreeNode< Val >*)((
char*)&v - offset_to_value);
483 constexpr bool operator()(
const Val& x,
const Val& y)
const {
484 return _cmp_(getPriority(x), getPriority(y));
491 static constexpr std::size_t offset_to_priority = offsetof(HashElt, second);
492 static constexpr std::size_t offset_to_value = offsetof(AVLTreeNode< Val >, value);
493 static constexpr std::size_t offset_from_value_to_priority
494 = offset_to_priority - offset_to_value;
523#ifndef DOXYGEN_SHOULD_SKIP_THIS
529 template <
typename T >
530 struct is_basic_string: std::false_type {};
532 template <
typename T1,
typename T2,
typename T3 >
533 struct is_basic_string<
std::basic_string< T1, T2, T3 > >: std::true_type {};
553 template <
typename Val,
typename Priority =
int,
typename Cmp = std::less< Priority > >
555 protected SharedAVLTreeReverseIterator<
557 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > {
583 const bool begin =
true) noexcept;
585#ifndef DOXYGEN_SHOULD_SKIP_THIS
589 SharedAVLTreeReverseIterator< Val, TreeCmp >(init) {}
671 template < typename Val, typename Priority =
int, typename
Cmp =
std::less< Val > >
673 protected SharedAVLTreeReverseIteratorSafe<
701 const bool rbegin =
true);
703#ifndef DOXYGEN_SHOULD_SKIP_THIS
707 SharedAVLTreeReverseIteratorSafe< Val, TreeCmp >(init) {}
789 template < typename Val, typename Priority =
int, typename
Cmp =
std::less< Val > >
791 protected SharedAVLTreeIterator<
820 const bool rbegin =
true) noexcept;
822#ifndef DOXYGEN_SHOULD_SKIP_THIS
826 SharedAVLTreeIterator< Val, TreeCmp >(init) {}
910 template < typename Val, typename Priority =
int, typename
Cmp =
std::less< Val > >
912 protected SharedAVLTreeIteratorSafe<
941 const bool rbegin =
true);
943#ifndef DOXYGEN_SHOULD_SKIP_THIS
947 SharedAVLTreeIteratorSafe< Val, TreeCmp >(init) {}
1022 template < typename Val, typename Priority, typename
Cmp >
1023 std::ostream& operator<<(
std::ostream& stream,
1025 return stream << queue.toString();
1029#ifndef DOXYGEN_SHOULD_SKIP_THIS
1040 _static_SortedPriorityQueue_end_;
1041 extern const SortedPriorityQueueReverseIterator< int, std::less< int > >
1042 _static_SortedPriorityQueue_rend_;
1043 extern const SortedPriorityQueueIteratorSafe< int, std::less< int > >
1044 _static_SortedPriorityQueue_end_safe_;
1045 extern const SortedPriorityQueueReverseIteratorSafe< int, std::less< int > >
1046 _static_SortedPriorityQueue_rend_safe_;
1048 inline constexpr void*
const _SortedPriorityQueue_end_
1049 = (
void*
const)&_static_SortedPriorityQueue_end_;
1050 inline constexpr void*
const _SortedPriorityQueue_rend_
1051 = (
void*
const)&_static_SortedPriorityQueue_rend_;
1052 inline constexpr void*
const _SortedPriorityQueue_end_safe_
1053 = (
void*
const)&_static_SortedPriorityQueue_end_safe_;
1054 inline constexpr void*
const _SortedPriorityQueue_rend_safe_
1055 = (
void*
const)&_static_SortedPriorityQueue_rend_safe_;
The class for generic Hash Tables.
Sorted priority queues safe (w.r.t.
~SortedPriorityQueueIteratorSafe() noexcept
destructor
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 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
~SortedPriorityQueueIterator() noexcept
destructor
const value_type & const_reference
SortedPriorityQueueIterator(SortedPriorityQueueIterator< Val, Priority, Cmp > &&from) noexcept
move constructor
const value_type * const_pointer
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
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
Sorted priority queue reverse iterator.
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
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 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
Val * pointer
Types for STL compliance.
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.
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.
AVLNode & getNode_(const Val &val) const
returns the node in the hash table corresponding to a given value
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
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
value_type pop()
Removes the top element from the priority queue and return it.
Val & reference
Types for STL compliance.
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
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.
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
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.
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
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.
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.