Stroika Library 3.0d18
 
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 const IteratorRep_& iteratorRep = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
123 if (nextI != nullptr) {
124 savedUnderlyingIndex = iteratorRep.fIterator.GetUnderlyingIteratorRep ();
125 }
126 fData_.SetAt (iteratorRep.fIterator, newValue);
127 fChangeCounts_.PerformedChange ();
128 if (nextI != nullptr) {
129 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
130 }
131 }
132 virtual void Insert (size_t at, const span<const value_type>& copyFrom) override
133 {
134 Require (at == _kSentinelLastItemIndex or at <= size ());
136 if (at == _kSentinelLastItemIndex) {
137 fData_.push_back (copyFrom);
138 }
139 // quickie poor impl
140 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
141 else if (at == 0) {
142 fData_.push_front (copyFrom);
143 }
144 else if (at == fData_.size ()) {
145 fData_.push_back (copyFrom);
146 }
147 else {
148 size_t index = at;
149 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
150 if (--index == 0) {
151 for (auto p = copyFrom.rbegin (); p != copyFrom.rend (); ++p) {
152 fData_.AddBefore (it, *p);
153 }
154 break;
155 }
156 }
157 //Assert (not it.Done ()); // cuz that would mean we never added
158 }
159 fChangeCounts_.PerformedChange ();
160 }
161 virtual void Remove (size_t from, size_t to) override
162 {
163 // quickie poor impl
164 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
165 size_t index = from;
166 size_t amountToRemove = to - from;
167 if (amountToRemove != 0) [[likely]] {
169 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
170 if (index-- == 0) {
171 while (amountToRemove-- != 0) {
172 it = fData_.erase (it);
173 }
174 break;
175 }
176 }
177 fChangeCounts_.PerformedChange ();
178 }
179 }
180
181 private:
182 using DataStructureImplType_ = DataStructures::DoublyLinkedList<value_type>;
183 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
184
185 private:
186 DataStructureImplType_ fData_;
187 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
188 };
189
190 /*
191 ********************************************************************************
192 ************************* Sequence_DoublyLinkedList<T> *************************
193 ********************************************************************************
194 */
195 template <typename T>
197 : inherited{Memory::MakeSharedPtr<Rep_> ()}
198 {
199 AssertRepValidType_ ();
200 }
201 template <typename T>
202 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (const initializer_list<value_type>& src)
204 {
205 this->AppendAll (src);
206 AssertRepValidType_ ();
207 }
208#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
209 template <typename T>
210 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
211 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Sequence_DoublyLinkedList<T>>)
212 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (ITERABLE_OF_ADDABLE&& src)
213 : Sequence_DoublyLinkedList{}
214 {
215 this->AppendAll (forward<ITERABLE_OF_ADDABLE> (src));
216 AssertRepValidType_ ();
217 }
218#endif
219 template <typename T>
220 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
221 inline Sequence_DoublyLinkedList<T>::Sequence_DoublyLinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
222 : Sequence_DoublyLinkedList{}
223 {
224 this->AppendAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
225 AssertRepValidType_ ();
226 }
227 template <typename T>
228 inline void Sequence_DoublyLinkedList<T>::AssertRepValidType_ () const
229 {
231 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
232 }
233 }
234
235}
#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.
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:302
nonvirtual bool empty() const
Returns true iff size() == 0.
Definition Iterable.inl:308
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:296
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...