Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
Collection_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 /*
12 */
13 template <typename T>
14 class Collection_LinkedList<T>::Rep_ : public IImplRep_, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
15 private:
16 using inherited = IImplRep_;
17
18 public:
19 Rep_ () = default;
20 Rep_ (const Rep_& from) = default;
21
22 public:
23 nonvirtual Rep_& operator= (const Rep_&) = delete;
24
25 // Iterable<T>::_IRep overrides
26 public:
27 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
28 {
30 return Memory::MakeSharedPtr<Rep_> (*this);
31 }
32 virtual Iterator<value_type> MakeIterator () const override
33 {
35 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
36 }
37 virtual size_t size () const override
38 {
40 return fData_.size ();
41 }
42 virtual bool empty () const override
43 {
45 return fData_.empty ();
46 }
47 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
48 {
50 fData_.Apply (doToElement);
51 }
52 virtual Iterator<value_type> Find ([[maybe_unused]] bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
54 {
56 if (auto iLink = fData_.Find (that)) {
57 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
58 }
59 return nullptr;
60 }
61
62 // Collection<T>::_IRep overrides
63 public:
64 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty () const override
65 {
66 return Memory::MakeSharedPtr<Rep_> ();
67 }
68 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
69 {
72 shared_ptr<Rep_> result = Memory::MakeSharedPtr<Rep_> (*this);
73 const IteratorRep_& iteratorRep = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
74 result->fData_.MoveIteratorHereAfterClone (&iteratorRep.fIterator, &fData_);
75 i->Refresh (); // reflect updated rep
76 return result;
77 }
78#if qCompilerAndStdLib_MemoryInsertAt_Buggy
79 // see the note on qCompilerAndStdLib_MemoryInsertAt_Buggy - without this, g++ <= 14 drops
80 // the `if (oAddedI != nullptr)` guard below and writes through the null pointer
81 [[gnu::noinline, gnu::optimize ("O0")]]
82#endif
83 virtual void Add (const span<const value_type>& items, Iterator<value_type>* oAddedI) override
84 {
85 Require (not items.empty ());
86 Require (oAddedI == nullptr or items.size () == 1);
88 for (const value_type& i : items) {
89 fData_.push_front (i); // order meaningless for collection, and prepend cheaper on linked list
90 }
91 fChangeCounts_.PerformedChange ();
92 if (oAddedI != nullptr) [[unlikely]] {
93 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
94 }
95 }
97 {
100 const IteratorRep_& iteratorRep = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
101 if (nextI != nullptr) {
102 savedUnderlyingIndex = iteratorRep.fIterator.GetUnderlyingIteratorRep ();
103 }
104 fData_.SetAt (iteratorRep.fIterator, newValue);
105 fChangeCounts_.PerformedChange ();
106 if (nextI != nullptr) {
108 }
109 }
110 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
111 {
113 const IteratorRep_& iteratorRep = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
114 if (nextI == nullptr) {
115 fData_.Remove (iteratorRep.fIterator);
116 fChangeCounts_.PerformedChange ();
117 }
118 else {
119 auto ret = fData_.erase (iteratorRep.fIterator);
120 fChangeCounts_.PerformedChange ();
122 }
123 }
124
125 private:
126 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
127 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
128
129 private:
130 DataStructureImplType_ fData_;
131 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
132 };
133
134 /*
135 ********************************************************************************
136 ************************** Collection_LinkedList<T> ****************************
137 ********************************************************************************
138 */
139 template <typename T>
141 : inherited{Memory::MakeSharedPtr<Rep_> ()}
142 {
143 AssertRepValidType_ ();
144 }
145 template <typename T>
146 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
147 inline Collection_LinkedList<T>::Collection_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
149 {
151 AssertRepValidType_ ();
152 }
153 template <typename T>
154 inline Collection_LinkedList<T>::Collection_LinkedList (const initializer_list<value_type>& src)
155 : Collection_LinkedList{}
156 {
157 this->AddAll (src);
158 AssertRepValidType_ ();
159 }
160#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
161 template <typename T>
162 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
163 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Collection_LinkedList<T>>)
164 inline Collection_LinkedList<T>::Collection_LinkedList (ITERABLE_OF_ADDABLE&& src)
165 : Collection_LinkedList{}
166 {
167 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
168 AssertRepValidType_ ();
169 }
170#endif
171 template <typename T>
172 inline void Collection_LinkedList<T>::AssertRepValidType_ () const
173 {
175 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
176 }
177 }
178
179}
#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
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< value_type > newValue, Iterator< value_type > *nextI=nullptr)
nonvirtual void Remove(ArgByValueType< value_type > item, EQUALS_COMPARER &&equalsComparer={})
Remove () the argument value (which must exist)
nonvirtual void Add(ArgByValueType< value_type > item)
Collection_LinkedList<T> is an LinkedList-based concrete implementation of the Collection<T> containe...
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 CONTAINER_OF_T As(CONTAINER_OF_T_CONSTRUCTOR_ARGS... args) const
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,...