Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
SortedSet_SkipList.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_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 (bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
66 Execution::SequencePolicy seq) const override
67 {
69 return this->inherited::Find (findFirst, that, seq); // @todo rewrite to use fData
70 }
71 virtual Iterator<value_type> Find_equal_to (const ArgByValueType<value_type>& v, [[maybe_unused]] Execution::SequencePolicy seq) const override
72 {
73 // if doing a find by 'equals-to' - we already have this indexed
74 auto found = fData_.Find (v);
75#if 0
76 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
77 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
78 .fIterator.GetUnderlyingIteratorRep ()));
79#endif
80 return Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, found)};
81 }
82
83 // Set<T>::_IRep overrides
84 public:
85 virtual ElementEqualityComparerType GetElementEqualsComparer () const override
86 {
88 return Common::EqualsComparerAdapter<T, COMPARER>{fData_.key_comp ()};
89 }
90 virtual shared_ptr<typename Set<T>::_IRep> CloneEmpty () const override
91 {
93 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
94 }
95 virtual shared_ptr<typename Set<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
96 {
99 auto result = Memory::MakeSharedPtr<Rep_> (*this);
100 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
101 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
102 i->Refresh (); // reflect updated rep
103 return result;
104 }
105 virtual bool Equals (const typename Iterable<value_type>::_IRep& rhs) const override
106 {
108 return this->_Equals_Reference_Implementation (rhs);
109 }
111 {
113 auto i = fData_.Find (item);
114 bool notDone = i != fData_.end ();
115 if (oResult != nullptr and notDone) {
116 *oResult = i->fKey;
117 }
118 if (iResult != nullptr and notDone) {
120 }
121 return notDone;
122 }
123 virtual void Add (ArgByValueType<value_type> item) override
124 {
126 fData_.Add (item);
127 fChangeCounts_.PerformedChange ();
128 }
129 virtual bool RemoveIf (ArgByValueType<value_type> item) override
130 {
132 fData_.Invariant ();
133 auto i = fData_.Find (item);
134 if (i != fData_.end ()) [[likely]] {
135 fData_.Remove (i);
136 fChangeCounts_.PerformedChange ();
137 return true;
138 }
139 return false;
140 }
141 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
142 {
143 Require (not i.AtEnd ());
144 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
145 mir.fIterator.AssertDataMatches (&fData_);
146 if (nextI == nullptr) {
147 fData_.Remove (mir.fIterator);
148 fChangeCounts_.PerformedChange ();
149 }
150 else {
151 auto ret = fData_.erase (mir.fIterator);
152 fChangeCounts_.PerformedChange ();
154 }
155 }
156
157 // SortedSet<T>::_IRep overrides
158 public:
159 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
160 {
162 return fData_.key_comp ();
163 }
164
165 private:
166 using DataStructureImplType_ = SKIPLIST<COMPARER>;
167 struct IterTraits_ : Private::IteratorImplHelper_DefaultTraits<value_type, DataStructureImplType_> {
168 static constexpr value_type ConvertDataStructureIterationResult2ContainerIteratorResult (const typename DataStructureImplType_::value_type& t)
169 {
170 return t.fKey;
171 }
172 };
173 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_, IterTraits_>;
174
175 private:
176 DataStructureImplType_ fData_;
177 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
178
179 private:
180 friend inherited;
181 };
182
183 /*
184 ********************************************************************************
185 **************************** SortedSet_SkipList<T> *****************************
186 ********************************************************************************
187 */
188 template <typename T>
190 : SortedSet_SkipList{compare_three_way{}}
191 {
192 AssertRepValidType_ ();
193 }
194 template <typename T>
195 template <IThreeWayComparer<T> COMPARER>
196 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer)
197 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (forward<COMPARER> (comparer))}
198 {
199 AssertRepValidType_ ();
200 }
201 template <typename T>
202 inline SortedSet_SkipList<T>::SortedSet_SkipList (const initializer_list<T>& src)
203 : SortedSet_SkipList{}
204 {
205 this->AddAll (src);
206 AssertRepValidType_ ();
207 }
208 template <typename T>
209 template <IThreeWayComparer<T> COMPARER>
210 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer, const initializer_list<T>& src)
211 : SortedSet_SkipList{forward<COMPARER> (comparer)}
212 {
213 this->AddAll (src);
214 AssertRepValidType_ ();
215 }
216#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
217 template <typename T>
218 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
219 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedSet_SkipList<T>>)
220 inline SortedSet_SkipList<T>::SortedSet_SkipList (ITERABLE_OF_ADDABLE&& src)
221 : SortedSet_SkipList{}
222 {
223 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
224 AssertRepValidType_ ();
225 }
226#endif
227 template <typename T>
228 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
229 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer, ITERABLE_OF_ADDABLE&& src)
230 : SortedSet_SkipList (forward<COMPARER> (comparer))
231 {
233 AssertRepValidType_ ();
234 }
235 template <typename T>
236 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
237 inline SortedSet_SkipList<T>::SortedSet_SkipList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
238 : SortedSet_SkipList{}
239 {
241 AssertRepValidType_ ();
242 }
243 template <typename T>
244 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
245 inline SortedSet_SkipList<T>::SortedSet_SkipList (COMPARER&& comparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
246 : SortedSet_SkipList (forward<COMPARER> (comparer))
247 {
249 AssertRepValidType_ ();
250 }
251 template <typename T>
252 inline void SortedSet_SkipList<T>::AssertRepValidType_ () const
253 {
255 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
256 }
257 }
258
259}
#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_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: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,...