Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SortedCollection_LinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/DataStructures/LinkedList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
8
10
11 /*
12 ********************************************************************************
13 *********************** SortedCollection_LinkedList<T>::Rep_ *******************
14 ********************************************************************************
15 */
16 template <typename T>
17 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (Common::IInOrderComparer<T>) INORDER_COMPARER>
18 class SortedCollection_LinkedList<T>::Rep_ : public IImplRepBase_, public Memory::UseBlockAllocationIfAppropriate<Rep_<INORDER_COMPARER>> {
19 private:
20 using inherited = IImplRepBase_;
21
22 public:
23 static_assert (not is_reference_v<INORDER_COMPARER>);
24
25 public:
26 Rep_ (const INORDER_COMPARER& inorderComparer)
27 : fInorderComparer_{inorderComparer}
28 {
29 }
30 Rep_ (const Rep_& from) = default;
31
32 public:
33 nonvirtual Rep_& operator= (const Rep_&) = delete;
34
35 private:
36 [[no_unique_address]] const INORDER_COMPARER fInorderComparer_;
37
38 // Iterable<T>::_IRep overrides
39 public:
40 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
41 {
43 return Memory::MakeSharedPtr<Rep_> (*this);
44 }
45 virtual Iterator<value_type> MakeIterator () const override
46 {
48 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
49 }
50 virtual size_t size () const override
51 {
53 return fData_.size ();
54 }
55 virtual bool empty () const override
56 {
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 (doToElement);
64 }
65 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that,
66 [[maybe_unused]] Execution::SequencePolicy seq) const override
67 {
69 if (auto iLink = fData_.Find (that)) {
70 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
71 }
72 return nullptr;
73 }
74
75 // Collection<T>::_IRep overrides
76 public:
77 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty () const override
78 {
79 return Memory::MakeSharedPtr<Rep_> (fInorderComparer_); // keep comparer, but lose data in clone
80 }
81 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<T>* i) const override
82 {
85 auto result = Memory::MakeSharedPtr<Rep_> (*this);
86 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
87 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
88 i->Refresh (); // reflect updated rep
89 return result;
90 }
91 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI) override
92 {
94 auto addedI = Add_ (item);
95 fChangeCounts_.PerformedChange ();
96 if (oAddedI != nullptr) [[unlikely]] {
97 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, addedI)};
98 }
99 }
100 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
101 {
103 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
104 if (nextI != nullptr) {
105 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
106 }
107 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
108 // equals might examine a subset of the object and we still want to update the whole object, but
109 // if its not already equal, the sort order could have changed so we must simulate with a remove/add
110 if (Common::EqualsComparerAdapter<value_type, INORDER_COMPARER>{fInorderComparer_}(*mir.fIterator, newValue)) {
111 fData_.SetAt (mir.fIterator, newValue);
112 }
113 else {
114 fData_.Remove (mir.fIterator);
115 Add_ (newValue);
116 }
117 fChangeCounts_.PerformedChange ();
118 if (nextI != nullptr) {
119 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
120 }
121 }
122 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
123 {
125 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
126 if (nextI == nullptr) {
127 fData_.Remove (mir.fIterator);
128 fChangeCounts_.PerformedChange ();
129 }
130 else {
131 auto ret = fData_.erase (mir.fIterator);
132 fChangeCounts_.PerformedChange ();
133 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
134 }
135 }
136
137 // SortedCollection<T>::_IRep overrides
138 public:
139 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
140 {
142 return Common::ThreeWayComparerAdapter<T, INORDER_COMPARER>{fInorderComparer_};
143 }
144 virtual bool Contains (ArgByValueType<value_type> item) const override
145 {
147 return fData_.Find (item, Common::EqualsComparerAdapter<value_type, INORDER_COMPARER>{fInorderComparer_}) != nullptr;
148 }
149 virtual void Remove (ArgByValueType<value_type> item) override
150 {
152 fData_.Remove (item, Common::EqualsComparerAdapter<value_type, INORDER_COMPARER>{fInorderComparer_});
153 fChangeCounts_.PerformedChange ();
154 }
155
156 private:
157 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
158 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
159
160 private:
161 nonvirtual auto Add_ (ArgByValueType<value_type> item) -> typename DataStructureImplType_::ForwardIterator
162 {
163 using ForwardIterator = typename DataStructureImplType_::ForwardIterator;
164 ForwardIterator it{&fData_};
165 // skip the smaller items
166 for (; not it.Done () and fInorderComparer_ (*it, item); ++it)
167 ;
168 // at this point - we are pointing at the first link >= item, so insert before it
169 ForwardIterator addedAt;
170 fData_.AddBefore (it, item, &addedAt);
171 return addedAt;
172 }
173
174 private:
175 DataStructureImplType_ fData_;
176 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
177 };
178
179 /*
180 ********************************************************************************
181 *********************** SortedCollection_LinkedList<T> *************************
182 ********************************************************************************
183 */
184 template <typename T>
186 : SortedCollection_LinkedList{less<T>{}}
187 {
188 AssertRepValidType_ ();
189 }
190 template <typename T>
191 template <Common::IInOrderComparer<T> INORDER_COMPARER>
192 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inorderComparer)
193 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<INORDER_COMPARER>>> (inorderComparer)}
194 {
195 AssertRepValidType_ ();
196 }
197 template <typename T>
198 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (const initializer_list<T>& src)
199 : SortedCollection_LinkedList{}
200 {
201 this->AddAll (src);
202 AssertRepValidType_ ();
203 }
204 template <typename T>
205 template <Common::IInOrderComparer<T> INORDER_COMPARER>
206 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inOrderComparer, const initializer_list<T>& src)
207 : SortedCollection_LinkedList{forward<INORDER_COMPARER> (inOrderComparer)}
208 {
209 this->AddAll (src);
210 AssertRepValidType_ ();
211 }
212#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
213 template <typename T>
214 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
215 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection_LinkedList<T>>)
216 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (ITERABLE_OF_ADDABLE&& src)
217 : SortedCollection_LinkedList{}
218 {
219 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
220 AssertRepValidType_ ();
221 }
222#endif
223 template <typename T>
224 template <Common::IInOrderComparer<T> INORDER_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
225 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
226 : SortedCollection_LinkedList{forward<INORDER_COMPARER> (inOrderComparer)}
227 {
228 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
229 AssertRepValidType_ ();
230 }
231 template <typename T>
232 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
233 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
234 : SortedCollection_LinkedList{}
235 {
236 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
237 AssertRepValidType_ ();
238 }
239 template <typename T>
240 template <Common::IInOrderComparer<T> INORDER_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
241 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start,
242 ITERATOR_OF_ADDABLE&& end)
243 : SortedCollection_LinkedList{forward<INORDER_COMPARER> (inOrderComparer)}
244 {
245 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
246 AssertRepValidType_ ();
247 }
248 template <typename T>
249 inline void SortedCollection_LinkedList<T>::AssertRepValidType_ () const
250 {
252 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
253 }
254 }
255
256}
#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_LinkedList<T> is an LinkedList-based concrete implementation of the SortedCollection...
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,...