10#ifndef XTENSOR_INDEX_VIEW_HPP
11#define XTENSOR_INDEX_VIEW_HPP
20#include "xexpression.hpp"
21#include "xiterable.hpp"
22#include "xoperation.hpp"
23#include "xsemantic.hpp"
24#include "xstrides.hpp"
36 template <
class Tag,
class CT,
class I>
39 template <
class CT,
class I>
45 template <
class CT,
class I>
50 template <
class CT,
class I>
58 template <
class CT,
class I>
61 template <
class CT,
class I>
64 using xexpression_type = std::decay_t<CT>;
68 template <
class CT,
class I>
71 using inner_shape_type = std::array<std::size_t, 1>;
90 template <
class CT,
class I>
93 public extension::xindex_view_base_t<CT, I>
98 using xexpression_type = std::decay_t<CT>;
101 using extension_base = extension::xindex_view_base_t<CT, I>;
102 using expression_tag =
typename extension_base::expression_tag;
104 using value_type =
typename xexpression_type::value_type;
106 using const_reference =
typename xexpression_type::const_reference;
107 using pointer =
typename xexpression_type::pointer;
108 using const_pointer =
typename xexpression_type::const_pointer;
109 using size_type =
typename xexpression_type::size_type;
110 using difference_type =
typename xexpression_type::difference_type;
113 using inner_shape_type =
typename iterable_base::inner_shape_type;
114 using shape_type = inner_shape_type;
116 using indices_type = I;
118 using stepper =
typename iterable_base::stepper;
119 using const_stepper =
typename iterable_base::const_stepper;
124 using bool_load_type =
typename xexpression_type::bool_load_type;
127 static constexpr bool contiguous_layout =
false;
129 template <
class CTA,
class I2>
138 size_type
size()
const noexcept;
140 const inner_shape_type&
shape()
const noexcept;
141 size_type
shape(size_type index)
const;
143 bool is_contiguous()
const noexcept;
146 void fill(
const T& value);
149 template <
class...
Args>
155 reference operator[](std::initializer_list<OI> index);
161 const_reference
operator()(size_type idx = size_type(0))
const;
162 template <
class...
Args>
164 const_reference
unchecked(size_type idx)
const;
168 const_reference operator[](std::initializer_list<OI> index)
const;
169 const_reference operator[](size_type
i)
const;
175 const xexpression_type&
expression()
const noexcept;
184 stepper stepper_begin(
const ST&
shape);
189 const_stepper stepper_begin(
const ST&
shape)
const;
202 const indices_type m_indices;
203 const inner_shape_type m_shape;
205 void assign_temporary_impl(temporary_type&&
tmp);
232 template <
class ECT,
class CCT>
238 using xexpression_type = std::decay_t<ECT>;
239 using const_reference =
typename xexpression_type::const_reference;
241 template <
class ECTA,
class CCTA>
286 template <
class CT,
class I>
287 template <
class CTA,
class I2>
289 : m_e(std::forward<CTA>(
e))
290 , m_indices(std::forward<I2>(
indices))
291 , m_shape({m_indices.size()})
304 template <
class CT,
class I>
308 return semantic_base::operator=(
e);
313 template <
class CT,
class I>
317 std::fill(this->begin(), this->end(),
e);
321 template <
class CT,
class I>
322 inline void xindex_view<CT, I>::assign_temporary_impl(temporary_type&& tmp)
324 std::copy(tmp.cbegin(), tmp.cend(), this->begin());
334 template <
class CT,
class I>
337 return compute_size(shape());
343 template <
class CT,
class I>
352 template <
class CT,
class I>
361 template <
class CT,
class I>
367 template <
class CT,
class I>
370 return static_layout;
373 template <
class CT,
class I>
374 inline bool xindex_view<CT, I>::is_contiguous() const noexcept
390 template <
class CT,
class I>
394 std::fill(this->begin(), this->end(), value);
402 template <
class CT,
class I>
405 return m_e[m_indices[idx]];
408 template <
class CT,
class I>
409 template <
class...
Args>
412 return this->operator()(
idx1,
static_cast<size_type
>(
args)...);
419 template <
class CT,
class I>
422 return this->operator()(idx);
430 template <
class CT,
class I>
433 return m_e[m_indices[idx]];
436 template <
class CT,
class I>
437 template <
class...
Args>
440 return this->operator()(
idx1,
args...);
447 template <
class CT,
class I>
450 return this->operator()(idx);
459 template <
class CT,
class I>
463 return m_e[m_indices[index[0]]];
466 template <
class CT,
class I>
470 return m_e[m_indices[*(index.begin())]];
473 template <
class CT,
class I>
474 inline auto xindex_view<CT, I>::operator[](size_type i) -> reference
476 return operator()(i);
485 template <
class CT,
class I>
489 return m_e[m_indices[index[0]]];
492 template <
class CT,
class I>
496 return m_e[m_indices[*(index.begin())]];
499 template <
class CT,
class I>
500 inline auto xindex_view<CT, I>::operator[](size_type i)
const -> const_reference
502 return operator()(i);
510 template <
class CT,
class I>
514 return m_e[m_indices[(*first)]];
522 template <
class CT,
class I>
526 return m_e[m_indices[(*first)]];
532 template <
class CT,
class I>
541 template <
class CT,
class I>
559 template <
class CT,
class I>
563 return xt::broadcast_shape(m_shape, shape);
571 template <
class CT,
class I>
584 template <
class CT,
class I>
588 size_type
offset = shape.size() - dimension();
589 return stepper(
this,
offset);
592 template <
class CT,
class I>
594 inline auto xindex_view<CT, I>::stepper_end(
const ST& shape,
layout_type) -> stepper
596 size_type offset = shape.size() - dimension();
597 return stepper(
this, offset,
true);
600 template <
class CT,
class I>
602 inline auto xindex_view<CT, I>::stepper_begin(
const ST& shape)
const -> const_stepper
604 size_type offset = shape.size() - dimension();
605 return const_stepper(
this, offset);
608 template <
class CT,
class I>
610 inline auto xindex_view<CT, I>::stepper_end(
const ST& shape,
layout_type)
const -> const_stepper
612 size_type offset = shape.size() - dimension();
613 return const_stepper(
this, offset,
true);
616 template <
class CT,
class I>
618 inline auto xindex_view<CT, I>::build_index_view(E&& e)
const -> rebind_t<E>
620 return rebind_t<E>(std::forward<E>(e), indices_type(m_indices));
638 template <
class ECT,
class CCT>
639 template <
class ECTA,
class CCTA>
657 template <
class ECT,
class CCT>
662 [
this, &
e](const_reference
v,
bool cond)
680 template <
class ECT,
class CCT>
685 [&
e](const_reference
v,
bool cond)
687 return cond ?
v +
e :
v;
697 template <
class ECT,
class CCT>
702 [&
e](const_reference
v,
bool cond)
704 return cond ?
v -
e :
v;
714 template <
class ECT,
class CCT>
719 [&
e](const_reference
v,
bool cond)
721 return cond ?
v *
e :
v;
731 template <
class ECT,
class CCT>
736 [&
e](const_reference
v,
bool cond)
738 return cond ?
v /
e :
v;
748 template <
class ECT,
class CCT>
753 [&
e](const_reference
v,
bool cond)
755 return cond ?
v %
e :
v;
760 template <
class ECT,
class CCT>
764 std::transform(m_e.cbegin(), m_e.cend(), m_condition.cbegin(), m_e.begin(),
func);
784 template <
class E,
class I>
788 return view_type(std::forward<E>(
e), std::forward<I>(
indices));
791 template <
class E, std::
size_t L>
792 inline auto index_view(E&& e,
const xindex (&indices)[L])
noexcept
794 using view_type = xindex_view<xclosure_t<E>, std::array<xindex, L>>;
795 return view_type(std::forward<E>(e), xt::to_array(indices));
819 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL,
class E,
class O>
824 return view_type(std::forward<E>(
e), std::move(
indices));
844 template <
class E,
class C>
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
Checks whether the xindex_view can be linearly assigned to an expression with the specified strides.
size_type size() const noexcept
Returns the size of the xindex_view.
reference unchecked(size_type idx)
Returns a reference to the element at the specified position in the xindex_view.
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
Returns a reference to the underlying expression of the view.
bool broadcast_shape(O &shape, bool reuse_cache=false) const
Broadcast the shape of the xindex_view to the specified parameter.
const inner_shape_type & shape() const noexcept
Returns the shape of the xindex_view.
void fill(const T &value)
Fills the view with the given value.
size_type dimension() const noexcept
Returns the number of dimensions of the xindex_view.
Base class for multidimensional iterable expressions.
Implementation of the xsemantic_base interface for multidimensional views.
standard mathematical functions for xexpressions
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.