Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Sequence_DoublyLinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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 {
40 // NOTE: O(N), but could easily be made faster caching the length
42 return fData_.size ();
43 }
44 virtual bool empty () const override
45 {
47 return fData_.empty ();
48 }
49 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
50 {
52 fData_.Apply (doToElement);
53 }
54 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that,
55 [[maybe_unused]] Execution::SequencePolicy seq) const override
56 {
58 if (auto iLink = fData_.Find (that)) {
59 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
60 }
61 return nullptr;
62 }
63
64 // Sequence<T>::_IRep overrides
65 public:
66 virtual shared_ptr<typename Sequence<T>::_IRep> CloneEmpty () const override
67 {
69 return Memory::MakeSharedPtr<Rep_> ();
70 }
71 virtual shared_ptr<typename Sequence<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
72 {
75 auto result = Memory::MakeSharedPtr<Rep_> (*this);
76 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
77 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
78 i->Refresh (); // reflect updated rep
79 return result;
80 }
81 virtual value_type GetAt (size_t i) const override
82 {
83 Require (not empty ());
84 Require (i == _kSentinelLastItemIndex or i < size ());
86 if (i == _kSentinelLastItemIndex) {
87 i = size () - 1;
88 }
89 return fData_.GetAt (i);
90 }
91 virtual void SetAt (size_t i, ArgByValueType<value_type> item) override
92 {
93 Require (i < size ());
95 fData_.SetAt (i, item);
96 fChangeCounts_.PerformedChange ();
97 }
98 virtual size_t IndexOf (const Iterator<value_type>& i) const override
99 {
100 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
102 return mir.fIterator.CurrentIndex (&fData_);
103 }
104 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
105 {
107 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
108 if (nextI == nullptr) {
109 fData_.Remove (mir.fIterator);
110 fChangeCounts_.PerformedChange ();
111 }
112 else {
113 auto ret = fData_.erase (mir.fIterator);
114 fChangeCounts_.PerformedChange ();
115 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
116 }
117 }
118 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
119 {
121 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
122 if (nextI != nullptr) {
123 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
124 }
125 fData_.SetAt (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
126 fChangeCounts_.PerformedChange ();
127 if (nextI != nullptr) {
128 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
129 }
130 }
131 virtual void Insert (size_t at, const value_type* from, const value_type* to) override
132 {
133 Require (at == _kSentinelLastItemIndex or at <= size ());
135 if (at == _kSentinelLastItemIndex) {
136 for (const T* p = from; p != to; ++p) {
137 fData_.push_back (*p);
138 }
139 }
140 // quickie poor impl
141 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
142 else if (at == 0) {
143 for (size_t i = (to - from); i > 0; --i) {
144 fData_.push_front (from[i - 1]);
145 }
146 }
147 else if (at == fData_.size ()) {
148 for (const T* p = from; p != to; ++p) {
149 fData_.push_back (*p);
150 }
151 }
152 else {
153 size_t index = at;
154 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
155 if (--index == 0) {
156 for (const T* p = from; p != to; ++p) {
157 fData_.AddBefore (it, *p);
158 //it.AddBefore (*p);
159 }
160 break;
161 }
162 }
163 //Assert (not it.Done ()); // cuz that would mean we never added
164 }
165 fChangeCounts_.PerformedChange ();
166 }
167 virtual void Remove (size_t from, size_t to) override
168 {
170 // quickie poor impl
171 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
172 size_t index = from;
173 size_t amountToRemove = to - from;
174 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
175 if (index-- == 0) {
176 while (amountToRemove-- != 0) {
177 it = fData_.erase (it);
178 }
179 break;
180 }
181 }
182 fChangeCounts_.PerformedChange ();
183 }
184
185 private:
186 using DataStructureImplType_ = DataStructures::DoublyLinkedList<value_type>;
187 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
188
189 private:
190 DataStructureImplType_ fData_;
191 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
192 };
193
194 /*
195 ********************************************************************************
196 ************************* Sequence_DoublyLinkedList<T> *************************
197 ********************************************************************************
198 */
199 template <typename T>
201 : inherited{Memory::MakeSharedPtr<Rep_> ()}
202 {
203 AssertRepValidType_ ();
204 }
205 template <typename T>
206 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (const initializer_list<value_type>& src)
208 {
209 this->AppendAll (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>, Sequence_DoublyLinkedList<T>>)
216 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (ITERABLE_OF_ADDABLE&& src)
217 : Sequence_DoublyLinkedList{}
218 {
219 this->AppendAll (forward<ITERABLE_OF_ADDABLE> (src));
220 AssertRepValidType_ ();
221 }
222#endif
223 template <typename T>
224 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
225 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
226 : Sequence_DoublyLinkedList{}
227 {
228 this->AppendAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
229 AssertRepValidType_ ();
230 }
231 template <typename T>
232 inline void Sequence_DoublyLinkedList<T>::AssertRepValidType_ () const
233 {
235 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
236 }
237 }
238
239}
#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
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.
Definition Sequence.h:187
nonvirtual void Insert(size_t i, ArgByValueType< value_type > item)
Definition Sequence.inl:281
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:244
nonvirtual value_type GetAt(size_t i) const
Definition Sequence.inl:237
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< value_type > newValue, Iterator< value_type > *nextI=nullptr)
Definition Sequence.inl:351
nonvirtual void AppendAll(ITERABLE_OF_ADDABLE &&s)
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,...