Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedMapping.h
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/Mapping.h"
10
11#ifndef _Stroika_Foundation_Containers_SortedMapping_h_
12#define _Stroika_Foundation_Containers_SortedMapping_h_ 1
13
14/**
15 *
16 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
17 *
18 */
19
21
22 using Common::ITotallyOrderingComparer;
23
24 /**
25 * A SortedMapping is a Mapping<Key,T> which remains sorted (iterator) by the Key.
26 *
27 * @aliases Dictionary
28 *
29 * \note \em Iterators
30 * Note that iterators always run in sorted order, from least
31 * to largest. Items inserted before the current iterator index will not
32 * be encountered, and items inserted after the current index will be encountered.
33 * Items inserted at the current index remain undefined if they will
34 * be encountered or not.
35 *
36 * @see Mapping<Key,T>
37 *
38 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
39 *
40 * \note See <a href="./ReadMe.md">ReadMe.md</a> for common features of all Stroika containers (especially
41 * constructors, iterators, etc)
42 *
43 * \em Concrete Implementations:
44 * o @see Concrete::SortedMapping_SkipList<>
45 * o @see Concrete::SortedMapping_stdset<>
46 *
47 * \em Factory:
48 * @see SortedMapping_Factory<> to see default implementations.
49 *
50 * \note <a href="ReadMe.md#Container Element comparisons">Container Element comparisons</a>:
51 * See about ElementInOrderComparerType, ElementThreeWayComparerType and GetElementThreeWayComparer etc
52 *
53 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
54 * o Mappings (base class) are already intrinsically equals-comparable.
55 *
56 * o Since SortedMapping implies an ordering on the elements of the mapping, we can use this to define a
57 * compare_three_way ordering for SortedMapping<>
58 *
59 * o Since this was never supported before (not a regression) - and close to end of C++17 specific development,
60 * only implementing three way compare for C++20 or later.
61 *
62 * o Note we don't need todo a comparison on value, since value uniquely defined by KEY, and we have a total ordering
63 * on the KEYS.
64 */
65 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
66 class [[nodiscard]] SortedMapping : public Mapping<KEY_TYPE, MAPPED_VALUE_TYPE> {
67 private:
69
70 protected:
71 class _IRep;
72
73 public:
74 /**
75 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
76 */
78
79 public:
80 /**
81 */
83 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eStrictInOrder, function<bool (ArgByValueType<KEY_TYPE>, ArgByValueType<KEY_TYPE>)>>;
84
85 public:
86 /**
87 */
89 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eThreeWayCompare,
90 function<strong_ordering (ArgByValueType<KEY_TYPE>, ArgByValueType<KEY_TYPE>)>>;
91
92 public:
93 /**
94 * This constructor creates a concrete sorted mapping object, either empty,
95 * or initialized with any argument values.
96 *
97 * The underlying data structure of the Mapping is chosen by @see Factory::SortedMapping_Factory<>
98 *
99 * \par Example Usage
100 * \code
101 * Mapping<int, int> m{{1, 2}, {2, 4}};
102 * SortedMapping<int, int> sm{m};
103 * \endcode
104 *
105 * \note <a href="ReadMe.md#Container Constructors">See general information about container constructors that applies here</a>
106 */
108 requires (ITotallyOrderingComparer<less<KEY_TYPE>, KEY_TYPE>);
109 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER>
110 explicit SortedMapping (KEY_COMPARER&& keyComparer);
111 SortedMapping (SortedMapping&&) noexcept = default;
112 SortedMapping (const SortedMapping&) noexcept = default;
113 SortedMapping (const initializer_list<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>& src)
114 requires (ITotallyOrderingComparer<less<KEY_TYPE>, KEY_TYPE>);
115 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER>
116 SortedMapping (KEY_COMPARER&& keyComparer, const initializer_list<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>& src);
117 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER>
118 SortedMapping (KEY_COMPARER&& keyComparer, const initializer_list<pair<KEY_TYPE, MAPPED_VALUE_TYPE>>& src);
119 template <IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
120 explicit SortedMapping (ITERABLE_OF_ADDABLE&& src)
121 requires (ITotallyOrderingComparer<less<KEY_TYPE>, KEY_TYPE> and
122 not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedMapping<KEY_TYPE, MAPPED_VALUE_TYPE>>)
123#if qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
124 : SortedMapping{}
125 {
126 _AssertRepValidType ();
127 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
128 _AssertRepValidType ();
129 }
130#endif
131 ;
132 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER, IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
133 SortedMapping (KEY_COMPARER&& keyComparer, ITERABLE_OF_ADDABLE&& src);
134 template <IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
135 SortedMapping (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
136 requires (ITotallyOrderingComparer<less<KEY_TYPE>, KEY_TYPE>);
137 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER, IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
138 SortedMapping (KEY_COMPARER&& keyComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end);
139
140 protected:
141 explicit SortedMapping (shared_ptr<_IRep>&& src) noexcept;
142 explicit SortedMapping (const shared_ptr<_IRep>& src) noexcept;
143
144 public:
145 /**
146 */
147 nonvirtual SortedMapping& operator= (SortedMapping&&) noexcept = default;
148 nonvirtual SortedMapping& operator= (const SortedMapping&) = default;
149
150 public:
151 /**
152 * Return the function used to compare if two elements are in-order (sorted properly)
153 */
154 nonvirtual KeyInOrderComparerType GetKeyInOrderComparer () const;
155
156 public:
157 /**
158 * Return the function used to compare if two elements are (sorted properly)
159 */
160 nonvirtual KeyThreeWayComparerType GetKeyThreeWayComparer () const;
161
162 public:
163 /**
164 * \brief 'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedMapping, and improve that case to clone properties from this rep (such is rep type, ordering, etc).
165 */
166 template <typename RESULT_CONTAINER = SortedMapping<KEY_TYPE, MAPPED_VALUE_TYPE>, invocable<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ELEMENT_MAPPER>
167 nonvirtual RESULT_CONTAINER Map (ELEMENT_MAPPER&& elementMapper) const
168 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>, typename RESULT_CONTAINER::value_type> or
169 convertible_to<invoke_result_t<ELEMENT_MAPPER, KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>, optional<typename RESULT_CONTAINER::value_type>>)
170 ;
171
172 public:
173 /**
174 * \brief subset of this SortedMapping matching filter-function
175 *
176 * Identical to base class code, but for different RESULT_CONTAINER default.
177 */
178 template <derived_from<Iterable<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>> RESULT_CONTAINER = SortedMapping<KEY_TYPE, MAPPED_VALUE_TYPE>, typename INCLUDE_PREDICATE>
179 nonvirtual RESULT_CONTAINER Where (INCLUDE_PREDICATE&& includeIfTrue) const
180 requires (predicate<INCLUDE_PREDICATE, KEY_TYPE> or predicate<INCLUDE_PREDICATE, KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>);
181
182 public:
183 /**
184 * Compare sequentially using the associated GetKeyThreeWayComparer ()
185 */
186 nonvirtual strong_ordering operator<=> (const SortedMapping& rhs) const;
187
188 protected:
189 /**
190 */
191 template <typename T2>
192 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
193
194 protected:
195 /**
196 */
197 template <typename T2>
198 using _SafeReadWriteRepAccessor = typename inherited::template _SafeReadWriteRepAccessor<T2>;
199
200 protected:
201 nonvirtual void _AssertRepValidType () const;
202 };
203
204 /**
205 * \brief Implementation detail for SortedMapping<T> implementors.
206 *
207 * Protected abstract interface to support concrete implementations of
208 * the SortedMapping<T> container API.
209 */
210 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
211 class SortedMapping<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep : public Mapping<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep {
212 private:
213 using inherited = typename Mapping<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep;
214
215 public:
216 virtual KeyThreeWayComparerType GetKeyThreeWayComparer () const = 0;
217 };
218
219}
220
221/*
222 ********************************************************************************
223 ******************************* Implementation Details *************************
224 ********************************************************************************
225 */
226#include "SortedMapping.inl"
227
228#endif /*_Stroika_Foundation_Containers_SortedMapping_h_ */
Implementation detail for Mapping<T> implementors.
Definition Mapping.h:667
typename inherited::value_type value_type
Definition Mapping.h:139
Implementation detail for SortedMapping<T> implementors.
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237