8#include "Stroika/Foundation/Containers/Private/IterableUtils.h"
31 struct Sequence<T>::TemporaryElementReference_ : conditional_t<is_class_v<T> or is_union_v<T>, T, Common::Empty> {
33 static constexpr bool kSubClass_ = is_class_v<T> or is_union_v<T>;
36 [[no_unique_address]] conditional_t<kSubClass_, Common::Empty, T> fValue_;
39 TemporaryElementReference_ (
const TemporaryElementReference_&) =
default;
40 TemporaryElementReference_ (TemporaryElementReference_&& src)
42 , fIndex_{src.fIndex_}
43 , fValue_{move (src.fValue_)}
47 TemporaryElementReference_ (Sequence<T>* s,
size_t i)
51 if constexpr (kSubClass_) {
52 *
static_cast<T*
> (
this) = s->GetAt (i);
55 fValue_ = s->GetAt (i);
58 TemporaryElementReference_& operator= (
const TemporaryElementReference_&) =
delete;
59 TemporaryElementReference_& operator= (TemporaryElementReference_&&) =
delete;
60 TemporaryElementReference_& operator= (ArgByValueType<T> v)
63 if constexpr (kSubClass_) {
64 *
static_cast<T*
> (
this) = v;
72 requires (not(kSubClass_))
75 if constexpr (kSubClass_) {
76 return *
static_cast<T*
> (
this);
83 requires (not(kSubClass_))
86 if constexpr (kSubClass_) {
87 return *
static_cast<T*
> (
this);
105 ~TemporaryElementReference_ ()
113 if constexpr (kSubClass_) {
114 IgnoreExceptionsForCall (fV->SetAt (fIndex_, *((T*)
this)));
117 IgnoreExceptionsForCall (fV->SetAt (fIndex_, fValue_));
128 template <
typename T>
130 :
inherited{Factory::Sequence_Factory<T>::Default () ()}
132 _AssertRepValidType ();
134 template <
typename T>
139 _AssertRepValidType ();
141#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
142 template <
typename T>
143 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
144 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Sequence<T>>)
148 AppendAll (forward<ITERABLE_OF_ADDABLE> (src));
149 _AssertRepValidType ();
152 template <
typename T>
156 _AssertRepValidType ();
158 template <
typename T>
162 _AssertRepValidType ();
164 template <
typename T>
165 template <IInputIterator<T> ITERATOR_OF_ADDABLE, sentinel_for<remove_cvref_t<ITERATOR_OF_ADDABLE>> ITERATOR_OF_ADDABLE2>
169 AppendAll (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE2> (
end));
170 _AssertRepValidType ();
172 template <
typename T>
173 template <
typename RESULT_CONTAINER, invocable<T> ELEMENT_MAPPER>
175 requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, T>,
typename RESULT_CONTAINER::value_type> or
176 convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, optional<typename RESULT_CONTAINER::value_type>>)
178 if constexpr (same_as<RESULT_CONTAINER, Sequence>) {
180 return inherited::template Map<RESULT_CONTAINER> (forward<ELEMENT_MAPPER> (elementMapper),
181 RESULT_CONTAINER{_SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().CloneEmpty ()});
184 return inherited ::template Map<RESULT_CONTAINER> (forward<ELEMENT_MAPPER> (elementMapper));
187 template <
typename T>
188 template <derived_from<Iterable<T>> RESULT_CONTAINER, predicate<T> INCLUDE_PREDICATE>
191 if constexpr (same_as<RESULT_CONTAINER, Sequence>) {
193 return inherited::template Where<RESULT_CONTAINER> (
194 forward<INCLUDE_PREDICATE> (includeIfTrue), RESULT_CONTAINER{_SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().CloneEmpty ()});
197 return inherited::template Where<RESULT_CONTAINER> (forward<INCLUDE_PREDICATE> (includeIfTrue));
200 template <
typename T>
201 template <IPotentiallyComparer<T> INORDER_COMPARER_TYPE>
204 vector<T> tmp{this->begin (), Iterator<T>{this->end ()}};
205 stable_sort (tmp.begin (), tmp.end (), forward<INORDER_COMPARER_TYPE> (inorderComparer));
206 return Concrete::Sequence_stdvector<T>{move (tmp)};
208 template <
typename T>
211 _SafeReadRepAccessor<_IRep> accessor{
this};
212 if (not accessor._ConstGetRep ().empty ()) {
213 this->_fRep = accessor._ConstGetRep ().CloneEmpty ();
216 template <
typename T>
217 template <predicate<T> PREDICATE>
225 for (Iterator<T> i = this->begin (); i != this->end ();) {
236 template <
typename T>
239 _SafeReadRepAccessor<_IRep> accessor{
this};
240 Require (i < accessor._ConstGetRep ().size ());
241 return accessor._ConstGetRep ().GetAt (i);
243 template <
typename T>
246 _SafeReadWriteRepAccessor<_IRep> accessor{
this};
247 Require (i < accessor._ConstGetRep ().size ());
248 accessor._GetWriteableRep ().SetAt (i, item);
250 template <
typename T>
253 _SafeReadRepAccessor<_IRep> accessor{
this};
254 Require (i < accessor._ConstGetRep ().size ());
255 return accessor._ConstGetRep ().GetAt (i);
257 template <
typename T>
260 return TemporaryElementReference_{
this, i};
262 template <
typename T>
263 template <Common::IEqualsComparer<T> EQUALS_COMPARER>
264 inline optional<size_t>
Sequence<T>::IndexOf (ArgByValueType<value_type> item, EQUALS_COMPARER&& equalsComparer)
const
266 return Private::IndexOf_<T, EQUALS_COMPARER> (*
this, item, forward<EQUALS_COMPARER> (equalsComparer));
268 template <
typename T>
269 template <Common::IEqualsComparer<T> EQUALS_COMPARER>
270 inline optional<size_t>
Sequence<T>::IndexOf (
const Sequence& s, EQUALS_COMPARER&& equalsComparer)
const
272 return Private::IndexOf_<T, EQUALS_COMPARER> (*
this, s, forward<EQUALS_COMPARER> (equalsComparer));
274 template <
typename T>
275 template <
typename IGNORED>
278 return _SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().IndexOf (i);
280 template <
typename T>
283 _SafeReadWriteRepAccessor<_IRep> accessor{
this};
284 Require (i <= accessor._ConstGetRep ().size ());
285 return accessor._GetWriteableRep ().Insert (i, &item, &item + 1);
287 template <
typename T>
290 _SafeReadWriteRepAccessor<_IRep> accessor{
this};
291 size_t idx = accessor._ConstGetRep ().IndexOf (i);
292 Require (idx <= accessor._ConstGetRep ().size ());
293 return accessor._GetWriteableRep ().Insert (idx, &item, &item + 1);
295 template <
typename T>
296 template <IInputIterator<T> ITERATOR_OF_ADDABLE, sentinel_for<ITERATOR_OF_ADDABLE> ITERATOR_OF_ADDABLE2>
299 Require (i <= this->size ());
301 for (
auto ii = forward<ITERATOR_OF_ADDABLE> (start); ii != forward<ITERATOR_OF_ADDABLE2> (end); ++ii) {
302 Insert (insertAt++, *ii);
305 template <
typename T>
306 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
309 Require (i <= this->size ());
310 InsertAll (i, s.begin (), s.end ());
312 template <
typename T>
317 template <
typename T>
318 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
321 InsertAll (0, forward<ITERABLE_OF_ADDABLE> (s));
323 template <
typename T>
324 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
327 InsertAll (0, forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
329 template <
typename T>
332 _SafeReadWriteRepAccessor<_IRep>{
this}._GetWriteableRep ().Insert (_IRep::_kSentinelLastItemIndex, &item, &item + 1);
334 template <
typename T>
335 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
338 AppendAll (s.begin (), s.end ());
340 template <
typename T>
341 template <IInputIterator<T> ITERATOR_OF_ADDABLE, sentinel_for<remove_cvref_t<ITERATOR_OF_ADDABLE>> ITERATOR_OF_ADDABLE2>
344 _SafeReadWriteRepAccessor<_IRep> accessor = {
this};
345 for (
auto i = forward<ITERATOR_OF_ADDABLE> (start); i != forward<ITERATOR_OF_ADDABLE2> (end); ++i) {
347 accessor._GetWriteableRep ().Insert (_IRep::_kSentinelLastItemIndex, &tmp, &tmp + 1);
350 template <
typename T>
353 Require (not i.
Done ());
354 auto [writerRep, patchedIterator] = _GetWritableRepAndPatchAssociatedIterator (i);
355 writerRep->Update (patchedIterator, newValue, nextI);
357 template <
typename T>
360 Require (i < this->size ());
361 _SafeReadWriteRepAccessor<_IRep>{
this}._GetWriteableRep ().Remove (i, i + 1);
363 template <
typename T>
366 Require (start <= end and end <= this->size ());
367 _SafeReadWriteRepAccessor<_IRep>{
this}._GetWriteableRep ().Remove (start, end);
369 template <
typename T>
370 inline void Sequence<T>::Remove (
const Iterator<value_type>& i, Iterator<value_type>* nextI)
372 Require (not i.Done ());
373 auto [writerRep, patchedIterator] = _GetWritableRepAndPatchAssociatedIterator (i);
374 writerRep->Remove (patchedIterator, nextI);
376 template <
typename T>
377 template <
typename CONTAINER_OF_ADDABLE>
381 if constexpr (derived_from<CONTAINER_OF_ADDABLE, Iterable<T>>) {
382 return CONTAINER_OF_ADDABLE{this->begin (), this->end ()};
385 return CONTAINER_OF_ADDABLE{this->begin (), Iterator<T>{this->end ()}};
388 template <
typename T>
389 template <
typename CONTAINER_OF_ADDABLE>
390 inline void Sequence<T>::As (CONTAINER_OF_ADDABLE* into)
const
394 if constexpr (derived_from<CONTAINER_OF_ADDABLE, Iterable<T>>) {
395 *into = CONTAINER_OF_ADDABLE{this->begin (), this->end ()};
399 *into = CONTAINER_OF_ADDABLE{this->begin (), Iterator<T>{this->end ()}};
402 template <
typename T>
405 return this->empty () ? optional<T>{} : GetAt (0);
407 template <
typename T>
408 inline auto Sequence<T>::First (
const function<
bool (ArgByValueType<value_type>)>& that)
const -> optional<value_type>
410 return inherited::First (that);
412 template <
typename T>
415 return this->empty () ? defaultValue : GetAt (0);
417 template <
typename T>
421 return this->empty () ? optional<T>{} : _SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().GetAt (_IRep::_kSentinelLastItemIndex);
423 template <
typename T>
424 inline auto Sequence<T>::Last (
const function<
bool (ArgByValueType<value_type>)>& that)
const -> optional<value_type>
427 return inherited::Last (that);
429 template <
typename T>
433 return this->empty () ? defaultValue : _SafeReadRepAccessor<_IRep>{
this}._ConstGetRep ().GetAt (_IRep::_kSentinelLastItemIndex);
435 template <
typename T>
440 template <
typename T>
445 template <
typename T>
450 template <
typename T>
455 template <
typename T>
460 template <
typename T>
464 this->Remove (i, &nextI);
467 template <
typename T>
473 template <
typename T>
479 template <
typename T>
482 Require (not i.Done ());
483 using element_type =
typename inherited::_SharedByValueRepType::element_type;
485 element_type* writableRep = this->_fRep.rwget ([&] (
const element_type& prevRepPtr) ->
typename inherited::_SharedByValueRepType::shared_ptr_type {
486 return Debug::UncheckedDynamicCast<const _IRep&> (prevRepPtr).CloneAndPatchIterator (&patchedIterator);
489 return make_tuple (Debug::UncheckedDynamicCast<_IRep*> (writableRep), move (patchedIterator));
491 template <
typename T>
492 inline void Sequence<T>::_AssertRepValidType ()
const
495 _SafeReadRepAccessor<_IRep>{
this};
498 template <
typename T>
500 requires (equality_comparable<T>)
504 template <
typename T>
506 requires (three_way_comparable<T>)
508 return ThreeWayComparer<>{}(*
this, rhs);
516 template <
typename T>
523 template <
typename T>
524 Sequence<T>
operator+ (
const Sequence<T>& lhs,
const Iterable<T>& rhs)
526 Sequence<T> result{lhs};
530 template <
typename T>
531 Sequence<T>
operator+ (
const Sequence<T>& lhs,
const Sequence<T>& rhs)
533 Sequence<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 generalization of a vector: a container whose elements are keyed by the natural numbers.
nonvirtual value_type FirstValue(ArgByValueType< value_type > defaultValue={}) const
nonvirtual void InsertAll(size_t i, ITERATOR_OF_ADDABLE &&start, ITERATOR_OF_ADDABLE2 &&end)
Insert all the given items into this sequence, starting at offset 'i'.
nonvirtual void erase(size_t i)
nonvirtual Sequence OrderBy(INORDER_COMPARER_TYPE &&inorderComparer=INORDER_COMPARER_TYPE{}) const
nonvirtual TemporaryElementReference_ operator()(size_t i)
operator() is similar to operator[] - but returning TemporaryElementReference_, and so is updatable/w...
nonvirtual void PrependAll(ITERABLE_OF_ADDABLE &&s)
nonvirtual void Remove(size_t i)
typename Iterable< value_type >::template SequentialEqualsComparer< T_EQUALS_COMPARER > EqualsComparer
nonvirtual void push_back(ArgByValueType< value_type > item)
typename inherited::value_type value_type
nonvirtual bool operator==(const Sequence &rhs) const
nonvirtual optional< value_type > First() const
nonvirtual auto operator<=>(const Sequence &rhs) const
nonvirtual void Insert(size_t i, ArgByValueType< value_type > item)
nonvirtual optional< size_t > IndexOf(ArgByValueType< value_type > i, EQUALS_COMPARER &&equalsComparer={}) const
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to Sequence, and improve that cas...
nonvirtual void RemoveAll()
RemoveAll removes all, or all matching (predicate, iterator range, equals comparer or whatever) items...
nonvirtual value_type LastValue(ArgByValueType< value_type > defaultValue={}) const
nonvirtual RESULT_CONTAINER Where(INCLUDE_PREDICATE &&includeIfTrue) const
nonvirtual void SetAt(size_t i, ArgByValueType< value_type > item)
nonvirtual void Prepend(ArgByValueType< value_type > item)
nonvirtual value_type back() const
nonvirtual Sequence & operator+=(ArgByValueType< value_type > item)
Alias for Append/AppendAll ().
nonvirtual value_type GetAt(size_t i) const
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 void Update(const Iterator< value_type > &i, ArgByValueType< value_type > newValue, Iterator< value_type > *nextI=nullptr)
nonvirtual optional< value_type > Last() const
nonvirtual const value_type operator[](size_t i) const
alias for GetAt (i) - but returning const T
nonvirtual void AppendAll(ITERABLE_OF_ADDABLE &&s)
nonvirtual void Append(ArgByValueType< value_type > item)
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
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 Done() const
Done () means there is nothing left in this iterator (a synonym for (it == container....
Collection< T > operator+(const Iterable< T > &lhs, const Collection< T > &rhs)