Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
SortedCollection.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/Collection.h"
10
11#ifndef _Stroika_Foundation_Containers_SortedCollection_h_
12#define _Stroika_Foundation_Containers_SortedCollection_h_ 1
13
14/**
15 * \file
16 *
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 *
20 * TODO:
21 * @todo Implement using red-black trees (kind of done via STL, but do with explicit RedBlackTree class)
22 *
23 */
24
26
27 using Common::ITotallyOrderingComparer;
28
29 /**
30 * \brief A SortedCollection is a Collection<T> which remains sorted (iteration produces items sorted) even as you add and remove entries.
31 *
32 * A SortedCollection is a Collection<T> which remains sorted (iteration produces items sorted) even as you add and remove entries.
33 *
34 * Note that even though you cannot remove elements by value, or check "Contains" etc on Collection - in general, you always
35 * can with a SortedCollection, because the well-ordering required to define a sorted collection also imputes
36 * a notion of equality which is used for Contains etc.
37 *
38 * \note \em Iterators
39 * Note that iterators always run in sorted order, from least
40 * to largest. Items inserted before the current iterator index will not
41 * be encountered, and items inserted after the current index will be encountered.
42 * Items inserted at the current index remain undefined if they will
43 * be encountered or not.
44 *
45 * @see Collection<T>
46 * @see SortedMapping<Key,T>
47 * @see SortedSet<T>
48 *
49 * \em Concrete Implementations:
50 * o @see Concrete::SortedCollection_LinkedList<>
51 * o @see Concrete::SortedCollection_stdmultiset<>
52 * o @see Concrete::SortedCollection_SkipList<>
53 *
54 * \em Factory:
55 * @see SortedCollection_Factory<> to see default implementations.
56 *
57 * \note \em Thread-Safety <a href="Thread-Safety.md#Automatically-LEGACY_Synchronized-Thread-Safety">Automatically-Synchronized-Thread-Safety</a>
58 *
59 * \note See <a href="./ReadMe.md">ReadMe.md</a> for common features of all Stroika containers (especially
60 * constructors, iterators, etc)
61 *
62 * \note <a href="ReadMe.md#Container Element comparisons">Container Element comparisons</a>:
63 * See about ElementInOrderComparerType, ElementThreeWayComparerType and GetElementThreeWayComparer etc
64 *
65 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
66 * o Even though the base class Collection<T> provides no intrinsic comparison operators, to be sorted,
67 * does imply a comparison operator, so a SortedCollection<T> fully supports the c++ standard operator<=> strong comparison
68 * feature.
69 * o Compare sequentially using the associated GetInOrderComparer ()
70 */
71 template <typename T>
72 class SortedCollection : public Collection<T> {
73 private:
75
76 protected:
77 class _IRep;
78
79 public:
80 /**
81 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
82 */
84
85 public:
86 /**
87 */
89 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eStrictInOrder, function<bool (ArgByValueType<T>, ArgByValueType<T>)>>;
90
91 public:
92 /**
93 */
95 Common::ComparisonRelationDeclaration<Common::ComparisonRelationType::eThreeWayCompare, function<strong_ordering (ArgByValueType<T>, ArgByValueType<T>)>>;
96
97 public:
98 /**
99 * All constructors either copy their source comparer (copy/move CTOR), or use the default COMPARER for 'T'.
100 *
101 * \pre ITotallyOrderingComparer<COMPARER,T> - for constructors with inorderComparer parameter
102 *
103 * \note <a href="ReadMe.md#Container Constructors">See general information about container constructors that applies here</a>
104 */
106 requires (ITotallyOrderingComparer<less<T>, T>);
107 template <ITotallyOrderingComparer<T> COMPARER>
108 explicit SortedCollection (COMPARER&& inorderComparer);
109 SortedCollection (SortedCollection&&) noexcept = default;
110 SortedCollection (const SortedCollection&) noexcept = default;
111 SortedCollection (const initializer_list<T>& src)
112 requires (ITotallyOrderingComparer<less<T>, T>);
113 template <ITotallyOrderingComparer<T> COMPARER>
114 SortedCollection (COMPARER&& inOrderComparer, const initializer_list<T>& src);
115 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
116 explicit SortedCollection (ITERABLE_OF_ADDABLE&& src)
117 requires (ITotallyOrderingComparer<less<T>, T> and not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection<T>>)
118#if qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
120 {
121 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
122 _AssertRepValidType ();
123 }
124#endif
125 ;
126 template <ITotallyOrderingComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
127 SortedCollection (COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src);
128 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
129 SortedCollection (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
130 requires (ITotallyOrderingComparer<less<T>, T>);
131 template <ITotallyOrderingComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
132 SortedCollection (COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end);
133
134 protected:
135 explicit SortedCollection (shared_ptr<_IRep>&& src) noexcept;
136 explicit SortedCollection (const shared_ptr<_IRep>& src) noexcept;
137
138 public:
139 /**
140 */
141 nonvirtual SortedCollection& operator= (SortedCollection&&) noexcept = default;
142 nonvirtual SortedCollection& operator= (const SortedCollection&) = default;
143
144 public:
145 /**
146 * Return the function used to compare if two elements are in-order (sorted properly)
147 */
148 nonvirtual ElementInOrderComparerType GetInOrderComparer () const;
149
150 public:
151 /**
152 * Return the function used to compare if two elements returning strong_ordering (sorted properly)
153 */
154 nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer () const;
155
156 public:
157 /**
158 * \brief Compares items with the already associated GetInOrderComparer(), and returns true if the item is found.
159 */
160 nonvirtual bool Contains (ArgByValueType<T> item) const;
161
162 public:
163 /**
164 * \brief Removes the argument item with the already associated GetInOrderComparer(), and returns true if the item is found.
165 *
166 * \note mutates container
167 */
168 using inherited::Remove;
169 nonvirtual void Remove (ArgByValueType<T> item);
170
171 public:
172 /**
173 * \brief 'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedCollection, and improve that case to clone properties from this rep (such is rep type, ordering, etc).
174 */
175 template <typename RESULT_CONTAINER = SortedCollection<T>, invocable<T> ELEMENT_MAPPER>
176 nonvirtual RESULT_CONTAINER Map (ELEMENT_MAPPER&& elementMapper) const
177 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, typename RESULT_CONTAINER::value_type> or
178 convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, optional<typename RESULT_CONTAINER::value_type>>);
179
180 public:
181 /**
182 * \brief subset of this SortedCollection matching filter-function
183 *
184 * Identical to base class code, but for different RESULT_CONTAINER default.
185 */
186 template <derived_from<Iterable<T>> RESULT_CONTAINER = SortedCollection<T>, predicate<T> INCLUDE_PREDICATE>
187 nonvirtual RESULT_CONTAINER Where (INCLUDE_PREDICATE&& includeIfTrue) const;
188
189 public:
190 /**
191 * Compare sequentially using the associated GetInOrderComparer ()
192 */
193 nonvirtual bool operator== (const SortedCollection& rhs) const;
194
195 public:
196 /**
197 * Compare sequentially using the associated GetInOrderComparer ()
198 */
199 nonvirtual strong_ordering operator<=> (const SortedCollection& rhs) const;
200
201 protected:
202 /**
203 */
204 template <typename T2>
205 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
206
207 protected:
208 /**
209 */
210 template <typename T2>
211 using _SafeReadWriteRepAccessor = typename inherited::template _SafeReadWriteRepAccessor<T2>;
212
213 protected:
214 nonvirtual void _AssertRepValidType () const;
215 };
216
217 /**
218 * \brief Implementation detail for SortedCollection<T> implementors.
219 *
220 * Protected abstract interface to support concrete implementations of
221 * the SortedCollection<T> container API.
222 *
223 * \note for some scenarios, it might be more efficient to have both a GetElementInOrderComparer and GetElementThreeWayComparer
224 * method, but at the cost of modest code bloat and additional complexity. Callers who really care about this performance
225 * difference can count on other application logic to assure an even better compare function is used to compare usages.
226 * On clang++ on macOS, this was about 50 bytes per class/instantiation. Not much, but for practically zero value added.
227 */
228 template <typename T>
229 class SortedCollection<T>::_IRep : public Collection<T>::_IRep {
230 public:
232 virtual bool Contains (ArgByValueType<T> item) const = 0;
233 using Collection<T>::_IRep::Remove;
234 virtual void Remove (ArgByValueType<T> item) = 0;
235 };
236}
237
238/*
239 ********************************************************************************
240 ******************************* Implementation Details *************************
241 ********************************************************************************
242 */
243
244#include "SortedCollection.inl"
245
246#endif /*_Stroika_Foundation_Containers_SortedCollection_h_ */
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
Implementation detail for SortedCollection<T> implementors.
A SortedCollection is a Collection<T> which remains sorted (iteration produces items sorted) even as ...
nonvirtual ElementInOrderComparerType GetInOrderComparer() const
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedCollection,...
nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer() const
nonvirtual RESULT_CONTAINER Where(INCLUDE_PREDICATE &&includeIfTrue) const
subset of this SortedCollection matching filter-function
nonvirtual bool Contains(ArgByValueType< T > item) const
Compares items with the already associated GetInOrderComparer(), and returns true if the item is foun...
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.