26 _AssertRepValidType ();
29 template <IInputIterator<T> ITERATOR_OF_ADDABLE, sentinel_for<remove_cvref_t<ITERATOR_OF_ADDABLE>> ITERATOR_OF_ADDABLE2>
34 _AssertRepValidType ();
41 _AssertRepValidType ();
47 _AssertRepValidType ();
54 _AssertRepValidType ();
56#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
58 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
59 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Collection<T>>)
63 AddAll (forward<ITERABLE_OF_ADDABLE> (src));
64 _AssertRepValidType ();
68 template <Common::IEqualsComparer<T> EQUALS_COMPARER>
71 return this->Find (item, forward<EQUALS_COMPARER> (equalsComparer)) != this->end ();
74 template <IInputIterator<T> ITERATOR_OF_ADDABLE, sentinel_for<remove_cvref_t<ITERATOR_OF_ADDABLE>> ITERATOR_OF_ADDABLE2>
77 _SafeReadWriteRepAccessor<_IRep> tmp{
this};
87 if constexpr (contiguous_iterator<remove_cvref_t<ITERATOR_OF_ADDABLE>> and
88 sized_sentinel_for<remove_cvref_t<ITERATOR_OF_ADDABLE2>, remove_cvref_t<ITERATOR_OF_ADDABLE>> and
89 same_as<remove_cvref_t<iter_value_t<remove_cvref_t<ITERATOR_OF_ADDABLE>>>, value_type>) {
90 if (start != end) [[likely]] {
91 tmp._GetWriteableRep ().Add (span<const value_type>{to_address (start),
static_cast<size_t> (end - start)},
nullptr);
103 Memory::StackBuffer<value_type> buf;
104 for (
auto i = forward<ITERATOR_OF_ADDABLE> (start); i != forward<ITERATOR_OF_ADDABLE2> (end); ++i) {
106 if (buf.size () == kChunkSize_) [[unlikely]] {
107 tmp._GetWriteableRep ().Add (span<const value_type>{buf.begin (), buf.size ()},
nullptr);
111 if (buf.size () != 0) [[likely]] {
112 tmp._GetWriteableRep ().Add (span<const value_type>{buf.begin (), buf.size ()},
nullptr);
116 template <
typename T>
117 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
120 if constexpr (std::is_convertible_v<remove_cvref_t<ITERABLE_OF_ADDABLE>*, Collection<value_type>*>) {
122 if (
static_cast<const Iterable<value_type>*
> (
this) ==
static_cast<const Iterable<value_type>*
> (&items)) [[unlikely]] {
124 vector<value_type> copy{std::begin (items), Iterator<value_type>{std::end (items)}};
125 AddAll (std::begin (copy), std::end (copy));
145 if constexpr (derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Iterable<value_type>>) {
146 if (
static_cast<const Iterable<value_type>*
> (
this) !=
static_cast<const Iterable<value_type>*
> (&items)) [[likely]] {
147 bool handled =
false;
149 _SafeReadWriteRepAccessor<_IRep> destAccessor{
this};
150 _IRep& destRep = destAccessor._GetWriteableRep ();
152 _SafeReadRepAccessor<typename Iterable<value_type>::_IRep> srcAccessor{&items};
153 if (
auto srcSpan = srcAccessor._ConstGetRep ().PeekContiguousStorage ()) {
154 if (not srcSpan->empty ()) [[likely]] {
155 destRep.Add (*srcSpan,
nullptr);
165 AddAll (std::begin (items), std::end (items));
167 template <
typename T>
170 _SafeReadWriteRepAccessor<_IRep>{
this}._GetWriteableRep ().Add (span<const value_type>{&item, 1},
nullptr);
171 Ensure (not this->empty ());
173 template <
typename T>
177 _SafeReadWriteRepAccessor<_IRep>{
this}._GetWriteableRep ().Add (span<const value_type>{&item, 1}, addedAt);
178 Ensure (not this->empty ());
179 Ensure (not addedAt->Done ());
181 template <
typename T>
184 Require (not i.
AtEnd ());
185 auto [writerRep, patchedIterator] = _GetWritableRepAndPatchAssociatedIterator (i);
186 writerRep->Update (patchedIterator, newValue, nextI);
188 template <
typename T>
191 Require (not i.
AtEnd ());
192 auto [writerRep, patchedIterator] = _GetWritableRepAndPatchAssociatedIterator (i);
193 writerRep->Remove (patchedIterator, nextI);
195 template <
typename T>
196 template <Common::IPotentiallyComparer<T> EQUALS_COMPARER>
199 auto i = this->Find (item, forward<EQUALS_COMPARER> (equalsComparer));
200 Require (i != this->end ());
201 auto [writerRep, patchedIterator] = _GetWritableRepAndPatchAssociatedIterator (i);
202 writerRep->Remove (patchedIterator,
nullptr);
204 template <
typename T>
205 template <Common::IEqualsComparer<T> EQUALS_COMPARER>
208 if (
auto i = this->Find (item, forward<EQUALS_COMPARER> (equalsComparer))) {
209 auto [writerRep, patchedIterator] = _GetWritableRepAndPatchAssociatedIterator (i);
210 writerRep->Remove (patchedIterator,
nullptr);
215 template <
typename T>
216 template <predicate<T> PREDICATE>
219 if (
auto i = this->Find (forward<PREDICATE> (p))) {
225 template <
typename T>
228 _SafeReadRepAccessor<_IRep> tmp{
this};
229 if (not tmp._ConstGetRep ().empty ()) {
230 this->_fRep = tmp._ConstGetRep ().CloneEmpty ();
233 template <
typename T>
234 template <Common::IEqualsComparer<T> EQUALS_COMPARER>
238 for (
auto i = start; i != end;) {
239 if (RemoveIf (*i, equalsComparer, &i)) {
245 template <
typename T>
246 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE,
typename EQUALS_COMPARER>
249 if (
static_cast<const void*
> (
this) ==
static_cast<const void*
> (addressof (c))) {
250 return RemoveAll (forward<EQUALS_COMPARER> (equalsComparer));
253 return RemoveAll (std::begin (c), std::end (c), forward<EQUALS_COMPARER> (equalsComparer));
256 template <
typename T>
257 template <predicate<T> PREDICATE>
261 for (Iterator<T> i = this->begin (); i != this->end ();) {
272 template <
typename T>
277 template <
typename T>
278 template <Common::IEqualsComparer<T> EQUALS_COMPARER>
279 inline void Collection<T>::erase (ArgByValueType<value_type> item, EQUALS_COMPARER&& equalsComparer)
281 Remove (item, forward<EQUALS_COMPARER> (equalsComparer));
283 template <
typename T>
286 Iterator<value_type> nextI{
nullptr};
290 template <
typename T>
291 template <
typename RESULT_CONTAINER, invocable<T> ELEMENT_MAPPER>
293 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, T>,
typename RESULT_CONTAINER::value_type> or
294 convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, optional<typename RESULT_CONTAINER::value_type>>)
296 if constexpr (same_as<RESULT_CONTAINER, Collection>) {
298 return inherited::template Map<RESULT_CONTAINER> (forward<ELEMENT_MAPPER> (elementMapper),
299 RESULT_CONTAINER{_SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().CloneEmpty ()});
302 return inherited::template Map<RESULT_CONTAINER> (forward<ELEMENT_MAPPER> (elementMapper));
305 template <
typename T>
306 template <derived_from<Iterable<T>> RESULT_CONTAINER, predicate<T> INCLUDE_PREDICATE>
309 if constexpr (same_as<RESULT_CONTAINER, Collection>) {
311 return inherited::template Where<RESULT_CONTAINER> (
312 forward<INCLUDE_PREDICATE> (includeIfTrue), RESULT_CONTAINER{_SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().CloneEmpty ()});
315 return inherited::template Where<RESULT_CONTAINER> (forward<INCLUDE_PREDICATE> (includeIfTrue));
318 template <
typename T>
324 template <
typename T>
330 template <
typename T>
333 Require (not i.AtEnd ());
334 using element_type =
typename inherited::_SharedByValueRepType::element_type;
336 element_type* writableRep = this->_fRep.rwget ([&] (
const element_type& prevRepPtr) ->
typename inherited::_SharedByValueRepType::shared_ptr_type {
337 return Debug::UncheckedDynamicCast<const _IRep&> (prevRepPtr).CloneAndPatchIterator (&patchedIterator);
340 return make_tuple (Debug::UncheckedDynamicCast<_IRep*> (writableRep), move (patchedIterator));
342 template <
typename T>
343 inline void Collection<T>::_AssertRepValidType ()
const
346 _SafeReadRepAccessor<_IRep>{
this};
355 template <
typename T>
362 template <
typename T>
363 Collection<T>
operator+ (
const Collection<T>& lhs,
const Iterable<T>& rhs)
365 Collection<T> result{lhs};
369 template <
typename T>
370 Collection<T>
operator+ (
const Collection<T>& lhs,
const Collection<T>& rhs)
372 Collection<T> result{lhs};
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
#define RequireNotNull(p)
#define RequireExpression(c)
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)
typename inherited::value_type value_type
nonvirtual void Update(const Iterator< value_type > &i, ArgByValueType< value_type > newValue, Iterator< value_type > *nextI=nullptr)
nonvirtual bool Contains(ArgByValueType< value_type > item, EQUALS_COMPARER &&equalsComparer={}) const
Compares items with TRAITS::EqualsCompareFunctionType::Equals, and returns true if any match.
nonvirtual void Remove(ArgByValueType< value_type > item, EQUALS_COMPARER &&equalsComparer={})
Remove () the argument value (which must exist)
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to Collection,...
nonvirtual void erase(ArgByValueType< value_type > item, EQUALS_COMPARER &&equalsComparer={})
nonvirtual bool RemoveIf(ArgByValueType< value_type > item, EQUALS_COMPARER &&equalsComparer={})
RemoveIf () the (the first matching) argument value, if present. Returns true if item removed.
nonvirtual RESULT_CONTAINER Where(INCLUDE_PREDICATE &&includeIfTrue) const
return a subset of the Collection for which includeIfTrue returns true.
nonvirtual void Add(ArgByValueType< value_type > item)
nonvirtual tuple< _IRep *, Iterator< value_type > > _GetWritableRepAndPatchAssociatedIterator(const Iterator< value_type > &i)
Utility to get WRITABLE underlying shared_ptr (replacement for what we normally do - _SafeReadWriteRe...
nonvirtual Collection & operator+=(ArgByValueType< value_type > item)
nonvirtual void RemoveAll()
RemoveAll removes all, or all matching (predicate, iterator range, equals comparer or whatever) items...
static constexpr size_t kMinCapacity
Implementation detail for iterator implementors.
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
nonvirtual CONTAINER_OF_T As(CONTAINER_OF_T_CONSTRUCTOR_ARGS... args) const
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.
An Iterator<T> is a copyable object which allows traversing the contents of some container.
nonvirtual bool AtEnd() const
AtEnd () means there is nothing left in this iterator (a synonym for (it == container....
Collection< T > operator+(const Iterable< T > &lhs, const Collection< T > &rhs)