Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedCollection.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
6
8
10
11 /*
12 ********************************************************************************
13 ***************************** SortedCollection<T> ******************************
14 ********************************************************************************
15 */
16 template <typename T>
18 requires (ITotallyOrderingComparer<less<T>, T>)
19 : SortedCollection{less<T>{}}
20 {
21 _AssertRepValidType ();
22 }
23 template <typename T>
24 template <ITotallyOrderingComparer<T> COMPARER>
25 inline SortedCollection<T>::SortedCollection (COMPARER&& comparer)
26 : inherited{Factory::SortedCollection_Factory<T, remove_cvref_t<COMPARER>> () (forward<COMPARER> (comparer))}
27 {
28 _AssertRepValidType ();
29 }
30 template <typename T>
31 inline SortedCollection<T>::SortedCollection (const shared_ptr<_IRep>& src) noexcept
32 : inherited{src}
33 {
34 RequireNotNull (src);
35 _AssertRepValidType ();
36 }
37 template <typename T>
38 inline SortedCollection<T>::SortedCollection (shared_ptr<_IRep>&& src) noexcept
39 : inherited{(RequireExpression (src != nullptr), move (src))}
40 {
41 _AssertRepValidType ();
42 }
43 template <typename T>
44 inline SortedCollection<T>::SortedCollection (const initializer_list<T>& src)
45 requires (ITotallyOrderingComparer<less<T>, T>)
46 : SortedCollection{}
47 {
48 this->AddAll (src);
49 _AssertRepValidType ();
50 }
51 template <typename T>
52 template <ITotallyOrderingComparer<T> COMPARER>
53 inline SortedCollection<T>::SortedCollection (COMPARER&& comparer, const initializer_list<T>& src)
54 : SortedCollection{forward<COMPARER> (comparer)}
55 {
56 this->AddAll (src);
57 _AssertRepValidType ();
58 }
59#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
60 template <typename T>
61 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
62 inline SortedCollection<T>::SortedCollection (ITERABLE_OF_ADDABLE&& src)
63 requires (ITotallyOrderingComparer<less<T>, T> and not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection<T>>)
64 : SortedCollection{}
65 {
66 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
67 _AssertRepValidType ();
68 }
69#endif
70 template <typename T>
71 template <ITotallyOrderingComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
72 inline SortedCollection<T>::SortedCollection (COMPARER&& comparer, ITERABLE_OF_ADDABLE&& src)
73 : SortedCollection{forward<COMPARER> (comparer)}
74 {
75 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
76 _AssertRepValidType ();
77 }
78 template <typename T>
79 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
80 inline SortedCollection<T>::SortedCollection (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
81 requires (ITotallyOrderingComparer<less<T>, T>)
82 : SortedCollection{}
83 {
84 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
85 _AssertRepValidType ();
86 }
87 template <typename T>
88 template <ITotallyOrderingComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
89 inline SortedCollection<T>::SortedCollection (COMPARER&& comparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
90 : SortedCollection{forward<COMPARER> (comparer)}
91 {
92 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
93 _AssertRepValidType ();
94 }
95 template <typename T>
96 inline void SortedCollection<T>::_AssertRepValidType () const
97 {
99 _SafeReadRepAccessor<_IRep>{this};
100 }
101 }
102 template <typename T>
104 {
106 _SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().GetElementThreeWayComparer ()};
107 }
108 template <typename T>
110 {
111 return _SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().GetElementThreeWayComparer ();
112 }
113 template <typename T>
114 inline bool SortedCollection<T>::Contains (ArgByValueType<T> item) const
115 {
116 return _SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().Contains (item);
117 }
118 template <typename T>
119 inline void SortedCollection<T>::Remove (ArgByValueType<T> item)
120 {
121 _SafeReadWriteRepAccessor<_IRep>{this}._GetWriteableRep ().Remove (item);
122 }
123 template <typename T>
124 template <typename RESULT_CONTAINER, invocable<T> ELEMENT_MAPPER>
125 inline RESULT_CONTAINER SortedCollection<T>::Map (ELEMENT_MAPPER&& elementMapper) const
126 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, typename RESULT_CONTAINER::value_type> or
127 convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, optional<typename RESULT_CONTAINER::value_type>>)
128 {
129 if constexpr (same_as<RESULT_CONTAINER, SortedCollection>) {
130 // clone the rep so we retain the ordering function etc
131 return inherited::template Map<RESULT_CONTAINER> (forward<ELEMENT_MAPPER> (elementMapper),
132 RESULT_CONTAINER{_SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().CloneEmpty ()});
133 }
134 else {
135 return inherited::template Map<RESULT_CONTAINER> (forward<ELEMENT_MAPPER> (elementMapper)); // default inherited interpretation
136 }
137 }
138 template <typename T>
139 template <derived_from<Iterable<T>> RESULT_CONTAINER, predicate<T> INCLUDE_PREDICATE>
140 inline RESULT_CONTAINER SortedCollection<T>::Where (INCLUDE_PREDICATE&& includeIfTrue) const
141 {
142 if constexpr (same_as<RESULT_CONTAINER, SortedCollection>) {
143 // clone the rep so we retain the ordering function etc
144 return inherited::template Where<RESULT_CONTAINER> (
145 forward<INCLUDE_PREDICATE> (includeIfTrue), RESULT_CONTAINER{_SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().CloneEmpty ()});
146 }
147 else {
148 return inherited::template Where<RESULT_CONTAINER> (forward<INCLUDE_PREDICATE> (includeIfTrue)); // default inherited interpretation
149 }
150 }
151 template <typename T>
153 {
154 auto elementEqualsComparer = Common::EqualsComparerAdapter{this->GetInOrderComparer ()};
155 // @todo understand/fix why decltype(elementEqualsComparer>) needed, and not deduced
156 return typename Iterable<T>::template SequentialEqualsComparer<decltype (elementEqualsComparer)>{elementEqualsComparer}(*this, rhs);
157 }
158 template <typename T>
159 inline strong_ordering SortedCollection<T>::operator<=> (const SortedCollection& rhs) const
160 {
161 auto elementThreeWayComparer = Common::ThreeWayComparerAdapter{this->GetInOrderComparer ()};
162 // @todo understand/fix why decltype(elementEqualsComparer>) needed, and not deduced
163 return typename Iterable<T>::template SequentialThreeWayComparer<decltype (elementThreeWayComparer)>{elementThreeWayComparer}(*this, rhs);
164 }
165
166}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:48
#define RequireNotNull(p)
Definition Assertions.h:347
#define RequireExpression(c)
Definition Assertions.h:267
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
A SortedCollection is a Collection<T> which remains sorted (iteration produces items sorted) even as ...
nonvirtual ElementInOrderComparerType GetInOrderComparer() const
nonvirtual strong_ordering operator<=>(const SortedCollection &rhs) const
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to SortedCollection,...
nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer() const
nonvirtual bool operator==(const SortedCollection &rhs) 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.
Use this to wrap any basic comparer, and produce an Equals comparer.
Definition Compare.h:356
Use this to wrap any basic comparer, and produce a Less comparer.
Definition Compare.h:386
Use this to wrap a basic (ITotallyOrderingComparer) comparer, and produce a Three-Way comparer.
Definition Compare.h:414