10#ifndef XTENSOR_AXIS_ITERATOR_HPP
11#define XTENSOR_AXIS_ITERATOR_HPP
13#include "../views/xstrided_view.hpp"
39 using xexpression_type = std::decay_t<CT>;
40 using size_type =
typename xexpression_type::size_type;
41 using difference_type =
typename xexpression_type::difference_type;
42 using shape_type =
typename xexpression_type::shape_type;
44 using reference = std::remove_reference_t<xtl::apply_cv_t<CT, value_type>>;
45 using pointer = xtl::xclosure_pointer<std::remove_reference_t<xtl::apply_cv_t<CT, value_type>>>;
47 using iterator_category = std::forward_iterator_tag;
52 xaxis_iterator(CTA&& e, size_type axis, size_type index, size_type offset);
60 bool equal(
const self_type& rhs)
const;
64 using storing_type = xtl::ptr_closure_type_t<CT>;
65 mutable storing_type p_expression;
67 size_type m_add_offset;
70 template <
class T,
class CTA>
71 std::enable_if_t<std::is_pointer<T>::value, T> get_storage_init(CTA&& e)
const;
73 template <
class T,
class CTA>
74 std::enable_if_t<!std::is_pointer<T>::value, T> get_storage_init(CTA&& e)
const;
87 auto axis_begin(E&& e,
typename std::decay_t<E>::size_type axis);
93 auto axis_end(E&& e,
typename std::decay_t<E>::size_type axis);
102 auto derive_xstrided_view(
104 typename std::decay_t<CT>::size_type axis,
105 typename std::decay_t<CT>::size_type offset
108 using xexpression_type = std::decay_t<CT>;
109 using shape_type =
typename xexpression_type::shape_type;
110 using strides_type =
typename xexpression_type::strides_type;
112 const auto& e_shape = e.shape();
113 shape_type shape(e_shape.size() - 1);
114 auto nxt = std::copy(e_shape.cbegin(), e_shape.cbegin() + axis, shape.begin());
115 std::copy(e_shape.cbegin() + axis + 1, e_shape.end(), nxt);
117 const auto& e_strides = e.strides();
118 strides_type
strides(e_strides.size() - 1);
119 auto nxt_strides = std::copy(e_strides.cbegin(), e_strides.cbegin() + axis,
strides.begin());
120 std::copy(e_strides.cbegin() + axis + 1, e_strides.end(), nxt_strides);
122 return strided_view(std::forward<CT>(e), std::move(shape), std::move(
strides), offset, e.layout());
127 template <
class T,
class CTA>
128 inline std::enable_if_t<std::is_pointer<T>::value, T> xaxis_iterator<CT>::get_storage_init(CTA&& e)
const
134 template <
class T,
class CTA>
135 inline std::enable_if_t<!std::is_pointer<T>::value, T> xaxis_iterator<CT>::get_storage_init(CTA&& e)
const
168 : p_expression(get_storage_init<storing_type>(std::forward<CTA>(e)))
170 , m_add_offset(static_cast<size_type>(e.
strides()[axis]))
171 , m_sv(detail::derive_xstrided_view<CTA>(std::forward<CTA>(e), axis, offset))
187 m_sv.set_offset(m_sv.data_offset() + m_add_offset);
199 self_type tmp(*
this);
229 return xtl::closure_pointer(
operator*());
247 return p_expression == rhs.p_expression && m_index == rhs.m_index
259 return lhs.
equal(rhs);
269 return !(lhs == rhs);
288 return return_type(std::forward<E>(e), 0);
299 inline auto axis_begin(E&& e,
typename std::decay_t<E>::size_type axis)
302 return return_type(std::forward<E>(e), axis);
315 using size_type =
typename std::decay_t<E>::size_type;
321 static_cast<size_type
>(e.strides()[0]) * e.shape()[0]
334 inline auto axis_end(E&& e,
typename std::decay_t<E>::size_type axis)
336 using size_type =
typename std::decay_t<E>::size_type;
342 static_cast<size_type
>(e.strides()[axis]) * e.shape()[axis]
Class for iteration over (N-1)-dimensional slices, where N is the dimension of the underlying express...
reference operator*() const
Returns the strided view at the current iteration position.
pointer operator->() const
Returns a pointer to the strided view at the current iteration position.
xaxis_iterator(CTA &&e, size_type axis)
Constructs an xaxis_iterator.
self_type & operator++()
Increments the iterator to the next position and returns it.
bool equal(const self_type &rhs) const
Checks equality of the xaxis_slice_iterator and rhs.
View of an xexpression using strides.
size_type data_offset() const noexcept
Returns the offset to the first element in the view.
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
standard mathematical functions for xexpressions
auto axis_begin(E &&e)
Returns an iterator to the first element of the expression for axis 0.
auto axis_end(E &&e)
Returns an iterator to the element following the last element of the expression for axis 0.
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.