4#include "Stroika/Foundation/Containers/DataStructures/SkipList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
17 template <qCompilerAndStdLib_Constra
intDiffersInTemplateRedeclaration_BWA (Common::IThreeWayComparer<T>) COMPARER>
18 class SortedCollection_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
51 return fData_.size ();
53 virtual bool empty ()
const override
56 return fData_.empty ();
58 virtual void Apply (
const function<
void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]]
Execution::SequencePolicy seq)
const override
61 fData_.Apply ([&] (
auto arg) { doToElement (arg.fKey); });
63 virtual Iterator<value_type>
Find (
const function<
bool (ArgByValueType<value_type> item)>& that,
67 if (
auto iLink = fData_.Find ([&] (
auto arg) { return that (arg.fKey); })) {
68 return Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, iLink)};
75 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty ()
const override
78 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ());
80 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<T>* i)
const override
84 auto result = Memory::MakeSharedPtr<Rep_> (*
this);
85 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
86 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
90 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI)
override
93 if (oAddedI ==
nullptr) [[likely]] {
95 fChangeCounts_.PerformedChange ();
98 typename DataStructureImplType_::ForwardIterator retIt;
99 fData_.Add (item, &retIt);
100 fChangeCounts_.PerformedChange ();
101 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, retIt)};
104 virtual void Update (
const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI)
override
107 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
108 fData_.Remove (mir.fIterator);
109 if (nextI ==
nullptr) {
110 fData_.Add (newValue);
111 fChangeCounts_.PerformedChange ();
114 typename DataStructureImplType_::ForwardIterator retIt;
115 fData_.Add (newValue, &retIt);
116 fChangeCounts_.PerformedChange ();
117 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, retIt)};
120 virtual void Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
override
123 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
124 if (nextI ==
nullptr) [[likely]] {
125 fData_.Remove (mir.fIterator);
126 fChangeCounts_.PerformedChange ();
129 auto ret = fData_.erase (mir.fIterator);
130 fChangeCounts_.PerformedChange ();
131 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
140 return fData_.key_comp ();
142 virtual bool Contains (ArgByValueType<value_type> item)
const override
145 return not fData_.Find (item).Done ();
147 virtual void Remove (ArgByValueType<value_type> item)
override
150 fData_.Remove (item);
151 fChangeCounts_.PerformedChange ();
155 using DataStructureImplType_ = SKIPLIST<COMPARER>;
156 struct IteratorRep_ : Private::IteratorImplHelper_<value_type, DataStructureImplType_> {
157 using inherited = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
158 using inherited::inherited;
160 virtual void More (optional<T>* result,
bool advance)
override
163 this->ValidateChangeCount ();
164 if (advance) [[likely]] {
165 Require (not this->fIterator.Done ());
168 if (this->fIterator.Done ()) [[unlikely]] {
172 *result = this->fIterator->fKey;
175 virtual auto Clone () const -> unique_ptr<typename Iterator<T>::IRep>
override
177 this->ValidateChangeCount ();
178 return make_unique<IteratorRep_> (*
this);
183 DataStructureImplType_ fData_;
184 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
195 template <
typename T>
199 AssertRepValidType_ ();
201 template <
typename T>
202 template <IThreeWayComparer<T> COMPARER>
204 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<COMPARER>>> (inorderComparer)}
206 AssertRepValidType_ ();
208 template <
typename T>
210 : SortedCollection_SkipList{}
213 AssertRepValidType_ ();
215 template <
typename T>
216 template <IThreeWayComparer<T> COMPARER>
218 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
221 AssertRepValidType_ ();
223#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
224 template <
typename T>
225 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
226 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedCollection_SkipList<T>>)
228 : SortedCollection_SkipList{}
230 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
231 AssertRepValidType_ ();
234 template <
typename T>
235 template <IThreeWayComparer<T> COMPARER, IIterableOfTo<T> ITERABLE_OF_ADDABLE>
237 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
239 this->
AddAll (forward<ITERABLE_OF_ADDABLE> (src));
240 AssertRepValidType_ ();
242 template <
typename T>
243 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
245 : SortedCollection_SkipList{}
247 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
248 AssertRepValidType_ ();
250 template <
typename T>
251 template <IThreeWayComparer<T> COMPARER, IInputIterator<T> ITERATOR_OF_ADDABLE>
253 : SortedCollection_SkipList{forward<COMPARER> (inOrderComparer)}
255 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
256 AssertRepValidType_ ();
258 template <
typename T>
259 inline void SortedCollection_SkipList<T>::AssertRepValidType_ ()
const
262 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)
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_SkipList<T> is an SkipList-based concrete implementation of the SortedCollection<T> ...
SortedCollection_SkipList()
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.
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,...