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 IterTraits_ : Private::IteratorImplHelper_DefaultTraits<value_type, DataStructureImplType_> {
167 static constexpr value_type ConvertDataStructureIterationResult2ContainerIteratorResult (
const typename DataStructureImplType_::value_type& t)
172 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_, IterTraits_>;
175 DataStructureImplType_ fData_;
176 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
187 template <
typename T>
191 AssertRepValidType_ ();
193 template <
typename T>
194 template <IThreeWayComparer<T> COMPARER>
196 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (forward<COMPARER> (comparer))}
198 AssertRepValidType_ ();
200 template <
typename T>
202 : SortedSet_SkipList{}
205 AssertRepValidType_ ();
207 template <
typename T>
208 template <IThreeWayComparer<T> COMPARER>
210 : SortedSet_SkipList{forward<COMPARER> (comparer)}
213 AssertRepValidType_ ();
215#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
216 template <
typename T>
217 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
218 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedSet_SkipList<T>>)
220 : SortedSet_SkipList{}
222 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
223 AssertRepValidType_ ();
226 template <
typename T>
227 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
229 : SortedSet_SkipList (forward<COMPARER> (comparer))
231 this->
AddAll (forward<ITERABLE_OF_ADDABLE> (src));
232 AssertRepValidType_ ();
234 template <
typename T>
235 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
237 : SortedSet_SkipList{}
239 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
240 AssertRepValidType_ ();
242 template <
typename T>
243 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
245 : SortedSet_SkipList (forward<COMPARER> (comparer))
247 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
248 AssertRepValidType_ ();
250 template <
typename T>
251 inline void SortedSet_SkipList<T>::AssertRepValidType_ ()
const
254 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,...