aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
sortedPriorityQueue.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49
50#ifndef GUM_SORTED_PRIORITY_QUEUE_H
51#define GUM_SORTED_PRIORITY_QUEUE_H
52
53#include <cstddef>
54#include <functional>
55#include <sstream>
56#include <string>
57#include <utility>
58#include <vector>
59
60#include <agrum/agrum.h>
61
64
65#include <initializer_list>
66#include <type_traits>
67
68namespace gum {
69
70#ifndef DOXYGEN_SHOULD_SKIP_THIS
71
72 template < typename Val, typename Priority, typename Cmp >
74 template < typename Val, typename Priority, typename Cmp >
76
77 template < typename Val, typename Priority, typename Cmp >
79 template < typename Val, typename Priority, typename Cmp >
81
82 // we declare this class to enable getNodeFromExternalValue_ optimizations
83 class GraphChange;
84
85#endif // DOXYGEN_SHOULD_SKIP_THIS
86
145 template < typename Val, typename Priority = int, typename Cmp = std::less< Priority > >
147 public:
150 using value_type = Val;
151 using reference = Val&;
152 using const_reference = const Val&;
153 using pointer = Val*;
154 using const_pointer = const Val*;
155 using difference_type = std::ptrdiff_t;
161
162
163 // ============================================================================
165 // ============================================================================
167
177 explicit SortedPriorityQueue(Cmp compare = Cmp(),
179
187 explicit SortedPriorityQueue(std::initializer_list< std::pair< Val, Priority > > list);
188
194
200
205
207
208 public:
209 // ============================================================================
211 // ============================================================================
213
227
236
238 const_reference operator[](const Val& val) const;
239
241
242 // ============================================================================
244 // ============================================================================
246
251 Size size() const noexcept;
252
257 bool empty() const noexcept;
258
264 bool contains(const Val& val) const noexcept;
265
275 [[nodiscard]] optional_ref< const value_type > tryGet(const Val& key) const;
276
278
279 const Val& top() const;
280
282
283 const Val& bottom() const;
284
290 const Priority& topPriority() const;
291
297 const Priority& bottomPriority() const;
298
305
312
319
328 const_reference insert(const Val& val, const Priority& priority);
329
338 const_reference insert(Val&& val, Priority&& priority);
339
348 template < typename... Args >
349 const_reference emplace(Args&&... args);
350
357 void eraseTop();
358
366
381 void erase(const Val& val, bool internal_val = false);
382
394 void setPriority(const Val& elt, const Priority& new_priority, bool internal_val = false);
395
407 void setPriority(const Val& elt, Priority&& new_priority, bool internal_val = false);
408
423 const Priority& priority(const Val& elt, bool internal_val = false) const;
424
428 void clear();
429
434 std::string toString() const;
435
437
438 // ============================================================================
440 // ============================================================================
442
445
447 constexpr const iterator& end() const;
448
451
453 constexpr const reverse_iterator& rend() const;
454
457
459 constexpr const iterator_safe& endSafe() const;
460
463
465 constexpr const reverse_iterator_safe& rendSafe() const;
466
468
469 // ============================================================================
471 // ============================================================================
473
480 Size capacity() const noexcept;
481
487 void resize(Size new_size);
488
490
491#ifndef DOXYGEN_SHOULD_SKIP_THIS
493 using AVLNode = AVLTreeNode< Val >;
494
496 using HashElt = typename std::pair< const AVLTreeNode< Val >, Priority >;
497
499 struct TreeCmp {
500 TreeCmp() = default;
501
502 TreeCmp(const Cmp& cmp);
503
504 TreeCmp(Cmp&& cmp);
505
506 // get the priority associated with a given AVLTreeNode value. It turns out
507 // that the elements stored into the HashTable are of type HashElt defined
508 // above. Now, the constexpr offset defined below contains precisely the
509 // offset in bytes between Value v and its priority. Function getPriority
510 // therefore just computes the location of the priority and returns it.
511 const Priority& getPriority(const Val& v) const;
512
513 // from a Val instance stored into memory, get the address of the start of
514 // the AVLNode structure that would have contained it. This is useful for
515 // comparing elements in the hashTable without having to create dummy AVLNodes
516 AVLTreeNode< Val >* getNode(const Val& v) const;
517
518 // the comparison function between two contents of two nodes of the AVL tree:
519 // the nodes should be sorted according to their priority
520 bool operator()(const Val& x, const Val& y) const;
521
522 Cmp _cmp_;
523
524 // compute the offset in bytes between the location of Val and Priority in
525 // the elements stored into the hash table
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;
530 };
531#endif // DOXYGEN_SHOULD_SKIP_THIS
532
533 private:
545 SharedAVLTree< Val, TreeCmp > _tree_;
546
549
551 TreeCmp _tree_cmp_;
552
559 AVLNode& getNodeFromInternalValue_(const Val& val) const;
560
567 AVLNode& getNodeFromExternalValue_(const Val& val) const;
568
577
578#ifndef DOXYGEN_SHOULD_SKIP_THIS
579 // the TreeCmp getnode function induces some warning on some compilers when Val
580 // is a string because some optimizations are made by the compiler in this case.
581 // to avoid this, the getNode_ function above executes different codes depending
582 // on whether Val is different from a string or not. The structs below allow
583 // to make this discrimination at compile time.
584 template < typename T >
585 struct is_basic_string: std::false_type {};
586
587 template < typename T1, typename T2, typename T3 >
588 struct is_basic_string< std::basic_string< T1, T2, T3 > >: std::true_type {};
589#endif // DOXYGEN_SHOULD_SKIP_THIS
590
592 friend iterator;
596 };
597
608 template < typename Val, typename Priority = int, typename Cmp = std::less< Priority > >
610 protected SharedAVLTreeReverseIterator<
611 Val,
612 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > {
613 public:
616 using iterator_category = std::bidirectional_iterator_tag;
617 using value_type = Val;
621 using const_pointer = const value_type*;
624
625
626 // ============================================================================
628 // ============================================================================
630
638 const bool begin = true) noexcept;
639
640#ifndef DOXYGEN_SHOULD_SKIP_THIS
641 // constructor for the static end iterator
642 // only sortedPriorityQueue.cpp should use this constructor
643 explicit consteval SortedPriorityQueueIterator(StaticInitializer init) noexcept :
644 SharedAVLTreeReverseIterator< Val, TreeCmp >(init) {}
645#endif // DOXYGEN_SHOULD_SKIP_THIS
646
650
653
656
658
659
660 // ============================================================================
662 // ============================================================================
664
666 SortedPriorityQueueIterator< Val, Priority, Cmp >&
667 operator=(const SortedPriorityQueueIterator< Val, Priority, Cmp >& from) noexcept;
668
670 SortedPriorityQueueIterator< Val, Priority, Cmp >&
671 operator=(SortedPriorityQueueIterator< Val, Priority, Cmp >&& from) noexcept;
672
674 bool operator==(const SortedPriorityQueueIterator< Val, Priority, Cmp >& from) const;
675
677 bool operator!=(const SortedPriorityQueueIterator< Val, Priority, Cmp >& from) const;
678
680
682 SortedPriorityQueueIterator< Val, Priority, Cmp >& operator++() noexcept;
683
685
687 SortedPriorityQueueIterator< Val, Priority, Cmp >& operator+=(const Size k) noexcept;
688
690
692 SortedPriorityQueueIterator< Val, Priority, Cmp >& operator--() noexcept;
693
695
697 SortedPriorityQueueIterator< Val, Priority, Cmp >& operator-=(const Size k) noexcept;
698
705 const_reference operator*() const;
706
708 const_pointer operator->() const;
709
711
712 // ============================================================================
714 // ============================================================================
716
718
719 [[nodiscard]] const_reference value() const;
720
722
723 [[nodiscard]] const Priority& priority() const;
724
726
728 friend SortedPriorityQueue< Val, Priority, Cmp >;
729
730 using TreeIterator = SharedAVLTreeReverseIterator<
731 Val,
732 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp >;
733 };
734
745 template < typename Val, typename Priority = int, typename Cmp = std::less< Val > >
747 protected SharedAVLTreeReverseIteratorSafe<
748 Val,
749 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > {
750 public:
753 using iterator_category = std::bidirectional_iterator_tag;
754 using value_type = Val;
758 using const_pointer = const value_type*;
761
762
763 // ============================================================================
765 // ============================================================================
767
775 const bool rbegin = true);
776
777#ifndef DOXYGEN_SHOULD_SKIP_THIS
778 // constructor for the static endSafe iterator
779 // only sortedPriorityQueue.cpp should use this constructor
780 explicit consteval SortedPriorityQueueIteratorSafe(StaticInitializer init) noexcept :
781 SharedAVLTreeReverseIteratorSafe< Val, TreeCmp >(init) {}
782#endif // DOXYGEN_SHOULD_SKIP_THIS
783
787
790
793
795
796 // ============================================================================
798 // ============================================================================
800
802 SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >&
803 operator=(const SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& from);
804
806 SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >&
807 operator=(SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >&& from);
808
810 bool operator==(const SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& from) const;
811
813 bool operator!=(const SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& from) const;
814
816
818 SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& operator++() noexcept;
819
821
823 SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& operator+=(const Size k) noexcept;
824
826
828 SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& operator--() noexcept;
829
831
833 SortedPriorityQueueIteratorSafe< Val, Priority, Cmp >& operator-=(const Size k) noexcept;
834
841 const_reference operator*() const;
842
844 const_pointer operator->() const;
845
847
848 // ============================================================================
850 // ============================================================================
852
854
855 [[nodiscard]] const_reference value() const;
856
858
859 [[nodiscard]] const Priority& priority() const;
860
862
863 protected:
866
867 using TreeIterator = SharedAVLTreeReverseIteratorSafe<
868 Val,
869 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp >;
870 };
871
882 template < typename Val, typename Priority = int, typename Cmp = std::less< Val > >
884 protected SharedAVLTreeIterator<
885 Val,
886 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > {
887 public:
890 using iterator_category = std::bidirectional_iterator_tag;
891 using value_type = Val;
895 using const_pointer = const value_type*;
898
899
900 // ============================================================================
902 // ============================================================================
904
913 const bool rbegin = true) noexcept;
914
915#ifndef DOXYGEN_SHOULD_SKIP_THIS
916 // constructor for the static rend iterator
917 // only sortedPriorityQueue.cpp should use this constructor
918 explicit consteval SortedPriorityQueueReverseIterator(StaticInitializer init) noexcept :
919 SharedAVLTreeIterator< Val, TreeCmp >(init) {}
920#endif // DOXYGEN_SHOULD_SKIP_THIS
921
925
929
932
934
935 // ============================================================================
937 // ============================================================================
939
941 SortedPriorityQueueReverseIterator< Val, Priority, Cmp >&
942 operator=(const SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& from) noexcept;
943
945 SortedPriorityQueueReverseIterator< Val, Priority, Cmp >&
946 operator=(SortedPriorityQueueReverseIterator< Val, Priority, Cmp >&& from) noexcept;
947
949 bool operator==(const SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& from) const;
950
952 bool operator!=(const SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& from) const;
953
955
957 SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& operator++() noexcept;
958
960
962 SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& operator+=(const Size k) noexcept;
963
965
967 SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& operator--() noexcept;
968
970
972 SortedPriorityQueueReverseIterator< Val, Priority, Cmp >& operator-=(const Size k) noexcept;
973
980 const_reference operator*() const;
981
983 const_pointer operator->() const;
984
986
987 // ============================================================================
989 // ============================================================================
991
993
994 [[nodiscard]] const_reference value() const;
995
997
998 [[nodiscard]] const Priority& priority() const;
999
1001
1002 protected:
1004 friend SortedPriorityQueue< Val, Priority, Cmp >;
1005
1007 = SharedAVLTreeIterator< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp >;
1008 };
1009
1020 template < typename Val, typename Priority = int, typename Cmp = std::less< Val > >
1022 protected SharedAVLTreeIteratorSafe<
1023 Val,
1024 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > {
1025 public:
1028 using iterator_category = std::bidirectional_iterator_tag;
1029 using value_type = Val;
1036
1037
1038 // ============================================================================
1040 // ============================================================================
1042
1051 const bool rbegin = true);
1052
1053#ifndef DOXYGEN_SHOULD_SKIP_THIS
1054 // constructor for the static rendSafe iterator
1055 // only sortedPriorityQueue.cpp should use this constructor
1056 explicit consteval SortedPriorityQueueReverseIteratorSafe(StaticInitializer init) noexcept :
1057 SharedAVLTreeIteratorSafe< Val, TreeCmp >(init) {}
1058#endif // DOXYGEN_SHOULD_SKIP_THIS
1059
1063
1067
1070
1072
1073 // ============================================================================
1075 // ============================================================================
1077
1080 operator=(const SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& from);
1081
1084 operator=(SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >&& from);
1085
1087 bool operator==(const SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& from) const;
1088
1090 bool operator!=(const SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& from) const;
1091
1093
1095 SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& operator++() noexcept;
1096
1098
1100 SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& operator+=(const Size k) noexcept;
1101
1103
1105 SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& operator--() noexcept;
1106
1108
1110 SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp >& operator-=(const Size k) noexcept;
1111
1118 const_reference operator*() const;
1119
1121 const_pointer operator->() const;
1122
1124
1125 // ============================================================================
1127 // ============================================================================
1129
1131
1132 [[nodiscard]] const_reference value() const;
1133
1135
1136 [[nodiscard]] const Priority& priority() const;
1137
1139
1140 protected:
1144
1146 = SharedAVLTreeIteratorSafe< Val,
1147 typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp >;
1148 };
1149
1151 template < typename Val, typename Priority, typename Cmp >
1152 std::ostream& operator<<(std::ostream& stream,
1153 const SortedPriorityQueue< Val, Priority, Cmp >& queue);
1154
1155
1156#ifndef DOXYGEN_SHOULD_SKIP_THIS
1157 // _static_SortedPriorityQueue_end_ is a 'constant' iterator initialized at
1158 // compile time that represents the end iterators for all the sorted priority
1159 // queues (whatever their type). This global variable avoids creating the same
1160 // iterators within every sorted priority queue instance (this would be quite
1161 // inefficient as end is precisely identical for all these queues). The same hold
1162 // for reverse and safe end iterators.
1163 // The type of _SortedPriorityQueue_end_ is a pointer to void because C++ allows
1164 // pointers to void to be cast into pointers to other types (and conversely).
1165 // This avoids the painful strict-aliasing rule warning
1167 _static_SortedPriorityQueue_end_;
1169 _static_SortedPriorityQueue_rend_;
1171 _static_SortedPriorityQueue_end_safe_;
1173 _static_SortedPriorityQueue_rend_safe_;
1174
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_;
1183#endif // DOXYGEN_SHOULD_SKIP_THIS
1184
1185} /* namespace gum */
1186
1187// always include the implementation of the templates
1189
1190#endif /* GUM_SORTED_PRIORITY_QUEUE_H */
AVL binary search tree.
Definition AVLTree.h:149
The class for generic Hash Tables.
Definition hashTable.h:640
Sorted priority queues safe (w.r.t.
~SortedPriorityQueueIteratorSafe() noexcept
destructor
SharedAVLTreeReverseIteratorSafe< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > TreeIterator
SortedPriorityQueueIteratorSafe(SortedPriorityQueueIteratorSafe< Val, Priority, Cmp > &&from)
move constructor
SortedPriorityQueueIteratorSafe(const SortedPriorityQueueIteratorSafe< Val, Priority, Cmp > &from)
copy constructor
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
Sorted priority queue iterator.
typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp TreeCmp
SharedAVLTreeReverseIterator< Val, typename SortedPriorityQueue< Val, Priority, Cmp >::TreeCmp > TreeIterator
~SortedPriorityQueueIterator() noexcept
destructor
SortedPriorityQueueIterator(SortedPriorityQueueIterator< Val, Priority, Cmp > &&from) noexcept
move constructor
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
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
SortedPriorityQueueReverseIteratorSafe(const SortedPriorityQueueReverseIteratorSafe< Val, Priority, Cmp > &from)
copy constructor
~SortedPriorityQueueReverseIteratorSafe() noexcept
destructor
Sorted priority queue reverse iterator.
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
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
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.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
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.
Definition hashTable.h:102