Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Set_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 /**
12 */
13 template <typename T>
14 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (IEqualsComparer<T>) EQUALS_COMPARER>
15 class Set_LinkedList<T>::Rep_ : public IImplRepBase_, public Memory::UseBlockAllocationIfAppropriate<Rep_<EQUALS_COMPARER>> {
16 private:
17 using inherited = IImplRepBase_;
18
19 public:
20 Rep_ (const EQUALS_COMPARER& equalsComparer)
21 : fEqualsComparer_{equalsComparer}
22 {
23 }
24 Rep_ (const Rep_& from) = default;
25
26 private:
27 [[no_unique_address]] EQUALS_COMPARER fEqualsComparer_;
28
29 public:
30 nonvirtual Rep_& operator= (const Rep_&) = delete;
31
32 // Iterable<T>::_IRep overrides
33 public:
34 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
35 {
37 return Memory::MakeSharedPtr<Rep_> (*this);
38 }
39 virtual Iterator<value_type> MakeIterator () const override
40 {
42 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
43 }
44 virtual size_t size () const override
45 {
47 return fData_.size ();
48 }
49 virtual bool empty () const override
50 {
52 return fData_.empty ();
53 }
54 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
55 {
57 fData_.Apply (doToElement);
58 }
59 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that,
60 [[maybe_unused]] Execution::SequencePolicy seq) const override
61 {
63 if (auto iLink = fData_.Find (that)) {
64 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
65 }
66 return nullptr;
67 }
68
69 // Set<T>::_IRep overrides
70 public:
71 virtual ElementEqualityComparerType GetElementEqualsComparer () const override
72 {
74 return fEqualsComparer_;
75 }
76 virtual shared_ptr<typename Set<T>::_IRep> CloneEmpty () const override
77 {
79 return Memory::MakeSharedPtr<Rep_> (fEqualsComparer_); // copy comparers, but not data
80 }
81 virtual shared_ptr<typename Set<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
82 {
85 auto result = Memory::MakeSharedPtr<Rep_> (*this);
86 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
87 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
88 i->Refresh (); // reflect updated rep
89 return result;
90 }
91 virtual bool Equals (const typename Iterable<value_type>::_IRep& rhs) const override
92 {
94 return this->_Equals_Reference_Implementation (rhs);
95 }
96 virtual bool Lookup (ArgByValueType<value_type> item, optional<value_type>* oResult, Iterator<value_type>* iResult) const override
97 {
99 typename DataStructureImplType_::UnderlyingIteratorRep l =
100 fData_.Find ([this, item] (ArgByValueType<value_type> i) { return fEqualsComparer_ (i, item); });
101 bool notDone = l != nullptr;
102 if (oResult != nullptr and notDone) {
103 *oResult = l->fItem;
104 }
105 if (iResult != nullptr and notDone) {
106 *iResult = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, l)};
107 }
108 return notDone;
109 }
110 virtual void Add (ArgByValueType<value_type> item) override
111 {
113 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
114 if (fEqualsComparer_ (*it, item)) {
115 fChangeCounts_.PerformedChange ();
116 return;
117 }
118 }
119 fData_.push_front (item); // order meaningless for set, and prepend cheaper on linked list
120 fChangeCounts_.PerformedChange ();
121 }
122 virtual bool RemoveIf (ArgByValueType<value_type> item) override
123 {
125 for (typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
126 if (fEqualsComparer_ (*it, item)) {
127 fData_.Remove (it);
128 fChangeCounts_.PerformedChange ();
129 return true;
130 }
131 }
132 return false;
133 }
134 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
135 {
137 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
138 if (nextI == nullptr) {
139 fData_.Remove (mir.fIterator);
140 fChangeCounts_.PerformedChange ();
141 }
142 else {
143 auto ret = fData_.erase (mir.fIterator);
144 fChangeCounts_.PerformedChange ();
145 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
146 }
147 }
148
149 private:
150 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
151 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
152
153 private:
154 DataStructureImplType_ fData_;
155 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
156 };
157
158 /*
159 ********************************************************************************
160 ******************************** Set_LinkedList<T> *****************************
161 ********************************************************************************
162 */
163 template <typename T>
165 : Set_LinkedList{equal_to<value_type>{}}
166 {
167 AssertRepValidType_ ();
168 }
169 template <typename T>
170 template <IEqualsComparer<T> EQUALS_COMPARER>
171 inline Set_LinkedList<T>::Set_LinkedList (EQUALS_COMPARER&& equalsComparer)
172 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<EQUALS_COMPARER>>> (forward<EQUALS_COMPARER> (equalsComparer))}
173 {
174 AssertRepValidType_ ();
175 }
176 template <typename T>
177 inline Set_LinkedList<T>::Set_LinkedList (const initializer_list<value_type>& src)
178 : Set_LinkedList{}
179 {
180 this->AddAll (src);
181 AssertRepValidType_ ();
182 }
183 template <typename T>
184 template <IEqualsComparer<T> EQUALS_COMPARER>
185 inline Set_LinkedList<T>::Set_LinkedList (EQUALS_COMPARER&& equalsComparer, const initializer_list<value_type>& src)
186 : Set_LinkedList{forward<EQUALS_COMPARER> (equalsComparer)}
187 {
188 this->AddAll (src);
189 AssertRepValidType_ ();
190 }
191#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
192 template <typename T>
193 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
194 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Set_LinkedList<T>>)
195 inline Set_LinkedList<T>::Set_LinkedList (ITERABLE_OF_ADDABLE&& src)
196 : Set_LinkedList{}
197 {
198 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
199 AssertRepValidType_ ();
200 }
201#endif
202 template <typename T>
203 template <IEqualsComparer<T> EQUALS_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
204 inline Set_LinkedList<T>::Set_LinkedList (EQUALS_COMPARER&& equalsComparer, ITERABLE_OF_ADDABLE&& src)
205 : Set_LinkedList{forward<EQUALS_COMPARER> (equalsComparer)}
206 {
207 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
208 AssertRepValidType_ ();
209 }
210 template <typename T>
211 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
212 inline Set_LinkedList<T>::Set_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
213 : Set_LinkedList{}
214 {
215 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
216 AssertRepValidType_ ();
217 }
218 template <typename T>
219 template <IEqualsComparer<T> EQUALS_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
220 inline Set_LinkedList<T>::Set_LinkedList (EQUALS_COMPARER&& equalsComparer, ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
221 : Set_LinkedList{forward<EQUALS_COMPARER> (equalsComparer)}
222 {
223 this->AddAll (start, end);
224 AssertRepValidType_ ();
225 }
226 template <typename T>
227 inline void Set_LinkedList<T>::AssertRepValidType_ () const
228 {
230 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
231 }
232 }
233
234}
#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
bool Equals(const T *lhs, const T *rhs)
strcmp or wsccmp() as appropriate == 0
Set_LinkedList<T> is an LinkedList-based concrete implementation of the Set<T> container pattern.
nonvirtual optional< value_type > Lookup(ArgByValueType< value_type > item) const
Definition Set.inl:131
nonvirtual ElementEqualityComparerType GetElementEqualsComparer() const
Definition Set.inl:96
nonvirtual Set CloneEmpty() const
for return Set<T> { s->GetEqualsComparer(); } - except more efficient - clones settings/dynamic subty...
Definition Set.inl:503
nonvirtual void Remove(ArgByValueType< value_type > item)
Remove the item (given by value or iterator pointing to it) from the contain. The item MUST exist.
Definition Set.inl:182
nonvirtual bool RemoveIf(ArgByValueType< value_type > item)
Definition Set.inl:194
nonvirtual void Add(ArgByValueType< value_type > item)
Definition Set.inl:138
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
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,...