Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
SortedCollection_stdmultiset.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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:
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 (bool findFirst, const function<bool (ArgByValueType<value_type> item)>& that,
63 Execution::SequencePolicy seq) const override
64 {
66 return this->inherited::Find (findFirst, that, seq); // @todo rewrite to use fData
67 }
68 virtual Iterator<value_type> Find_equal_to (const ArgByValueType<value_type>& v, [[maybe_unused]] Execution::SequencePolicy seq) const override
69 {
70 // if doing a find by 'equals-to' - we already have this indexed
71 // NOTE: must use lower_bound (), not find (), to get the FIRST (in iteration order) of a run of equal
72 // elements - find () only guarantees returning SOME matching element, which would violate the
73 // Find_equal_to () contract (always returns the first match) when there are duplicate elements.
74 auto found = fData_.lower_bound (v);
75 if (found != fData_.end () and (fData_.key_comp () (v, *found) or fData_.key_comp () (*found, v))) {
76 found = fData_.end ();
77 }
78 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
79 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
80 .fIterator.GetUnderlyingIteratorRep ()));
81 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, found)};
82 }
83
84 // Collection<T>::_IRep overrides
85 public:
86 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty () const override
87 {
88 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ()); // keep comparer, but lose data in clone
89 }
90 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i) const override
91 {
94 auto result = Memory::MakeSharedPtr<Rep_> (*this);
95 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
96 result->fData_.MoveIteratorHereAfterClone (
97 &mir.fIterator, &fData_,
98 [targetI = mir.fIterator.GetUnderlyingIteratorRep ()] (auto oldI, [[maybe_unused]] auto newI) { return targetI == oldI; });
99 i->Refresh (); // reflect updated rep
100 return result;
101 }
102#if qCompilerAndStdLib_MemoryInsertAt_Buggy
103 // see the note on qCompilerAndStdLib_MemoryInsertAt_Buggy - without this, g++ <= 14 drops
104 // the `if (oAddedI != nullptr)` guard below and writes through the null pointer
105 [[gnu::noinline, gnu::optimize ("O0")]]
106#endif
107 virtual void Add (const span<const value_type>& items, Iterator<value_type>* oAddedI) override
108 {
109 Require (not items.empty ());
110 Require (oAddedI == nullptr or items.size () == 1);
112 if (oAddedI == nullptr) [[likely]] {
113 fData_.insert (items.begin (), items.end ()); // multiset has a range insert
114 fChangeCounts_.PerformedChange ();
115 }
116 else {
117 auto addedAtI = fData_.insert (items[0]);
118 fChangeCounts_.PerformedChange ();
119 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, addedAtI)};
120 }
121 }
123 {
125 auto nextIR = fData_.erase (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ());
126 fData_.insert (newValue);
127 fChangeCounts_.PerformedChange ();
128 if (nextI != nullptr) {
129 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextIR)};
130 }
131 }
132 virtual void Remove (const Iterator<value_type>& i, Iterator<value_type>* nextI) override
133 {
135 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
136 mir.fIterator.AssertDataMatches (&fData_);
137 auto nextIR = fData_.erase (mir.fIterator.GetUnderlyingIteratorRep ());
138 fChangeCounts_.PerformedChange ();
139 if (nextI != nullptr) {
140 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, nextIR)};
141 }
142 }
143
144 // SortedCollection<T>::_IRep overrides
145 public:
146 virtual ElementThreeWayComparerType GetElementThreeWayComparer () const override
147 {
149 return Common::ThreeWayComparerAdapter<T, INORDER_COMPARER>{fData_.key_comp ()};
150 }
151 virtual bool Contains (ArgByValueType<value_type> item) const override
152 {
154 return fData_.contains (item);
155 }
156 virtual void Remove (ArgByValueType<value_type> item) override
157 {
159 fData_.Invariant ();
160 auto i = fData_.find (item);
161 if (i != fData_.end ()) {
162 fData_.erase (i);
163 fChangeCounts_.PerformedChange ();
164 }
165 }
166
167 private:
168 using DataStructureImplType_ = DataStructures::STLContainerWrapper<STDMULTISET<INORDER_COMPARER>>;
169 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
170
171 private:
172 DataStructureImplType_ fData_;
173 qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE Private::ContainerDebugChangeCounts_ fChangeCounts_;
174 };
175
176 /*
177 ********************************************************************************
178 ************************* SortedCollection_stdmultiset<T> **********************
179 ********************************************************************************
180 */
181 template <typename T>
184 {
185 AssertRepValidType_ ();
186 }
187 template <typename T>
188 template <Common::IInOrderComparer<T> INORDER_COMPARER>
189 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inorderComparer)
190 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<INORDER_COMPARER>>> (inorderComparer)}
191 {
192 AssertRepValidType_ ();
193 }
194 template <typename T>
195 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (const initializer_list<T>& src)
196 : SortedCollection_stdmultiset{}
197 {
198 this->AddAll (src);
199 AssertRepValidType_ ();
200 }
201 template <typename T>
202 template <Common::IInOrderComparer<T> INORDER_COMPARER>
203 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inOrderComparer, const initializer_list<T>& src)
204 : SortedCollection_stdmultiset{forward<INORDER_COMPARER> (inOrderComparer)}
205 {
206 this->AddAll (src);
207 AssertRepValidType_ ();
208 }
209#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
210 template <typename T>
211 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
212 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection_stdmultiset<T>>)
214 : SortedCollection_stdmultiset{}
215 {
216 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
217 AssertRepValidType_ ();
218 }
219#endif
220 template <typename T>
221 template <Common::IInOrderComparer<T> INORDER_COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
222 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inOrderComparer, ITERABLE_OF_ADDABLE&& src)
223 : SortedCollection_stdmultiset{forward<INORDER_COMPARER> (inOrderComparer)}
224 {
226 AssertRepValidType_ ();
227 }
228 template <typename T>
229 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
230 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
231 : SortedCollection_stdmultiset{}
232 {
234 AssertRepValidType_ ();
235 }
236 template <typename T>
237 template <Common::IInOrderComparer<T> INORDER_COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
238 inline SortedCollection_stdmultiset<T>::SortedCollection_stdmultiset (INORDER_COMPARER&& inOrderComparer, ITERATOR_OF_ADDABLE&& start,
239 ITERATOR_OF_ADDABLE&& end)
240 : SortedCollection_stdmultiset{forward<INORDER_COMPARER> (inOrderComparer)}
241 {
243 AssertRepValidType_ ();
244 }
245 template <typename T>
246 inline void SortedCollection_stdmultiset<T>::AssertRepValidType_ () const
247 {
249 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{this}; // for side-effect of AssertMember
250 }
251 }
252
253}
#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
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...
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,...