4#include "Stroika/Foundation/Containers/DataStructures/LinkedList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
16 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
17 template <qCompilerAndStdLib_Constra
intDiffersInTemplateRedeclaration_BWA (IEqualsComparer<KEY_TYPE>) KEY_EQUALS_COMPARER>
18 class Mapping_LinkedList<KEY_TYPE, MAPPED_VALUE_TYPE>::Rep_ :
public IImplRepBase_,
19 public Memory::UseBlockAllocationIfAppropriate<Rep_<KEY_EQUALS_COMPARER>> {
21 using inherited = IImplRepBase_;
24 static_assert (not is_reference_v<KEY_EQUALS_COMPARER>);
27 Rep_ (
const KEY_EQUALS_COMPARER& keyEqualsComparer)
28 : fKeyEqualsComparer_{keyEqualsComparer}
31 Rep_ (
const Rep_& from) =
default;
34 nonvirtual Rep_& operator= (
const Rep_&) =
delete;
37 [[no_unique_address]]
const KEY_EQUALS_COMPARER fKeyEqualsComparer_;
41 virtual shared_ptr<typename Iterable<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>::_IRep> Clone ()
const override
44 return Memory::MakeSharedPtr<Rep_> (*
this);
46 virtual Iterator<value_type>
MakeIterator ()
const override
49 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
51 virtual size_t size ()
const override
54 return fData_.size ();
56 virtual bool empty ()
const override
59 return fData_.empty ();
61 virtual void Apply (
const function<
void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]]
Execution::SequencePolicy seq)
const override
64 fData_.Apply (doToElement);
66 virtual Iterator<value_type>
Find (
const function<
bool (ArgByValueType<value_type> item)>& that,
70 if (
auto iLink = fData_.Find (that)) {
71 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
78 virtual KeyEqualsCompareFunctionType GetKeyEqualsComparer ()
const override
81 return fKeyEqualsComparer_;
83 virtual shared_ptr<typename Mapping<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep> CloneEmpty ()
const override
86 return Memory::MakeSharedPtr<Rep_> (fKeyEqualsComparer_);
88 virtual shared_ptr<typename Mapping<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep> CloneAndPatchIterator ([[maybe_unused]] Iterator<value_type>* i)
const override
91 auto result = Memory::MakeSharedPtr<Rep_> (*
this);
92 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
93 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
97 virtual bool Lookup (ArgByValueType<KEY_TYPE> key, optional<MAPPED_VALUE_TYPE>* item)
const override
100 for (
typename DataStructures::LinkedList<value_type>::ForwardIterator it{&fData_}; not it.Done (); ++it) {
101 if (fKeyEqualsComparer_ (it->fKey, key)) {
102 if (item !=
nullptr) {
108 if (item !=
nullptr) {
113 virtual bool Add (ArgByValueType<KEY_TYPE> key, ArgByValueType<MAPPED_VALUE_TYPE> newElt,
AddReplaceMode addReplaceMode)
override
116 for (
typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
117 if (fKeyEqualsComparer_ (it->fKey, key)) {
118 switch (addReplaceMode) {
120 fData_.SetAt (it, value_type{key, newElt});
121 fChangeCounts_.PerformedChange ();
131 fData_.push_front (value_type{key, newElt});
132 fChangeCounts_.PerformedChange ();
135 virtual bool RemoveIf (ArgByValueType<KEY_TYPE> key)
override
138 for (
typename DataStructureImplType_::ForwardIterator it{&fData_}; not it.Done (); ++it) {
139 if (fKeyEqualsComparer_ (it->fKey, key)) {
141 fChangeCounts_.PerformedChange ();
147 virtual void Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
override
150 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
151 if (nextI ==
nullptr) {
152 fData_.Remove (mir.fIterator);
153 fChangeCounts_.PerformedChange ();
156 auto ret = fData_.erase (mir.fIterator);
157 fChangeCounts_.PerformedChange ();
158 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
161 virtual void Update (
const Iterator<value_type>& i, ArgByValueType<mapped_type> newValue, Iterator<value_type>* nextI)
override
164 auto dataStructureIterator = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator;
165 fData_.PeekAt (dataStructureIterator)->fValue = newValue;
166 fChangeCounts_.PerformedChange ();
167 if (nextI !=
nullptr) {
168 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, dataStructureIterator.GetUnderlyingIteratorRep ())};
173 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
174 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
177 DataStructureImplType_ fData_;
178 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
186 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
190 AssertRepValidType_ ();
192 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
193 template <IEqualsComparer<KEY_TYPE> KEY_EQUALS_COMPARER>
194 inline Mapping_LinkedList<KEY_TYPE, MAPPED_VALUE_TYPE>::Mapping_LinkedList (KEY_EQUALS_COMPARER&& keyEqualsComparer)
195 : inherited{Memory::MakeSharedPtr<Rep_<remove_cvref_t<KEY_EQUALS_COMPARER>>> (keyEqualsComparer)}
197 AssertRepValidType_ ();
199 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
201 : Mapping_LinkedList{}
204 AssertRepValidType_ ();
206 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
207 template <IEqualsComparer<KEY_TYPE> KEY_EQUALS_COMPARER>
209 const initializer_list<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>>& src)
210 : Mapping_LinkedList{forward<KEY_EQUALS_COMPARER> (keyEqualsComparer)}
213 AssertRepValidType_ ();
215#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
216 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
217 template <IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
218 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Mapping_LinkedList<KEY_TYPE, MAPPED_VALUE_TYPE>>)
220 : Mapping_LinkedList{}
222 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
223 AssertRepValidType_ ();
226 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
227 template <IEqualsComparer<KEY_TYPE> KEY_EQUALS_COMPARER, IIterableOfTo<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERABLE_OF_ADDABLE>
229 : Mapping_LinkedList{forward<KEY_EQUALS_COMPARER> (keyEqualsComparer)}
231 this->
AddAll (forward<ITERABLE_OF_ADDABLE> (src));
232 AssertRepValidType_ ();
234 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
235 template <IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
237 : Mapping_LinkedList{}
239 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
240 AssertRepValidType_ ();
242 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
243 template <IEqualsComparer<KEY_TYPE> KEY_EQUALS_COMPARER, IInputIterator<KeyValuePair<KEY_TYPE, MAPPED_VALUE_TYPE>> ITERATOR_OF_ADDABLE>
245 ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
246 : Mapping_LinkedList{forward<KEY_EQUALS_COMPARER> (keyEqualsComparer)}
248 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
249 AssertRepValidType_ ();
251 template <
typename KEY_TYPE,
typename MAPPED_VALUE_TYPE>
252 inline void Mapping_LinkedList<KEY_TYPE, MAPPED_VALUE_TYPE>::AssertRepValidType_ ()
const
255 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 AssertNotReached()
Mapping_LinkedList<KEY_TYPE, MAPPED_VALUE_TYPE, TRAITS> is an LinkedList-based concrete implementatio...
nonvirtual bool Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
nonvirtual optional< mapped_type > Lookup(ArgByValueType< key_type > key) const
nonvirtual unsigned int AddAll(ITERABLE_OF_ADDABLE &&items, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< mapped_type > newValue, Iterator< value_type > *nextI=nullptr)
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).
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'.
AddReplaceMode
Mode flag to say if Adding to a container replaces, or if the first addition wins.
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...