10#ifndef XTENSOR_SEMANTIC_HPP
11#define XTENSOR_SEMANTIC_HPP
16#include "../core/xassign.hpp"
17#include "../core/xassign_proxy.hpp"
18#include "../core/xexpression_traits.hpp"
27 static constexpr bool value =
true;
30 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
31 struct is_sharable<xfixed_container<ET, S, L, SH, Tag>>
33 static constexpr bool value = SH;
36 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
37 struct is_sharable<xfixed_adaptor<ET, S, L, SH, Tag>>
39 static constexpr bool value = SH;
44 using select_expression_base_t = std::
58 class xsemantic_base :
public select_expression_base_t<D>
62 using base_type = select_expression_base_t<D>;
63 using derived_type =
typename base_type::derived_type;
68 disable_xexpression<E, derived_type&>
operator+=(
const E&);
71 disable_xexpression<E, derived_type&>
operator-=(
const E&);
74 disable_xexpression<E, derived_type&>
operator*=(
const E&);
77 disable_xexpression<E, derived_type&>
operator/=(
const E&);
80 disable_xexpression<E, derived_type&>
operator%=(
const E&);
83 disable_xexpression<E, derived_type&>
operator&=(
const E&);
86 disable_xexpression<E, derived_type&>
operator|=(
const E&);
89 disable_xexpression<E, derived_type&>
operator^=(
const E&);
115 template <
class E,
class P>
118 template <
class E,
class P>
121 template <
class E,
class P>
124 template <
class E,
class P>
127 template <
class E,
class P>
130 template <
class E,
class P>
133 template <
class E,
class P>
136 template <
class E,
class P>
139 template <
class E,
class P>
144 xsemantic_base() =
default;
145 ~xsemantic_base() =
default;
147 xsemantic_base(
const xsemantic_base&) =
default;
148 xsemantic_base& operator=(
const xsemantic_base&) =
default;
150 xsemantic_base(xsemantic_base&&) =
default;
151 xsemantic_base& operator=(xsemantic_base&&) =
default;
158 template <
class E,
class P>
163 using is_assignable = is_crtp_base_of<xsemantic_base, E>;
165 template <
class E,
class R =
void>
166 using enable_assignable =
typename std::enable_if<is_assignable<E>::value, R>::type;
168 template <
class E,
class R =
void>
169 using disable_assignable =
typename std::enable_if<!is_assignable<E>::value, R>::type;
183 class xcontainer_semantic :
public xsemantic_base<D>
187 using base_type = xsemantic_base<D>;
188 using derived_type = D;
189 using temporary_type =
typename base_type::temporary_type;
193 template <
class E,
class P>
196 template <
class E,
class P>
199 template <
class E,
class F,
class P = assign_policy<>>
200 derived_type& scalar_computed_assign(
const E& e, F&& f, P p = P{});
218 using has_container_semantics = is_crtp_base_of<xcontainer_semantic, E>;
220 template <
class E,
class R =
void>
221 using enable_xcontainer_semantics =
typename std::enable_if<has_container_semantics<E>::value, R>::type;
223 template <
class E,
class R =
void>
224 using disable_xcontainer_semantics =
typename std::enable_if<!has_container_semantics<E>::value, R>::type;
228 class xview_semantic;
233 std::enable_if_t<!addressable_to_expression<E> && is_crtp_base_of<xview_semantic, E>::value>>
235 static bool check_overlap(
const E& expr,
const memory_range& dst_range)
237 if (expr.size() == 0)
243 using ChildE = std::decay_t<
decltype(expr.expression())>;
244 return overlapping_memory_checker_traits<ChildE>::check_overlap(expr.expression(), dst_range);
260 class xview_semantic :
public xsemantic_base<D>
264 using base_type = xsemantic_base<D>;
265 using derived_type = D;
266 using temporary_type =
typename base_type::temporary_type;
270 template <
class E,
class P>
273 template <
class E,
class P>
276 template <
class E,
class F,
class P = assign_policy<>>
277 derived_type& scalar_computed_assign(
const E& e, F&& f, P p = P{});
295 using has_view_semantics = is_crtp_base_of<xview_semantic, E>;
297 template <
class E,
class R =
void>
298 using enable_xview_semantics =
typename std::enable_if<has_view_semantics<E>::value, R>::type;
300 template <
class E,
class R =
void>
301 using disable_xview_semantics =
typename std::enable_if<!has_view_semantics<E>::value, R>::type;
320 return this->derived_cast().scalar_computed_assign(e, std::plus<>());
332 return this->derived_cast().scalar_computed_assign(e, std::minus<>());
344 return this->derived_cast().scalar_computed_assign(e, std::multiplies<>());
356 return this->derived_cast().scalar_computed_assign(e, std::divides<>());
368 return this->derived_cast().scalar_computed_assign(e, std::modulus<>());
380 return this->derived_cast().scalar_computed_assign(e, std::bit_and<>());
392 return this->derived_cast().scalar_computed_assign(e, std::bit_or<>());
404 return this->derived_cast().scalar_computed_assign(e, std::bit_xor<>());
416 return this->derived_cast().computed_assign(this->derived_cast() + e.derived_cast(), default_assign);
428 return this->derived_cast().computed_assign(this->derived_cast() - e.derived_cast(), default_assign);
440 return this->derived_cast().computed_assign(this->derived_cast() * e.derived_cast(), default_assign);
452 return this->derived_cast().computed_assign(this->derived_cast() / e.derived_cast(), default_assign);
464 return this->derived_cast().computed_assign(this->derived_cast() % e.derived_cast(), default_assign);
476 return this->derived_cast().computed_assign(this->derived_cast() & e.derived_cast(), default_assign);
488 return this->derived_cast().computed_assign(this->derived_cast() | e.derived_cast(), default_assign);
500 return this->derived_cast().computed_assign(this->derived_cast() ^ e.derived_cast(), default_assign);
516 template <
class E,
class P>
521#ifdef XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS
522 temporary_type tmp(e);
523 return this->derived_cast().assign_temporary(std::move(tmp));
525 auto&& this_derived = this->derived_cast();
526 auto memory_checker = make_overlapping_memory_checker(this_derived);
527 if (memory_checker.check_overlap(e.derived_cast()))
529 temporary_type tmp(e);
530 return this_derived.assign_temporary(std::move(tmp));
535 return this->derived_cast().assign_xexpression(e, new_policy{});
541 return this->derived_cast().assign_xexpression(e, p);
552 template <
class E,
class P>
555 return this->derived_cast().computed_assign(this->derived_cast() + e.derived_cast(), p);
565 template <
class E,
class P>
568 return this->derived_cast().computed_assign(this->derived_cast() - e.derived_cast(), p);
578 template <
class E,
class P>
581 return this->derived_cast().computed_assign(this->derived_cast() * e.derived_cast(), p);
591 template <
class E,
class P>
594 return this->derived_cast().computed_assign(this->derived_cast() / e.derived_cast(), p);
604 template <
class E,
class P>
607 return this->derived_cast().computed_assign(this->derived_cast() % e.derived_cast(), p);
617 template <
class E,
class P>
620 return this->derived_cast().computed_assign(this->derived_cast() & e.derived_cast(), p);
630 template <
class E,
class P>
633 return this->derived_cast().computed_assign(this->derived_cast() | e.derived_cast(), p);
643 template <
class E,
class P>
646 return this->derived_cast().computed_assign(this->derived_cast() ^ e.derived_cast(), p);
651 inline auto xsemantic_base<D>::operator=(
const xexpression<E>& e) -> derived_type&
653 return this->assign(e, default_assign);
657 template <
class E,
class P>
658 inline auto xsemantic_base<D>::computed_assign(
const xexpression<E>& e, P p) -> derived_type&
660 if constexpr (has_aliasing<P>)
662 temporary_type tmp(e);
663 return this->derived_cast().assign_temporary(std::move(tmp));
667 return this->derived_cast().eval_computed_assign(e, p);
683 return (this->derived_cast() = std::move(tmp));
687 template <
class E,
class P>
688 inline auto xcontainer_semantic<D>::assign_xexpression(
const xexpression<E>& e, P p) -> derived_type&
690 xt::assign_to_container(*
this, e, p);
691 return this->derived_cast();
695 template <
class E,
class P>
696 inline auto xcontainer_semantic<D>::eval_computed_assign(
const xexpression<E>& e, P p) -> derived_type&
698 xt::computed_assign(*
this, e, p);
699 return this->derived_cast();
703 template <
class E,
class F,
class P>
704 inline auto xcontainer_semantic<D>::scalar_computed_assign(
const E& e, F&& f, P p) -> derived_type&
706 xt::scalar_computed_assign(*
this, e, std::forward<F>(f), p);
707 return this->derived_cast();
712 inline auto xcontainer_semantic<D>::operator=(
const xexpression<E>& e) -> derived_type&
714 return base_type::operator=(e);
729 this->derived_cast().assign_temporary_impl(std::move(tmp));
730 return this->derived_cast();
734 template <
class E,
class P>
735 inline auto xview_semantic<D>::assign_xexpression(
const xexpression<E>& e, P p) -> derived_type&
737 xt::assign_to_view(*
this, e, p);
738 return this->derived_cast();
742 template <
class E,
class P>
743 inline auto xview_semantic<D>::eval_computed_assign(
const xexpression<E>& e, P p) -> derived_type&
745 xt::assign_to_view(*
this, e, p);
746 return this->derived_cast();
749 namespace xview_semantic_detail
752 auto get_begin(D&& lhs, std::true_type)
754 return lhs.linear_begin();
758 auto get_begin(D&& lhs, std::false_type)
765 template <
class E,
class F,
class P>
766 inline auto xview_semantic<D>::scalar_computed_assign(
const E& e, F&& f, P) -> derived_type&
768 D& d = this->derived_cast();
770 using size_type =
typename D::size_type;
771 auto dst = xview_semantic_detail::get_begin(d, std::integral_constant<bool, D::contiguous_layout>());
772 for (size_type i = d.size(); i > 0; --i)
777 return this->derived_cast();
782 inline auto xview_semantic<D>::operator=(
const xexpression<E>& rhs) -> derived_type&
784 bool cond = (rhs.derived_cast().shape().size() == this->derived_cast().dimension())
786 this->derived_cast().shape().begin(),
787 this->derived_cast().shape().end(),
788 rhs.derived_cast().shape().begin()
793 base_type::operator=(
broadcast(rhs.derived_cast(), this->derived_cast().shape()));
797 base_type::operator=(rhs);
799 return this->derived_cast();
Implementation of the xsemantic_base interface for dense multidimensional containers.
derived_type & assign_temporary(temporary_type &&)
Assigns the temporary tmp to *this.
Base class for xexpressions.
disable_xexpression< E, derived_type & > operator-=(const E &)
Subtracts the scalar e from *this.
derived_type & modulus_assign(const xexpression< E > &e, P p)
Computes the remainder of *this after division by the xexpression e.
disable_xexpression< E, derived_type & > operator+=(const E &)
Adds the scalar e to *this.
disable_xexpression< E, derived_type & > operator*=(const E &)
Multiplies *this with the scalar e.
disable_xexpression< E, derived_type & > operator^=(const E &)
Computes the bitwise xor of *this and the scalar e and assigns it to *this.
derived_type & plus_assign(const xexpression< E > &e, P p)
Adds the xexpression e to *this.
derived_type & bit_or_assign(const xexpression< E > &e, P p)
Computes the bitwise or of e to *this.
derived_type & bit_xor_assign(const xexpression< E > &e, P p)
Computes the bitwise xor of e to *this.
disable_xexpression< E, derived_type & > operator%=(const E &)
Computes the remainder of *this after division by the scalar e.
disable_xexpression< E, derived_type & > operator&=(const E &)
Computes the bitwise and of *this and the scalar e and assigns it to *this.
disable_xexpression< E, derived_type & > operator|=(const E &)
Computes the bitwise or of *this and the scalar e and assigns it to *this.
derived_type & minus_assign(const xexpression< E > &e, P p)
Subtracts the xexpression e to *this.
derived_type & multiplies_assign(const xexpression< E > &e, P p)
Multiplies *this with the xexpression e.
derived_type & divides_assign(const xexpression< E > &e, P p)
Divides *this by the xexpression e.
derived_type & assign(const xexpression< E > &e, P p)
Assigns the xexpression e to *this.
disable_xexpression< E, derived_type & > operator/=(const E &)
Divides *this by the scalar e.
derived_type & bit_and_assign(const xexpression< E > &e, P p)
Computes the bitwise and of e to *this.
Implementation of the xsemantic_base interface for multidimensional views.
derived_type & assign_temporary(temporary_type &&)
Assigns the temporary tmp to *this.
standard mathematical functions for xexpressions
auto broadcast(E &&e, const S &s)
Returns an xexpression broadcasting the given expression to a specified shape.