10#ifndef XTENSOR_STRIDED_VIEW_HPP
11#define XTENSOR_STRIDED_VIEW_HPP
19#include <xtl/xsequence.hpp>
20#include <xtl/xvariant.hpp>
22#include "xexpression.hpp"
23#include "xiterable.hpp"
25#include "xsemantic.hpp"
26#include "xstorage.hpp"
27#include "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;
183 template <
class CTA,
class SA>
188 self_type& operator=(
const self_type&);
194 disable_xexpression<E, self_type>& operator=(
const E& e);
198 using base_type::is_contiguous;
204 using base_type::operator();
206 using base_type::unchecked;
207 using base_type::operator[];
208 using base_type::data;
210 using base_type::element;
220 linear_iterator linear_begin();
221 linear_iterator linear_end();
222 const_linear_iterator linear_begin()
const;
223 const_linear_iterator linear_end()
const;
224 const_linear_iterator linear_cbegin()
const;
225 const_linear_iterator linear_cend()
const;
227 reverse_linear_iterator linear_rbegin();
228 reverse_linear_iterator linear_rend();
229 const_reverse_linear_iterator linear_rbegin()
const;
230 const_reverse_linear_iterator linear_rend()
const;
231 const_reverse_linear_iterator linear_crbegin()
const;
232 const_reverse_linear_iterator linear_crend()
const;
234 template <
class ST,
class STEP = stepper>
235 disable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape);
236 template <
class ST,
class STEP = stepper>
237 disable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l);
239 template <
class ST,
class STEP = stepper>
240 enable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape);
241 template <
class ST,
class STEP = stepper>
244 template <
class ST,
class STEP = const_stepper>
245 disable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape)
const;
246 template <
class ST,
class STEP = const_stepper>
247 disable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l)
const;
249 template <
class ST,
class STEP = const_stepper>
250 enable_indexed_stepper_t<STEP> stepper_begin(
const ST&
shape)
const;
251 template <
class ST,
class STEP = const_stepper>
252 enable_indexed_stepper_t<STEP> stepper_end(
const ST&
shape,
layout_type l)
const;
254 template <
class requested_type>
255 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
257 template <
class T,
class R>
258 using enable_simd_interface = std::enable_if_t<has_simd_interface<T>::value && L !=
layout_type::dynamic, R>;
260 template <
class align,
class simd,
class T = xexpression_type>
261 enable_simd_interface<T, void> store_simd(size_type i,
const simd& e);
264 class requested_type = value_type,
265 std::size_t N = xt_simd::simd_traits<requested_type>::size,
266 class T = xexpression_type>
267 enable_simd_interface<T, simd_return_type<requested_type>> load_simd(size_type i)
const;
269 reference data_element(size_type i);
270 const_reference data_element(size_type i)
const;
272 reference flat(size_type i);
273 const_reference flat(size_type i)
const;
275 using container_iterator = std::
276 conditional_t<is_const, typename storage_type::const_iterator, typename storage_type::iterator>;
277 using const_container_iterator =
typename storage_type::const_iterator;
283 rebind_t<E> build_view(E&& e)
const;
287 container_iterator data_xbegin() noexcept;
288 const_container_iterator data_xbegin() const noexcept;
289 container_iterator data_xend(
layout_type l, size_type offset) noexcept;
290 const_container_iterator data_xend(
layout_type l, size_type offset) const noexcept;
293 It data_xbegin_impl(It begin) const noexcept;
296 It data_xend_impl(It end,
layout_type l, size_type offset) const noexcept;
298 void assign_temporary_impl(temporary_type&& tmp);
300 using base_type::set_offset;
303 friend class xstepper;
304 friend class xview_semantic<self_type>;
308 friend class xaxis_iterator;
310 friend class xaxis_slice_iterator;
318 using xstrided_slice = xtl::variant<
330 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, placeholders::xtuph>,
368 template <class CT, class S,
layout_type L, class FST>
369 template <class CTA, class SA>
377 : base_type(std::forward<CTA>(e), std::forward<SA>(
shape), std::move(
strides), offset,
layout)
383 template <
class CT,
class S, layout_type L,
class FST>
384 inline auto xstrided_view<CT, S, L, FST>::operator=(
const self_type& rhs) -> self_type&
397 template <
class CT,
class S, layout_type L,
class FST>
399 inline auto xstrided_view<CT, S, L, FST>::operator=(
const xexpression<E>& e) -> self_type&
401 return semantic_base::operator=(e);
406 template <
class CT,
class S, layout_type L,
class FST>
408 inline auto xstrided_view<CT, S, L, FST>::operator=(
const E& e) -> disable_xexpression<E, self_type>&
414 namespace xstrided_view_detail
416 template <
class V,
class T>
417 inline void run_assign_temporary_impl(V& v,
const T& t, std::true_type )
419 strided_loop_assigner<true>::run(v, t);
422 template <
class V,
class T>
424 run_assign_temporary_impl(V& v,
const T& t, std::false_type )
426 std::copy(t.cbegin(), t.cend(), v.begin());
430 template <
class CT,
class S, layout_type L,
class FST>
434 fast_assign = xassign_traits<xstrided_view<CT, S, L, FST>, temporary_type>::simd_strided_assign();
435 xstrided_view_detail::run_assign_temporary_impl(*
this, tmp, std::integral_constant<bool, fast_assign>{});
447 template <
class CT,
class S, layout_type L,
class FST>
453 std::fill(this->linear_begin(), this->linear_end(), value);
457 std::fill(this->begin(), this->end(), value);
463 template <
class CT,
class S, layout_type L,
class FST>
464 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) -> reference
469 template <
class CT,
class S, layout_type L,
class FST>
470 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i)
const -> const_reference
475 template <
class CT,
class S, layout_type L,
class FST>
481 template <
class CT,
class S, layout_type L,
class FST>
487 template <
class CT,
class S, layout_type L,
class FST>
493 template <
class CT,
class S, layout_type L,
class FST>
499 template <
class CT,
class S, layout_type L,
class FST>
502 return this->linear_cbegin();
505 template <
class CT,
class S, layout_type L,
class FST>
508 return this->linear_cend();
511 template <
class CT,
class S, layout_type L,
class FST>
517 template <
class CT,
class S, layout_type L,
class FST>
523 template <
class CT,
class S, layout_type L,
class FST>
526 return reverse_linear_iterator(this->linear_begin());
529 template <
class CT,
class S, layout_type L,
class FST>
532 return reverse_linear_iterator(this->linear_end());
535 template <
class CT,
class S, layout_type L,
class FST>
538 return this->linear_crbegin();
541 template <
class CT,
class S, layout_type L,
class FST>
544 return this->linear_crend();
547 template <
class CT,
class S, layout_type L,
class FST>
550 return const_reverse_linear_iterator(this->linear_cbegin());
553 template <
class CT,
class S, layout_type L,
class FST>
556 return const_reverse_linear_iterator(this->linear_cend());
563 template <
class CT,
class S, layout_type L,
class FST>
564 template <
class ST,
class STEP>
568 return stepper(
this, data_xbegin(), offset);
571 template <
class CT,
class S, layout_type L,
class FST>
572 template <
class ST,
class STEP>
574 -> disable_indexed_stepper_t<STEP>
577 return stepper(
this, data_xend(l, offset), offset);
580 template <
class CT,
class S, layout_type L,
class FST>
581 template <
class ST,
class STEP>
585 return stepper(
this, offset);
588 template <
class CT,
class S, layout_type L,
class FST>
589 template <
class ST,
class STEP>
591 -> enable_indexed_stepper_t<STEP>
594 return stepper(
this, offset,
true);
597 template <
class CT,
class S, layout_type L,
class FST>
598 template <
class ST,
class STEP>
600 -> disable_indexed_stepper_t<STEP>
603 return const_stepper(
this, data_xbegin(), offset);
606 template <
class CT,
class S, layout_type L,
class FST>
607 template <
class ST,
class STEP>
609 -> disable_indexed_stepper_t<STEP>
612 return const_stepper(
this, data_xend(l, offset), offset);
615 template <
class CT,
class S, layout_type L,
class FST>
616 template <
class ST,
class STEP>
618 -> enable_indexed_stepper_t<STEP>
621 return const_stepper(
this, offset);
624 template <
class CT,
class S, layout_type L,
class FST>
625 template <
class ST,
class STEP>
627 -> enable_indexed_stepper_t<STEP>
630 return const_stepper(
this, offset,
true);
633 template <
class CT,
class S, layout_type L,
class FST>
637 return begin +
static_cast<std::ptrdiff_t
>(this->
data_offset());
640 template <
class CT,
class S, layout_type L,
class FST>
645 return strided_data_end(*
this, begin + std::ptrdiff_t(this->
data_offset()), l, offset);
648 template <
class CT,
class S, layout_type L,
class FST>
651 return data_xbegin_impl(this->
storage().begin());
654 template <
class CT,
class S, layout_type L,
class FST>
657 return data_xbegin_impl(this->
storage().cbegin());
660 template <
class CT,
class S, layout_type L,
class FST>
662 -> container_iterator
664 return data_xend_impl(this->
storage().begin(), l, offset);
667 template <
class CT,
class S, layout_type L,
class FST>
669 -> const_container_iterator
671 return data_xend_impl(this->
storage().cbegin(), l, offset);
674 template <
class CT,
class S, layout_type L,
class FST>
675 template <
class alignment,
class simd,
class T>
677 -> enable_simd_interface<T, void>
679 using align_mode = driven_align_mode_t<alignment, data_alignment>;
680 xt_simd::store_as(&(
storage()[i]), e, align_mode());
683 template <
class CT,
class S, layout_type L,
class FST>
684 template <
class alignment,
class requested_type, std::
size_t N,
class T>
686 -> enable_simd_interface<T, simd_return_type<requested_type>>
688 using align_mode = driven_align_mode_t<alignment, data_alignment>;
689 return xt_simd::load_as<requested_type>(&(
storage()[i]), align_mode());
692 template <
class CT,
class S, layout_type L,
class FST>
696 inner_shape_type sh(this->
shape());
697 inner_strides_type str(this->
strides());
727 template <layout_type L,
class E,
class S,
class X>
731 return view_type(std::forward<E>(e), std::forward<S>(shape), std::forward<X>(
strides), offset,
layout);
736 struct no_adj_strides_policy
740 inline void resize(std::size_t)
744 inline void set_fake_slice(std::size_t)
748 template <
class ST,
class S>
750 const xstrided_slice_vector& ,
792 detail::strided_view_args<detail::no_adj_strides_policy> args;
795 detail::get_strides<XTENSOR_DEFAULT_TRAVERSAL>(e),
796 detail::get_offset<XTENSOR_DEFAULT_TRAVERSAL>(e),
803 std::move(args.new_shape),
804 std::move(args.new_strides),
812 template <
typename S>
815 template <std::size_t... X>
816 struct rebind_shape<
xt::fixed_shape<X...>>
824 using type = rebind_container_t<size_t, S>;
829 std::enable_if_t<std::is_signed<get_value_type_t<typename std::decay<S>::type>>::value,
bool> =
true>
830 inline void recalculate_shape_impl(S& shape,
size_t size)
832 using value_type = get_value_type_t<typename std::decay_t<S>>;
833 XTENSOR_ASSERT(std::count(shape.cbegin(), shape.cend(), -1) <= 1);
834 auto iter = std::find(shape.begin(), shape.end(), -1);
835 if (iter != std::end(shape))
837 const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies<int>{});
838 const auto missing_dimension = size / total;
839 (*iter) =
static_cast<value_type
>(missing_dimension);
845 std::enable_if_t<!std::is_signed<get_value_type_t<typename std::decay<S>::type>>::value,
bool> =
true>
846 inline void recalculate_shape_impl(S&,
size_t)
851 inline auto recalculate_shape(S&& shape,
size_t size)
853 return recalculate_shape_impl(shape, size);
857 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class S>
858 inline auto reshape_view(E&& e, S&&
shape)
862 "traversal has to be row or column major"
865 using shape_type = std::decay_t<
decltype(
shape)>;
866 using unsigned_shape_type =
typename detail::rebind_shape<shape_type>::type;
867 get_strides_t<unsigned_shape_type>
strides;
869 detail::recalculate_shape(
shape, e.size());
872 constexpr auto computed_layout = std::decay_t<E>::static_layout == L ? L :
layout_type::dynamic;
877 detail::flat_adaptor_getter<xclosure_t<E>, L>>;
880 xtl::forward_sequence<unsigned_shape_type, S>(
shape),
900 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class S>
903 return reshape_view<L>(std::forward<E>(e), std::forward<S>(shape));
906 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class I, std::
size_t N>
909 using shape_type = std::array<std::size_t, N>;
910 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type,
decltype(
shape)>(
shape), order);
913 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class I, std::
size_t N>
914 inline auto reshape_view(E&& e,
const I (&
shape)[N])
916 using shape_type = std::array<I, N>;
917 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.