Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
SortedCollection_LinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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:
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 qStroika_ATTRIBUTE_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 ([[maybe_unused]] bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
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#if qCompilerAndStdLib_MemoryInsertAt_Buggy
92 // see the note on qCompilerAndStdLib_MemoryInsertAt_Buggy - without this, g++ <= 14 drops
93 // the `if (oAddedI != nullptr)` guard below and writes through the null pointer
94 [[gnu::noinline, gnu::optimize ("O0")]]
95#endif
96 virtual void Add (const span<const value_type>& items, Iterator<value_type>* oAddedI) override
97 {
98 Require (not items.empty ());
99 Require (oAddedI == nullptr or items.size () == 1);
101 if (oAddedI == nullptr) [[likely]] {
102 for (const value_type& i : items) {
103 (void)Add_ (i); // sorted insert; no bulk form on a sorted linked list
104 }
105 fChangeCounts_.PerformedChange ();
106 }
107 else {
108 auto addedI = Add_ (items[0]);
109 fChangeCounts_.PerformedChange ();
111 }
112 }
114 {
117 if (nextI != nullptr) {
118 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
119 }
120 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
121 // equals might examine a subset of the object and we still want to update the whole object, but
122 // if its not already equal, the sort order could have changed so we must simulate with a remove/add
123 if (Common::EqualsComparerAdapter<value_type, INORDER_COMPARER>{fInorderComparer_}(*mir.fIterator, newValue)) {
124 fData_.SetAt (mir.fIterator, newValue);
125 }
126 else {
127 fData_.Remove (mir.fIterator);
128 Add_ (newValue);
129 }
130 fChangeCounts_.PerformedChange ();
131 if (nextI != nullptr) {
133 }
134 }
135 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
136 {
138 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
139 if (nextI == nullptr) {
140 fData_.Remove (mir.fIterator);
141 fChangeCounts_.PerformedChange ();
142 }
143 else {
144 auto ret = fData_.erase (mir.fIterator);
145 fChangeCounts_.PerformedChange ();
147 }
148 }
149
150 // SortedCollection<T>::_IRep overrides
151 public:
152 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
153 {
155 return Common::ThreeWayComparerAdapter<T, INORDER_COMPARER>{fInorderComparer_};
156 }
157 virtual bool Contains (ArgByValueType<value_type> item) const override
158 {
160 return fData_.Find (item, Common::EqualsComparerAdapter<value_type, INORDER_COMPARER>{fInorderComparer_}) != nullptr;
161 }
162 virtual void Remove (ArgByValueType<value_type> item) override
163 {
165 fData_.Remove (item, Common::EqualsComparerAdapter<value_type, INORDER_COMPARER>{fInorderComparer_});
166 fChangeCounts_.PerformedChange ();
167 }
168
169 private:
170 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
171 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
172
173 private:
174 nonvirtual auto Add_ (ArgByValueType<value_type> item) -> typename DataStructureImplType_::ForwardIterator
175 {
176 using ForwardIterator = typename DataStructureImplType_::ForwardIterator;
177 ForwardIterator it{&fData_};
178 // skip the smaller items
179 for (; not it.AtEnd () and fInorderComparer_ (*it, item); ++it)
180 ;
181 // at this point - we are pointing at the first link >= item, so insert before it
182 ForwardIterator addedAt;
183 fData_.AddBefore (it, item, &addedAt);
184 return addedAt;
185 }
186
187 private:
188 DataStructureImplType_ fData_;
189 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS Private::ContainerDebugChangeCounts_ fChangeCounts_;
190 };
191
192 /*
193 ********************************************************************************
194 *********************** SortedCollection_LinkedList<T> *************************
195 ********************************************************************************
196 */
197 template <typename T>
199 : SortedCollection_LinkedList{less<T>{}}
200 {
201 AssertRepValidType_ ();
202 }
203 template <typename T>
204 template <Common::IInOrderComparer<T> INORDER_COMPARER>
205 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inorderComparer)
206 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<INORDER_COMPARER>>> (inorderComparer)}
207 {
208 AssertRepValidType_ ();
209 }
210 template <typename T>
211 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (const initializer_list<T>& src)
212 : SortedCollection_LinkedList{}
213 {
214 this->AddAll (src);
215 AssertRepValidType_ ();
216 }
217 template <typename T>
218 template <Common::IInOrderComparer<T> INORDER_COMPARER>
219 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inOrderComparer, const initializer_list<T>& src)
220 : SortedCollection_LinkedList{forward<INORDER_COMPARER> (inOrderComparer)}
221 {
222 this->AddAll (src);
223 AssertRepValidType_ ();
224 }
225#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
226 template <typename T>
227 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
228 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection_LinkedList<T>>)
229 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (ITERABLE_OF_ADDABLE&& src)
230 : SortedCollection_LinkedList{}
231 {
232 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
233 AssertRepValidType_ ();
234 }
235#endif
236 template <typename T>
237 template <Common::IInOrderComparer<T> INORDER_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
238 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
239 : SortedCollection_LinkedList{forward<INORDER_COMPARER> (inOrderComparer)}
240 {
242 AssertRepValidType_ ();
243 }
244 template <typename T>
245 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
246 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
247 : SortedCollection_LinkedList{}
248 {
250 AssertRepValidType_ ();
251 }
252 template <typename T>
253 template <Common::IInOrderComparer<T> INORDER_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
254 inline SortedCollection_LinkedList<T>::SortedCollection_LinkedList (INORDER_COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start,
255 ITERATOR_OF_ADDABLE&& end)
256 : SortedCollection_LinkedList{forward<INORDER_COMPARER> (inOrderComparer)}
257 {
259 AssertRepValidType_ ();
260 }
261 template <typename T>
262 inline void SortedCollection_LinkedList<T>::AssertRepValidType_ () const
263 {
265 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
266 }
267 }
268
269}
#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
qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS - used for the attribute [[no_unique_address]]
Definition StdCompat.h:433
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...
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,...