10#ifndef XTENSOR_INDEX_VIEW_HPP
11#define XTENSOR_INDEX_VIEW_HPP
19#include "../core/xexpression.hpp"
20#include "../core/xiterable.hpp"
21#include "../core/xoperation.hpp"
22#include "../core/xsemantic.hpp"
23#include "../core/xstrides.hpp"
24#include "../utils/xutils.hpp"
35 template <
class Tag,
class CT,
class I>
38 template <
class CT,
class I>
44 template <
class CT,
class I>
49 template <
class CT,
class I>
57 template <
class CT,
class I>
60 template <
class CT,
class I>
63 using xexpression_type = std::decay_t<CT>;
67 template <
class CT,
class I>
70 using inner_shape_type = std::array<std::size_t, 1>;
89 template <
class CT,
class I>
90 class xindex_view :
public xview_semantic<xindex_view<CT, I>>,
92 public extension::xindex_view_base_t<CT, I>
97 using xexpression_type = std::decay_t<CT>;
98 using semantic_base = xview_semantic<self_type>;
100 using extension_base = extension::xindex_view_base_t<CT, I>;
101 using expression_tag =
typename extension_base::expression_tag;
103 using value_type =
typename xexpression_type::value_type;
104 using reference = inner_reference_t<CT>;
105 using const_reference =
typename xexpression_type::const_reference;
106 using pointer =
typename xexpression_type::pointer;
107 using const_pointer =
typename xexpression_type::const_pointer;
108 using size_type =
typename xexpression_type::size_type;
109 using difference_type =
typename xexpression_type::difference_type;
112 using inner_shape_type =
typename iterable_base::inner_shape_type;
113 using shape_type = inner_shape_type;
115 using indices_type = I;
117 using stepper =
typename iterable_base::stepper;
118 using const_stepper =
typename iterable_base::const_stepper;
121 using base_index_type = xindex_type_t<shape_type>;
123 using bool_load_type =
typename xexpression_type::bool_load_type;
126 static constexpr bool contiguous_layout =
false;
128 template <
class CTA,
class I2>
135 disable_xexpression<E, self_type>& operator=(
const E& e);
137 size_type
size() const noexcept;
139 const inner_shape_type&
shape() const noexcept;
140 size_type
shape(size_type index) const;
142 bool is_contiguous() const noexcept;
147 reference operator()(size_type idx = size_type(0));
148 template <class... Args>
149 reference operator()(size_type idx0, size_type idx1, Args... args);
152 disable_integral_t<S, reference> operator[](const S& index);
154 reference operator[](std::initializer_list<OI> index);
155 reference operator[](size_type i);
158 reference element(It first, It last);
160 const_reference operator()(size_type idx = size_type(0)) const;
161 template <class... Args>
162 const_reference operator()(size_type idx0, size_type idx1, Args... args) const;
165 disable_integral_t<S, const_reference> operator[](const S& index) const;
167 const_reference operator[](std::initializer_list<OI> index) const;
168 const_reference operator[](size_type i) const;
171 const_reference element(It first, It last) const;
183 stepper stepper_begin(const ST&
shape);
188 const_stepper stepper_begin(const ST&
shape) const;
196 rebind_t<E> build_index_view(E&& e) const;
201 const indices_type m_indices;
202 const inner_shape_type m_shape;
204 void assign_temporary_impl(temporary_type&& tmp);
231 template <class ECT, class CCT>
237 using xexpression_type = std::decay_t<ECT>;
238 using const_reference =
typename xexpression_type::const_reference;
240 template <
class ECTA,
class CCTA>
244 disable_xexpression<E, self_type&> operator=(
const E&);
247 disable_xexpression<E, self_type&>
operator+=(
const E&);
250 disable_xexpression<E, self_type&>
operator-=(
const E&);
253 disable_xexpression<E, self_type&>
operator*=(
const E&);
256 disable_xexpression<E, self_type&>
operator/=(
const E&);
259 disable_xexpression<E, self_type&>
operator%=(
const E&);
264 self_type& apply(F&& func);
285 template <
class CT,
class I>
286 template <
class CTA,
class I2>
288 : m_e(std::forward<CTA>(e))
289 , m_indices(std::forward<I2>(indices))
290 , m_shape({m_indices.size()})
303 template <
class CT,
class I>
307 return semantic_base::operator=(e);
312 template <
class CT,
class I>
314 inline auto xindex_view<CT, I>::operator=(
const E& e) -> disable_xexpression<E, self_type>&
316 std::fill(this->
begin(), this->
end(), e);
320 template <
class CT,
class I>
321 inline void xindex_view<CT, I>::assign_temporary_impl(temporary_type&& tmp)
323 std::copy(tmp.cbegin(), tmp.cend(), this->begin());
333 template <
class CT,
class I>
336 return compute_size(
shape());
342 template <
class CT,
class I>
351 template <
class CT,
class I>
360 template <
class CT,
class I>
366 template <
class CT,
class I>
367 inline layout_type xindex_view<CT, I>::layout() const noexcept
369 return static_layout;
372 template <
class CT,
class I>
373 inline bool xindex_view<CT, I>::is_contiguous() const noexcept
389 template <
class CT,
class I>
393 std::fill(this->
begin(), this->
end(), value);
401 template <
class CT,
class I>
404 return m_e[m_indices[idx]];
407 template <
class CT,
class I>
408 template <
class... Args>
411 return this->
operator()(idx1,
static_cast<size_type
>(args)...);
418 template <
class CT,
class I>
429 template <
class CT,
class I>
432 return m_e[m_indices[idx]];
435 template <
class CT,
class I>
436 template <
class... Args>
446 template <
class CT,
class I>
458 template <
class CT,
class I>
460 inline auto xindex_view<CT, I>::operator[](
const S& index) -> disable_integral_t<S, reference>
462 return m_e[m_indices[index[0]]];
465 template <
class CT,
class I>
467 inline auto xindex_view<CT, I>::operator[](std::initializer_list<OI> index) -> reference
469 return m_e[m_indices[*(index.begin())]];
472 template <
class CT,
class I>
473 inline auto xindex_view<CT, I>::operator[](size_type i) -> reference
484 template <
class CT,
class I>
486 inline auto xindex_view<CT, I>::operator[](
const S& index)
const -> disable_integral_t<S, const_reference>
488 return m_e[m_indices[index[0]]];
491 template <
class CT,
class I>
493 inline auto xindex_view<CT, I>::operator[](std::initializer_list<OI> index)
const -> const_reference
495 return m_e[m_indices[*(index.begin())]];
498 template <
class CT,
class I>
499 inline auto xindex_view<CT, I>::operator[](size_type i)
const -> const_reference
510 template <
class CT,
class I>
512 inline auto xindex_view<CT, I>::element(It first, It ) -> reference
514 return m_e[m_indices[(*first)]];
523 template <
class CT,
class I>
525 inline auto xindex_view<CT, I>::element(It first, It )
const -> const_reference
527 return m_e[m_indices[(*first)]];
533 template <
class CT,
class I>
542 template <
class CT,
class I>
560 template <
class CT,
class I>
564 return xt::broadcast_shape(m_shape,
shape);
572 template <
class CT,
class I>
585 template <
class CT,
class I>
587 inline auto xindex_view<CT, I>::stepper_begin(
const ST& shape) -> stepper
589 size_type offset = shape.size() -
dimension();
590 return stepper(
this, offset);
593 template <
class CT,
class I>
595 inline auto xindex_view<CT, I>::stepper_end(
const ST&
shape,
layout_type) -> stepper
598 return stepper(
this, offset,
true);
601 template <
class CT,
class I>
606 return const_stepper(
this, offset);
609 template <
class CT,
class I>
614 return const_stepper(
this, offset,
true);
617 template <
class CT,
class I>
621 return rebind_t<E>(std::forward<E>(e), indices_type(m_indices));
639 template <
class ECT,
class CCT>
640 template <
class ECTA,
class CCTA>
642 : m_e(std::forward<ECTA>(e))
643 , m_condition(std::forward<CCTA>(condition))
658 template <
class ECT,
class CCT>
660 inline auto xfiltration<ECT, CCT>::operator=(
const E& e) -> disable_xexpression<E, self_type&>
663 [
this, &e](const_reference v,
bool cond)
681 template <
class ECT,
class CCT>
683 inline auto xfiltration<ECT, CCT>::operator+=(
const E& e) -> disable_xexpression<E, self_type&>
686 [&e](const_reference v,
bool cond)
688 return cond ? v + e : v;
698 template <
class ECT,
class CCT>
700 inline auto xfiltration<ECT, CCT>::operator-=(
const E& e) -> disable_xexpression<E, self_type&>
703 [&e](const_reference v,
bool cond)
705 return cond ? v - e : v;
715 template <
class ECT,
class CCT>
717 inline auto xfiltration<ECT, CCT>::operator*=(
const E& e) -> disable_xexpression<E, self_type&>
720 [&e](const_reference v,
bool cond)
722 return cond ? v * e : v;
732 template <
class ECT,
class CCT>
734 inline auto xfiltration<ECT, CCT>::operator/=(
const E& e) -> disable_xexpression<E, self_type&>
737 [&e](const_reference v,
bool cond)
739 return cond ? v / e : v;
749 template <
class ECT,
class CCT>
751 inline auto xfiltration<ECT, CCT>::operator%=(
const E& e) -> disable_xexpression<E, self_type&>
754 [&e](const_reference v,
bool cond)
756 return cond ? v % e : v;
761 template <
class ECT,
class CCT>
763 inline auto xfiltration<ECT, CCT>::apply(F&& func) -> self_type&
765 std::transform(m_e.cbegin(), m_e.cend(), m_condition.cbegin(), m_e.begin(), func);
785 template <
class E,
class I>
789 return view_type(std::forward<E>(e), std::forward<I>(indices));
792 template <
class E, std::
size_t L>
793 inline auto index_view(E&& e,
const xindex (&indices)[L])
noexcept
795 using view_type = xindex_view<xclosure_t<E>, std::array<xindex, L>>;
796 return view_type(std::forward<E>(e), xt::to_array(indices));
820 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class O>
821 inline auto filter(E&& e, O&& condition)
noexcept
823 auto indices =
argwhere<L>(std::forward<O>(condition));
825 return view_type(std::forward<E>(e), std::move(indices));
845 template <
class E,
class C>
849 return filtration_type(std::forward<E>(e), std::forward<C>(condition));
Base class for xexpressions.
Filter of a xexpression for fast scalar assign.
xfiltration(ECTA &&e, CCTA &&condition)
Constructs a xfiltration on the given expression e, selecting the elements matching the specified con...
View of an xexpression from vector of indices.
xindex_view(CTA &&e, I2 &&indices) noexcept
Constructs an xindex_view, selecting the indices specified by indices.
bool has_linear_assign(const O &) const noexcept
size_type size() const noexcept
Returns the size of the xindex_view.
reference unchecked(size_type idx)
reference operator()(size_type idx=size_type(0))
Returns a reference to the element at the specified position in the xindex_view.
xexpression_type & expression() noexcept
bool broadcast_shape(O &shape, bool reuse_cache=false) const
const inner_shape_type & shape() const noexcept
void fill(const T &value)
size_type dimension() const noexcept
Base class for multidimensional iterable expressions.
auto end() noexcept -> layout_iterator< L >
auto begin() noexcept -> layout_iterator< L >
auto operator%=(const E &e) -> disable_xexpression< E, derived_type & >
auto operator+=(const E &e) -> disable_xexpression< E, derived_type & >
auto operator*=(const E &e) -> disable_xexpression< E, derived_type & >
auto operator/=(const E &e) -> disable_xexpression< E, derived_type & >
auto operator-=(const E &e) -> disable_xexpression< E, derived_type & >
auto argwhere(const T &arr)
return vector of indices where arr is not zero
standard mathematical functions for xexpressions
xarray_container< uvector< T, A >, L, xt::svector< typename uvector< T, A >::size_type, 4, SA, true > > xarray
Alias template on xarray_container with default parameters for data container type and shape / stride...
auto filtration(E &&e, C &&condition) noexcept
creates a filtration of e filtered by condition.
auto index_view(E &&e, I &&indices) noexcept
creates an indexview from a container of indices.
auto filter(E &&e, O &&condition) noexcept
creates a view into e filtered by condition.