5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
17 template <qCompilerAndStdLib_Constra
intDiffersInTemplateRedeclaration_BWA (IThreeWayComparer<T>) COMPARER>
18 class SortedSet_SkipList<T>::Rep_ :
public Private::SkipListBasedContainerRepImpl<Rep_<COMPARER>, IImplRepBase_>,
19 public Memory::UseBlockAllocationIfAppropriate<Rep_<COMPARER>> {
21 using inherited = Private::SkipListBasedContainerRepImpl<Rep_<COMPARER>, IImplRepBase_>;
24 static_assert (not is_reference_v<COMPARER>);
27 Rep_ (
const COMPARER& comparer)
31 Rep_ (
const Rep_& from) =
default;
34 nonvirtual Rep_& operator= (
const Rep_&) =
delete;
38 virtual shared_ptr<typename Iterable<T>::_IRep> Clone ()
const override
41 return Memory::MakeSharedPtr<Rep_> (*
this);
43 virtual Iterator<value_type>
MakeIterator ()
const override
46 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
48 virtual size_t size ()
const override
52 return fData_.size ();
54 virtual bool empty ()
const override
58 return fData_.empty ();
60 virtual void Apply (
const function<
void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]]
Execution::SequencePolicy seq)
const override
63 fData_.Apply ([&] (
const auto& k) { doToElement (k.fKey); });
65 virtual Iterator<value_type>
Find (
const function<
bool (ArgByValueType<value_type> item)>& that,
Execution::SequencePolicy seq)
const override
70 virtual Iterator<value_type> Find_equal_to (
const ArgByValueType<value_type>& v, [[maybe_unused]]
Execution::SequencePolicy seq)
const override
73 auto found = fData_.Find (v);
75 Ensure ((found == fData_.end () and this->inherited::Find_equal_to (v, seq) == Iterator<value_type>{nullptr}) or
76 (found == Debug::UncheckedDynamicCast<const IteratorRep_&> (this->inherited::Find_equal_to (v, seq).ConstGetRep ())
77 .fIterator.GetUnderlyingIteratorRep ()));
79 return Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, found)};
87 return Common::EqualsComparerAdapter<T, COMPARER>{fData_.key_comp ()};
89 virtual shared_ptr<typename Set<T>::_IRep>
CloneEmpty ()
const override
92 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ());
94 virtual shared_ptr<typename Set<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i)
const override
98 auto result = Memory::MakeSharedPtr<Rep_> (*
this);
99 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
100 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
104 virtual bool Equals (
const typename Iterable<value_type>::_IRep& rhs)
const override
107 return this->_Equals_Reference_Implementation (rhs);
109 virtual bool Lookup (ArgByValueType<value_type> item, optional<value_type>* oResult, Iterator<value_type>* iResult)
const override
112 auto i = fData_.Find (item);
113 bool notDone = i != fData_.end ();
114 if (oResult !=
nullptr and notDone) {
117 if (iResult !=
nullptr and notDone) {
118 *iResult = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, i)};
122 virtual void Add (ArgByValueType<value_type> item)
override
126 fChangeCounts_.PerformedChange ();
128 virtual bool RemoveIf (ArgByValueType<value_type> item)
override
132 auto i = fData_.Find (item);
133 if (i != fData_.end ()) [[likely]] {
135 fChangeCounts_.PerformedChange ();
140 virtual void Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
override
142 Require (not i.Done ());
143 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
144 mir.fIterator.AssertDataMatches (&fData_);
145 if (nextI ==
nullptr) {
146 fData_.Remove (mir.fIterator);
147 fChangeCounts_.PerformedChange ();
150 auto ret = fData_.erase (mir.fIterator);
151 fChangeCounts_.PerformedChange ();
152 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
161 return fData_.key_comp ();
165 using DataStructureImplType_ = SKIPLIST<COMPARER>;
166 struct IteratorRep_ : Private::IteratorImplHelper_<value_type, DataStructureImplType_> {
167 using inherited = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
168 using inherited::inherited;
170 virtual void More (optional<T>* result,
bool advance)
override
173 this->ValidateChangeCount ();
174 if (advance) [[likely]] {
175 Require (not this->fIterator.Done ());
178 if (this->fIterator.Done ()) [[unlikely]] {
182 *result = this->fIterator->fKey;
185 virtual auto Clone () const -> unique_ptr<typename Iterator<T>::IRep>
override
187 this->ValidateChangeCount ();
188 return make_unique<IteratorRep_> (*
this);
193 DataStructureImplType_ fData_;
194 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
205 template <
typename T>
209 AssertRepValidType_ ();
211 template <
typename T>
212 template <IThreeWayComparer<T> COMPARER>
214 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (forward<COMPARER> (comparer))}
216 AssertRepValidType_ ();
218 template <
typename T>
220 : SortedSet_SkipList{}
223 AssertRepValidType_ ();
225 template <
typename T>
226 template <IThreeWayComparer<T> COMPARER>
228 : SortedSet_SkipList{forward<COMPARER> (comparer)}
231 AssertRepValidType_ ();
233#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
234 template <
typename T>
235 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
236 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedSet_SkipList<T>>)
238 : SortedSet_SkipList{}
240 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
241 AssertRepValidType_ ();
244 template <
typename T>
245 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
247 : SortedSet_SkipList (forward<COMPARER> (comparer))
249 this->
AddAll (forward<ITERABLE_OF_ADDABLE> (src));
250 AssertRepValidType_ ();
252 template <
typename T>
253 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
255 : SortedSet_SkipList{}
257 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
258 AssertRepValidType_ ();
260 template <
typename T>
261 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
263 : SortedSet_SkipList (forward<COMPARER> (comparer))
265 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
266 AssertRepValidType_ ();
268 template <
typename T>
269 inline void SortedSet_SkipList<T>::AssertRepValidType_ ()
const
272 typename inherited::template _SafeReadRepAccessor<IImplRepBase_> tmp{
this};
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
#define RequireNotNull(p)
bool Equals(const T *lhs, const T *rhs)
strcmp or wsccmp() as appropriate == 0
SortedSet_SkipList<T> is an std::set-based concrete implementation of the SortedSet<T> container patt...
nonvirtual optional< value_type > Lookup(ArgByValueType< value_type > item) const
nonvirtual ElementEqualityComparerType GetElementEqualsComparer() const
nonvirtual Set CloneEmpty() const
for return Set<T> { s->GetEqualsComparer(); } - except more efficient - clones settings/dynamic subty...
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.
nonvirtual bool RemoveIf(ArgByValueType< value_type > item)
nonvirtual void Add(ArgByValueType< value_type > item)
nonvirtual void AddAll(ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
nonvirtual ElementThreeWayComparerType GetElementThreeWayComparer() const
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.
nonvirtual bool empty() const
Returns true iff size() == 0.
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'.
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...