Stroika Library 3.0d16
 
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 (&mir.fIterator, &fData_);
77 i->Refresh (); // reflect updated rep
78 return result;
79 }
80 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI) override
81 {
83 fData_.push_front (item);
84 fChangeCounts_.PerformedChange ();
85 if (oAddedI != nullptr) [[unlikely]] {
86 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
87 }
88 }
89 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
90 {
92 Require (not i.Done ());
93 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
94 if (nextI != nullptr) {
95 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
96 }
97 *fData_.remove_constness (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ()) = newValue;
98 fChangeCounts_.PerformedChange ();
99 if (nextI != nullptr) {
100 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
101 }
102 }
103 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
104 {
106 // Horrible API - must revisit/rethink. Maybe just a bad fit? But to erase an element from a forward_list,
107 // given a link to it, you must walk from the start of the list and find its prev pointer
108 typename STDFORWARDLIST::const_iterator victim =
109 Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
110 typename STDFORWARDLIST::const_iterator prevI;
111 for (prevI = fData_.before_begin (); std::next (prevI) != victim; ++prevI)
112 ;
113 Assert (prevI != fData_.end ()); // must be able to find prevI (if
114 auto nextStdI = fData_.erase_after (prevI);
115 fChangeCounts_.PerformedChange ();
116 if (nextI != nullptr) {
117 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextStdI)};
118 }
119 }
120
121 private:
122 using DataStructureImplType_ = DataStructures::STLContainerWrapper<STDFORWARDLIST>;
123 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
124
125 private:
126 DataStructureImplType_ fData_;
127 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
128 };
129
130 /*
131 ********************************************************************************
132 ************************* Collection_stdforward_list<T> ************************
133 ********************************************************************************
134 */
135 template <typename T>
137 : inherited{Memory::MakeSharedPtr<Rep_> ()}
138 {
139 AssertRepValidType_ ();
140 }
141 template <typename T>
142 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
143 inline Collection_stdforward_list<T>::Collection_stdforward_list (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
145 {
146 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
147 AssertRepValidType_ ();
148 }
149 template <typename T>
150 inline Collection_stdforward_list<T>::Collection_stdforward_list (const initializer_list<value_type>& src)
151 : Collection_stdforward_list{}
152 {
153 this->AddAll (src);
154 AssertRepValidType_ ();
155 }
156#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
157 template <typename T>
158 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
159 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Collection_stdforward_list<T>>)
160 inline Collection_stdforward_list<T>::Collection_stdforward_list (ITERABLE_OF_ADDABLE&& src)
161 : Collection_stdforward_list{}
162 {
163 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
164 AssertRepValidType_ ();
165 }
166#endif
167 template <typename T>
168 inline void Collection_stdforward_list<T>::AssertRepValidType_ () const
169 {
171 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
172 }
173 }
174
175}
#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 ...
Definition Collection.h:102
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: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,...