Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
SortedAssociation_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 ****** SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::Rep_ ***********
14 ********************************************************************************
15 */
16 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
17 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (IThreeWayComparer<KEY_TYPE>) KEY_COMPARER>
18 class SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::Rep_
19 : public Private::SkipListBasedContainerRepImpl<Rep_<KEY_COMPARER>, IImplRepBase_>,
20 public Memory::UseBlockAllocationIfAppropriate<Rep_<KEY_COMPARER>> {
21 private:
22 using inherited = Private::SkipListBasedContainerRepImpl<Rep_<KEY_COMPARER>, IImplRepBase_>;
23
24 public:
25 static_assert (not is_reference_v<KEY_COMPARER>);
26
27 public:
28 Rep_ (const KEY_COMPARER& comparer)
29 : fData_{comparer}
30 {
31 }
33 : fData_{move (src)}
34 {
35 }
36 Rep_ (const Rep_& from) = default;
37
38 public:
39 nonvirtual Rep_& operator= (const Rep_&) = delete;
40
41 // Iterable<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>::_IRep overrides
42 public:
44 {
46 return Memory::MakeSharedPtr<Rep_> (*this);
47 }
48 virtual Iterator<value_type> MakeIterator () const override
49 {
51 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
52 }
53 virtual size_t size () const override
54 {
56 fData_.Invariant ();
57 return fData_.size ();
58 }
59 virtual bool empty () const override
60 {
62 fData_.Invariant ();
63 return fData_.empty ();
64 }
65 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
66 {
68 fData_.Apply (doToElement);
69 }
70 virtual Iterator<value_type> Find (bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
71 Execution::SequencePolicy seq) const override
72 {
74 return this->inherited::Find (findFirst, that, seq); // @todo rewrite to use fData
75 }
76
77 // Association<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep overrides
78 public:
79 virtual KeyEqualsCompareFunctionType GetKeyEqualsComparer () const override
80 {
82 return Common::EqualsComparerAdapter<KEY_TYPE, KEY_COMPARER>{fData_.key_comp ()};
83 }
85 {
87 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
88 }
90 {
93 auto result = Memory::MakeSharedPtr<Rep_> (*this);
94 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
95 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
96 i->Refresh (); // reflect updated rep
97 return result;
98 }
100 {
101 // @todo consider doing custom class here, or using CreateGenerator
103 // NOTE: must use FindFirst (), not Find () - Find () only guarantees returning AN element with an
104 // equivalent key (it can shortcut via higher-level links into the middle of a run of duplicate
105 // keys), so a run of duplicate keys could have some skipped/missed by the forward walk below.
106 auto i = fData_.FindFirst (key);
107 vector<mapped_type> result;
108 if (i != fData_.end ()) {
109 Assert (GetKeyEqualsComparer () (key, i->fKey));
110 result.push_back (i->fValue);
111 // the items in a multimap are all in order so we know the current i->key is not less
112 Assert (fData_.key_comp () (key, i->fKey) == strong_ordering::equal);
113 for (++i; i != fData_.end () and fData_.key_comp () (key, i->fKey) == strong_ordering::equal; ++i) {
114 result.push_back (i->fValue);
115 }
116 }
117 return Iterable<mapped_type>{move (result)};
118 }
120 {
122 fData_.Invariant ();
123 (void)fData_.Add (key, newElt);
124 fChangeCounts_.PerformedChange ();
125 fData_.Invariant ();
126 }
127 virtual bool RemoveIf (ArgByValueType<KEY_TYPE> key) override
128 {
130 fData_.Invariant ();
131 auto i = fData_.Find (key);
132 if (i != fData_.end ()) {
133 fData_.Remove (i);
134 fChangeCounts_.PerformedChange ();
135 return true;
136 }
137 return false;
138 }
139 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
140 {
142 auto newI = fData_.erase (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator);
143 fChangeCounts_.PerformedChange ();
144 if (nextI != nullptr) {
146 }
147 }
149 {
152 if (nextI != nullptr) {
153 *nextI = i;
154 }
155 fData_.Update (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
156 fChangeCounts_.PerformedChange ();
157 if (nextI != nullptr) {
158 savedNextI->Refresh ();
159 }
160 }
161
162 // SortedAssociation<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep overrides
163 public:
164 virtual KeyThreeWayComparerType GetKeyThreeWayComparer () const override
165 {
167 return fData_.key_comp ();
168 }
169
170 private:
171 using DataStructureImplType_ = SKIPLIST<KEY_COMPARER>;
172 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
173
174 private:
175 DataStructureImplType_ fData_;
176 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
177
178 private:
179 friend inherited;
180 };
181
182 /*
183 ********************************************************************************
184 ********* SortedAssociation_SkipList<KEY_TYPE,MAPPED_VALUE_TYPE> ***************
185 ********************************************************************************
186 */
187 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
189 : SortedAssociation_SkipList{compare_three_way{}}
190 {
191 AssertRepValidType_ ();
192 }
193 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
194 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER>
196 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<KEY_COMPARER>>> (forward<KEY_COMPARER> (comparer))}
197 {
198 AssertRepValidType_ ();
199 }
200 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
201 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER>
203 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<KEY_COMPARER>>> (move (src))}
204 {
205 AssertRepValidType_ ();
206 }
207 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
208 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER>
210 KEY_COMPARER&& comparer, const initializer_list<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>& src)
211 : SortedAssociation_SkipList{forward<KEY_COMPARER> (comparer)}
212 {
213 this->AddAll (src);
214 AssertRepValidType_ ();
215 }
216#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
217 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
218 template <IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
219 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>>)
221 : SortedAssociation_SkipList{}
222 {
223 AssertRepValidType_ ();
224 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
225 AssertRepValidType_ ();
226 }
227#endif
228 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
229 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER, IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
230 inline SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::SortedAssociation_SkipList (KEY_COMPARER&& comparer, ITERABLE_OF_ADDABLE&& src)
231 : SortedAssociation_SkipList{forward<KEY_COMPARER> (comparer)}
232 {
233 AssertRepValidType_ ();
235 AssertRepValidType_ ();
236 }
237 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
238 template <IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
239 SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::SortedAssociation_SkipList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
240 : SortedAssociation_SkipList{}
241 {
243 AssertRepValidType_ ();
244 }
245 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
246 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER, IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
247 SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::SortedAssociation_SkipList (KEY_COMPARER&& comparer, ITERATOR_OF_ADDABLE&& start,
248 ITERATOR_OF_ADDABLE&& end)
249 : SortedAssociation_SkipList{forward<KEY_COMPARER> (comparer)}
250 {
252 AssertRepValidType_ ();
253 }
254 template <typename KEY_TYPE, typename MAPPED_VALUE_TYPE>
255 inline void SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::AssertRepValidType_ () const
256 {
258 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
259 }
260 }
261
262}
#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 CONTAINER_OF_Key_T As() const
nonvirtual bool RemoveIf(ArgByValueType< key_type > key)
Remove the given item, if it exists. Return true if found and removed.
nonvirtual void Remove(ArgByValueType< key_type > key)
Remove the given item (which must exist).
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< mapped_type > newValue, Iterator< value_type > *nextI=nullptr)
nonvirtual void Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt)
nonvirtual void AddAll(ITERABLE_OF_ADDABLE &&items)
nonvirtual Traversal::Iterable< mapped_type > Lookup(ArgByValueType< key_type > key) const
Return an Iterable<mapped_type> of all the associated items (can be empty if none)....
SortedAssociation_SkipList<KEY_TYPE,MAPPED_VALUE_TYPE> is an SkipList-based concrete implementation o...
nonvirtual KeyThreeWayComparerType GetKeyThreeWayComparer() 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 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,...