Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedKeyedCollection.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
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Common/Concepts.h"
10#include "Stroika/Foundation/Containers/KeyedCollection.h"
11
12#ifndef _Stroika_Foundation_Containers_SortedKeyedCollection_h_
13#define _Stroika_Foundation_Containers_SortedKeyedCollection_h_ 1
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 *
20 */
21
23
24 using Common::ITotallyOrderingComparer;
25
26 /**
27 * \brief A SortedKeyedCollection is a KeyedCollection<T> which remains sorted (iteration produces items sorted) even as you add and remove entries.
28 *
29 * A SortedKeyedCollection is a KeyedCollection<T> which remains sorted (iteration produces items sorted) even as you add and remove entries.
30 *
31 * Note that even though you cannot remove elements by value, or check "Contains" etc on Collection - in general, you always
32 * can with a SortedKeyedCollection, because the well-ordering required to define a sorted collection also imputes
33 * a notion of equality which is used for Contains etc.
34 *
35 * \note \em Iterators
36 * Note that iterators always run in sorted order, from least
37 * to largest. Items inserted before the current iterator index will not
38 * be encountered, and items inserted after the current index will be encountered.
39 * Items inserted at the current index remain undefined if they will
40 * be encountered or not.
41 *
42 * \em Concrete Implementations:
43 * o @see Concrete::SortedKeyedCollection_SkipList<>
44 * o @see Concrete::SortedKeyedCollection_stdset<>
45 *
46 * \em Factory:
47 * @see SortedKeyedCollection_Factory<> to see default implementations.
48 *
49 * @see Collection<T, TRAITS>
50 * @see SortedMapping<Key,T>
51 * @see SortedSet<T, TRAITS>
52 *
53 * \note <a href="ReadMe.md#Container Element comparisons">Container Element comparisons</a>:
54 * See about ElementInOrderComparerType, ElementThreeWayComparerType and GetElementThreeWayComparer etc
55 *
56 * \note \em Thread-Safety <a href="Thread-Safety.md#Automatically-LEGACY_Synchronized-Thread-Safety">Automatically-Synchronized-Thread-Safety</a>
57 *
58 */
59 template <typename T, typename KEY_TYPE, typename TRAITS = KeyedCollection_DefaultTraits<T, KEY_TYPE>>
60 class [[nodiscard]] SortedKeyedCollection : public KeyedCollection<T, KEY_TYPE, TRAITS> {
61 private:
63
64 protected:
65 class _IRep;
66
67 public:
68 /**
69 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
70 */
72
73 public:
74 /**
75 * Just a short-hand for the 'TRAITS' part of SortedKeyedCollection<T,TRAITS>. This is often handy to use in
76 * building other templates.
77 */
78 using TraitsType = TRAITS;
79
80 public:
81 /**
82 */
84 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eStrictInOrder, function<bool (ArgByValueType<KEY_TYPE>, ArgByValueType<KEY_TYPE>)>>;
85
86 public:
87 /**
88 */
90 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eThreeWayCompare,
91 function<strong_ordering (ArgByValueType<KEY_TYPE>, ArgByValueType<KEY_TYPE>)>>;
92
93 public:
94 using KeyEqualityComparerType = typename inherited::KeyEqualityComparerType;
95 using KeyExtractorType = typename inherited::KeyExtractorType;
96
97 public:
98 /**
99 * @see inherited::value_type
100 */
102
103 public:
104 /**
105 * All constructors either copy their source comparer (copy/move CTOR), or use the provided argument comparer
106 * (which in turn defaults to equal_to<T>).
107 *
108 * If TRAITS (TraitsType) has a valid default extractor, enable certain constructors.
109 *
110 * \pre ITotallyOrderingComparer<KEY_COMPARER,KEY_TYPE> - for constructors with that type parameter
111 *
112 * \note sort order specified/determined at construction time, and cannot be reset (without creating a new SortedKeyCollection)
113 *
114 * \par Example Usage
115 * \code
116 * \endcode
117 *
118 * \note <a href="ReadMe.md#Container Constructors">See general information about container constructors that applies here</a>
119 */
120 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER = less<KEY_TYPE>>
121 SortedKeyedCollection (KEY_COMPARER&& keyComparer = KEY_COMPARER{})
123 SortedKeyedCollection (SortedKeyedCollection&&) noexcept = default;
124 SortedKeyedCollection (const SortedKeyedCollection&) noexcept = default;
125 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER = less<KEY_TYPE>>
126 SortedKeyedCollection (const KeyExtractorType& keyExtractor, KEY_COMPARER&& keyComparer = KEY_COMPARER{});
127 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE, ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER = less<KEY_TYPE>>
128 SortedKeyedCollection (ITERABLE_OF_ADDABLE&& src)
129 requires (IKeyedCollection_ExtractorCanBeDefaulted<T, KEY_TYPE, TRAITS> and
130 not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedKeyedCollection<T, KEY_TYPE, TRAITS>>)
131#if qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
132 : SortedKeyedCollection{}
133 {
134 this->AddAll (src);
135 _AssertRepValidType ();
136 }
137#endif
138 ;
139 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE, ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER = less<KEY_TYPE>>
140 SortedKeyedCollection (KEY_COMPARER&& keyComparer, ITERABLE_OF_ADDABLE&& src)
141 requires (IKeyedCollection_ExtractorCanBeDefaulted<T, KEY_TYPE, TRAITS>);
142 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
143 SortedKeyedCollection (const KeyExtractorType& keyExtractor, KEY_COMPARER&& keyComparer, ITERABLE_OF_ADDABLE&& src);
144 template <IInputIterator<T> ITERATOR_OF_ADDABLE, ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER = less<KEY_TYPE>>
145 SortedKeyedCollection (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
146 requires (IKeyedCollection_ExtractorCanBeDefaulted<T, KEY_TYPE, TRAITS>);
147 template <IInputIterator<T> ITERATOR_OF_ADDABLE, ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER = less<KEY_TYPE>>
148 SortedKeyedCollection (KEY_COMPARER&& keyComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
149 requires (IKeyedCollection_ExtractorCanBeDefaulted<T, KEY_TYPE, TRAITS>);
150 template <ITotallyOrderingComparer<KEY_TYPE> KEY_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
151 SortedKeyedCollection (const KeyExtractorType& keyExtractor, KEY_COMPARER&& keyComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end);
152
153 protected:
154 explicit SortedKeyedCollection (shared_ptr<_IRep>&& src) noexcept;
155 explicit SortedKeyedCollection (const shared_ptr<_IRep>& src) noexcept;
156
157 public:
158 /**
159 */
160 nonvirtual SortedKeyedCollection& operator= (SortedKeyedCollection&&) noexcept = default;
161 nonvirtual SortedKeyedCollection& operator= (const SortedKeyedCollection&) = default;
162
163 public:
164 /**
165 */
166 nonvirtual KeyInOrderComparerType GetInOrderKeyComparer () const;
167
168 public:
169 /**
170 */
171 nonvirtual KeyThreeWayComparerType GetThreeWayKeyComparer () const;
172
173 public:
174 /**
175 * \brief 'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedKeyedCollection, and improve that case to clone properties from this rep (such is rep type, ordering, etc).
176 */
177 template <typename RESULT_CONTAINER = SortedKeyedCollection<T, KEY_TYPE, TRAITS>, invocable<T> ELEMENT_MAPPER>
178 nonvirtual RESULT_CONTAINER Map (ELEMENT_MAPPER&& elementMapper) const
179 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, typename RESULT_CONTAINER::value_type> or
180 convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, optional<typename RESULT_CONTAINER::value_type>>);
181
182 public:
183 /**
184 * \brief subset of this SortedKeyedCollection matching filter-function
185 *
186 * Identical to base class code, but for differnt RESULT_CONTAINER default.
187 */
188 template <derived_from<Iterable<T>> RESULT_CONTAINER = SortedKeyedCollection<T, KEY_TYPE, TRAITS>, predicate<T> INCLUDE_PREDICATE>
189 nonvirtual RESULT_CONTAINER Where (INCLUDE_PREDICATE&& includeIfTrue) const;
190
191 protected:
192 /**
193 */
194 template <typename T2>
195 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
196
197 protected:
198 /**
199 */
200 template <typename T2>
201 using _SafeReadWriteRepAccessor = typename inherited::template _SafeReadWriteRepAccessor<T2>;
202
203 protected:
204 nonvirtual void _AssertRepValidType () const;
205 };
206
207 /**
208 * \brief Implementation detail for SortedKeyedCollection<T, TRAITS> implementors.
209 *
210 * Protected abstract interface to support concrete implementations of
211 * the SortedKeyedCollection<T, KEY_TYPE, TRAITS> container API.
212 */
213 template <typename T, typename KEY_TYPE, typename TRAITS>
214 class SortedKeyedCollection<T, KEY_TYPE, TRAITS>::_IRep : public KeyedCollection<T, KEY_TYPE, TRAITS>::_IRep {
215 public:
216 virtual KeyThreeWayComparerType GetThreeWayKeyComparer () const = 0;
217 };
218
219}
220
221/*
222 ********************************************************************************
223 ******************************* Implementation Details *************************
224 ********************************************************************************
225 */
226
227#include "SortedKeyedCollection.inl"
228
229#endif /*_Stroika_Foundation_Containers_SortedKeyedCollection_h_ */
a cross between Mapping<KEY, T> and Collection<T> and Set<T>
typename TRAITS::KeyExtractorType KeyExtractorType
Implementation detail for SortedKeyedCollection<T, TRAITS> implementors.
A SortedKeyedCollection is a KeyedCollection<T> which remains sorted (iteration produces items sorted...
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237