Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
SortedMapping.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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 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 */
84
85 public:
86 /**
87 */
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 */
109 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER>
111 SortedMapping (SortedMapping&&) noexcept = default;
114 requires (ITotallyOrderingComparer<less<KEY_TYPE>, KEY_TYPE>);
115 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER>
117 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER>
121 requires (ITotallyOrderingComparer<less<KEY_TYPE>, KEY_TYPE> and
123#if qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
124 : SortedMapping{}
125 {
126 _AssertRepValidType ();
128 _AssertRepValidType ();
129 }
130#endif
131 ;
132 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER, IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
134 template <IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
137 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER, IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
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 */
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 */
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 */
192 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
193
194 protected:
195 /**
196 */
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 */
218
219}
220
221/*
222 ********************************************************************************
223 ******************************* Implementation Details *************************
224 ********************************************************************************
225 */
226#include "SortedMapping.inl"
227
228#endif /*_Stroika_Foundation_Containers_SortedMapping_h_ */
nonvirtual CONTAINER_OF_Key_T As() const
nonvirtual unsigned int AddAll(ITERABLE_OF_ADDABLE &&items, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
Implementation detail for SortedMapping<T> implementors.
nonvirtual RESULT_CONTAINER Where(INCLUDE_PREDICATE &&includeIfTrue) const
subset of this SortedMapping matching filter-function
nonvirtual KeyInOrderComparerType GetKeyInOrderComparer() const
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedMapping,...
nonvirtual KeyThreeWayComparerType GetKeyThreeWayComparer() const
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.