Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
SortedSet_stdset.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
8
10
11 /*
12 ********************************************************************************
13 *************************** SortedSet_stdset<T>::Rep_ **************************
14 ********************************************************************************
15 */
16 template <typename T>
17 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (IInOrderComparer<T>) INORDER_COMPARER>
18 class SortedSet_stdset<T>::Rep_ : public IImplRepBase_, public Memory::UseBlockAllocationIfAppropriate<Rep_<INORDER_COMPARER>> {
19 private:
20 using inherited = IImplRepBase_;
21
22 public:
23 static_assert (not is_reference_v<INORDER_COMPARER>);
24
25 public:
26 Rep_ (const INORDER_COMPARER& inorderComparer)
27 : fData_{inorderComparer}
28 {
29 }
30 Rep_ (const Rep_& from) = default;
31
32 public:
33 nonvirtual Rep_& operator= (const Rep_&) = delete;
34
35 // Iterable<T>::_IRep overrides
36 public:
37 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
38 {
40 return Memory::MakeSharedPtr<Rep_> (*this);
41 }
42 virtual Iterator<value_type> MakeIterator () const override
43 {
45 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
46 }
47 virtual size_t size () const override
48 {
50 fData_.Invariant ();
51 return fData_.size ();
52 }
53 virtual bool empty () const override
54 {
56 fData_.Invariant ();
57 return fData_.empty ();
58 }
59 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
60 {
62 fData_.Apply (doToElement);
63 }
64 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that, Execution::SequencePolicy seq) const override
65 {
67 return this->inherited::Find (that, seq); // @todo rewrite to use fData
68 }
69 virtual Iterator<value_type> Find_equal_to (const ArgByValueType<value_type>& v, [[maybe_unused]] Execution::SequencePolicy seq) const override
70 {
71 // if doing a find by 'equals-to' - we already have this indexed
72 auto found = fData_.find (v);
73 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
74 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
75 .fIterator.GetUnderlyingIteratorRep ()));
76 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, found)};
77 }
78
79 // Set<T>::_IRep overrides
80 public:
81 virtual ElementEqualityComparerType GetElementEqualsComparer () const override
82 {
84 return Common::EqualsComparerAdapter<T, INORDER_COMPARER>{fData_.key_comp ()};
85 }
86 virtual shared_ptr<typename Set<T>::_IRep> CloneEmpty () const override
87 {
89 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
90 }
91 virtual shared_ptr<typename Set<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
92 {
95 auto result = Memory::MakeSharedPtr<Rep_> (*this);
96 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
97 result->fData_.MoveIteratorHereAfterClone (
98 &mir.fIterator, &fData_,
99 [targetI = mir.fIterator.GetUnderlyingIteratorRep ()] (auto oldI, [[maybe_unused]] auto newI) { return targetI == oldI; });
100 i->Refresh (); // reflect updated rep
101 return result;
102 }
103 virtual bool Equals (const typename Iterable<value_type>::_IRep& rhs) const override
104 {
106 return this->_Equals_Reference_Implementation (rhs);
107 }
108 virtual bool Lookup (ArgByValueType<value_type> item, optional<value_type>* oResult, Iterator<value_type>* iResult) const override
109 {
111 auto i = fData_.find (item);
112 bool notDone = i != fData_.end ();
113 if (oResult != nullptr and notDone) {
114 *oResult = *i;
115 }
116 if (iResult != nullptr and notDone) {
117 *iResult = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, i)};
118 }
119 return notDone;
120 }
121 virtual void Add (ArgByValueType<value_type> item) override
122 {
124 fData_.insert (item);
125 fChangeCounts_.PerformedChange ();
126 }
127 virtual bool RemoveIf (ArgByValueType<value_type> item) override
128 {
130 fData_.Invariant ();
131 auto i = fData_.find (item);
132 if (i != fData_.end ()) [[likely]] {
133 fData_.erase (i);
134 fChangeCounts_.PerformedChange ();
135 return true;
136 }
137 return false;
138 }
139 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
140 {
141 Require (not i.Done ());
143 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
144 mir.fIterator.AssertDataMatches (&fData_);
145 auto nextIResult = fData_.erase (mir.fIterator.GetUnderlyingIteratorRep ());
146 fChangeCounts_.PerformedChange ();
147 if (nextI != nullptr) {
148 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextIResult)};
149 }
150 }
151
152 // SortedSet<T>::_IRep overrides
153 public:
154 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
155 {
157 return Common::ThreeWayComparerAdapter<T, INORDER_COMPARER>{fData_.key_comp ()};
158 }
159
160 private:
161 using DataStructureImplType_ = DataStructures::STLContainerWrapper<STDSET<INORDER_COMPARER>>;
162 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
163
164 private:
165 DataStructureImplType_ fData_;
166 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
167 };
168
169 /*
170 ********************************************************************************
171 ****************************** SortedSet_stdset<T> *****************************
172 ********************************************************************************
173 */
174 template <typename T>
176 requires (totally_ordered<T>)
177 : SortedSet_stdset{less<T>{}}
178 {
179 AssertRepValidType_ ();
180 }
181 template <typename T>
182 template <IInOrderComparer<T> INORDER_COMPARER>
183 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inorderComparer)
184 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<INORDER_COMPARER>>> (forward<INORDER_COMPARER> (inorderComparer))}
185 {
186 AssertRepValidType_ ();
187 }
188 template <typename T>
189 inline SortedSet_stdset<T>::SortedSet_stdset (const initializer_list<T>& src)
190 requires (totally_ordered<T>)
191 : SortedSet_stdset{}
192 {
193 this->AddAll (src);
194 AssertRepValidType_ ();
195 }
196 template <typename T>
197 template <IInOrderComparer<T> INORDER_COMPARER>
198 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inOrderComparer, const initializer_list<T>& src)
199 : SortedSet_stdset{forward<INORDER_COMPARER> (inOrderComparer)}
200 {
201 this->AddAll (src);
202 AssertRepValidType_ ();
203 }
204#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
205 template <typename T>
206 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
207 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedSet_stdset<T>>)
208 inline SortedSet_stdset<T>::SortedSet_stdset (ITERABLE_OF_ADDABLE&& src)
209 : SortedSet_stdset{}
210 {
211 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
212 AssertRepValidType_ ();
213 }
214#endif
215 template <typename T>
216 template <IInOrderComparer<T> INORDER_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
217 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
218 : SortedSet_stdset (forward<INORDER_COMPARER> (inOrderComparer))
219 {
220 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
221 AssertRepValidType_ ();
222 }
223 template <typename T>
224 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
225 inline SortedSet_stdset<T>::SortedSet_stdset (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
226 : SortedSet_stdset{}
227 {
228 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
229 AssertRepValidType_ ();
230 }
231 template <typename T>
232 template <IInOrderComparer<T> INORDER_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
233 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
234 : SortedSet_stdset (forward<INORDER_COMPARER> (inOrderComparer))
235 {
236 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
237 AssertRepValidType_ ();
238 }
239 template <typename T>
240 inline void SortedSet_stdset<T>::AssertRepValidType_ () const
241 {
243 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
244 }
245 }
246
247}
#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
bool Equals(const T *lhs, const T *rhs)
strcmp or wsccmp() as appropriate == 0
SortedSet_stdset<T> is an std::set-based concrete implementation of the SortedSet<T> container patter...
nonvirtual optional< value_type > Lookup(ArgByValueType< value_type > item) const
Definition Set.inl:131
nonvirtual ElementEqualityComparerType GetElementEqualsComparer() const
Definition Set.inl:96
nonvirtual Set CloneEmpty() const
for return Set<T> { s->GetEqualsComparer(); } - except more efficient - clones settings/dynamic subty...
Definition Set.inl:503
nonvirtual void Remove(ArgByValueType< value_type > item)
Remove the item (given by value or iterator pointing to it) from the contain. The item MUST exist.
Definition Set.inl:182
nonvirtual bool RemoveIf(ArgByValueType< value_type > item)
Definition Set.inl:194
nonvirtual void Add(ArgByValueType< value_type > item)
Definition Set.inl:138
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer() const
shared_lock< const AssertExternallySynchronizedMutex > ReadContext
Instantiate AssertExternallySynchronizedMutex::ReadContext to designate an area of code where protect...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...
nonvirtual void Apply(const function< void(ArgByValueType< T > item)> &doToElement, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
Run the argument function (or lambda) on each element of the container.
nonvirtual Iterator< T > Find(THAT_FUNCTION &&that, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
Run the argument bool-returning function (or lambda) on each element of the container,...
nonvirtual size_t size() const
Returns the number of items contained.
Definition Iterable.inl:302
nonvirtual bool empty() const
Returns true iff size() == 0.
Definition Iterable.inl:308
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.
nonvirtual Iterator< T > MakeIterator() const
Create an iterator object which can be used to traverse the 'Iterable'.
Definition Iterable.inl:296
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...