Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
SortedCollection_stdmultiset.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
8
10
11 /*
12 ********************************************************************************
13 ******************** SortedCollection_stdmultiset<T>::Rep_ *********************
14 ********************************************************************************
15 */
16 template <typename T>
17 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (Common::IInOrderComparer<T>) INORDER_COMPARER>
18 class SortedCollection_stdmultiset<T>::Rep_ : public IImplRepBase_, public Memory::UseBlockAllocationIfAppropriate<Rep_<INORDER_COMPARER>> {
19 private:
20 using inherited = IImplRepBase_;
21
22 public:
23 static_assert (not is_reference_v<INORDER_COMPARER>);
24
25 public:
26 Rep_ (const INORDER_COMPARER& inorderComparer)
27 : fData_{inorderComparer}
28 {
29 }
30 Rep_ (const Rep_& from) = default;
31
32 public:
33 nonvirtual Rep_& operator= (const Rep_&) = delete;
34
35 // Iterable<T>::_IRep overrides
36 public:
37 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
38 {
40 return Memory::MakeSharedPtr<Rep_> (*this);
41 }
42 virtual Iterator<value_type> MakeIterator () const override
43 {
45 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
46 }
47 virtual size_t size () const override
48 {
50 return fData_.size ();
51 }
52 virtual bool empty () const override
53 {
55 return fData_.empty ();
56 }
57 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
58 {
60 fData_.Apply (doToElement);
61 }
62 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that, Execution::SequencePolicy seq) const override
63 {
65 return this->inherited::Find (that, seq); // @todo rewrite to use fData
66 }
67 virtual Iterator<value_type> Find_equal_to (const ArgByValueType<value_type>& v, [[maybe_unused]] Execution::SequencePolicy seq) const override
68 {
69 // if doing a find by 'equals-to' - we already have this indexed
70 auto found = fData_.find (v);
71 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
72 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
73 .fIterator.GetUnderlyingIteratorRep ()));
74 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, found)};
75 }
76
77 // Collection<T>::_IRep overrides
78 public:
79 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty () const override
80 {
81 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
82 }
83 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
84 {
87 auto result = Memory::MakeSharedPtr<Rep_> (*this);
88 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
89 result->fData_.MoveIteratorHereAfterClone (
90 &mir.fIterator, &fData_,
91 [targetI = mir.fIterator.GetUnderlyingIteratorRep ()] (auto oldI, [[maybe_unused]] auto newI) { return targetI == oldI; });
92 i->Refresh (); // reflect updated rep
93 return result;
94 }
95 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI) override
96 {
98 auto addedAtI = fData_.insert (item);
99 fChangeCounts_.PerformedChange ();
100 if (oAddedI != nullptr) [[unlikely]] {
101 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, addedAtI)};
102 }
103 }
104 virtual void Update (const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI) override
105 {
107 auto nextIR = fData_.erase (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ());
108 fData_.insert (newValue);
109 fChangeCounts_.PerformedChange ();
110 if (nextI != nullptr) {
111 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextIR)};
112 }
113 }
114 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
115 {
117 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
118 mir.fIterator.AssertDataMatches (&fData_);
119 auto nextIR = fData_.erase (mir.fIterator.GetUnderlyingIteratorRep ());
120 fChangeCounts_.PerformedChange ();
121 if (nextI != nullptr) {
122 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextIR)};
123 }
124 }
125
126 // SortedCollection<T>::_IRep overrides
127 public:
128 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
129 {
131 return Common::ThreeWayComparerAdapter<T, INORDER_COMPARER>{fData_.key_comp ()};
132 }
133 virtual bool Contains (ArgByValueType<value_type> item) const override
134 {
136 return fData_.contains (item);
137 }
138 virtual void Remove (ArgByValueType<value_type> item) override
139 {
141 fData_.Invariant ();
142 auto i = fData_.find (item);
143 if (i != fData_.end ()) {
144 fData_.erase (i);
145 fChangeCounts_.PerformedChange ();
146 }
147 }
148
149 private:
150 using DataStructureImplType_ = DataStructures::STLContainerWrapper<STDMULTISET<INORDER_COMPARER>>;
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 ************************* SortedCollection_stdmultiset<T> **********************
161 ********************************************************************************
162 */
163 template <typename T>
166 {
167 AssertRepValidType_ ();
168 }
169 template <typename T>
170 template <Common::IInOrderComparer<T> INORDER_COMPARER>
171 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inorderComparer)
172 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<INORDER_COMPARER>>> (inorderComparer)}
173 {
174 AssertRepValidType_ ();
175 }
176 template <typename T>
177 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (const initializer_list<T>& src)
178 : SortedCollection_stdmultiset{}
179 {
180 this->AddAll (src);
181 AssertRepValidType_ ();
182 }
183 template <typename T>
184 template <Common::IInOrderComparer<T> INORDER_COMPARER>
185 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inOrderComparer, const initializer_list<T>& src)
186 : SortedCollection_stdmultiset{forward<INORDER_COMPARER> (inOrderComparer)}
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>, SortedCollection_stdmultiset<T>>)
196 : SortedCollection_stdmultiset{}
197 {
198 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
199 AssertRepValidType_ ();
200 }
201#endif
202 template <typename T>
203 template <Common::IInOrderComparer<T> INORDER_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
204 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
205 : SortedCollection_stdmultiset{forward<INORDER_COMPARER> (inOrderComparer)}
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 SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
213 : SortedCollection_stdmultiset{}
214 {
215 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
216 AssertRepValidType_ ();
217 }
218 template <typename T>
219 template <Common::IInOrderComparer<T> INORDER_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
220 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start,
221 ITERATOR_OF_ADDABLE&& end)
222 : SortedCollection_stdmultiset{forward<INORDER_COMPARER> (inOrderComparer)}
223 {
224 this->AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
225 AssertRepValidType_ ();
226 }
227 template <typename T>
228 inline void SortedCollection_stdmultiset<T>::AssertRepValidType_ () const
229 {
231 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
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
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 Add(ArgByValueType< value_type > item)
SortedCollection_stdmultiset<T> is an stdmultiset-based concrete implementation of the SortedCollecti...
nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer() const
nonvirtual bool Contains(ArgByValueType< T > item) const
Compares items with the already associated GetInOrderComparer(), and returns true if the item is foun...
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,...