Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedMultiSet.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5// Moved #includes outside #include guard to avoid deadly embrace
6#include "Stroika/Foundation/StroikaPreComp.h"
7
8#include "Stroika/Foundation/Common/Concepts.h"
9#include "Stroika/Foundation/Containers/DefaultTraits/MultiSet.h"
11
12#ifndef _Stroika_Foundation_Containers_SortedMultiSet_h_
13#define _Stroika_Foundation_Containers_SortedMultiSet_h_ 1
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 *
20 *
21 */
22
24
25 using Common::ITotallyOrderingComparer;
26
27 /**
28 * A SortedMultiSet is a MultiSet<T, TRAITS> which remains sorted (iterator).
29 *
30 * \note \em Iterators
31 * Note that iterators always run in sorted order, from least
32 * to largest. Items inserted before the current iterator index will not
33 * be encountered, and items inserted after the current index will be encountered.
34 * Items inserted at the current index remain undefined if they will
35 * be encountered or not.
36 *
37 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
38 *
39 * \note See <a href="./ReadMe.md">ReadMe.md</a> for common features of all Stroika containers (especially
40 * constructors, iterators, etc)
41 *
42 * \em Concrete Implementations:
43 * o @see Concrete::SortedMultiSet_SkipList<>
44 * o @see Concrete::SortedMultiSet_stdmap<>
45 *
46 * \em Factory:
47 * @see SortedMultiSet_Factory<> to see default implementations.
48 *
49 * \note <a href="ReadMe.md#Container Element comparisons">Container Element comparisons</a>:
50 * See about ElementInOrderComparerType, ElementThreeWayComparerType and GetElementThreeWayComparer etc
51 *
52 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
53 * o MultiSet (base class) are already intrinsically equals-comparable.
54 *
55 * o Since SortedMultiSet implies an ordering on the elements of the MultiSet, we can use this to define a
56 * compare_three_way ordering for SortedMultiSet<>
57 *
58 * o Since this was never supported before (not a regression) - and close to end of C++17 specific development,
59 * only implementing three way compare for C++20 or later.
60 */
61 template <typename T, typename TRAITS = DefaultTraits::MultiSet<T>>
62 class [[nodiscard]] SortedMultiSet : public MultiSet<T, TRAITS> {
63 private:
65
66 public:
67 /**
68 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
69 */
71
72 protected:
73 class _IRep;
74
75 public:
76 using TraitsType = typename inherited::TraitsType;
77 using value_type = typename inherited::value_type;
78
79 public:
80 /**
81 */
83 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eStrictInOrder, function<bool (ArgByValueType<T>, ArgByValueType<T>)>>;
84
85 public:
86 /**
87 */
89 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eThreeWayCompare, function<strong_ordering (ArgByValueType<T>, ArgByValueType<T>)>>;
90
91 public:
92 /**
93 * \note <a href="ReadMe.md#Container Constructors">See general information about container constructors that applies here</a>
94 */
97 template <Common::ITotallyOrderingComparer<T> COMPARER>
98 explicit SortedMultiSet (COMPARER&& comparer);
99 SortedMultiSet (SortedMultiSet&&) noexcept = default;
100 SortedMultiSet (const SortedMultiSet&) noexcept = default;
101 SortedMultiSet (const initializer_list<T>& src)
102 requires (Common::ITotallyOrderingComparer<less<T>, T>);
103 template <Common::ITotallyOrderingComparer<T> COMPARER>
104 SortedMultiSet (COMPARER&& comparer, const initializer_list<T>& src);
105 SortedMultiSet (const initializer_list<value_type>& src)
106 requires (Common::ITotallyOrderingComparer<less<T>, T>);
107 template <Common::ITotallyOrderingComparer<T> COMPARER>
108 SortedMultiSet (COMPARER&& comparer, const initializer_list<value_type>& src);
109 template <IIterableOfTo<typename TRAITS::CountedValueType> ITERABLE_OF_ADDABLE>
110 explicit SortedMultiSet (ITERABLE_OF_ADDABLE&& src)
111 requires (Common::ITotallyOrderingComparer<less<T>, T> and not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedMultiSet<T, TRAITS>>)
112#if qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
114 {
115 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
116 _AssertRepValidType ();
117 }
118#endif
119 ;
120 template <Common::ITotallyOrderingComparer<T> COMPARER, IIterableOfTo<typename TRAITS::CountedValueType> ITERABLE_OF_ADDABLE>
121 SortedMultiSet (COMPARER&& comparer, ITERABLE_OF_ADDABLE&& src);
122 template <IInputIterator<typename TRAITS::CountedValueType> ITERATOR_OF_ADDABLE>
123 SortedMultiSet (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
125 template <IInputIterator<typename TRAITS::CountedValueType> COMPARER, IInputIterator<typename TRAITS::CountedValueType> ITERATOR_OF_ADDABLE>
126 SortedMultiSet (COMPARER&& comparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end);
127
128 protected:
129 explicit SortedMultiSet (shared_ptr<_IRep>&& src) noexcept;
130 explicit SortedMultiSet (const shared_ptr<_IRep>& src) noexcept;
131
132 public:
133 /**
134 */
135 nonvirtual SortedMultiSet& operator= (SortedMultiSet&&) noexcept = default;
136 nonvirtual SortedMultiSet& operator= (const SortedMultiSet&) = default;
137
138 public:
139 /**
140 * Return the function used to compare if two elements are in-order (sorted properly)
141 */
142 nonvirtual ElementInOrderComparerType GetElementInOrderComparer () const;
143
144 public:
145 /**
146 */
147 nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer () const;
148
149 public:
150 /**
151 * \brief 'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedMultiSet, and improve that case to clone properties from this rep (such is rep type, ordering, etc).
152 */
153 template <typename RESULT_CONTAINER = SortedMultiSet<T, TRAITS>, invocable<T> ELEMENT_MAPPER>
154 nonvirtual RESULT_CONTAINER Map (ELEMENT_MAPPER&& elementMapper) const
155 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, typename TRAITS::CountedValueType>, typename RESULT_CONTAINER::value_type> or
156 convertible_to<invoke_result_t<ELEMENT_MAPPER, typename TRAITS::CountedValueType>, optional<typename RESULT_CONTAINER::value_type>>);
157
158 public:
159 /**
160 * See Iterable<T>::Where - except defaults to MultiSet, and handles cloning rep properties for that special case
161 */
162 template <derived_from<Iterable<typename TRAITS::CountedValueType>> RESULT_CONTAINER = SortedMultiSet<T, TRAITS>, predicate<typename TRAITS::CountedValueType> INCLUDE_PREDICATE>
163 nonvirtual RESULT_CONTAINER Where (INCLUDE_PREDICATE&& includeIfTrue) const;
164
165 public:
166 /**
167 * Compare sequentially using the associated GetElementThreeWayComparer ()
168 */
169 nonvirtual strong_ordering operator<=> (const SortedMultiSet& rhs) const;
170
171 protected:
172 /**
173 */
174 template <typename T2>
175 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
176
177 protected:
178 /**
179 */
180 template <typename T2>
181 using _SafeReadWriteRepAccessor = typename inherited::template _SafeReadWriteRepAccessor<T2>;
182
183 protected:
184 nonvirtual void _AssertRepValidType () const;
185 };
186
187 /**
188 * \brief Implementation detail for SortedMultiSet<T, TRAITS> implementors.
189 *
190 * Protected abstract interface to support concrete implementations of
191 * the SortedMultiSet<T, TRAITS> container API.
192 *
193 * Note that this doesn't add any methods, but still serves the purpose of allowing
194 * testing/validation that the subtype information is correct (it is sorted).
195 */
196 template <typename T, typename TRAITS>
197 class SortedMultiSet<T, TRAITS>::_IRep : public MultiSet<T, TRAITS>::_IRep {
198 public:
199 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const = 0;
200 };
201
202}
203
204/*
205 ********************************************************************************
206 ******************************* Implementation Details *************************
207 ********************************************************************************
208 */
209#include "SortedMultiSet.inl"
210
211#endif /*_Stroika_Foundation_Containers_SortedMultiSet_h_ */
typename inherited::value_type value_type
Definition MultiSet.h:149
Implementation detail for SortedMultiSet<T, TRAITS> implementors.