Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
Sequence_DoublyLinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/DataStructures/DoublyLinkedList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
8
10
11 template <typename T>
12 class Sequence_DoublyLinkedList<T>::Rep_ : public Sequence<T>::_IRep, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
13 private:
14 using inherited = typename Sequence<T>::_IRep;
15
16 protected:
17 static constexpr size_t _kSentinelLastItemIndex = inherited::_kSentinelLastItemIndex;
18
19 public:
20 Rep_ () = default;
21 Rep_ (const Rep_& from) = default;
22
23 public:
24 nonvirtual Rep_& operator= (const Rep_&) = delete;
25
26 // Iterable<T>::_IRep overrides
27 public:
28 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
29 {
31 return Memory::MakeSharedPtr<Rep_> (*this);
32 }
33 virtual Iterator<T> MakeIterator () const override
34 {
36 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
37 }
38 virtual size_t size () const override
39 {
41 return fData_.size ();
42 }
43 virtual bool empty () const override
44 {
46 return fData_.empty ();
47 }
48 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
49 {
51 fData_.Apply (doToElement);
52 }
53 virtual Iterator<value_type> Find ([[maybe_unused]] bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
55 {
57 if (auto iLink = fData_.Find (that)) {
58 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
59 }
60 return nullptr;
61 }
62
63 // Sequence<T>::_IRep overrides
64 public:
65 virtual shared_ptr<typename Sequence<T>::_IRep> CloneEmpty () const override
66 {
68 return Memory::MakeSharedPtr<Rep_> ();
69 }
70 virtual shared_ptr<typename Sequence<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
71 {
74 auto result = Memory::MakeSharedPtr<Rep_> (*this);
75 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
76 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
77 i->Refresh (); // reflect updated rep
78 return result;
79 }
80 virtual value_type GetAt (size_t i) const override
81 {
82 Require (not empty ());
83 Require (i == _kSentinelLastItemIndex or i < size ());
85 if (i == _kSentinelLastItemIndex) {
86 i = size () - 1;
87 }
88 return fData_.GetAt (i);
89 }
90 virtual BidirectionalIterator<value_type> GetBidirectionalIterator () const override
91 {
94 }
95 virtual RandomAccessIterator<value_type> GetRandomAccessIterator () const override
96 {
97 // no efficient native random-access iterator is possible for a doubly-linked list - always use the generic
98 // GetAt ()-based implementation
99 return this->_MakeRandomAccessIterator_ViaGetAt ();
100 }
101 virtual void SetAt (size_t i, ArgByValueType<value_type> item) override
102 {
103 Require (i < size ());
105 fData_.SetAt (i, item);
106 fChangeCounts_.PerformedChange ();
107 }
108 virtual size_t IndexOf (const Iterator<value_type>& i) const override
109 {
110 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
112 return mir.fIterator.CurrentIndex (&fData_);
113 }
114 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
115 {
117 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
118 if (nextI == nullptr) {
119 fData_.Remove (mir.fIterator);
120 fChangeCounts_.PerformedChange ();
121 }
122 else {
123 auto ret = fData_.erase (mir.fIterator);
124 fChangeCounts_.PerformedChange ();
126 }
127 }
129 {
132 const IteratorRep_& iteratorRep = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
133 if (nextI != nullptr) {
134 savedUnderlyingIndex = iteratorRep.fIterator.GetUnderlyingIteratorRep ();
135 }
136 fData_.SetAt (iteratorRep.fIterator, newValue);
137 fChangeCounts_.PerformedChange ();
138 if (nextI != nullptr) {
140 }
141 }
142 virtual void Insert (size_t at, const span<const value_type>& copyFrom) override
143 {
144 Require (at == _kSentinelLastItemIndex or at <= size ());
146 if (at == _kSentinelLastItemIndex) {
147 fData_.push_back (copyFrom);
148 }
149 // quickie poor impl
150 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
151 else if (at == 0) {
152 fData_.push_front (copyFrom);
153 }
154 else if (at == fData_.size ()) {
155 fData_.push_back (copyFrom);
156 }
157 else {
158 /*
159 * Walk to the element currently sitting AT 'at' and insert ahead of it.
160 *
161 * This used to read 'if (--index == 0)', which stops one element EARLY: with at==2 the
162 * counter reaches 0 while the iterator is still on element 1, so Insert (2, ...) into
163 * [1, 2, 6, 7] produced [1, 3, 4, 5, 2, 6, 7]. It applied to a single-element Insert () just
164 * as much as to a span. Nothing caught it because this branch is only reached for
165 * 0 < at < size () - both ends are special-cased above - and no test inserted into the
166 * middle of a linked-list-backed Sequence until Tests/21 grew one.
167 */
168 size_t remaining = at;
169 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.AtEnd (); ++it) {
170 if (remaining == 0) {
171 // FORWARD, not reversed: AddBefore () takes 'it' by const ref and inserts ahead of
172 // the SAME node every time, so each element lands after the previously added one.
173 // Walking the source backwards here yielded the span reversed.
174 for (auto p = copyFrom.begin (); p != copyFrom.end (); ++p) {
175 fData_.AddBefore (it, *p);
176 }
177 break;
178 }
179 --remaining;
180 }
181 //Assert (not it.AtEnd ()); // cuz that would mean we never added
182 }
183 fChangeCounts_.PerformedChange ();
184 }
185 virtual void Remove (size_t from, size_t to) override
186 {
187 // quickie poor impl
188 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
189 size_t index = from;
190 size_t amountToRemove = to - from;
191 if (amountToRemove != 0) [[likely]] {
193 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.AtEnd (); ++it) {
194 if (index-- == 0) {
195 while (amountToRemove-- != 0) {
196 it = fData_.erase (it);
197 }
198 break;
199 }
200 }
201 fChangeCounts_.PerformedChange ();
202 }
203 }
204
205 private:
206 using DataStructureImplType_ = DataStructures::DoublyLinkedList<value_type>;
207 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
208
209 private:
210 // same as IteratorImplHelper_DefaultTraits<value_type, DataStructureImplType_>, but pointing at
211 // DoublyLinkedList<T>::BidirectionalIterator instead of its (forward-only) ForwardIterator
212 struct BidirectionalIteratorTraits_ : Private::IteratorImplHelper_DefaultTraits<value_type, DataStructureImplType_> {
213 using DataStructureIteratorT = typename DataStructureImplType_::BidirectionalIterator;
214 };
215 using BidirectionalIteratorRep_ = Private::BidirectionalIteratorImplHelper_<value_type, DataStructureImplType_, BidirectionalIteratorTraits_>;
216
217 private:
218 DataStructureImplType_ fData_;
219 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
220 };
221
222 /*
223 ********************************************************************************
224 ************************* Sequence_DoublyLinkedList<T> *************************
225 ********************************************************************************
226 */
227 template <typename T>
229 : inherited{Memory::MakeSharedPtr<Rep_> ()}
230 {
231 AssertRepValidType_ ();
232 }
233 template <typename T>
234 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (const initializer_list<value_type>& src)
236 {
237 this->AppendAll (src);
238 AssertRepValidType_ ();
239 }
240#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
241 template <typename T>
242 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
243 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Sequence_DoublyLinkedList<T>>)
244 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (ITERABLE_OF_ADDABLE&& src)
245 : Sequence_DoublyLinkedList{}
246 {
247 this->AppendAll (forward<ITERABLE_OF_ADDABLE> (src));
248 AssertRepValidType_ ();
249 }
250#endif
251 template <typename T>
252 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
253 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
254 : Sequence_DoublyLinkedList{}
255 {
257 AssertRepValidType_ ();
258 }
259 template <typename T>
260 inline void Sequence_DoublyLinkedList<T>::AssertRepValidType_ () const
261 {
263 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
264 }
265 }
266
267}
#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
Sequence_DoublyLinkedList<T> is an Array-based concrete implementation of the Sequence<T> container p...
A generalization of a vector: a container whose elements are keyed by the natural numbers.
nonvirtual void As(CONTAINER_OF_ADDABLE *into) const
write this Sequence into an existing container
nonvirtual void Insert(size_t i, ArgByValueType< value_type > item)
Definition Sequence.inl:464
nonvirtual optional< size_t > IndexOf(ArgByValueType< value_type > i, EQUALS_COMPARER &&equalsComparer={}) const
nonvirtual void SetAt(size_t i, ArgByValueType< value_type > item)
Definition Sequence.inl:396
nonvirtual value_type GetAt(size_t i) const
Definition Sequence.inl:389
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< value_type > newValue, Iterator< value_type > *nextI=nullptr)
Definition Sequence.inl:716
nonvirtual void AppendAll(ITERABLE_OF_ADDABLE &&s)
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,...