5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
16 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
17 template <qCompilerAndStdLib_Constra
intDiffersInTemplateRedeclaration_BWA (IThreeWayComparer<KEY_TYPE>) KEY_COMPARER>
18 class SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::Rep_
19 :
public Private::SkipListBasedContainerRepImpl<Rep_<KEY_COMPARER>, IImplRepBase_>,
20 public Memory::UseBlockAllocationIfAppropriate<Rep_<KEY_COMPARER>> {
22 using inherited = Private::SkipListBasedContainerRepImpl<Rep_<KEY_COMPARER>, IImplRepBase_>;
25 static_assert (not is_reference_v<KEY_COMPARER>);
28 Rep_ (
const KEY_COMPARER& comparer)
32 Rep_ (SKIPLIST<KEY_COMPARER>&& src)
36 Rep_ (
const Rep_& from) =
default;
39 nonvirtual Rep_& operator= (
const Rep_&) =
delete;
43 virtual shared_ptr<typename Iterable<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>::_IRep> Clone ()
const override
46 return Memory::MakeSharedPtr<Rep_> (*
this);
48 virtual Iterator<value_type>
MakeIterator ()
const override
51 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
53 virtual size_t size ()
const override
57 return fData_.size ();
59 virtual bool empty ()
const override
63 return fData_.empty ();
65 virtual void Apply (
const function<
void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]]
Execution::SequencePolicy seq)
const override
68 fData_.Apply (doToElement);
70 virtual Iterator<value_type>
Find (
const function<
bool (ArgByValueType<value_type> item)>& that,
Execution::SequencePolicy seq)
const override
78 virtual KeyEqualsCompareFunctionType GetKeyEqualsComparer ()
const override
81 return Common::EqualsComparerAdapter<KEY_TYPE, KEY_COMPARER>{fData_.key_comp ()};
83 virtual shared_ptr<typename Association<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep> CloneEmpty ()
const override
86 return Memory::MakeSharedPtr<Rep_> (fData_.key_comp ());
88 virtual shared_ptr<typename Association<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i)
const override
92 auto result = Memory::MakeSharedPtr<Rep_> (*
this);
93 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
94 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
98 virtual Iterable<mapped_type>
Lookup (ArgByValueType<KEY_TYPE> key)
const override
102 auto i = fData_.Find (key);
103 vector<mapped_type> result;
104 if (i != fData_.end ()) {
105 Assert (GetKeyEqualsComparer () (key, i->fKey));
106 result.push_back (i->fValue);
108 Assert (fData_.key_comp () (key, i->fKey) == strong_ordering::equal);
109 for (++i; i != fData_.end () and fData_.key_comp () (key, i->fKey) == strong_ordering::equal; ++i) {
110 result.push_back (i->fValue);
113 return Iterable<mapped_type>{move (result)};
115 virtual void Add (ArgByValueType<KEY_TYPE> key, ArgByValueType<MAPPED_VALUE_TYPE> newElt)
override
119 (void)fData_.Add (key, newElt);
120 fChangeCounts_.PerformedChange ();
123 virtual bool RemoveIf (ArgByValueType<KEY_TYPE> key)
override
127 auto i = fData_.Find (key);
128 if (i != fData_.end ()) {
130 fChangeCounts_.PerformedChange ();
135 virtual void Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
override
138 auto newI = fData_.erase (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator);
139 fChangeCounts_.PerformedChange ();
140 if (nextI !=
nullptr) {
141 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, newI)};
144 virtual void Update (
const Iterator<value_type>& i, ArgByValueType<mapped_type> newValue, Iterator<value_type>* nextI)
override
147 optional<Iterator<value_type>> savedNextI;
148 if (nextI !=
nullptr) {
151 fData_.Update (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
152 fChangeCounts_.PerformedChange ();
153 if (nextI !=
nullptr) {
154 savedNextI->Refresh ();
163 return fData_.key_comp ();
167 using DataStructureImplType_ = SKIPLIST<KEY_COMPARER>;
168 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
171 DataStructureImplType_ fData_;
172 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
183 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
187 AssertRepValidType_ ();
189 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
190 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER>
192 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<KEY_COMPARER>>> (forward<KEY_COMPARER> (comparer))}
194 AssertRepValidType_ ();
196 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
197 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER>
199 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<KEY_COMPARER>>> (move (src))}
201 AssertRepValidType_ ();
203 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
204 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER>
206 KEY_COMPARER&& comparer,
const initializer_list<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>& src)
207 : SortedAssociation_SkipList{forward<KEY_COMPARER> (comparer)}
210 AssertRepValidType_ ();
212#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
213 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
214 template <IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
215 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>>)
217 : SortedAssociation_SkipList{}
219 AssertRepValidType_ ();
220 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
221 AssertRepValidType_ ();
224 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
225 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER, IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
227 : SortedAssociation_SkipList{forward<KEY_COMPARER> (comparer)}
229 AssertRepValidType_ ();
230 this->
AddAll (forward<ITERABLE_OF_ADDABLE> (src));
231 AssertRepValidType_ ();
233 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
234 template <IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
236 : SortedAssociation_SkipList{}
238 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
239 AssertRepValidType_ ();
241 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
242 template <IThreeWayComparer<KEY_TYPE> KEY_COMPARER, IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
244 ITERATOR_OF_ADDABLE&& end)
245 : SortedAssociation_SkipList{forward<KEY_COMPARER> (comparer)}
247 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
248 AssertRepValidType_ ();
250 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
251 inline void SortedAssociation_SkipList<KEY_TYPE, MAPPED_VALUE_TYPE>::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)
nonvirtual bool RemoveIf(ArgByValueType< key_type > key)
Remove the given item, if it exists. Return true if found and removed.
nonvirtual void Remove(ArgByValueType< key_type > key)
Remove the given item (which must exist).
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< mapped_type > newValue, Iterator< value_type > *nextI=nullptr)
nonvirtual void Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt)
nonvirtual void AddAll(ITERABLE_OF_ADDABLE &&items)
nonvirtual Traversal::Iterable< mapped_type > Lookup(ArgByValueType< key_type > key) const
Return an Iterable<mapped_type> of all the associated items (can be empty if none)....
SortedAssociation_SkipList<KEY_TYPE,MAPPED_VALUE_TYPE> is an SkipList-based concrete implementation o...
SortedAssociation_SkipList()
nonvirtual KeyThreeWayComparerType GetKeyThreeWayComparer() 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,...