10#ifndef XTENSOR_STRIDED_VIEW_HPP
11#define XTENSOR_STRIDED_VIEW_HPP
20#include <xtl/xsequence.hpp>
22#include "../containers/xstorage.hpp"
23#include "../core/xexpression.hpp"
24#include "../core/xiterable.hpp"
25#include "../core/xlayout.hpp"
26#include "../core/xsemantic.hpp"
27#include "../utils/xutils.hpp"
28#include "../views/xstrided_view_base.hpp"
38 template <
class Tag,
class CT,
class S, layout_type L,
class FST>
41 template <
class CT,
class S, layout_type L,
class FST>
47 template <
class CT,
class S, layout_type L,
class FST>
52 template <
class CT,
class S, layout_type L,
class FST>
56 template <layout_type L1, layout_type L2,
class T>
59 using type = std::conditional_t<L1 == L2 && L1 != layout_type::dynamic, xcontiguous_iterable<T>,
xiterable<T>>;
62 template <layout_type L1, layout_type L2,
class T>
63 using select_iterable_base_t =
typename select_iterable_base<L1, L2, T>::type;
66 template <
class CT,
class S, layout_type L,
class FST>
69 template <
class CT,
class S, layout_type L,
class FST>
72 using xexpression_type = std::decay_t<CT>;
73 using undecay_expression = CT;
74 using reference = inner_reference_t<undecay_expression>;
75 using const_reference =
typename xexpression_type::const_reference;
76 using size_type =
typename xexpression_type::size_type;
77 using shape_type = std::decay_t<S>;
78 using undecay_shape = S;
79 using storage_getter = FST;
80 using inner_storage_type =
typename storage_getter::type;
81 using temporary_type =
typename detail::xtype_for_shape<
82 S>::template type<typename xexpression_type::value_type, xexpression_type::static_layout>;
83 using storage_type = std::remove_reference_t<inner_storage_type>;
87 template <
class CT,
class S, layout_type L,
class FST>
90 using inner_shape_type = std::decay_t<S>;
91 using inner_strides_type = get_strides_t<inner_shape_type>;
92 using inner_backstrides_type_type = inner_strides_type;
94 using const_stepper = std::conditional_t<
99 using stepper = std::conditional_t<
105 template <
class CT,
class S, layout_type L,
class FST,
class RHS>
128 template <
class CT,
class S, layout_type L = layout_type::dynamic,
class FST = detail::flat_storage_getter<CT, XTENSOR_DEFAULT_TRAVERSAL>>
130 :
public xview_semantic<xstrided_view<CT, S, L, FST>>,
131 public select_iterable_base_t<L, std::decay_t<CT>::static_layout, xstrided_view<CT, S, L, FST>>,
133 public extension::xstrided_view_base_t<CT, S, L, FST>
139 using semantic_base = xview_semantic<self_type>;
140 using extension_base = extension::xstrided_view_base_t<CT, S, L, FST>;
141 using expression_tag =
typename extension_base::expression_tag;
143 using xexpression_type =
typename base_type::xexpression_type;
144 using base_type::is_const;
146 using value_type =
typename base_type::value_type;
147 using reference =
typename base_type::reference;
148 using const_reference =
typename base_type::const_reference;
149 using pointer =
typename base_type::pointer;
150 using const_pointer =
typename base_type::const_pointer;
151 using size_type =
typename base_type::size_type;
152 using difference_type =
typename base_type::difference_type;
154 using inner_storage_type =
typename base_type::inner_storage_type;
155 using storage_type =
typename base_type::storage_type;
156 using linear_iterator =
typename storage_type::iterator;
157 using const_linear_iterator =
typename storage_type::const_iterator;
158 using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
159 using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
161 using iterable_base = select_iterable_base_t<L, xexpression_type::static_layout, self_type>;
162 using inner_shape_type =
typename base_type::inner_shape_type;
163 using inner_strides_type =
typename base_type::inner_strides_type;
164 using inner_backstrides_type =
typename base_type::inner_backstrides_type;
165 using shape_type =
typename base_type::shape_type;
166 using strides_type =
typename base_type::strides_type;
167 using backstrides_type =
typename base_type::backstrides_type;
169 using stepper =
typename iterable_base::stepper;
170 using const_stepper =
typename iterable_base::const_stepper;
172 using base_type::contiguous_layout;
173 using base_type::static_layout;
176 using base_index_type = xindex_type_t<shape_type>;
178 using data_alignment = xt_simd::container_alignment_t<storage_type>;
179 using simd_type = xt_simd::simd_type<value_type>;
180 using simd_value_type = xt_simd::simd_type<value_type>;
181 using bool_load_type =
typename base_type::bool_load_type;
186 template <
class CTA,
class SA>
191 self_type& operator=(
const self_type&);
197 disable_xexpression<E, self_type>& operator=(
const E& e);
201 using base_type::is_contiguous;
207 using base_type::operator();
209 using base_type::unchecked;
210 using base_type::operator[];
213 using base_type::element;
223 linear_iterator linear_begin();
224 linear_iterator linear_end();
225 const_linear_iterator linear_begin()
const;
226 const_linear_iterator linear_end()
const;
227 const_linear_iterator linear_cbegin()
const;
228 const_linear_iterator linear_cend()
const;
230 reverse_linear_iterator linear_rbegin();
231 reverse_linear_iterator linear_rend();
232 const_reverse_linear_iterator linear_rbegin()
const;
233 const_reverse_linear_iterator linear_rend()
const;
234 const_reverse_linear_iterator linear_crbegin()
const;
235 const_reverse_linear_iterator linear_crend()
const;
237 template <
class ST,
class STEP = stepper>
238 disable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape);
239 template <
class ST,
class STEP = stepper>
240 disable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l);
242 template <
class ST,
class STEP = stepper>
243 enable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape);
244 template <
class ST,
class STEP = stepper>
247 template <
class ST,
class STEP = const_stepper>
248 disable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape)
const;
249 template <
class ST,
class STEP = const_stepper>
250 disable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l)
const;
252 template <
class ST,
class STEP = const_stepper>
253 enable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape)
const;
254 template <
class ST,
class STEP = const_stepper>
255 enable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l)
const;
257 template <
class requested_type>
258 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
260 template <
class align,
class simd>
261 void store_simd(size_type i,
const simd& e)
262 requires provides_simd_interface;
264 template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
265 simd_return_type<requested_type> load_simd(size_type i)
const
266 requires provides_simd_interface;
268 reference data_element(size_type i);
269 const_reference data_element(size_type i)
const;
271 reference flat(size_type i);
272 const_reference flat(size_type i)
const;
274 using container_iterator = std::
275 conditional_t<is_const, typename storage_type::const_iterator, typename storage_type::iterator>;
276 using const_container_iterator =
typename storage_type::const_iterator;
282 rebind_t<E> build_view(E&& e)
const;
286 container_iterator data_xbegin() noexcept;
287 const_container_iterator data_xbegin() const noexcept;
288 container_iterator data_xend(
layout_type l, size_type offset) noexcept;
289 const_container_iterator data_xend(
layout_type l, size_type offset) const noexcept;
292 It data_xbegin_impl(It begin) const noexcept;
295 It data_xend_impl(It end,
layout_type l, size_type offset) const noexcept;
297 void assign_temporary_impl(temporary_type&& tmp);
299 using base_type::set_offset;
302 friend class xstepper;
303 friend class xview_semantic<self_type>;
307 friend class xaxis_iterator;
309 friend class xaxis_slice_iterator;
317 using xstrided_slice = std::variant<
329 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, placeholders::xtuph>,
367 template <class CT, class S,
layout_type L, class FST>
368 template <class CTA, class SA>
376 : base_type(std::forward<CTA>(e), std::forward<SA>(
shape), std::move(
strides), offset,
layout)
382 template <
class CT,
class S, layout_type L,
class FST>
383 inline auto xstrided_view<CT, S, L, FST>::operator=(
const self_type& rhs) -> self_type&
396 template <
class CT,
class S, layout_type L,
class FST>
398 inline auto xstrided_view<CT, S, L, FST>::operator=(
const xexpression<E>& e) -> self_type&
400 return semantic_base::operator=(e);
405 template <
class CT,
class S, layout_type L,
class FST>
407 inline auto xstrided_view<CT, S, L, FST>::operator=(
const E& e) -> disable_xexpression<E, self_type>&
413 namespace xstrided_view_detail
415 template <
class V,
class T>
416 inline void run_assign_temporary_impl(V& v,
const T& t, std::true_type )
418 strided_loop_assigner<true>::run(v, t);
421 template <
class V,
class T>
423 run_assign_temporary_impl(V& v,
const T& t, std::false_type )
425 std::copy(t.cbegin(), t.cend(), v.begin());
429 template <
class CT,
class S, layout_type L,
class FST>
433 fast_assign = xassign_traits<xstrided_view<CT, S, L, FST>, temporary_type>::simd_strided_assign();
434 xstrided_view_detail::run_assign_temporary_impl(*
this, tmp, std::integral_constant<bool, fast_assign>{});
446 template <
class CT,
class S, layout_type L,
class FST>
452 std::fill(this->linear_begin(), this->linear_end(), value);
456 std::fill(this->begin(), this->end(), value);
462 template <
class CT,
class S, layout_type L,
class FST>
463 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) -> reference
468 template <
class CT,
class S, layout_type L,
class FST>
469 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i)
const -> const_reference
474 template <
class CT,
class S, layout_type L,
class FST>
480 template <
class CT,
class S, layout_type L,
class FST>
486 template <
class CT,
class S, layout_type L,
class FST>
492 template <
class CT,
class S, layout_type L,
class FST>
498 template <
class CT,
class S, layout_type L,
class FST>
501 return this->linear_cbegin();
504 template <
class CT,
class S, layout_type L,
class FST>
507 return this->linear_cend();
510 template <
class CT,
class S, layout_type L,
class FST>
516 template <
class CT,
class S, layout_type L,
class FST>
522 template <
class CT,
class S, layout_type L,
class FST>
525 return reverse_linear_iterator(this->linear_begin());
528 template <
class CT,
class S, layout_type L,
class FST>
531 return reverse_linear_iterator(this->linear_end());
534 template <
class CT,
class S, layout_type L,
class FST>
537 return this->linear_crbegin();
540 template <
class CT,
class S, layout_type L,
class FST>
543 return this->linear_crend();
546 template <
class CT,
class S, layout_type L,
class FST>
549 return const_reverse_linear_iterator(this->linear_cbegin());
552 template <
class CT,
class S, layout_type L,
class FST>
555 return const_reverse_linear_iterator(this->linear_cend());
562 template <
class CT,
class S, layout_type L,
class FST>
563 template <
class ST,
class STEP>
567 return stepper(
this, data_xbegin(), offset);
570 template <
class CT,
class S, layout_type L,
class FST>
571 template <
class ST,
class STEP>
573 -> disable_indexed_stepper_t<STEP>
576 return stepper(
this, data_xend(l, offset), offset);
579 template <
class CT,
class S, layout_type L,
class FST>
580 template <
class ST,
class STEP>
584 return stepper(
this, offset);
587 template <
class CT,
class S, layout_type L,
class FST>
588 template <
class ST,
class STEP>
590 -> enable_indexed_stepper_t<STEP>
593 return stepper(
this, offset,
true);
596 template <
class CT,
class S, layout_type L,
class FST>
597 template <
class ST,
class STEP>
599 -> disable_indexed_stepper_t<STEP>
602 return const_stepper(
this, data_xbegin(), offset);
605 template <
class CT,
class S, layout_type L,
class FST>
606 template <
class ST,
class STEP>
608 -> disable_indexed_stepper_t<STEP>
611 return const_stepper(
this, data_xend(l, offset), offset);
614 template <
class CT,
class S, layout_type L,
class FST>
615 template <
class ST,
class STEP>
617 -> enable_indexed_stepper_t<STEP>
620 return const_stepper(
this, offset);
623 template <
class CT,
class S, layout_type L,
class FST>
624 template <
class ST,
class STEP>
626 -> enable_indexed_stepper_t<STEP>
629 return const_stepper(
this, offset,
true);
632 template <
class CT,
class S, layout_type L,
class FST>
636 return begin +
static_cast<std::ptrdiff_t
>(this->
data_offset());
639 template <
class CT,
class S, layout_type L,
class FST>
644 return strided_data_end(*
this, begin + std::ptrdiff_t(this->
data_offset()), l, offset);
647 template <
class CT,
class S, layout_type L,
class FST>
650 return data_xbegin_impl(this->
storage().begin());
653 template <
class CT,
class S, layout_type L,
class FST>
656 return data_xbegin_impl(this->
storage().cbegin());
659 template <
class CT,
class S, layout_type L,
class FST>
661 -> container_iterator
663 return data_xend_impl(this->
storage().begin(), l, offset);
666 template <
class CT,
class S, layout_type L,
class FST>
668 -> const_container_iterator
670 return data_xend_impl(this->
storage().cbegin(), l, offset);
673 template <
class CT,
class S, layout_type L,
class FST>
674 template <
class alignment,
class simd>
676 requires provides_simd_interface
678 using align_mode = driven_align_mode_t<alignment, data_alignment>;
679 xt_simd::store_as(&(
storage()[i]), e, align_mode());
682 template <
class CT,
class S, layout_type L,
class FST>
683 template <
class alignment,
class requested_type, std::
size_t N>
685 requires provides_simd_interface
687 using align_mode = driven_align_mode_t<alignment, data_alignment>;
688 return xt_simd::load_as<requested_type>(&(
storage()[i]), align_mode());
691 template <
class CT,
class S, layout_type L,
class FST>
695 inner_shape_type sh(this->
shape());
696 inner_strides_type str(this->
strides());
726 template <layout_type L,
class E,
class S,
class X>
730 return view_type(std::forward<E>(e), std::forward<S>(shape), std::forward<X>(
strides), offset,
layout);
735 struct no_adj_strides_policy
739 inline void resize(std::size_t)
743 inline void set_fake_slice(std::size_t)
747 template <
class ST,
class S>
749 const xstrided_slice_vector& ,
791 detail::strided_view_args<detail::no_adj_strides_policy> args;
794 detail::get_strides<XTENSOR_DEFAULT_TRAVERSAL>(e),
795 detail::get_offset<XTENSOR_DEFAULT_TRAVERSAL>(e),
802 std::move(args.new_shape),
803 std::move(args.new_strides),
811 template <
typename S>
814 template <std::size_t... X>
815 struct rebind_shape<
xt::fixed_shape<X...>>
823 using type = rebind_container_t<size_t, S>;
827 inline void recalculate_shape_impl(S& shape,
size_t size)
829 if constexpr (std::is_signed_v<get_value_type_t<typename std::decay<S>::type>>)
831 using value_type = get_value_type_t<typename std::decay_t<S>>;
832 XTENSOR_ASSERT(std::count(shape.cbegin(), shape.cend(), -1) <= 1);
833 auto iter = std::find(shape.begin(), shape.end(), -1);
834 if (iter != std::end(shape))
836 const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies<int>{});
837 const auto missing_dimension = size / total;
838 (*iter) =
static_cast<value_type
>(missing_dimension);
844 inline auto recalculate_shape(S&& shape,
size_t size)
846 return recalculate_shape_impl(shape, size);
850 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class S>
851 inline auto reshape_view(E&& e, S&&
shape)
855 "traversal has to be row or column major"
858 using shape_type = std::decay_t<
decltype(
shape)>;
859 using unsigned_shape_type =
typename detail::rebind_shape<shape_type>::type;
860 get_strides_t<unsigned_shape_type>
strides;
862 detail::recalculate_shape(
shape, e.size());
865 constexpr auto computed_layout = std::decay_t<E>::static_layout == L ? L :
layout_type::dynamic;
870 detail::flat_adaptor_getter<xclosure_t<E>, L>>;
873 xtl::forward_sequence<unsigned_shape_type, S>(
shape),
893 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class S>
896 return reshape_view<L>(std::forward<E>(e), std::forward<S>(shape));
899 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class I, std::
size_t N>
902 using shape_type = std::array<std::size_t, N>;
903 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type,
decltype(
shape)>(
shape), order);
906 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class I, std::
size_t N>
907 inline auto reshape_view(E&& e,
const I (&
shape)[N])
909 using shape_type = std::array<I, N>;
910 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
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.