Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
SortedSet_stdset.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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:
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 (bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
65 Execution::SequencePolicy seq) const override
66 {
68 return this->inherited::Find (findFirst, that, seq); // @todo rewrite to use fData
69 }
70 virtual Iterator<value_type> Find_equal_to (const ArgByValueType<value_type>& v, [[maybe_unused]] Execution::SequencePolicy seq) const override
71 {
72 // if doing a find by 'equals-to' - we already have this indexed
73 auto found = fData_.find (v);
74 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
75 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
76 .fIterator.GetUnderlyingIteratorRep ()));
77 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, found)};
78 }
79
80 // Set<T>::_IRep overrides
81 public:
82 virtual ElementEqualityComparerType GetElementEqualsComparer () const override
83 {
85 return Common::EqualsComparerAdapter<T, INORDER_COMPARER>{fData_.key_comp ()};
86 }
87 virtual shared_ptr<typename Set<T>::_IRep> CloneEmpty () const override
88 {
90 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
91 }
92 virtual shared_ptr<typename Set<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
93 {
96 auto result = Memory::MakeSharedPtr<Rep_> (*this);
97 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
98 result->fData_.MoveIteratorHereAfterClone (
99 &mir.fIterator, &fData_,
100 [targetI = mir.fIterator.GetUnderlyingIteratorRep ()] (auto oldI, [[maybe_unused]] auto newI) { return targetI == oldI; });
101 i->Refresh (); // reflect updated rep
102 return result;
103 }
104 virtual bool Equals (const typename Iterable<value_type>::_IRep& rhs) const override
105 {
107 return this->_Equals_Reference_Implementation (rhs);
108 }
110 {
112 auto i = fData_.find (item);
113 bool notDone = i != fData_.end ();
114 if (oResult != nullptr and notDone) {
115 *oResult = *i;
116 }
117 if (iResult != nullptr and notDone) {
118 *iResult = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, i)};
119 }
120 return notDone;
121 }
122 virtual void Add (ArgByValueType<value_type> item) override
123 {
125 fData_.insert (item);
126 fChangeCounts_.PerformedChange ();
127 }
128 virtual bool RemoveIf (ArgByValueType<value_type> item) override
129 {
131 fData_.Invariant ();
132 auto i = fData_.find (item);
133 if (i != fData_.end ()) [[likely]] {
134 fData_.erase (i);
135 fChangeCounts_.PerformedChange ();
136 return true;
137 }
138 return false;
139 }
140 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
141 {
142 Require (not i.AtEnd ());
144 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
145 mir.fIterator.AssertDataMatches (&fData_);
146 auto nextIResult = fData_.erase (mir.fIterator.GetUnderlyingIteratorRep ());
147 fChangeCounts_.PerformedChange ();
148 if (nextI != nullptr) {
149 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextIResult)};
150 }
151 }
152
153 // SortedSet<T>::_IRep overrides
154 public:
155 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
156 {
158 return Common::ThreeWayComparerAdapter<T, INORDER_COMPARER>{fData_.key_comp ()};
159 }
160
161 private:
162 using DataStructureImplType_ = DataStructures::STLContainerWrapper<STDSET<INORDER_COMPARER>>;
163 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
164
165 private:
166 DataStructureImplType_ fData_;
167 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
168 };
169
170 /*
171 ********************************************************************************
172 ****************************** SortedSet_stdset<T> *****************************
173 ********************************************************************************
174 */
175 template <typename T>
177 requires (totally_ordered<T>)
178 : SortedSet_stdset{less<T>{}}
179 {
180 AssertRepValidType_ ();
181 }
182 template <typename T>
183 template <IInOrderComparer<T> INORDER_COMPARER>
184 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inorderComparer)
185 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<INORDER_COMPARER>>> (forward<INORDER_COMPARER> (inorderComparer))}
186 {
187 AssertRepValidType_ ();
188 }
189 template <typename T>
190 inline SortedSet_stdset<T>::SortedSet_stdset (const initializer_list<T>& src)
191 requires (totally_ordered<T>)
192 : SortedSet_stdset{}
193 {
194 this->AddAll (src);
195 AssertRepValidType_ ();
196 }
197 template <typename T>
198 template <IInOrderComparer<T> INORDER_COMPARER>
199 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inOrderComparer, const initializer_list<T>& src)
200 : SortedSet_stdset{forward<INORDER_COMPARER> (inOrderComparer)}
201 {
202 this->AddAll (src);
203 AssertRepValidType_ ();
204 }
205#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
206 template <typename T>
207 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
208 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedSet_stdset<T>>)
209 inline SortedSet_stdset<T>::SortedSet_stdset (ITERABLE_OF_ADDABLE&& src)
210 : SortedSet_stdset{}
211 {
212 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
213 AssertRepValidType_ ();
214 }
215#endif
216 template <typename T>
217 template <IInOrderComparer<T> INORDER_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
218 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
219 : SortedSet_stdset (forward<INORDER_COMPARER> (inOrderComparer))
220 {
222 AssertRepValidType_ ();
223 }
224 template <typename T>
225 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
226 inline SortedSet_stdset<T>::SortedSet_stdset (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
227 : SortedSet_stdset{}
228 {
230 AssertRepValidType_ ();
231 }
232 template <typename T>
233 template <IInOrderComparer<T> INORDER_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
234 inline SortedSet_stdset<T>::SortedSet_stdset (INORDER_COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
235 : SortedSet_stdset (forward<INORDER_COMPARER> (inOrderComparer))
236 {
238 AssertRepValidType_ ();
239 }
240 template <typename T>
241 inline void SortedSet_stdset<T>::AssertRepValidType_ () const
242 {
244 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
245 }
246 }
247
248}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:49
#define RequireNotNull(p)
Definition Assertions.h:348
bool Equals(const T *lhs, const T *rhs)
strcmp or wsccmp() as appropriate == 0
#define qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE
[[msvc::no_unique_address]] isn't always broken in MSVC. Annotate with this on things where its not b...
Definition StdCompat.h:443
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:538
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:217
nonvirtual bool RemoveIf(ArgByValueType< value_type > item)
Definition Set.inl:229
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
unique_lock< AssertExternallySynchronizedChecker > WriteContext
Instantiate AssertExternallySynchronizedChecker::WriteContext to designate an area of code where prot...
shared_lock< const AssertExternallySynchronizedChecker > ReadContext
Instantiate AssertExternallySynchronizedChecker::ReadContext to designate an area of code where prote...
nonvirtual Iterator< T > Find(THAT_FUNCTION &&that) const
Run the argument bool-returning function (or lambda) on the elements of the container,...
nonvirtual CONTAINER_OF_T As(CONTAINER_OF_T_CONSTRUCTOR_ARGS... args) const
nonvirtual size_t size() const
Returns the number of items contained.
Definition Iterable.inl:311
nonvirtual void Apply(const function< void(ArgByValueType< T > item)> &doToElement) const
Run the argument function (or lambda) on each element of the container.
nonvirtual bool empty() const
Returns true iff size() == 0.
Definition Iterable.inl:317
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:305
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...