4#include "Stroika/Foundation/Containers/DataStructures/LinkedList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
14 class Collection_LinkedList<T>::Rep_ :
public IImplRep_,
public Memory::UseBlockAllocationIfAppropriate<Rep_> {
16 using inherited = IImplRep_;
20 Rep_ (
const Rep_& from) =
default;
23 nonvirtual Rep_& operator= (
const Rep_&) =
delete;
27 virtual shared_ptr<typename Iterable<T>::_IRep> Clone ()
const override
30 return Memory::MakeSharedPtr<Rep_> (*
this);
32 virtual Iterator<value_type>
MakeIterator ()
const override
35 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
37 virtual size_t size ()
const override
40 return fData_.size ();
42 virtual bool empty ()
const override
45 return fData_.empty ();
47 virtual void Apply (
const function<
void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]]
Execution::SequencePolicy seq)
const override
50 fData_.Apply (doToElement);
52 virtual Iterator<value_type>
Find (
const function<
bool (ArgByValueType<value_type> item)>& that,
56 if (
auto iLink = fData_.Find (that)) {
57 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
64 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty ()
const override
66 return Memory::MakeSharedPtr<Rep_> ();
68 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i)
const override
72 auto result = Memory::MakeSharedPtr<Rep_> (*
this);
73 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
74 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
78 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI)
override
81 fData_.push_front (item);
82 fChangeCounts_.PerformedChange ();
83 if (oAddedI !=
nullptr) [[unlikely]] {
84 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
87 virtual void Update (
const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI)
override
90 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
91 if (nextI !=
nullptr) {
92 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
94 fData_.SetAt (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
95 fChangeCounts_.PerformedChange ();
96 if (nextI !=
nullptr) {
97 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
100 virtual void Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
override
103 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
104 if (nextI ==
nullptr) {
105 fData_.Remove (mir.fIterator);
106 fChangeCounts_.PerformedChange ();
109 auto ret = fData_.erase (mir.fIterator);
110 fChangeCounts_.PerformedChange ();
111 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, ret)};
116 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
117 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
120 DataStructureImplType_ fData_;
121 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
129 template <
typename T>
131 :
inherited{Memory::MakeSharedPtr<Rep_> ()}
133 AssertRepValidType_ ();
135 template <
typename T>
136 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
140 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
141 AssertRepValidType_ ();
143 template <
typename T>
145 : Collection_LinkedList{}
148 AssertRepValidType_ ();
150#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
151 template <
typename T>
152 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
153 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Collection_LinkedList<T>>)
155 : Collection_LinkedList{}
157 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
158 AssertRepValidType_ ();
161 template <
typename T>
162 inline void Collection_LinkedList<T>::AssertRepValidType_ ()
const
165 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{
this};
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
#define RequireNotNull(p)
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
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 Remove(ArgByValueType< value_type > item, EQUALS_COMPARER &&equalsComparer={})
Remove () the argument value (which must exist)
nonvirtual void Add(ArgByValueType< value_type > item)
Collection_LinkedList<T> is an LinkedList-based concrete implementation of the Collection<T> containe...
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,...