Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
SortedSet_SkipList.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_SkipList<T>::Rep_ ************************
14 ********************************************************************************
15 */
16 template <typename T>
17 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (IThreeWayComparer<T>) COMPARER>
18 class SortedSet_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 fData_.Invariant ();
52 return fData_.size ();
53 }
54 virtual bool empty () const override
55 {
57 fData_.Invariant ();
58 return fData_.empty ();
59 }
60 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
61 {
63 fData_.Apply ([&] (const auto& k) { doToElement (k.fKey); });
64 }
65 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that, Execution::SequencePolicy seq) const override
66 {
68 return this->inherited::Find (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#if 0
75 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
76 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
77 .fIterator.GetUnderlyingIteratorRep ()));
78#endif
79 return Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, found)};
80 }
81
82 // Set<T>::_IRep overrides
83 public:
84 virtual ElementEqualityComparerType GetElementEqualsComparer () const override
85 {
87 return Common::EqualsComparerAdapter<T, COMPARER>{fData_.key_comp ()};
88 }
89 virtual shared_ptr<typename Set<T>::_IRep> CloneEmpty () const override
90 {
92 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
93 }
94 virtual shared_ptr<typename Set<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
95 {
98 auto result = Memory::MakeSharedPtr<Rep_> (*this);
99 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
100 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
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 }
109 virtual bool Lookup (ArgByValueType<value_type> item, optional<value_type>* oResult, Iterator<value_type>* iResult) const override
110 {
112 auto i = fData_.Find (item);
113 bool notDone = i != fData_.end ();
114 if (oResult != nullptr and notDone) {
115 *oResult = i->fKey;
116 }
117 if (iResult != nullptr and notDone) {
118 *iResult = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, i)};
119 }
120 return notDone;
121 }
122 virtual void Add (ArgByValueType<value_type> item) override
123 {
125 fData_.Add (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_.Remove (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.Done ());
143 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
144 mir.fIterator.AssertDataMatches (&fData_);
145 if (nextI == nullptr) {
146 fData_.Remove (mir.fIterator);
147 fChangeCounts_.PerformedChange ();
148 }
149 else {
150 auto ret = fData_.erase (mir.fIterator);
151 fChangeCounts_.PerformedChange ();
152 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
153 }
154 }
155
156 // SortedSet<T>::_IRep overrides
157 public:
158 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
159 {
161 return fData_.key_comp ();
162 }
163
164 private:
165 using DataStructureImplType_ = SKIPLIST<COMPARER>;
166 struct IterTraits_ : Private::IteratorImplHelper_DefaultTraits<value_type, DataStructureImplType_> {
167 static constexpr value_type ConvertDataStructureIterationResult2ContainerIteratorResult (const typename DataStructureImplType_::value_type& t)
168 {
169 return t.fKey;
170 }
171 };
172 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_, IterTraits_>;
173
174 private:
175 DataStructureImplType_ fData_;
176 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
177
178 private:
179 friend inherited;
180 };
181
182 /*
183 ********************************************************************************
184 **************************** SortedSet_SkipList<T> *****************************
185 ********************************************************************************
186 */
187 template <typename T>
189 : SortedSet_SkipList{compare_three_way{}}
190 {
191 AssertRepValidType_ ();
192 }
193 template <typename T>
194 template <IThreeWayComparer<T> COMPARER>
195 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer)
196 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (forward<COMPARER> (comparer))}
197 {
198 AssertRepValidType_ ();
199 }
200 template <typename T>
201 inline SortedSet_SkipList<T>::SortedSet_SkipList (const initializer_list<T>& src)
202 : SortedSet_SkipList{}
203 {
204 this->AddAll (src);
205 AssertRepValidType_ ();
206 }
207 template <typename T>
208 template <IThreeWayComparer<T> COMPARER>
209 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer, const initializer_list<T>& src)
210 : SortedSet_SkipList{forward<COMPARER> (comparer)}
211 {
212 this->AddAll (src);
213 AssertRepValidType_ ();
214 }
215#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
216 template <typename T>
217 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
218 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedSet_SkipList<T>>)
219 inline SortedSet_SkipList<T>::SortedSet_SkipList (ITERABLE_OF_ADDABLE&& src)
220 : SortedSet_SkipList{}
221 {
222 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
223 AssertRepValidType_ ();
224 }
225#endif
226 template <typename T>
227 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
228 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer, ITERABLE_OF_ADDABLE&& src)
229 : SortedSet_SkipList (forward<COMPARER> (comparer))
230 {
231 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
232 AssertRepValidType_ ();
233 }
234 template <typename T>
235 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
236 inline SortedSet_SkipList<T>::SortedSet_SkipList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
237 : SortedSet_SkipList{}
238 {
239 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
240 AssertRepValidType_ ();
241 }
242 template <typename T>
243 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
244 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
245 : SortedSet_SkipList (forward<COMPARER> (comparer))
246 {
247 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
248 AssertRepValidType_ ();
249 }
250 template <typename T>
251 inline void SortedSet_SkipList<T>::AssertRepValidType_ () const
252 {
254 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
255 }
256 }
257
258}
#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_SkipList<T> is an std::set-based concrete implementation of the SortedSet<T> container patt...
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,...