4#include "Stroika/Foundation/Containers/DataStructures/Array.h"
6#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
18 class Collection_Array<T>::Rep_ :
public Collection_Array<T>::IImplRep_,
public Memory::UseBlockAllocationIfAppropriate<Rep_> {
20 using inherited =
typename Collection<T>::_IRep;
24 Rep_ (
const Rep_& from) =
default;
27 nonvirtual Rep_& operator= (
const Rep_&) =
delete;
31 virtual shared_ptr<typename Iterable<T>::_IRep> Clone ()
const override
34 return Memory::MakeSharedPtr<Rep_> (*
this);
36 virtual Iterator<value_type>
MakeIterator ()
const override
39 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
41 virtual size_t size ()
const override
44 return fData_.size ();
46 virtual bool empty ()
const override
49 return fData_.empty ();
54 fData_.Apply (doToElement, seq);
56 virtual Iterator<value_type>
Find (
const function<
bool (ArgByValueType<value_type> item)>& that,
60 if (
auto i = fData_.Find (that)) {
61 return Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, i)};
68 virtual shared_ptr<typename Collection<T>::_IRep> CloneEmpty ()
const override
71 return Memory::MakeSharedPtr<Rep_> ();
73 virtual shared_ptr<typename Collection<T>::_IRep> CloneAndPatchIterator (Iterator<value_type>* i)
const override
77 auto result = Memory::MakeSharedPtr<Rep_> (*
this);
78 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i->ConstGetRep ());
79 result->fData_.MoveIteratorHereAfterClone (&mir.fIterator, &fData_);
83 virtual void Add (ArgByValueType<value_type> item, Iterator<value_type>* oAddedI)
override
87 fData_.push_back (item);
88 fChangeCounts_.PerformedChange ();
89 if (oAddedI !=
nullptr) [[unlikely]] {
90 *oAddedI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, fData_.size () - 1)};
93 virtual void Update (
const Iterator<value_type>& i, ArgByValueType<value_type> newValue, Iterator<value_type>* nextI)
override
96 optional<typename DataStructureImplType_::UnderlyingIteratorRep> savedUnderlyingIndex;
97 if (nextI !=
nullptr) {
98 savedUnderlyingIndex = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator.GetUnderlyingIteratorRep ();
100 fData_.SetAt (Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ()).fIterator, newValue);
101 fChangeCounts_.PerformedChange ();
102 if (nextI !=
nullptr) {
103 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, *savedUnderlyingIndex)};
106 virtual void Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
override
109 auto& mir = Debug::UncheckedDynamicCast<const IteratorRep_&> (i.ConstGetRep ());
110 if (nextI ==
nullptr) {
111 fData_.Remove (mir.fIterator);
112 fChangeCounts_.PerformedChange ();
115 auto retI = fData_.erase (mir.fIterator);
116 fChangeCounts_.PerformedChange ();
117 *nextI = Iterator<value_type>{make_unique<IteratorRep_> (&fChangeCounts_, retI)};
122 using DataStructureImplType_ = DataStructures::Array<value_type>;
123 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
126 DataStructureImplType_ fData_;
127 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
135 template <
typename T>
137 :
inherited{Memory::MakeSharedPtr<Rep_> ()}
139 AssertRepValidType_ ();
141 template <
typename T>
142 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
146 if constexpr (random_access_iterator<ITERATOR_OF_ADDABLE>) {
151 this->
AddAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (
end));
152 AssertRepValidType_ ();
154 template <
typename T>
160 AssertRepValidType_ ();
162#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
163 template <
typename T>
164 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
165 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Collection_Array<T>>)
169 reserve (src.size ());
170 this->AddAll (forward<ITERABLE_OF_ADDABLE> (src));
171 AssertRepValidType_ ();
174 template <
typename T>
175 inline void Collection_Array<T>::AssertRepValidType_ ()
const
178 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)
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_Array<T> is an Array-based concrete implementation of the Collection<T> container pattern.
nonvirtual void reserve(size_t slotsAlloced)
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,...