Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Sequence_LinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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 {
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<T>* i) const override
72 {
75 auto result = Memory::MakeSharedPtr<Rep_> (*this);
76 result->fData_.MoveIteratorHereAfterClone (&Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ()).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 void SetAt (size_t i, ArgByValueType<value_type> item) override
91 {
92 Require (i < size ());
94 fData_.SetAt (item, i);
95 fChangeCounts_.PerformedChange ();
96 }
97 virtual size_t IndexOf (const Iterator<value_type>& i) const override
98 {
99 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
101 return mir.fIterator.CurrentIndex (&fData_);
102 }
103 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
104 {
106 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
107 if (nextI == nullptr) {
108 fData_.Remove (mir.fIterator);
109 fChangeCounts_.PerformedChange ();
110 }
111 else {
112 auto ret = fData_.erase (mir.fIterator);
113 fChangeCounts_.PerformedChange ();
114 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
115 }
116 }
117 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
118 {
120 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
121 if (nextI != nullptr) {
122 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
123 }
124 fData_.SetAt (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
125 fChangeCounts_.PerformedChange ();
126 if (nextI != nullptr) {
127 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
128 }
129 }
130 virtual void Insert (size_t at, const T* from, const T* to) override
131 {
132 Require (at == _kSentinelLastItemIndex or at <= size ());
134 if (at == _kSentinelLastItemIndex) {
135 at = fData_.size ();
136 }
137 // quickie poor impl
138 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
139 if (at == 0) {
140 for (size_t i = (to - from); i > 0; --i) {
141 fData_.push_front (from[i - 1]);
142 }
143 }
144 else if (at == fData_.size ()) { // ** very costly - see if below case works for both - I THINK it does
145 for (const T* p = from; p != to; ++p) {
146 fData_.push_back (*p);
147 }
148 }
149 else {
150 size_t index = at;
151 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
152 if (--index == 0) {
153 for (const T* p = from; p != to; ++p) {
154 fData_.AddBefore (it, *p);
155 }
156 break;
157 }
158 }
159 //Assert (not it.Done ()); // cuz that would mean we never added
160 }
161 fChangeCounts_.PerformedChange ();
162 }
163 virtual void Remove (size_t from, size_t to) override
164 {
166 // quickie poor impl
167 // See Stroika v1 - much better - handling cases of remove near start or end of linked list
168 size_t index = from;
169 size_t amountToRemove = (to - from);
170 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
171 if (index-- == 0) {
172 while (amountToRemove-- != 0) {
173 it = fData_.erase (it);
174 }
175 break;
176 }
177 }
178 fChangeCounts_.PerformedChange ();
179 }
180
181 private:
182 using DataStructureImplType_ = DataStructures::LinkedList<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_LinkedList<T> ****************************
193 ********************************************************************************
194 */
195 template <typename T>
197 : inherited{Memory::MakeSharedPtr<Rep_> ()}
198 {
199 AssertRepValidType_ ();
200 }
201 template <typename T>
202 inline Sequence_LinkedList<T>::Sequence_LinkedList (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_LinkedList<T>>)
212 inline Sequence_LinkedList<T>::Sequence_LinkedList (ITERABLE_OF_ADDABLE&& src)
213 : Sequence_LinkedList{}
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_LinkedList<T>::Sequence_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
222 : Sequence_LinkedList{}
223 {
224 this->AppendAll (start, end);
225 AssertRepValidType_ ();
226 }
227 template <typename T>
228 inline void Sequence_LinkedList<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_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.
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,...