Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
SortedCollection_SkipList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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 ([[maybe_unused]] bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
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#if qCompilerAndStdLib_MemoryInsertAt_Buggy
91 // see the note on qCompilerAndStdLib_MemoryInsertAt_Buggy - without this, g++ <= 14 drops
92 // the `if (oAddedI != nullptr)` guard below and writes through the null pointer
93 [[gnu::noinline, gnu::optimize ("O0")]]
94#endif
95 virtual void Add (const span<const value_type>& items, Iterator<value_type>* oAddedI) override
96 {
97 Require (not items.empty ());
98 Require (oAddedI == nullptr or items.size () == 1);
100 if (oAddedI == nullptr) [[likely]] {
101 for (const value_type& i : items) {
102 fData_.Add (i); // skiplist insert; no bulk form
103 }
104 fChangeCounts_.PerformedChange ();
105 }
106 else {
107 typename DataStructureImplType_::ForwardIterator retIt;
108 fData_.Add (items[0], &retIt);
109 fChangeCounts_.PerformedChange ();
111 }
112 }
114 {
116 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
117 fData_.Remove (mir.fIterator);
118 if (nextI == nullptr) {
119 fData_.Add (newValue);
120 fChangeCounts_.PerformedChange ();
121 }
122 else {
123 typename DataStructureImplType_::ForwardIterator retIt;
124 fData_.Add (newValue, &retIt);
125 fChangeCounts_.PerformedChange ();
127 }
128 }
129 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
130 {
132 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
133 if (nextI == nullptr) [[likely]] {
134 fData_.Remove (mir.fIterator);
135 fChangeCounts_.PerformedChange ();
136 }
137 else {
138 auto ret = fData_.erase (mir.fIterator);
139 fChangeCounts_.PerformedChange ();
141 }
142 }
143
144 // SortedCollection<T>::_IRep overrides
145 public:
146 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
147 {
149 return fData_.key_comp ();
150 }
151 virtual bool Contains (ArgByValueType<value_type> item) const override
152 {
154 return not fData_.Find (item).AtEnd ();
155 }
156 virtual void Remove (ArgByValueType<value_type> item) override
157 {
159 fData_.Remove (item);
160 fChangeCounts_.PerformedChange ();
161 }
162
163 private:
164 using DataStructureImplType_ = SKIPLIST<COMPARER>;
165 struct IterTraits_ : Private::IteratorImplHelper_DefaultTraits<value_type, DataStructureImplType_> {
166 static constexpr value_type ConvertDataStructureIterationResult2ContainerIteratorResult (const typename DataStructureImplType_::value_type& t)
167 {
168 return t.fKey;
169 }
170 };
171 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_, IterTraits_>;
172
173 private:
174 DataStructureImplType_ fData_;
175 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
176
177 private:
178 friend inherited;
179 };
180
181 /*
182 ********************************************************************************
183 *********************** SortedCollection_SkipList<T> ***************************
184 ********************************************************************************
185 */
186 template <typename T>
188 : SortedCollection_SkipList{compare_three_way{}}
189 {
190 AssertRepValidType_ ();
191 }
192 template <typename T>
193 template <IThreeWayComparer<T> COMPARER>
194 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inorderComparer)
195 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (inorderComparer)}
196 {
197 AssertRepValidType_ ();
198 }
199 template <typename T>
200 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (const initializer_list<T>& src)
201 : SortedCollection_SkipList{}
202 {
203 this->AddAll (src);
204 AssertRepValidType_ ();
205 }
206 template <typename T>
207 template <IThreeWayComparer<T> COMPARER>
208 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inOrderComparer, const initializer_list<T>& src)
209 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
210 {
211 this->AddAll (src);
212 AssertRepValidType_ ();
213 }
214#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
215 template <typename T>
216 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
217 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection_SkipList<T>>)
218 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (ITERABLE_OF_ADDABLE&& src)
219 : SortedCollection_SkipList{}
220 {
221 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
222 AssertRepValidType_ ();
223 }
224#endif
225 template <typename T>
226 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
227 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
228 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
229 {
231 AssertRepValidType_ ();
232 }
233 template <typename T>
234 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
235 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
236 : SortedCollection_SkipList{}
237 {
239 AssertRepValidType_ ();
240 }
241 template <typename T>
242 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
243 inline SortedCollection_SkipList<T>::SortedCollection_SkipList (COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
244 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
245 {
247 AssertRepValidType_ ();
248 }
249 template <typename T>
250 inline void SortedCollection_SkipList<T>::AssertRepValidType_ () const
251 {
253 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
254 }
255 }
256
257}
#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
#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
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...
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,...