Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Collection_stdforward_list.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
7
9
10 /*
11 ********************************************************************************
12 ******************* Collection_stdforward_list<T>::Rep_ ************************
13 ********************************************************************************
14 */
15 template <typename T>
16 class Collection_stdforward_list<T>::Rep_ : public Collection<T>::_IRep, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
17 private:
18 using inherited = typename Collection<T>::_IRep;
19
20 public:
21 Rep_ () = default;
22 Rep_ (const Rep_& from) = default;
23
24 public:
25 nonvirtual Rep_& operator= (const Rep_&) = delete;
26
27 // Iterable<T>::_IRep overrides
28 public:
29 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
30 {
32 return Memory::MakeSharedPtr<Rep_> (*this);
33 }
34 virtual Iterator<value_type> MakeIterator () const override
35 {
37 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
38 }
39 virtual size_t size () const override
40 {
42 size_t cnt = 0;
43 for (auto i = fData_.begin (); i != fData_.end (); ++i, ++cnt)
44 ;
45 return cnt;
46 }
47 virtual bool empty () const override
48 {
50 return fData_.empty ();
51 }
52 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
53 {
55 fData_.Apply (doToElement);
56 }
57 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that,
58 [[maybe_unused]] Execution::SequencePolicy seq) const override
59 {
61 return this->inherited::Find (that, seq); // @todo rewrite to use fData
62 }
63
64 // Collection<T>::_IRep overrides
65 public:
66 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty () const override
67 {
68 return Memory::MakeSharedPtr<Rep_> ();
69 }
70 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
71 {
74 auto result = Memory::MakeSharedPtr<Rep_> (*this);
75 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
76 result->fData_.MoveIteratorHereAfterClone (
77 &mir.fIterator, &fData_,
78 [targetI = mir.fIterator.GetUnderlyingIteratorRep ()] (auto oldI, [[maybe_unused]] auto newI) { return targetI == oldI; });
79 i->Refresh (); // reflect updated rep
80 return result;
81 }
82 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI) override
83 {
85 fData_.push_front (item);
86 fChangeCounts_.PerformedChange ();
87 if (oAddedI != nullptr) [[unlikely]] {
88 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
89 }
90 }
91 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
92 {
94 Require (not i.Done ());
95 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
96 if (nextI != nullptr) {
97 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
98 }
99 *fData_.remove_constness (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ()) = newValue;
100 fChangeCounts_.PerformedChange ();
101 if (nextI != nullptr) {
102 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
103 }
104 }
105 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
106 {
108 // Horrible API - must revisit/rethink. Maybe just a bad fit? But to erase an element from a forward_list,
109 // given a link to it, you must walk from the start of the list and find its prev pointer
110 typename STDFORWARDLIST::const_iterator victim =
111 Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
112 typename STDFORWARDLIST::const_iterator prevI;
113 for (prevI = fData_.before_begin (); std::next (prevI) != victim; ++prevI)
114 ;
115 Assert (prevI != fData_.end ()); // must be able to find prevI (if
116 auto nextStdI = fData_.erase_after (prevI);
117 fChangeCounts_.PerformedChange ();
118 if (nextI != nullptr) {
119 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextStdI)};
120 }
121 }
122
123 private:
124 using DataStructureImplType_ = DataStructures::STLContainerWrapper<STDFORWARDLIST>;
125 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
126
127 private:
128 DataStructureImplType_ fData_;
129 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
130 };
131
132 /*
133 ********************************************************************************
134 ************************* Collection_stdforward_list<T> ************************
135 ********************************************************************************
136 */
137 template <typename T>
139 : inherited{Memory::MakeSharedPtr<Rep_> ()}
140 {
141 AssertRepValidType_ ();
142 }
143 template <typename T>
144 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
145 inline Collection_stdforward_list<T>::Collection_stdforward_list (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
147 {
148 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
149 AssertRepValidType_ ();
150 }
151 template <typename T>
152 inline Collection_stdforward_list<T>::Collection_stdforward_list (const initializer_list<value_type>& src)
153 : Collection_stdforward_list{}
154 {
155 this->AddAll (src);
156 AssertRepValidType_ ();
157 }
158#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
159 template <typename T>
160 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
161 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Collection_stdforward_list<T>>)
162 inline Collection_stdforward_list<T>::Collection_stdforward_list (ITERABLE_OF_ADDABLE&& src)
163 : Collection_stdforward_list{}
164 {
165 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
166 AssertRepValidType_ ();
167 }
168#endif
169 template <typename T>
170 inline void Collection_stdforward_list<T>::AssertRepValidType_ () const
171 {
173 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
174 }
175 }
176
177}
#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
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_stdforward_list<T> is an std::forward_list (singly linked list)-based concrete implementat...
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,...