Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
Sequence_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 template <typename T>
12 class Sequence_LinkedList<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<value_type> 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<T>* 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 {
92 // singly-linked, so cannot produce a native BidirectionalIterator: always use the generic GetAt ()-based implementation
93 return this->_MakeBidirectionalIterator_ViaGetAt ();
94 }
95 virtual RandomAccessIterator<value_type> GetRandomAccessIterator () const override
96 {
97 // singly-linked, so cannot produce a native RandomAccessIterator: always use the generic GetAt ()-based implementation
98 return this->_MakeRandomAccessIterator_ViaGetAt ();
99 }
100 virtual void SetAt (size_t i, ArgByValueType<value_type> item) override
101 {
102 Require (i < size ());
104 fData_.SetAt (item, i);
105 fChangeCounts_.PerformedChange ();
106 }
107 virtual size_t IndexOf (const Iterator<value_type>& i) const override
108 {
109 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
111 return mir.fIterator.CurrentIndex (&fData_);
112 }
113 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
114 {
116 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
117 if (nextI == nullptr) {
118 fData_.Remove (mir.fIterator);
119 fChangeCounts_.PerformedChange ();
120 }
121 else {
122 auto ret = fData_.erase (mir.fIterator);
123 fChangeCounts_.PerformedChange ();
125 }
126 }
128 {
131 if (nextI != nullptr) {
132 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
133 }
134 fData_.SetAt (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
135 fChangeCounts_.PerformedChange ();
136 if (nextI != nullptr) {
138 }
139 }
140 virtual void Insert (size_t at, const span<const value_type>& copyFrom) override
141 {
142 Require (at == _kSentinelLastItemIndex or at <= size ());
144 if (at == _kSentinelLastItemIndex) {
145 at = fData_.size ();
146 }
147 // quickie poor impl
148 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
149 if (at == 0) {
150 fData_.push_front (copyFrom);
151 }
152 else if (at == fData_.size ()) {
153 fData_.push_back (copyFrom);
154 }
155 else {
156 // identical off-by-one to the one fixed in Sequence_DoublyLinkedList::Insert () - see the
157 // comment there; these two branches are copy-paste siblings, so they were both wrong
158 size_t remaining = at;
159 typename DataStructureImplType_::ForwardIterator it{&fData_};
160 for (; not it.AtEnd (); ++it) {
161 if (remaining == 0) {
162 // forward, not reversed - see the comment in Sequence_DoublyLinkedList::Insert ()
163 for (auto p = copyFrom.begin (); p != copyFrom.end (); ++p) {
164 fData_.AddBefore (it, *p);
165 }
166 break;
167 }
168 --remaining;
169 }
170 Assert (not it.AtEnd ()); // cuz that would mean we never added
171 }
172 fChangeCounts_.PerformedChange ();
173 }
174 virtual void Remove (size_t from, size_t to) override
175 {
176 // quickie poor impl
177 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
178 size_t index = from;
179 size_t amountToRemove = (to - from);
180 if (amountToRemove != 0) [[likely]] {
182 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.AtEnd (); ++it) {
183 if (index-- == 0) {
184 while (amountToRemove-- != 0) {
185 it = fData_.erase (it);
186 }
187 break;
188 }
189 }
190 fChangeCounts_.PerformedChange ();
191 }
192 }
193
194 private:
195 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
196 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
197
198 private:
199 DataStructureImplType_ fData_;
200 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
201 };
202
203 /*
204 ********************************************************************************
205 **************************** Sequence_LinkedList<T> ****************************
206 ********************************************************************************
207 */
208 template <typename T>
210 : inherited{Memory::MakeSharedPtr<Rep_> ()}
211 {
212 AssertRepValidType_ ();
213 }
214 template <typename T>
215 inline Sequence_LinkedList<T>::Sequence_LinkedList (const initializer_list<value_type>& src)
217 {
218 this->AppendAll (src);
219 AssertRepValidType_ ();
220 }
221#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
222 template <typename T>
223 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
224 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Sequence_LinkedList<T>>)
225 inline Sequence_LinkedList<T>::Sequence_LinkedList (ITERABLE_OF_ADDABLE&& src)
226 : Sequence_LinkedList{}
227 {
228 this->AppendAll (forward<ITERABLE_OF_ADDABLE> (src));
229 AssertRepValidType_ ();
230 }
231#endif
232 template <typename T>
233 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
234 inline Sequence_LinkedList<T>::Sequence_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
235 : Sequence_LinkedList{}
236 {
237 this->AppendAll (start, end);
238 AssertRepValidType_ ();
239 }
240 template <typename T>
241 inline void Sequence_LinkedList<T>::AssertRepValidType_ () const
242 {
244 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
245 }
246 }
247
248}
#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_LinkedList<T> is a LinkedList-based concrete implementation of the Sequence<T> container pat...
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,...