Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedCollection_SkipList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/DataStructures/SkipList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
8
10
11 /*
12 ********************************************************************************
13 *********************** SortedCollection_SkipList<T>::Rep_ *********************
14 ********************************************************************************
15 */
16 template <typename T>
17 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (Common::IThreeWayComparer<T>) COMPARER>
18 class SortedCollection_SkipList<T>::Rep_ : public Private::SkipListBasedContainerRepImpl<Rep_<COMPARER>, IImplRepBase_>,
19 public Memory::UseBlockAllocationIfAppropriate<Rep_<COMPARER>> {
20 private:
21 using inherited = Private::SkipListBasedContainerRepImpl<Rep_<COMPARER>, IImplRepBase_>;
22
23 public:
24 static_assert (not is_reference_v<COMPARER>);
25
26 public:
27 Rep_ (const COMPARER& comparer)
28 : fData_{comparer}
29 {
30 }
31 Rep_ (const Rep_& from) = default;
32
33 public:
34 nonvirtual Rep_& operator= (const Rep_&) = delete;
35
36 // Iterable<T>::_IRep overrides
37 public:
38 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
39 {
41 return Memory::MakeSharedPtr<Rep_> (*this);
42 }
43 virtual Iterator<value_type> MakeIterator () const override
44 {
46 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
47 }
48 virtual size_t size () const override
49 {
51 return fData_.size ();
52 }
53 virtual bool empty () const override
54 {
56 return fData_.empty ();
57 }
58 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
59 {
61 fData_.Apply ([&] (auto arg) { doToElement (arg.fKey); });
62 }
63 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that,
64 [[maybe_unused]] Execution::SequencePolicy seq) const override
65 {
67 if (auto iLink = fData_.Find ([&] (auto arg) { return that (arg.fKey); })) {
68 return Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, iLink)};
69 }
70 return nullptr;
71 }
72
73 // Collection<T>::_IRep overrides
74 public:
75 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty () const override
76 {
78 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
79 }
80 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<T>* i) const override
81 {
84 auto result = Memory::MakeSharedPtr<Rep_> (*this);
85 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
86 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
87 i->Refresh (); // reflect updated rep
88 return result;
89 }
90 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI) override
91 {
93 if (oAddedI == nullptr) [[likely]] {
94 fData_.Add (item);
95 fChangeCounts_.PerformedChange ();
96 }
97 else {
98 typename DataStructureImplType_::ForwardIterator retIt;
99 fData_.Add (item, &retIt);
100 fChangeCounts_.PerformedChange ();
101 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, retIt)};
102 }
103 }
104 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
105 {
107 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
108 fData_.Remove (mir.fIterator);
109 if (nextI == nullptr) {
110 fData_.Add (newValue);
111 fChangeCounts_.PerformedChange ();
112 }
113 else {
114 typename DataStructureImplType_::ForwardIterator retIt;
115 fData_.Add (newValue, &retIt);
116 fChangeCounts_.PerformedChange ();
117 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, retIt)};
118 }
119 }
120 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
121 {
123 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
124 if (nextI == nullptr) [[likely]] {
125 fData_.Remove (mir.fIterator);
126 fChangeCounts_.PerformedChange ();
127 }
128 else {
129 auto ret = fData_.erase (mir.fIterator);
130 fChangeCounts_.PerformedChange ();
131 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
132 }
133 }
134
135 // SortedCollection<T>::_IRep overrides
136 public:
137 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
138 {
140 return fData_.key_comp ();
141 }
142 virtual bool Contains (ArgByValueType<value_type> item) const override
143 {
145 return not fData_.Find (item).Done ();
146 }
147 virtual void Remove (ArgByValueType<value_type> item) override
148 {
150 fData_.Remove (item);
151 fChangeCounts_.PerformedChange ();
152 }
153
154 private:
155 using DataStructureImplType_ = SKIPLIST<COMPARER>;
156 struct IteratorRep_ : Private::IteratorImplHelper_<value_type, DataStructureImplType_> {
157 using inherited = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
158 using inherited::inherited; // forward base class constructors
159 // override to map just the key part to 'T'
160 virtual void More (optional<T>* result, bool advance) override
161 {
162 RequireNotNull (result);
163 this->ValidateChangeCount ();
164 if (advance) [[likely]] {
165 Require (not this->fIterator.Done ());
166 ++this->fIterator;
167 }
168 if (this->fIterator.Done ()) [[unlikely]] {
169 *result = nullopt;
170 }
171 else {
172 *result = this->fIterator->fKey;
173 }
174 }
175 virtual auto Clone () const -> unique_ptr<typename Iterator<T>::IRep> override
176 {
177 this->ValidateChangeCount ();
178 return make_unique<IteratorRep_> (*this);
179 }
180 };
181
182 private:
183 DataStructureImplType_ fData_;
184 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
185
186 private:
187 friend inherited;
188 };
189
190 /*
191 ********************************************************************************
192 *********************** SortedCollection_SkipList<T> ***************************
193 ********************************************************************************
194 */
195 template <typename T>
197 : SortedCollection_SkipList{compare_three_way{}}
198 {
199 AssertRepValidType_ ();
200 }
201 template <typename T>
202 template <IThreeWayComparer<T> COMPARER>
203 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inorderComparer)
204 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (inorderComparer)}
205 {
206 AssertRepValidType_ ();
207 }
208 template <typename T>
209 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (const initializer_list<T>& src)
210 : SortedCollection_SkipList{}
211 {
212 this->AddAll (src);
213 AssertRepValidType_ ();
214 }
215 template <typename T>
216 template <IThreeWayComparer<T> COMPARER>
217 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inOrderComparer, const initializer_list<T>& src)
218 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
219 {
220 this->AddAll (src);
221 AssertRepValidType_ ();
222 }
223#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
224 template <typename T>
225 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
226 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection_SkipList<T>>)
227 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (ITERABLE_OF_ADDABLE&& src)
228 : SortedCollection_SkipList{}
229 {
230 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
231 AssertRepValidType_ ();
232 }
233#endif
234 template <typename T>
235 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
236 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
237 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
238 {
239 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
240 AssertRepValidType_ ();
241 }
242 template <typename T>
243 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
244 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
245 : SortedCollection_SkipList{}
246 {
247 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
248 AssertRepValidType_ ();
249 }
250 template <typename T>
251 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
252 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
253 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
254 {
255 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
256 AssertRepValidType_ ();
257 }
258 template <typename T>
259 inline void SortedCollection_SkipList<T>::AssertRepValidType_ () const
260 {
262 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
263 }
264 }
265
266}
#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
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< value_type > newValue, Iterator< value_type > *nextI=nullptr)
nonvirtual void Add(ArgByValueType< value_type > item)
SortedCollection_SkipList<T> is an SkipList-based concrete implementation of the SortedCollection<T> ...
nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer() const
nonvirtual bool Contains(ArgByValueType< T > item) const
Compares items with the already associated GetInOrderComparer(), and returns true if the item is foun...
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:300
nonvirtual bool empty() const
Returns true iff size() == 0.
Definition Iterable.inl:306
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:294
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...