10#ifndef XTENSOR_VIEW_HPP
11#define XTENSOR_VIEW_HPP
20#include <xtl/xclosure.hpp>
21#include <xtl/xmeta_utils.hpp>
22#include <xtl/xsequence.hpp>
23#include <xtl/xtype_traits.hpp>
25#include "../containers/xarray.hpp"
26#include "../containers/xcontainer.hpp"
27#include "../containers/xtensor.hpp"
28#include "../core/xaccessible.hpp"
29#include "../core/xiterable.hpp"
30#include "../core/xsemantic.hpp"
31#include "../core/xtensor_config.hpp"
32#include "../core/xtensor_forward.hpp"
33#include "../views/xbroadcast.hpp"
34#include "../views/xslice.hpp"
35#include "../views/xview_utils.hpp"
46 template <
class Tag,
class CT,
class... S>
49 template <
class CT,
class... S>
55 template <
class CT,
class... S>
60 template <
class CT,
class... S>
61 using xview_base_t =
typename xview_base<CT, S...>::type;
68 template <
bool is_const,
class CT,
class... S>
71 template <
class ST,
class... S>
78 struct is_xrange : std::false_type
83 struct is_xrange<xrange<T>> : std::true_type
88 struct is_xall_slice : std::false_type
93 struct is_xall_slice<xall<T>> : std::true_type
97 template <layout_type L,
bool val
id,
bool all_seen,
bool range_seen,
class V>
98 struct is_contiguous_view_impl
100 static constexpr bool value =
false;
104 struct static_dimension
106 static constexpr std::ptrdiff_t value = -1;
109 template <
class T, std::
size_t N>
110 struct static_dimension<std::array<T, N>>
112 static constexpr std::ptrdiff_t value =
static_cast<std::ptrdiff_t
>(N);
115 template <
class T, std::
size_t N>
116 struct static_dimension<xt::const_array<T, N>>
118 static constexpr std::ptrdiff_t value =
static_cast<std::ptrdiff_t
>(N);
121 template <std::size_t... I>
122 struct static_dimension<xt::fixed_shape<I...>>
124 static constexpr std::ptrdiff_t value =
sizeof...(I);
129 template <
class CT,
class... S>
130 struct is_xscalar_impl<xview<CT, S...>>
132 static constexpr bool value =
static_cast<std::ptrdiff_t
>(integral_count<S...>()
133 ) == static_dimension<
typename std::decay_t<CT>::shape_type>::value
139 struct is_strided_slice_impl : std::true_type
144 struct is_strided_slice_impl<xkeep_slice<T>> : std::false_type
149 struct is_strided_slice_impl<xdrop_slice<T>> : std::false_type
154 template <
class E,
class... S>
155 struct is_strided_view
156 : std::integral_constant<
158 std::conjunction<has_data_interface<E>, is_strided_slice_impl<std::decay_t<S>>...>::value>
172 template <
bool val
id,
bool all_seen,
bool range_seen,
class V>
175 using slice = xtl::mpl::front_t<V>;
176 static constexpr bool is_range_slice = is_xrange<slice>::value;
177 static constexpr bool is_int_slice = xtl::is_integral<slice>::value;
178 static constexpr bool is_all_slice = is_xall_slice<slice>::value;
179 static constexpr bool have_all_seen = all_seen || is_all_slice;
180 static constexpr bool have_range_seen = is_range_slice;
182 static constexpr bool is_valid = valid
185 : (!range_seen && (is_int_slice || is_range_slice)));
188 have_all_seen, range_seen || is_range_slice,
189 xtl::mpl::pop_front_t < V >> ::value;
192 template <
bool val
id,
bool all_seen,
bool range_seen>
193 struct is_contiguous_view_impl<
layout_type::
row_major, valid, all_seen, range_seen, xtl::mpl::vector<>>
195 static constexpr bool value = valid;
201 template <
bool val
id,
bool int_seen,
bool range_seen,
class V>
204 using slice = xtl::mpl::front_t<V>;
205 static constexpr bool is_range_slice = is_xrange<slice>::value;
206 static constexpr bool is_int_slice = xtl::is_integral<slice>::value;
207 static constexpr bool is_all_slice = is_xall_slice<slice>::value;
209 static constexpr bool have_int_seen = int_seen || is_int_slice;
211 static constexpr bool is_valid = valid
214 : (!range_seen && (is_all_slice || is_range_slice)));
216 have_int_seen, is_range_slice || range_seen,
217 xtl::mpl::pop_front_t < V >> ::value;
220 template <
bool val
id,
bool int_seen,
bool range_seen>
223 static constexpr bool value = valid;
227 template <
class E,
class... S>
228 struct is_contiguous_view
229 : std::integral_constant<
231 has_data_interface<E>::value
233 E::static_layout == layout_type::column_major
234 && static_cast<std::size_t>(static_dimension<typename E::shape_type>::value) != sizeof...(S)
236 && is_contiguous_view_impl<E::static_layout, true, false, false, xtl::mpl::vector<S...>>::value>
240 template <layout_type L,
class T, std::ptrdiff_t offset>
241 struct unwrap_offset_container
246 template <
class T, std::ptrdiff_t offset>
249 using type = sequence_view<T, offset, static_dimension<T>::value>;
252 template <
class T, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t offset>
255 using type = sequence_view<T, start + offset, end>;
258 template <
class T, std::ptrdiff_t offset>
261 using type = sequence_view<T, 0, static_dimension<T>::value - offset>;
264 template <
class T, std::ptrdiff_t start, std::ptrdiff_t end, std::ptrdiff_t offset>
267 using type = sequence_view<T, start, end - offset>;
270 template <
class E,
class... S>
271 struct get_contigous_shape_type
274 using type = std::conditional_t<
275 std::disjunction<is_xrange<S>...>::value,
276 typename xview_shape_type<
typename E::shape_type, S...>::type,
278 typename unwrap_offset_container<E::static_layout,
typename E::inner_shape_type, integral_count<S...>()>::type>;
282 struct is_sequence_view : std::integral_constant<bool, false>
286 template <
class T, std::ptrdiff_t S, std::ptrdiff_t E>
287 struct is_sequence_view<sequence_view<T, S, E>> : std::integral_constant<bool, true>
292 template <
class E,
class... S>
294 template <
class E,
class... S>
297 template <
class CT,
class... S>
300 using xexpression_type = std::decay_t<CT>;
301 using reference = inner_reference_t<CT>;
302 using const_reference =
typename xexpression_type::const_reference;
303 using size_type =
typename xexpression_type::size_type;
304 using temporary_type = view_temporary_type_t<xexpression_type, S...>;
306 static constexpr layout_type layout = detail::is_contiguous_view<xexpression_type, S...>::value
307 ? xexpression_type::static_layout
310 static constexpr bool is_const = std::is_const<std::remove_reference_t<CT>>::value;
312 using extract_storage_type = xtl::mpl::eval_if_t<
314 detail::expr_storage_type<xexpression_type>,
316 using storage_type = std::conditional_t<is_const, const extract_storage_type, extract_storage_type>;
319 template <
class CT,
class... S>
322 using xexpression_type = std::decay_t<CT>;
324 static constexpr bool is_strided_view = detail::is_strided_view<xexpression_type, S...>::value;
325 static constexpr bool is_contiguous_view = detail::is_contiguous_view<xexpression_type, S...>::value;
327 using inner_shape_type = std::conditional_t<
329 typename detail::get_contigous_shape_type<xexpression_type, S...>::type,
330 typename xview_shape_type<
typename xexpression_type::shape_type, S...>::type>;
332 using stepper = std::conditional_t<
337 using const_stepper = std::conditional_t<
357 template <
class CT,
class... S>
358 class xview :
public xview_semantic<xview<CT, S...>>,
359 public std::conditional_t<
360 detail::is_contiguous_view<std::decay_t<CT>, S...>::value,
361 xcontiguous_iterable<xview<CT, S...>>,
362 xiterable<xview<CT, S...>>>,
363 public xaccessible<xview<CT, S...>>,
364 public extension::xview_base_t<CT, S...>
368 using self_type =
xview<CT, S...>;
370 using xexpression_type = std::decay_t<CT>;
371 using semantic_base = xview_semantic<self_type>;
374 using accessible_base = xaccessible<self_type>;
375 using extension_base = extension::xview_base_t<CT, S...>;
376 using expression_tag =
typename extension_base::expression_tag;
378 static constexpr bool is_const = std::is_const<std::remove_reference_t<CT>>::value;
379 using value_type =
typename xexpression_type::value_type;
380 using simd_value_type = xt_simd::simd_type<value_type>;
381 using bool_load_type =
typename xexpression_type::bool_load_type;
382 using reference =
typename inner_types::reference;
383 using const_reference =
typename inner_types::const_reference;
384 using pointer = std::
385 conditional_t<is_const, typename xexpression_type::const_pointer, typename xexpression_type::pointer>;
386 using const_pointer =
typename xexpression_type::const_pointer;
387 using size_type =
typename inner_types::size_type;
388 using difference_type =
typename xexpression_type::difference_type;
390 static constexpr layout_type static_layout = inner_types::layout;
393 static constexpr bool is_strided_view = detail::is_strided_view<xexpression_type, S...>::value;
394 static constexpr bool is_contiguous_view = contiguous_layout;
397 using inner_shape_type =
typename iterable_base::inner_shape_type;
398 using shape_type =
typename xview_shape_type<
typename xexpression_type::shape_type, S...>::type;
400 using xexpression_inner_strides_type = xtl::mpl::eval_if_t<
402 detail::expr_inner_strides_type<xexpression_type>,
405 using xexpression_inner_backstrides_type = xtl::mpl::eval_if_t<
407 detail::expr_inner_backstrides_type<xexpression_type>,
410 using storage_type =
typename inner_types::storage_type;
412 static constexpr bool has_trivial_strides = is_contiguous_view
413 && !std::disjunction<detail::is_xrange<S>...>::value;
414 using inner_strides_type = std::conditional_t<
416 typename detail::unwrap_offset_container<
417 xexpression_type::static_layout,
418 xexpression_inner_strides_type,
419 integral_count<S...>()>::type,
420 get_strides_t<shape_type>>;
422 using inner_backstrides_type = std::conditional_t<
424 typename detail::unwrap_offset_container<
425 xexpression_type::static_layout,
426 xexpression_inner_backstrides_type,
427 integral_count<S...>()>::type,
428 get_strides_t<shape_type>>;
430 using strides_type = get_strides_t<shape_type>;
431 using backstrides_type = strides_type;
434 using slice_type = std::tuple<S...>;
436 using stepper =
typename iterable_base::stepper;
437 using const_stepper =
typename iterable_base::const_stepper;
439 using linear_iterator = std::conditional_t<
441 std::conditional_t<is_const, typename xexpression_type::const_linear_iterator, typename xexpression_type::linear_iterator>,
442 typename iterable_base::linear_iterator>;
443 using const_linear_iterator = std::conditional_t<
445 typename xexpression_type::const_linear_iterator,
446 typename iterable_base::const_linear_iterator>;
448 using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
449 using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
451 using container_iterator = pointer;
452 using const_container_iterator = const_pointer;
453 static constexpr std::size_t rank = SIZE_MAX;
457 template <
class CTA,
class FSL,
class... SL>
458 explicit xview(CTA&& e, FSL&& first_slice, SL&&...
slices)
noexcept;
461 self_type& operator=(
const xview& rhs);
467 disable_xexpression<E, self_type>& operator=(
const E& e);
469 const inner_shape_type&
shape() const noexcept;
470 const slice_type&
slices() const noexcept;
472 bool is_contiguous() const noexcept;
473 using accessible_base::
shape;
478 template <class... Args>
479 reference operator()(Args... args);
480 template <class... Args>
481 reference unchecked(Args... args);
483 reference element(It first, It last);
485 template <class... Args>
486 const_reference operator()(Args... args) const;
487 template <class... Args>
488 const_reference unchecked(Args... args) const;
490 const_reference element(It first, It last) const;
502 stepper stepper_begin(const ST&
shape);
507 const_stepper stepper_begin(const ST&
shape) const;
511 template <class T = xexpression_type>
512 storage_type& storage()
515 template <class T = xexpression_type>
516 const storage_type& storage() const
519 template <class T = xexpression_type>
520 linear_iterator linear_begin()
523 template <class T = xexpression_type>
524 linear_iterator linear_end()
527 template <class T = xexpression_type>
528 const_linear_iterator linear_begin() const
531 template <class T = xexpression_type>
532 const_linear_iterator linear_end() const
535 template <class T = xexpression_type>
536 const_linear_iterator linear_cbegin() const
539 template <class T = xexpression_type>
540 const_linear_iterator linear_cend() const
543 template <class T = xexpression_type>
544 reverse_linear_iterator linear_rbegin()
547 template <class T = xexpression_type>
548 reverse_linear_iterator linear_rend()
551 template <class T = xexpression_type>
552 const_reverse_linear_iterator linear_rbegin() const
555 template <class T = xexpression_type>
556 const_reverse_linear_iterator linear_rend() const
559 template <class T = xexpression_type>
560 const_reverse_linear_iterator linear_crbegin() const
563 template <class T = xexpression_type>
564 const_reverse_linear_iterator linear_crend() const
567 template <class T = xexpression_type>
568 const inner_strides_type& strides() const
571 template <class T = xexpression_type>
572 const inner_strides_type& backstrides() const
575 template <class T = xexpression_type>
576 const_pointer data() const
579 template <class T = xexpression_type>
583 template <class T = xexpression_type>
588 inline It data_xbegin_impl(It begin) const noexcept;
591 inline It data_xend_impl(It begin,
layout_type l, size_type offset) const noexcept;
592 inline container_iterator data_xbegin() noexcept;
593 inline const_container_iterator data_xbegin() const noexcept;
594 inline container_iterator data_xend(
layout_type l, size_type offset) noexcept;
596 inline const_container_iterator data_xend(
layout_type l, size_type offset) const noexcept;
605 template <xscalar_concept ST = self_type>
606 operator const_reference()
const
611 size_type underlying_size(size_type dim)
const;
613 xtl::xclosure_pointer<self_type&> operator&() &;
614 xtl::xclosure_pointer<const self_type&> operator&() const&;
615 xtl::xclosure_pointer<self_type> operator&() &&;
617 template <class E, class T = xexpression_type>
618 void assign_to(xexpression<E>& e,
bool force_resize) const
619 requires(has_data_interface_concept<T> and contiguous_view_concept<E, S...>);
622 using rebind_t =
xview<E, S...>;
625 rebind_t<E> build_view(E&& e) const;
631 template <class requested_type>
632 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
634 template <class align, class simd, class T = xexpression_type>
635 void store_simd(size_type i, const simd& e)
636 requires(has_simd_interface_concept<T> and strided_view_concept<CT, S...>);
640 class requested_type = value_type,
641 std::
size_t N = xt_simd::simd_traits<requested_type>::
size,
642 class T = xexpression_type>
643 simd_return_type<requested_type> load_simd(size_type i) const
644 requires(has_simd_interface_concept<T> and strided_view_concept<CT, S...>);
646 template <class T = xexpression_type>
647 reference data_element(size_type i)
648 requires(has_simd_interface_concept<T> and strided_view_concept<CT, S...>);
650 template <class T = xexpression_type>
651 const_reference data_element(size_type i) const
652 requires(has_simd_interface_concept<T> and strided_view_concept<CT, S...>);
654 template <class T = xexpression_type>
655 reference flat(size_type i)
656 requires(has_simd_interface_concept<T> and strided_view_concept<CT, S...>);
658 template <class T = xexpression_type>
659 const_reference flat(size_type i) const
660 requires(has_simd_interface_concept<T> and strided_view_concept<CT, S...>);
665 template <std::
size_t I>
666 struct lesser_condition
668 static constexpr bool value = (I + newaxis_count_before<S...>(I + 1) <
sizeof...(S));
673 inner_shape_type m_shape;
674 mutable inner_strides_type m_strides;
675 mutable inner_backstrides_type m_backstrides;
676 mutable std::size_t m_data_offset;
677 mutable bool m_strides_computed;
679 template <
class CTA,
class FSL,
class... SL>
680 explicit xview(std::true_type, CTA&& e, FSL&& first_slice, SL&&...
slices)
noexcept;
682 template <
class CTA,
class FSL,
class... SL>
683 explicit xview(std::false_type, CTA&& e, FSL&& first_slice, SL&&...
slices)
noexcept;
685 template <
class... Args>
686 auto make_index_sequence(Args... args)
const noexcept;
688 void compute_strides(std::true_type)
const;
689 void compute_strides(std::false_type)
const;
693 template <
class Arg,
class... Args>
694 reference access(Arg
arg, Args... args);
696 const_reference access()
const;
698 template <
class Arg,
class... Args>
699 const_reference access(Arg
arg, Args... args)
const;
701 template <typename std::decay_t<CT>::size_type... I,
class... Args>
702 reference unchecked_impl(std::index_sequence<I...>, Args... args);
704 template <typename std::decay_t<CT>::size_type... I,
class... Args>
705 const_reference unchecked_impl(std::index_sequence<I...>, Args... args)
const;
707 template <typename std::decay_t<CT>::size_type... I,
class... Args>
708 reference access_impl(std::index_sequence<I...>, Args... args);
710 template <typename std::decay_t<CT>::size_type... I,
class... Args>
711 const_reference access_impl(std::index_sequence<I...>, Args... args)
const;
713 template <typename std::decay_t<CT>::size_type I,
class... Args>
714 size_type index(Args... args)
const;
716 template <typename std::decay_t<CT>::size_type,
class T>
717 size_type sliced_access(const xslice<T>& slice)
const;
719 template <typename std::decay_t<CT>::size_type I,
class T, class Arg, class... Args>
720 size_type sliced_access(const xslice<T>& slice, Arg
arg, Args... args)
const;
722 template <typename std::decay_t<CT>::size_type I,
class T, class... Args>
723 disable_xslice<T, size_type> sliced_access(const T&
squeeze, Args...)
const;
725 using base_index_type = xindex_type_t<typename xexpression_type::shape_type>;
728 base_index_type make_index(It first, It last)
const;
730 void assign_temporary_impl(temporary_type&& tmp);
732 template <std::size_t... I>
733 std::size_t data_offset_impl(std::index_sequence<I...>)
const noexcept;
735 template <std::size_t... I>
736 auto compute_strides_impl(std::index_sequence<I...>)
const noexcept;
738 inner_shape_type compute_shape(std::true_type)
const;
739 inner_shape_type compute_shape(std::false_type)
const;
741 template <
class E, std::size_t... I>
742 rebind_t<E> build_view_impl(E&& e, std::index_sequence<I...>)
const;
744 friend class xview_semantic<
xview<CT, S...>>;
747 template <
class E,
class... S>
748 auto view(E&& e, S&&... slices);
751 auto row(E&& e, std::ptrdiff_t index);
754 auto col(E&& e, std::ptrdiff_t index);
763 struct get_stepper_impl
765 using xexpression_type =
typename V::xexpression_type;
766 using type =
typename xexpression_type::stepper;
770 struct get_stepper_impl<const V>
772 using xexpression_type =
typename V::xexpression_type;
773 using type =
typename xexpression_type::const_stepper;
778 using get_stepper =
typename detail::get_stepper_impl<V>::type;
780 template <
bool is_const,
class CT,
class... S>
785 using view_type = std::conditional_t<is_const,
const xview<CT, S...>,
xview<CT, S...>>;
786 using substepper_type = get_stepper<view_type>;
788 using value_type =
typename substepper_type::value_type;
789 using reference =
typename substepper_type::reference;
790 using pointer =
typename substepper_type::pointer;
791 using difference_type =
typename substepper_type::difference_type;
792 using size_type =
typename view_type::size_type;
794 using shape_type =
typename substepper_type::shape_type;
796 xview_stepper() =
default;
805 reference operator*()
const;
807 void step(size_type dim);
808 void step_back(size_type dim);
809 void step(size_type dim, size_type n);
810 void step_back(size_type dim, size_type n);
811 void reset(size_type dim);
812 void reset_back(size_type dim);
819 bool is_newaxis_slice(size_type index)
const noexcept;
823 void common_step_forward(size_type dim, F f);
825 void common_step_backward(size_type dim, F f);
828 void common_step_forward(size_type dim, size_type n, F f);
830 void common_step_backward(size_type dim, size_type n, F f);
833 void common_reset(size_type dim, F f,
bool backwards);
836 substepper_type m_it;
838 std::array<std::size_t,
sizeof...(S)> m_index_keeper;
842 template <
class ST,
class... S>
848 template <
class I, std::size_t L,
class... S>
851 using type = std::array<I, L - integral_count<S...>() + newaxis_count<S...>()>;
854 template <std::size_t... I,
class... S>
857 using type =
typename xview_shape_type<std::array<std::size_t,
sizeof...(I)>, S...>::type;
878 template <
class CT,
class... S>
879 template <
class CTA,
class FSL,
class... SL>
882 std::integral_constant<bool, has_trivial_strides>{},
883 std::forward<CTA>(e),
884 std::forward<FSL>(first_slice),
885 std::forward<SL>(slices)...
891 template <
class CT,
class... S>
892 template <
class CTA,
class FSL,
class... SL>
894 : m_e(std::forward<CTA>(e))
895 , m_slices(std::forward<FSL>(first_slice), std::forward<SL>(slices)...)
896 , m_shape(compute_shape(detail::is_sequence_view<inner_shape_type>{}))
897 , m_strides(m_e.strides())
898 , m_backstrides(m_e.backstrides())
899 , m_data_offset(data_offset_impl(std::make_index_sequence<
sizeof...(S)>()))
900 , m_strides_computed(
true)
904 template <
class CT,
class... S>
905 template <
class CTA,
class FSL,
class... SL>
907 : m_e(std::forward<CTA>(e))
908 , m_slices(std::forward<FSL>(first_slice), std::forward<SL>(slices)...)
909 , m_shape(compute_shape(std::false_type{}))
910 , m_strides_computed(
false)
916 template <
class CT,
class... S>
917 inline auto xview<CT, S...>::operator=(
const xview& rhs) -> self_type&
920 return this->assign_temporary(std::move(tmp));
930 template <
class CT,
class... S>
934 return semantic_base::operator=(e);
939 template <
class CT,
class... S>
941 inline auto xview<CT, S...>::operator=(
const E& e) -> disable_xexpression<E, self_type>&
954 template <
class CT,
class... S>
963 template <
class CT,
class... S>
972 template <
class CT,
class... S>
975 if constexpr (is_strided_view)
979 return static_layout;
983 bool strides_match = do_strides_match(
shape(),
strides(), m_e.layout(),
true);
993 template <
class CT,
class... S>
994 inline bool xview<CT, S...>::is_contiguous() const noexcept
1010 template <
class CT,
class... S>
1016 std::fill(linear_begin(), linear_end(), value);
1020 std::fill(this->begin(), this->end(), value);
1030 template <
class CT,
class... S>
1031 template <
class... Args>
1032 inline auto xview<CT, S...>::operator()(Args... args) -> reference
1034 XTENSOR_TRY(check_index(
shape(), args...));
1035 XTENSOR_CHECK_DIMENSION(
shape(), args...);
1038 return access(
static_cast<size_type
>(args)...);
1060 template <
class CT,
class... S>
1061 template <
class... Args>
1062 inline auto xview<CT, S...>::unchecked(Args... args) -> reference
1064 return unchecked_impl(make_index_sequence(args...),
static_cast<size_type
>(args)...);
1067 template <
class CT,
class... S>
1069 inline auto xview<CT, S...>::element(It first, It last) -> reference
1071 XTENSOR_TRY(check_element_index(shape(), first, last));
1073 auto index = make_index(first, last);
1074 return m_e.element(index.cbegin(), index.cend());
1083 template <
class CT,
class... S>
1084 template <
class... Args>
1085 inline auto xview<CT, S...>::operator()(Args... args)
const -> const_reference
1087 XTENSOR_TRY(check_index(
shape(), args...));
1088 XTENSOR_CHECK_DIMENSION(
shape(), args...);
1091 return access(
static_cast<size_type
>(args)...);
1113 template <
class CT,
class... S>
1114 template <
class... Args>
1115 inline auto xview<CT, S...>::unchecked(Args... args)
const -> const_reference
1117 return unchecked_impl(make_index_sequence(args...),
static_cast<size_type
>(args)...);
1120 template <
class CT,
class... S>
1122 inline auto xview<CT, S...>::element(It first, It last)
const -> const_reference
1125 auto index = make_index(first, last);
1126 return m_e.element(index.cbegin(), index.cend());
1132 template <
class CT,
class... S>
1141 template <
class CT,
class... S>
1152 template <
class CT,
class... S>
1154 inline auto xview<CT, S...>::storage() -> storage_type&
1157 return m_e.storage();
1160 template <
class CT,
class... S>
1162 inline auto xview<CT, S...>::storage() const -> const storage_type&
1165 return m_e.storage();
1168 template <
class CT,
class... S>
1170 auto xview<CT, S...>::linear_begin() -> linear_iterator
1171 requires(has_data_interface_concept<T> and strided_view_concept<CT, S...>)
1173 return m_e.storage().begin() + data_offset();
1176 template <
class CT,
class... S>
1178 auto xview<CT, S...>::linear_end() -> linear_iterator
1181 return m_e.storage().begin() + data_offset() + this->size();
1184 template <
class CT,
class... S>
1186 auto xview<CT, S...>::linear_begin() const -> const_linear_iterator
1189 return linear_cbegin();
1192 template <
class CT,
class... S>
1194 auto xview<CT, S...>::linear_end() const -> const_linear_iterator
1197 return linear_cend();
1200 template <
class CT,
class... S>
1202 auto xview<CT, S...>::linear_cbegin() const -> const_linear_iterator
1205 return m_e.storage().cbegin() + data_offset();
1208 template <
class CT,
class... S>
1210 auto xview<CT, S...>::linear_cend() const -> const_linear_iterator
1213 return m_e.storage().cbegin() + data_offset() + this->size();
1216 template <
class CT,
class... S>
1218 auto xview<CT, S...>::linear_rbegin() -> reverse_linear_iterator
1221 return reverse_linear_iterator(linear_end());
1224 template <
class CT,
class... S>
1226 auto xview<CT, S...>::linear_rend() -> reverse_linear_iterator
1229 return reverse_linear_iterator(linear_begin());
1232 template <
class CT,
class... S>
1234 auto xview<CT, S...>::linear_rbegin() const -> const_reverse_linear_iterator
1237 return linear_crbegin();
1240 template <
class CT,
class... S>
1242 auto xview<CT, S...>::linear_rend() const -> const_reverse_linear_iterator
1245 return linear_crend();
1248 template <
class CT,
class... S>
1250 auto xview<CT, S...>::linear_crbegin() const -> const_reverse_linear_iterator
1253 return const_reverse_linear_iterator(linear_end());
1256 template <
class CT,
class... S>
1258 auto xview<CT, S...>::linear_crend() const -> const_reverse_linear_iterator
1261 return const_reverse_linear_iterator(linear_begin());
1267 template <
class CT,
class... S>
1269 inline auto xview<CT, S...>::strides() const
1271 if (!m_strides_computed)
1274 m_strides_computed =
true;
1279 template <
class CT,
class... S>
1281 inline auto xview<CT, S...>::backstrides() const
1283 if (!m_strides_computed)
1286 m_strides_computed =
true;
1288 return m_backstrides;
1294 template <
class CT,
class... S>
1296 inline auto xview<CT, S...>::data() const -> const_pointer
1302 template <
class CT,
class... S>
1304 inline auto xview<CT, S...>::data() -> pointer
1310 template <
class CT,
class... S>
1311 template <std::size_t... I>
1312 inline std::size_t xview<CT, S...>::data_offset_impl(std::index_sequence<I...>)
const noexcept
1314 auto temp = std::array<std::ptrdiff_t,
sizeof...(S)>(
1315 {(
static_cast<ptrdiff_t
>(xt::value(std::get<I>(m_slices), 0)))...}
1318 std::ptrdiff_t result = 0;
1320 for (; i < std::min(
sizeof...(S), m_e.strides().size()); ++i)
1322 result += temp[i] * m_e.strides()[i - newaxis_count_before<S...>(i)];
1324 for (; i <
sizeof...(S); ++i)
1328 return static_cast<std::size_t
>(result) + m_e.data_offset();
1334 template <
class CT,
class... S>
1339 if (!m_strides_computed)
1342 m_strides_computed =
true;
1344 return m_data_offset;
1349 template <
class CT,
class... S>
1350 inline auto xview<CT, S...>::underlying_size(size_type dim)
const -> size_type
1352 return m_e.shape()[dim];
1355 template <
class CT,
class... S>
1356 inline auto xview<CT, S...>::operator&() & -> xtl::xclosure_pointer<self_type&>
1358 return xtl::closure_pointer(*
this);
1361 template <
class CT,
class... S>
1362 inline auto xview<CT, S...>::operator&() const& -> xtl::xclosure_pointer<const self_type&>
1364 return xtl::closure_pointer(*
this);
1367 template <
class CT,
class... S>
1368 inline auto xview<CT, S...>::operator&() && -> xtl::xclosure_pointer<self_type>
1370 return xtl::closure_pointer(std::move(*
this));
1383 template <
class CT,
class... S>
1387 return xt::broadcast_shape(m_shape,
shape);
1395 template <
class CT,
class... S>
1399 if constexpr (is_strided_view)
1401 return str.size() ==
strides().size() && std::equal(str.cbegin(), str.cend(), strides().begin());
1411 template <
class CT,
class... S>
1413 inline It xview<CT, S...>::data_xbegin_impl(It begin)
const noexcept
1415 return begin + data_offset();
1418 template <
class CT,
class... S>
1420 inline It xview<CT, S...>::data_xend_impl(It begin,
layout_type l, size_type offset)
const noexcept
1422 return strided_data_end(*
this, begin, l, offset);
1425 template <
class CT,
class... S>
1426 inline auto xview<CT, S...>::data_xbegin() noexcept -> container_iterator
1428 return data_xbegin_impl(data());
1431 template <
class CT,
class... S>
1432 inline auto xview<CT, S...>::data_xbegin() const noexcept -> const_container_iterator
1434 return data_xbegin_impl(data());
1437 template <
class CT,
class... S>
1438 inline auto xview<CT, S...>::data_xend(
layout_type l, size_type offset)
noexcept -> container_iterator
1440 return data_xend_impl(data() + data_offset(), l, offset);
1443 template <
class CT,
class... S>
1444 inline auto xview<CT, S...>::data_xend(
layout_type l, size_type offset)
const noexcept
1445 -> const_container_iterator
1447 return data_xend_impl(data() + data_offset(), l, offset);
1451 template <
class CT,
class... S>
1452 template <
class E,
class T>
1453 void xview<CT, S...>::assign_to(
xexpression<E>& e,
bool force_resize)
const
1456 auto& de = e.derived_cast();
1457 de.resize(shape(), force_resize);
1458 std::copy(data() + data_offset(), data() + data_offset() + de.size(), de.template begin<static_layout>());
1461 template <
class CT,
class... S>
1462 template <
class E, std::size_t... I>
1463 inline auto xview<CT, S...>::build_view_impl(E&& e, std::index_sequence<I...>)
const -> rebind_t<E>
1465 return rebind_t<E>(std::forward<E>(e), std::get<I>(m_slices)...);
1468 template <
class CT,
class... S>
1470 inline auto xview<CT, S...>::build_view(E&& e)
const -> rebind_t<E>
1472 return build_view_impl(std::forward<E>(e), std::make_index_sequence<
sizeof...(S)>());
1475 template <
class CT,
class... S>
1476 template <
class align,
class simd,
class T>
1477 inline auto xview<CT, S...>::store_simd(size_type i,
const simd& e) ->
void
1480 return m_e.template store_simd<xt_simd::unaligned_mode>(data_offset() + i, e);
1483 template <
class CT,
class... S>
1484 template <
class align,
class requested_type, std::
size_t N,
class T>
1485 inline auto xview<CT, S...>::load_simd(size_type i)
const -> simd_return_type<requested_type>
1488 return m_e.template load_simd<xt_simd::unaligned_mode, requested_type>(data_offset() + i);
1491 template <
class CT,
class... S>
1493 inline auto xview<CT, S...>::data_element(size_type i) -> reference
1496 return m_e.data_element(data_offset() + i);
1499 template <
class CT,
class... S>
1501 inline auto xview<CT, S...>::data_element(size_type i)
const -> const_reference
1504 return m_e.data_element(data_offset() + i);
1507 template <
class CT,
class... S>
1509 inline auto xview<CT, S...>::flat(size_type i) -> reference
1512 XTENSOR_ASSERT(is_contiguous());
1513 return m_e.flat(data_offset() + i);
1516 template <
class CT,
class... S>
1518 inline auto xview<CT, S...>::flat(size_type i)
const -> const_reference
1521 XTENSOR_ASSERT(is_contiguous());
1522 return m_e.flat(data_offset() + i);
1525 template <
class CT,
class... S>
1526 template <
class... Args>
1527 inline auto xview<CT, S...>::make_index_sequence(Args...) const noexcept
1529 return std::make_index_sequence<
1530 (
sizeof...(Args) + integral_count<S...>() > newaxis_count<S...>()
1531 ?
sizeof...(Args) + integral_count<S...>() - newaxis_count<S...>()
1535 template <
class CT,
class... S>
1536 template <std::size_t... I>
1537 inline auto xview<CT, S...>::compute_strides_impl(std::index_sequence<I...>)
const noexcept
1539 std::size_t original_dim = m_e.dimension();
1540 return std::array<std::ptrdiff_t,
sizeof...(I)>(
1541 {(
static_cast<std::ptrdiff_t
>(xt::step_size(std::get<integral_skip<S...>(I)>(m_slices), 1))
1542 * ((integral_skip<S...>(I) - newaxis_count_before<S...>(integral_skip<S...>(I))) < original_dim
1543 ? m_e.strides()[integral_skip<S...>(I) - newaxis_count_before<S...>(integral_skip<S...>(I))]
1548 template <
class CT,
class... S>
1549 inline void xview<CT, S...>::compute_strides(std::false_type)
const
1551 m_strides = xtl::make_sequence<inner_strides_type>(this->dimension(), 0);
1552 m_backstrides = xtl::make_sequence<inner_strides_type>(this->dimension(), 0);
1554 constexpr std::size_t n_strides =
sizeof...(S) - integral_count<S...>();
1556 auto slice_strides = compute_strides_impl(std::make_index_sequence<n_strides>());
1558 for (std::size_t i = 0; i < n_strides; ++i)
1560 m_strides[i] = slice_strides[i];
1562 detail::adapt_strides(shape(), m_strides, &m_backstrides, i);
1564 for (std::size_t i = n_strides; i < this->dimension(); ++i)
1566 m_strides[i] = m_e.strides()[i + integral_count<S...>() - newaxis_count<S...>()];
1567 detail::adapt_strides(shape(), m_strides, &m_backstrides, i);
1570 m_data_offset = data_offset_impl(std::make_index_sequence<
sizeof...(S)>());
1573 template <
class CT,
class... S>
1574 inline void xview<CT, S...>::compute_strides(std::true_type)
const
1578 template <
class CT,
class... S>
1579 inline auto xview<CT, S...>::access() -> reference
1581 return access_impl(make_index_sequence());
1584 template <
class CT,
class... S>
1585 template <
class Arg,
class... Args>
1586 inline auto xview<CT, S...>::access(Arg
arg, Args... args) -> reference
1588 if (
sizeof...(Args) >= this->dimension())
1590 return access(args...);
1592 return access_impl(make_index_sequence(
arg, args...),
arg, args...);
1595 template <
class CT,
class... S>
1596 inline auto xview<CT, S...>::access() const -> const_reference
1598 return access_impl(make_index_sequence());
1601 template <
class CT,
class... S>
1602 template <
class Arg,
class... Args>
1603 inline auto xview<CT, S...>::access(Arg
arg, Args... args)
const -> const_reference
1605 if (
sizeof...(Args) >= this->dimension())
1607 return access(args...);
1609 return access_impl(make_index_sequence(
arg, args...),
arg, args...);
1612 template <
class CT,
class... S>
1613 template <typename std::decay_t<CT>::size_type... I,
class... Args>
1614 inline auto xview<CT, S...>::unchecked_impl(std::index_sequence<I...>, Args... args) -> reference
1616 return m_e.unchecked(index<I>(args...)...);
1619 template <
class CT,
class... S>
1620 template <typename std::decay_t<CT>::size_type... I,
class... Args>
1621 inline auto xview<CT, S...>::unchecked_impl(std::index_sequence<I...>, Args... args)
const
1624 return m_e.unchecked(index<I>(args...)...);
1627 template <
class CT,
class... S>
1628 template <typename std::decay_t<CT>::size_type... I,
class... Args>
1629 inline auto xview<CT, S...>::access_impl(std::index_sequence<I...>, Args... args) -> reference
1631 return m_e(index<I>(args...)...);
1634 template <
class CT,
class... S>
1635 template <typename std::decay_t<CT>::size_type... I,
class... Args>
1636 inline auto xview<CT, S...>::access_impl(std::index_sequence<I...>, Args... args)
const -> const_reference
1638 return m_e(index<I>(args...)...);
1641 template <
class CT,
class... S>
1642 template <typename std::decay_t<CT>::size_type I,
class... Args>
1643 inline auto xview<CT, S...>::index(Args... args)
const -> size_type
1645 if constexpr (lesser_condition<I>::value)
1647 return sliced_access<I - integral_count_before<S...>(I) + newaxis_count_before<S...>(I + 1)>(
1648 std::get<I + newaxis_count_before<S...>(I + 1)>(m_slices),
1654 return argument<I - integral_count<S...>() + newaxis_count<S...>()>(args...);
1658 template <
class CT,
class... S>
1659 template <typename std::decay_t<CT>::size_type I,
class T>
1660 inline auto
xview<CT, S...>::sliced_access(const xslice<T>& slice) const -> size_type
1662 return static_cast<size_type
>(slice.derived_cast()(0));
1665 template <
class CT,
class... S>
1666 template <typename std::decay_t<CT>::size_type I,
class T, class Arg, class... Args>
1667 inline auto
xview<CT, S...>::sliced_access(const xslice<T>& slice, Arg arg, Args... args) const -> size_type
1669 using ST =
typename T::size_type;
1670 return static_cast<size_type
>(
1671 slice.derived_cast()(argument<I>(
static_cast<ST
>(arg),
static_cast<ST
>(args)...))
1675 template <
class CT,
class... S>
1676 template <typename std::decay_t<CT>::size_type I,
class T, class... Args>
1677 inline auto
xview<CT, S...>::sliced_access(const T& squeeze, Args...) const -> disable_xslice<T, size_type>
1679 return static_cast<size_type
>(
squeeze);
1682 template <
class CT,
class... S>
1684 inline auto xview<CT, S...>::make_index(It first, It last)
const -> base_index_type
1686 auto index = xtl::make_sequence<base_index_type>(m_e.dimension(), 0);
1687 using diff_type =
typename std::iterator_traits<It>::difference_type;
1688 using ivalue_type =
typename base_index_type::value_type;
1689 auto func1 = [&first](
const auto& s)
noexcept
1691 return get_slice_value(s, first);
1693 auto func2 = [](
const auto& s)
noexcept
1695 return xt::value(s, 0);
1698 auto s =
static_cast<diff_type
>(
1699 (std::min)(
static_cast<size_type
>(std::distance(first, last)), this->dimension())
1701 auto first_copy = last - s;
1702 for (size_type i = 0; i != m_e.dimension(); ++i)
1704 size_type k = newaxis_skip<S...>(i);
1708 std::advance(first,
static_cast<diff_type
>(k - xt::integral_count_before<S...>(i)));
1712 index[i] = k <
sizeof...(S) ? apply<size_type>(k, func1, m_slices)
1713 :
static_cast<ivalue_type
>(*first);
1717 index[i] = k <
sizeof...(S) ? apply<size_type>(k, func2, m_slices) : ivalue_type(0);
1723 template <
class CT,
class... S>
1724 inline auto xview<CT, S...>::compute_shape(std::true_type)
const -> inner_shape_type
1726 return inner_shape_type(m_e.shape());
1729 template <
class CT,
class... S>
1730 inline auto xview<CT, S...>::compute_shape(std::false_type)
const -> inner_shape_type
1732 std::size_t dim = m_e.dimension() - integral_count<S...>() + newaxis_count<S...>();
1733 auto shape = xtl::make_sequence<inner_shape_type>(dim, 0);
1734 auto func = [](
const auto& s)
noexcept
1738 for (size_type i = 0; i != dim; ++i)
1740 size_type index = integral_skip<S...>(i);
1741 shape[i] = index <
sizeof...(S) ? apply<size_type>(index, func, m_slices)
1742 : m_e.shape()[index - newaxis_count_before<S...>(index)];
1747 namespace xview_detail
1749 template <
class V,
class T>
1750 inline void run_assign_temporary_impl(V& v,
const T& t, std::true_type )
1752 strided_loop_assigner<true>::run(v, t);
1755 template <
class V,
class T>
1757 run_assign_temporary_impl(V& v,
const T& t, std::false_type )
1759 std::copy(t.cbegin(), t.cend(), v.begin());
1763 template <
class CT,
class... S>
1764 inline void xview<CT, S...>::assign_temporary_impl(
temporary_type&& tmp)
1766 constexpr bool fast_assign = detail::is_strided_view<xexpression_type, S...>::value
1768 xview_detail::run_assign_temporary_impl(*
this, tmp, std::integral_constant<bool, fast_assign>{});
1773 template <
class E,
class... S>
1774 inline std::size_t get_underlying_shape_index(std::size_t I)
1776 return I - newaxis_count_before<get_slice_type<E, S>...>(I);
1779 template <
class... S>
1783 struct check_slice<>
1785 using type = void_t<>;
1788 template <
class S,
class... SL>
1789 struct check_slice<S, SL...>
1791 static_assert(!std::is_same<S, xellipsis_tag>::value,
"ellipsis not supported vith xview");
1792 using type =
typename check_slice<SL...>::type;
1795 template <
class E, std::size_t... I,
class... S>
1796 inline auto make_view_impl(E&& e, std::index_sequence<I...>, S&&... slices)
1799 using view_type = xview<xtl::closure_type_t<E>, get_slice_type<std::decay_t<E>, S>...>;
1802 get_slice_implementation(
1804 std::forward<S>(slices),
1805 get_underlying_shape_index<std::decay_t<E>, S...>(I)
1820 template <
class E,
class... S>
1821 inline auto view(E&& e, S&&... slices)
1823 return detail::make_view_impl(
1825 std::make_index_sequence<
sizeof...(S)>(),
1826 std::forward<S>(slices)...
1837 inline static auto make(E&& e,
const std::ptrdiff_t index)
1839 const auto shape = e.shape();
1840 check_dimension(shape);
1847 inline static void check_dimension(
const S& shape)
1849 if (shape.size() != 2)
1852 std::invalid_argument,
1853 "A row can only be accessed on an expression with exact two dimensions"
1858 template <
class T, std::
size_t N>
1859 inline static void check_dimension(
const std::array<T, N>&)
1861 static_assert(N == 2,
"A row can only be accessed on an expression with exact two dimensions");
1870 inline static auto make(E&& e,
const std::ptrdiff_t index)
1872 const auto shape = e.shape();
1873 check_dimension(shape);
1880 inline static void check_dimension(
const S& shape)
1882 if (shape.size() != 2)
1885 std::invalid_argument,
1886 "A column can only be accessed on an expression with exact two dimensions"
1891 template <
class T, std::
size_t N>
1892 inline static void check_dimension(
const std::array<T, N>&)
1894 static_assert(N == 2,
"A column can only be accessed on an expression with exact two dimensions");
1909 inline auto row(E&& e, std::ptrdiff_t index)
1911 return detail::row_impl::make(e, index);
1924 inline auto col(E&& e, std::ptrdiff_t index)
1926 return detail::column_impl::make(e, index);
1933 template <
class CT,
class... S>
1935 inline auto xview<CT, S...>::stepper_begin(
const ST& shape) -> stepper
1937 const size_type offset = shape.size() - this->dimension();
1938 if constexpr (is_strided_view)
1940 return stepper(
this, data_xbegin(), offset);
1944 return stepper(
this, m_e.stepper_begin(m_e.shape()), offset);
1948 template <
class CT,
class... S>
1950 inline auto xview<CT, S...>::stepper_end(
const ST& shape,
layout_type l) -> stepper
1952 const size_type offset = shape.size() - this->dimension();
1953 if constexpr (is_strided_view)
1955 return stepper(
this, data_xend(l, offset), offset);
1959 return stepper(
this, m_e.stepper_end(m_e.shape(), l), offset,
true, l);
1963 template <
class CT,
class... S>
1965 inline auto xview<CT, S...>::stepper_begin(
const ST& shape)
const -> const_stepper
1967 const size_type offset = shape.size() - this->dimension();
1968 if constexpr (is_strided_view)
1970 return const_stepper(
this, data_xbegin(), offset);
1974 const xexpression_type& e = m_e;
1975 return const_stepper(
this, e.stepper_begin(m_e.shape()), offset);
1979 template <
class CT,
class... S>
1981 inline auto xview<CT, S...>::stepper_end(
const ST& shape,
layout_type l)
const -> const_stepper
1983 const size_type offset = shape.size() - this->dimension();
1984 if constexpr (is_strided_view)
1986 return const_stepper(
this, data_xend(l, offset), offset);
1990 const xexpression_type& e = m_e;
1991 return const_stepper(
this, e.stepper_end(m_e.shape(), l), offset,
true, l);
1999 template <
bool is_const,
class CT,
class... S>
2000 inline xview_stepper<is_const, CT, S...>::xview_stepper(
2013 std::fill(m_index_keeper.begin(), m_index_keeper.end(), 0);
2014 auto func = [](
const auto& s)
noexcept
2016 return xt::value(s, 0);
2018 for (size_type i = 0; i <
sizeof...(S); ++i)
2020 if (!is_newaxis_slice(i))
2022 size_type s = apply<size_type>(i, func, p_view->slices());
2023 size_type index = i - newaxis_count_before<S...>(i);
2024 m_it.step(index, s);
2034 template <
bool is_const,
class CT,
class... S>
2035 inline auto xview_stepper<is_const, CT, S...>::operator*() const -> reference
2040 template <
bool is_const,
class CT,
class... S>
2041 inline void xview_stepper<is_const, CT, S...>::step(size_type dim)
2043 auto func = [
this](size_type index, size_type offset)
2045 m_it.step(index, offset);
2047 common_step_forward(dim, func);
2050 template <
bool is_const,
class CT,
class... S>
2051 inline void xview_stepper<is_const, CT, S...>::step_back(size_type dim)
2053 auto func = [
this](size_type index, size_type offset)
2055 m_it.step_back(index, offset);
2057 common_step_backward(dim, func);
2060 template <
bool is_const,
class CT,
class... S>
2061 inline void xview_stepper<is_const, CT, S...>::step(size_type dim, size_type n)
2063 auto func = [
this](size_type index, size_type offset)
2065 m_it.step(index, offset);
2067 common_step_forward(dim, n, func);
2070 template <
bool is_const,
class CT,
class... S>
2071 inline void xview_stepper<is_const, CT, S...>::step_back(size_type dim, size_type n)
2073 auto func = [
this](size_type index, size_type offset)
2075 m_it.step_back(index, offset);
2077 common_step_backward(dim, n, func);
2080 template <
bool is_const,
class CT,
class... S>
2081 inline void xview_stepper<is_const, CT, S...>::reset(size_type dim)
2083 auto func = [
this](size_type index, size_type offset)
2085 m_it.step_back(index, offset);
2087 common_reset(dim, func,
false);
2090 template <
bool is_const,
class CT,
class... S>
2091 inline void xview_stepper<is_const, CT, S...>::reset_back(size_type dim)
2093 auto func = [
this](size_type index, size_type offset)
2095 m_it.step(index, offset);
2097 common_reset(dim, func,
true);
2100 template <
bool is_const,
class CT,
class... S>
2101 inline void xview_stepper<is_const, CT, S...>::to_begin()
2103 std::fill(m_index_keeper.begin(), m_index_keeper.end(), 0);
2107 template <
bool is_const,
class CT,
class... S>
2108 inline void xview_stepper<is_const, CT, S...>::to_end(layout_type l)
2114 template <
bool is_const,
class CT,
class... S>
2115 inline bool xview_stepper<is_const, CT, S...>::is_newaxis_slice(size_type index)
const noexcept
2118 return newaxis_count_before<S...>(index + 1) != newaxis_count_before<S...>(index);
2121 template <
bool is_const,
class CT,
class... S>
2122 inline void xview_stepper<is_const, CT, S...>::to_end_impl(layout_type l)
2124 auto func = [](
const auto& s)
noexcept
2126 return xt::value(s, get_size(s) - 1);
2128 auto size_func = [](
const auto& s)
noexcept
2133 for (size_type i = 0; i <
sizeof...(S); ++i)
2135 if (!is_newaxis_slice(i))
2137 size_type s = apply<size_type>(i, func, p_view->slices());
2138 size_type ix = apply<size_type>(i, size_func, p_view->slices());
2139 m_index_keeper[i] = ix - size_type(1);
2140 size_type index = i - newaxis_count_before<S...>(i);
2141 s = p_view->underlying_size(index) - 1 - s;
2142 m_it.step_back(index, s);
2145 if (l == layout_type::row_major)
2147 for (size_type i =
sizeof...(S); i > 0; --i)
2149 if (!is_newaxis_slice(i - 1))
2151 m_index_keeper[i - 1]++;
2156 else if (l == layout_type::column_major)
2158 for (size_type i = 0; i <
sizeof...(S); ++i)
2160 if (!is_newaxis_slice(i))
2162 m_index_keeper[i]++;
2169 XTENSOR_THROW(std::runtime_error,
"Iteration only allowed in row or column major.");
2173 template <
bool is_const,
class CT,
class... S>
2175 void xview_stepper<is_const, CT, S...>::common_step_forward(size_type dim, F f)
2177 if (dim >= m_offset)
2179 auto func = [&dim,
this](
const auto& s)
noexcept
2181 return step_size(s, this->m_index_keeper[dim]++, 1);
2183 size_type index = integral_skip<S...>(dim);
2184 if (!is_newaxis_slice(index))
2186 size_type step_size = index <
sizeof...(S) ? apply<size_type>(index, func, p_view->slices())
2188 index -= newaxis_count_before<S...>(index);
2189 f(index, step_size);
2194 template <
bool is_const,
class CT,
class... S>
2196 void xview_stepper<is_const, CT, S...>::common_step_forward(size_type dim, size_type n, F f)
2198 if (dim >= m_offset)
2200 auto func = [&dim, &n,
this](
const auto& s)
noexcept
2202 auto st_size = step_size(s, this->m_index_keeper[dim], n);
2203 this->m_index_keeper[dim] += n;
2204 return size_type(st_size);
2207 size_type index = integral_skip<S...>(dim);
2208 if (!is_newaxis_slice(index))
2210 size_type step_size = index <
sizeof...(S) ? apply<size_type>(index, func, p_view->slices())
2212 index -= newaxis_count_before<S...>(index);
2213 f(index, step_size);
2218 template <
bool is_const,
class CT,
class... S>
2220 void xview_stepper<is_const, CT, S...>::common_step_backward(size_type dim, F f)
2222 if (dim >= m_offset)
2224 auto func = [&dim,
this](
const auto& s)
noexcept
2226 this->m_index_keeper[dim]--;
2227 return step_size(s, this->m_index_keeper[dim], 1);
2229 size_type index = integral_skip<S...>(dim);
2230 if (!is_newaxis_slice(index))
2232 size_type step_size = index <
sizeof...(S) ? apply<size_type>(index, func, p_view->slices())
2234 index -= newaxis_count_before<S...>(index);
2235 f(index, step_size);
2240 template <
bool is_const,
class CT,
class... S>
2242 void xview_stepper<is_const, CT, S...>::common_step_backward(size_type dim, size_type n, F f)
2244 if (dim >= m_offset)
2246 auto func = [&dim, &n,
this](
const auto& s)
noexcept
2248 this->m_index_keeper[dim] -= n;
2249 return step_size(s, this->m_index_keeper[dim], n);
2252 size_type index = integral_skip<S...>(dim);
2253 if (!is_newaxis_slice(index))
2255 size_type step_size = index <
sizeof...(S) ? apply<size_type>(index, func, p_view->slices())
2257 index -= newaxis_count_before<S...>(index);
2258 f(index, step_size);
2263 template <
bool is_const,
class CT,
class... S>
2265 void xview_stepper<is_const, CT, S...>::common_reset(size_type dim, F f,
bool backwards)
2267 auto size_func = [](
const auto& s)
noexcept
2271 auto end_func = [](
const auto& s)
noexcept
2273 return xt::value(s, get_size(s) - 1) - xt::value(s, 0);
2276 size_type index = integral_skip<S...>(dim);
2277 if (!is_newaxis_slice(index))
2279 if (dim < m_index_keeper.size())
2281 size_type size = index <
sizeof...(S) ? apply<size_type>(index, size_func, p_view->slices())
2282 : p_view->shape()[dim];
2283 m_index_keeper[dim] = backwards ? size - 1 : 0;
2286 size_type reset_n = index <
sizeof...(S) ? apply<size_type>(index, end_func, p_view->slices())
2287 : p_view->shape()[dim] - 1;
2288 index -= newaxis_count_before<S...>(index);
Fixed shape implementation for compile time defined arrays.
size_type size() const noexcept
Base class for xexpressions.
Base class for multidimensional iterable expressions.
Multidimensional view with tensor semantic.
xview(CTA &&e, FSL &&first_slice, SL &&... slices) noexcept
Constructs a view on the specified xexpression.
const slice_type & slices() const noexcept
bool has_linear_assign(const ST &strides) const
const inner_shape_type & shape() const noexcept
Returns the shape of the view.
bool broadcast_shape(ST &shape, bool reuse_cache=false) const
xexpression_type & expression() noexcept
void fill(const T &value)
std::size_t data_offset() const noexcept
layout_type layout() const noexcept
auto arg(E &&e) noexcept
Calculates the phase angle (in radians) elementwise for the complex numbers in e.
auto squeeze(E &&e)
Returns a squeeze view of the given expression.
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
auto all() noexcept
Returns a slice representing a full dimension, to be used as an argument of view function.
auto row(E &&e, std::ptrdiff_t index)
Constructs and returns a row (sliced view) on the specified expression.
auto col(E &&e, std::ptrdiff_t index)
Constructs and returns a column (sliced view) on the specified expression.
auto view(E &&e, S &&... slices)
Constructs and returns a view on the specified xexpression.