10#ifndef XTENSOR_STRIDED_VIEW_HPP
11#define XTENSOR_STRIDED_VIEW_HPP
19#include <xtl/xsequence.hpp>
21#include "../containers/xstorage.hpp"
22#include "../core/xexpression.hpp"
23#include "../core/xiterable.hpp"
24#include "../core/xlayout.hpp"
25#include "../core/xsemantic.hpp"
26#include "../utils/xutils.hpp"
27#include "../views/xstrided_view_base.hpp"
37 template <
class Tag,
class CT,
class S, layout_type L,
class FST>
40 template <
class CT,
class S, layout_type L,
class FST>
46 template <
class CT,
class S, layout_type L,
class FST>
51 template <
class CT,
class S, layout_type L,
class FST>
55 template <layout_type L1, layout_type L2,
class T>
58 using type = std::conditional_t<L1 == L2 && L1 != layout_type::dynamic, xcontiguous_iterable<T>,
xiterable<T>>;
61 template <layout_type L1, layout_type L2,
class T>
62 using select_iterable_base_t =
typename select_iterable_base<L1, L2, T>::type;
65 template <
class CT,
class S, layout_type L,
class FST>
68 template <
class CT,
class S, layout_type L,
class FST>
71 using xexpression_type = std::decay_t<CT>;
72 using undecay_expression = CT;
73 using reference = inner_reference_t<undecay_expression>;
74 using const_reference =
typename xexpression_type::const_reference;
75 using size_type =
typename xexpression_type::size_type;
76 using shape_type = std::decay_t<S>;
77 using undecay_shape = S;
78 using storage_getter = FST;
79 using inner_storage_type =
typename storage_getter::type;
80 using temporary_type =
typename detail::xtype_for_shape<
81 S>::template type<typename xexpression_type::value_type, xexpression_type::static_layout>;
82 using storage_type = std::remove_reference_t<inner_storage_type>;
86 template <
class CT,
class S, layout_type L,
class FST>
89 using inner_shape_type = std::decay_t<S>;
90 using inner_strides_type = get_strides_t<inner_shape_type>;
91 using inner_backstrides_type_type = inner_strides_type;
93 using const_stepper = std::conditional_t<
98 using stepper = std::conditional_t<
104 template <
class CT,
class S, layout_type L,
class FST,
class RHS>
127 template <
class CT,
class S, layout_type L = layout_type::dynamic,
class FST = detail::flat_storage_getter<CT, XTENSOR_DEFAULT_TRAVERSAL>>
129 :
public xview_semantic<xstrided_view<CT, S, L, FST>>,
130 public select_iterable_base_t<L, std::decay_t<CT>::static_layout, xstrided_view<CT, S, L, FST>>,
132 public extension::xstrided_view_base_t<CT, S, L, FST>
138 using semantic_base = xview_semantic<self_type>;
139 using extension_base = extension::xstrided_view_base_t<CT, S, L, FST>;
140 using expression_tag =
typename extension_base::expression_tag;
142 using xexpression_type =
typename base_type::xexpression_type;
143 using base_type::is_const;
145 using value_type =
typename base_type::value_type;
146 using reference =
typename base_type::reference;
147 using const_reference =
typename base_type::const_reference;
148 using pointer =
typename base_type::pointer;
149 using const_pointer =
typename base_type::const_pointer;
150 using size_type =
typename base_type::size_type;
151 using difference_type =
typename base_type::difference_type;
153 using inner_storage_type =
typename base_type::inner_storage_type;
154 using storage_type =
typename base_type::storage_type;
155 using linear_iterator =
typename storage_type::iterator;
156 using const_linear_iterator =
typename storage_type::const_iterator;
157 using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
158 using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
160 using iterable_base = select_iterable_base_t<L, xexpression_type::static_layout, self_type>;
161 using inner_shape_type =
typename base_type::inner_shape_type;
162 using inner_strides_type =
typename base_type::inner_strides_type;
163 using inner_backstrides_type =
typename base_type::inner_backstrides_type;
164 using shape_type =
typename base_type::shape_type;
165 using strides_type =
typename base_type::strides_type;
166 using backstrides_type =
typename base_type::backstrides_type;
168 using stepper =
typename iterable_base::stepper;
169 using const_stepper =
typename iterable_base::const_stepper;
171 using base_type::contiguous_layout;
172 using base_type::static_layout;
175 using base_index_type = xindex_type_t<shape_type>;
177 using data_alignment = xt_simd::container_alignment_t<storage_type>;
178 using simd_type = xt_simd::simd_type<value_type>;
179 using simd_value_type = xt_simd::simd_type<value_type>;
180 using bool_load_type =
typename base_type::bool_load_type;
185 template <
class CTA,
class SA>
190 self_type& operator=(
const self_type&);
196 disable_xexpression<E, self_type>& operator=(
const E& e);
200 using base_type::is_contiguous;
206 using base_type::operator();
208 using base_type::unchecked;
209 using base_type::operator[];
212 using base_type::element;
222 linear_iterator linear_begin();
223 linear_iterator linear_end();
224 const_linear_iterator linear_begin()
const;
225 const_linear_iterator linear_end()
const;
226 const_linear_iterator linear_cbegin()
const;
227 const_linear_iterator linear_cend()
const;
229 reverse_linear_iterator linear_rbegin();
230 reverse_linear_iterator linear_rend();
231 const_reverse_linear_iterator linear_rbegin()
const;
232 const_reverse_linear_iterator linear_rend()
const;
233 const_reverse_linear_iterator linear_crbegin()
const;
234 const_reverse_linear_iterator linear_crend()
const;
236 template <
class ST,
class STEP = stepper>
237 disable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape);
238 template <
class ST,
class STEP = stepper>
239 disable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l);
241 template <
class ST,
class STEP = stepper>
242 enable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape);
243 template <
class ST,
class STEP = stepper>
246 template <
class ST,
class STEP = const_stepper>
247 disable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape)
const;
248 template <
class ST,
class STEP = const_stepper>
249 disable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l)
const;
251 template <
class ST,
class STEP = const_stepper>
252 enable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape)
const;
253 template <
class ST,
class STEP = const_stepper>
254 enable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l)
const;
256 template <
class requested_type>
257 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
259 template <
class align,
class simd>
260 void store_simd(size_type i,
const simd& e)
261 requires provides_simd_interface;
263 template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
264 simd_return_type<requested_type> load_simd(size_type i)
const
265 requires provides_simd_interface;
267 reference data_element(size_type i);
268 const_reference data_element(size_type i)
const;
270 reference flat(size_type i);
271 const_reference flat(size_type i)
const;
273 using container_iterator = std::
274 conditional_t<is_const, typename storage_type::const_iterator, typename storage_type::iterator>;
275 using const_container_iterator =
typename storage_type::const_iterator;
281 rebind_t<E> build_view(E&& e)
const;
285 container_iterator data_xbegin() noexcept;
286 const_container_iterator data_xbegin() const noexcept;
287 container_iterator data_xend(
layout_type l, size_type offset) noexcept;
288 const_container_iterator data_xend(
layout_type l, size_type offset) const noexcept;
291 It data_xbegin_impl(It begin) const noexcept;
294 It data_xend_impl(It end,
layout_type l, size_type offset) const noexcept;
296 void assign_temporary_impl(temporary_type&& tmp);
298 using base_type::set_offset;
301 friend class xstepper;
302 friend class xview_semantic<self_type>;
306 friend class xaxis_iterator;
308 friend class xaxis_slice_iterator;
316 using xstrided_slice = std::variant<
328 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, placeholders::xtuph>,
366 template <class CT, class S,
layout_type L, class FST>
367 template <class CTA, class SA>
375 : base_type(std::forward<CTA>(e), std::forward<SA>(
shape), std::move(
strides), offset,
layout)
381 template <
class CT,
class S, layout_type L,
class FST>
382 inline auto xstrided_view<CT, S, L, FST>::operator=(
const self_type& rhs) -> self_type&
395 template <
class CT,
class S, layout_type L,
class FST>
397 inline auto xstrided_view<CT, S, L, FST>::operator=(
const xexpression<E>& e) -> self_type&
399 return semantic_base::operator=(e);
404 template <
class CT,
class S, layout_type L,
class FST>
406 inline auto xstrided_view<CT, S, L, FST>::operator=(
const E& e) -> disable_xexpression<E, self_type>&
412 namespace xstrided_view_detail
414 template <
class V,
class T>
415 inline void run_assign_temporary_impl(V& v,
const T& t, std::true_type )
417 strided_loop_assigner<true>::run(v, t);
420 template <
class V,
class T>
422 run_assign_temporary_impl(V& v,
const T& t, std::false_type )
424 std::copy(t.cbegin(), t.cend(), v.begin());
428 template <
class CT,
class S, layout_type L,
class FST>
432 fast_assign = xassign_traits<xstrided_view<CT, S, L, FST>, temporary_type>::simd_strided_assign();
433 xstrided_view_detail::run_assign_temporary_impl(*
this, tmp, std::integral_constant<bool, fast_assign>{});
445 template <
class CT,
class S, layout_type L,
class FST>
451 std::fill(this->linear_begin(), this->linear_end(), value);
455 std::fill(this->begin(), this->end(), value);
461 template <
class CT,
class S, layout_type L,
class FST>
462 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) -> reference
467 template <
class CT,
class S, layout_type L,
class FST>
468 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i)
const -> const_reference
473 template <
class CT,
class S, layout_type L,
class FST>
479 template <
class CT,
class S, layout_type L,
class FST>
485 template <
class CT,
class S, layout_type L,
class FST>
491 template <
class CT,
class S, layout_type L,
class FST>
497 template <
class CT,
class S, layout_type L,
class FST>
500 return this->linear_cbegin();
503 template <
class CT,
class S, layout_type L,
class FST>
506 return this->linear_cend();
509 template <
class CT,
class S, layout_type L,
class FST>
515 template <
class CT,
class S, layout_type L,
class FST>
521 template <
class CT,
class S, layout_type L,
class FST>
524 return reverse_linear_iterator(this->linear_begin());
527 template <
class CT,
class S, layout_type L,
class FST>
530 return reverse_linear_iterator(this->linear_end());
533 template <
class CT,
class S, layout_type L,
class FST>
536 return this->linear_crbegin();
539 template <
class CT,
class S, layout_type L,
class FST>
542 return this->linear_crend();
545 template <
class CT,
class S, layout_type L,
class FST>
548 return const_reverse_linear_iterator(this->linear_cbegin());
551 template <
class CT,
class S, layout_type L,
class FST>
554 return const_reverse_linear_iterator(this->linear_cend());
561 template <
class CT,
class S, layout_type L,
class FST>
562 template <
class ST,
class STEP>
566 return stepper(
this, data_xbegin(), offset);
569 template <
class CT,
class S, layout_type L,
class FST>
570 template <
class ST,
class STEP>
572 -> disable_indexed_stepper_t<STEP>
575 return stepper(
this, data_xend(l, offset), offset);
578 template <
class CT,
class S, layout_type L,
class FST>
579 template <
class ST,
class STEP>
583 return stepper(
this, offset);
586 template <
class CT,
class S, layout_type L,
class FST>
587 template <
class ST,
class STEP>
589 -> enable_indexed_stepper_t<STEP>
592 return stepper(
this, offset,
true);
595 template <
class CT,
class S, layout_type L,
class FST>
596 template <
class ST,
class STEP>
598 -> disable_indexed_stepper_t<STEP>
601 return const_stepper(
this, data_xbegin(), offset);
604 template <
class CT,
class S, layout_type L,
class FST>
605 template <
class ST,
class STEP>
607 -> disable_indexed_stepper_t<STEP>
610 return const_stepper(
this, data_xend(l, offset), offset);
613 template <
class CT,
class S, layout_type L,
class FST>
614 template <
class ST,
class STEP>
616 -> enable_indexed_stepper_t<STEP>
619 return const_stepper(
this, offset);
622 template <
class CT,
class S, layout_type L,
class FST>
623 template <
class ST,
class STEP>
625 -> enable_indexed_stepper_t<STEP>
628 return const_stepper(
this, offset,
true);
631 template <
class CT,
class S, layout_type L,
class FST>
635 return begin +
static_cast<std::ptrdiff_t
>(this->
data_offset());
638 template <
class CT,
class S, layout_type L,
class FST>
643 return strided_data_end(*
this, begin + std::ptrdiff_t(this->
data_offset()), l, offset);
646 template <
class CT,
class S, layout_type L,
class FST>
649 return data_xbegin_impl(this->
storage().begin());
652 template <
class CT,
class S, layout_type L,
class FST>
655 return data_xbegin_impl(this->
storage().cbegin());
658 template <
class CT,
class S, layout_type L,
class FST>
660 -> container_iterator
662 return data_xend_impl(this->
storage().begin(), l, offset);
665 template <
class CT,
class S, layout_type L,
class FST>
667 -> const_container_iterator
669 return data_xend_impl(this->
storage().cbegin(), l, offset);
672 template <
class CT,
class S, layout_type L,
class FST>
673 template <
class alignment,
class simd>
675 requires provides_simd_interface
677 using align_mode = driven_align_mode_t<alignment, data_alignment>;
678 xt_simd::store_as(&(
storage()[i]), e, align_mode());
681 template <
class CT,
class S, layout_type L,
class FST>
682 template <
class alignment,
class requested_type, std::
size_t N>
684 requires provides_simd_interface
686 using align_mode = driven_align_mode_t<alignment, data_alignment>;
687 return xt_simd::load_as<requested_type>(&(
storage()[i]), align_mode());
690 template <
class CT,
class S, layout_type L,
class FST>
694 inner_shape_type sh(this->
shape());
695 inner_strides_type str(this->
strides());
725 template <layout_type L,
class E,
class S,
class X>
729 return view_type(std::forward<E>(e), std::forward<S>(shape), std::forward<X>(
strides), offset,
layout);
734 struct no_adj_strides_policy
738 inline void resize(std::size_t)
742 inline void set_fake_slice(std::size_t)
746 template <
class ST,
class S>
748 const xstrided_slice_vector& ,
790 detail::strided_view_args<detail::no_adj_strides_policy> args;
793 detail::get_strides<XTENSOR_DEFAULT_TRAVERSAL>(e),
794 detail::get_offset<XTENSOR_DEFAULT_TRAVERSAL>(e),
801 std::move(args.new_shape),
802 std::move(args.new_strides),
810 template <
typename S>
813 template <std::size_t... X>
814 struct rebind_shape<
xt::fixed_shape<X...>>
822 using type = rebind_container_t<size_t, S>;
826 inline void recalculate_shape_impl(S& shape,
size_t size)
828 if constexpr (std::is_signed_v<get_value_type_t<typename std::decay<S>::type>>)
830 using value_type = get_value_type_t<typename std::decay_t<S>>;
831 XTENSOR_ASSERT(std::count(shape.cbegin(), shape.cend(), -1) <= 1);
832 auto iter = std::find(shape.begin(), shape.end(), -1);
833 if (iter != std::end(shape))
835 const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies<int>{});
836 const auto missing_dimension = size /
static_cast<size_t>(total);
837 (*iter) =
static_cast<value_type
>(missing_dimension);
843 inline auto recalculate_shape(S&& shape,
size_t size)
845 return recalculate_shape_impl(shape, size);
849 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class S>
850 inline auto reshape_view(E&& e, S&&
shape)
854 "traversal has to be row or column major"
857 using shape_type = std::decay_t<
decltype(
shape)>;
858 using unsigned_shape_type =
typename detail::rebind_shape<shape_type>::type;
859 get_strides_t<unsigned_shape_type>
strides;
861 detail::recalculate_shape(
shape, e.size());
864 constexpr auto computed_layout = std::decay_t<E>::static_layout == L ? L :
layout_type::dynamic;
869 detail::flat_adaptor_getter<xclosure_t<E>, L>>;
872 xtl::forward_sequence<unsigned_shape_type, S>(
shape),
892 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class S>
895 return reshape_view<L>(std::forward<E>(e), std::forward<S>(shape));
898 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class I, std::
size_t N>
901 using shape_type = std::array<std::size_t, N>;
902 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type,
decltype(
shape)>(
shape), order);
905 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class I, std::
size_t N>
906 inline auto reshape_view(E&& e,
const I (&
shape)[N])
908 using shape_type = std::array<I, N>;
909 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type,
decltype(
shape)>(
shape));
Fixed shape implementation for compile time defined arrays.
Base class for implementation of common expression access methods.
Base class for implementation of common expression constant access methods.
size_type size() const noexcept(noexcept(derived_cast().shape()))
size_type dimension() const noexcept
Base class for xexpressions.
Base class for multidimensional iterable expressions.
layout_type layout() const noexcept
xstrided_view_base(CTA &&e, SA &&shape, strides_type &&strides, size_type offset, layout_type layout) noexcept
Constructs an xstrided_view_base.
bool has_linear_assign(const O &strides) const noexcept
const inner_strides_type & strides() const noexcept
bool broadcast_shape(O &shape, bool reuse_cache=false) const
const inner_backstrides_type & backstrides() const noexcept
const inner_shape_type & shape() const noexcept
size_type data_offset() const noexcept
storage_type & storage() noexcept
xexpression_type & expression() noexcept
View of an xexpression using strides.
void fill(const T &value)
Fills the view with the given value.
storage_type & storage() noexcept
Returns a reference to the buffer containing the elements of the view.
size_type data_offset() const noexcept
Returns the offset to the first element in the view.
const inner_shape_type & shape() const noexcept
layout_type layout() const noexcept
xstrided_view(CTA &&e, SA &&shape, strides_type &&strides, std::size_t offset, layout_type layout) noexcept
Constructs an xstrided_view.
const inner_strides_type & strides() const noexcept
derived_type & assign_temporary(temporary_type &&)
Assigns the temporary tmp to *this.
std::size_t compute_strides(const shape_type &shape, layout_type l, strides_type &strides)
Compute the strides given the shape and the layout of an array.
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
standard mathematical functions for xexpressions
std::vector< xstrided_slice< std::ptrdiff_t > > xstrided_slice_vector
vector of slices used to build a xstrided_view
auto strided_view(E &&e, S &&shape, X &&stride, std::size_t offset=0, layout_type layout=L) noexcept
Construct a strided view from an xexpression, shape, strides and offset.