10#ifndef XTENSOR_ASSIGN_HPP
11#define XTENSOR_ASSIGN_HPP
18#include <xtl/xcomplex.hpp>
19#include <xtl/xsequence.hpp>
21#include "../core/xassign_proxy.hpp"
22#include "../core/xassigner.hpp"
23#include "../core/xexpression.hpp"
24#include "../core/xfunction.hpp"
25#include "../core/xiterator.hpp"
26#include "../core/xstrides.hpp"
27#include "../core/xtensor_config.hpp"
28#include "../core/xtensor_forward.hpp"
29#include "../utils/xutils.hpp"
38 template <
class E1,
class E2,
class P>
41 template <
class E1,
class E2,
class F,
class P>
42 void scalar_computed_assign(
xexpression<E1>& e1,
const E2& e2, F&& f, P p);
44 template <
class E1,
class E2,
class P>
47 template <
class E1,
class E2,
class P>
62 template <
class E1,
class E2,
class P>
73 template <
class E1,
class E2,
class P>
76 template <
class E1,
class E2,
class F,
class P>
77 static void scalar_computed_assign(
xexpression<E1>& e1,
const E2& e2, F&& f, P p);
79 template <
class E1,
class E2,
class P>
82 template <
class E1,
class E2,
class P>
87 template <
class S,
class E>
88 static S get_nobroadcast_shape(
const E& e);
90 template <
class S,
class F,
class... CT>
93 template <
class S,
size_t I,
class... CT>
94 static S get_nobroadcast_shape(
const std::tuple<CT...>& args);
96 template <
class E1,
class E2,
class P>
97 static bool resize(E1& e1,
const E2& e2, P);
99 template <
class E1,
class F,
class... CT,
class P>
102 template <
class E1,
class E2,
class P>
110 template <
class E1,
class E2,
class P>
113 using tag = xexpression_tag_t<E1, E2>;
114 xexpression_assigner<tag>::computed_assign(e1, e2, p);
117 template <
class E1,
class E2,
class F,
class P>
118 inline void scalar_computed_assign(xexpression<E1>& e1,
const E2& e2, F&& f, P p)
120 using tag = xexpression_tag_t<E1, E2>;
121 xexpression_assigner<tag>::scalar_computed_assign(e1, e2, std::forward<F>(f), p);
124 template <
class E1,
class E2,
class P>
129 e2.derived_cast().assign_to(e1);
133 using tag = xexpression_tag_t<E1, E2>;
134 xexpression_assigner<tag>::assign_to_container(e1, e2, p);
138 template <
class E1,
class E2,
class P>
141 using tag = xexpression_tag_t<E1, E2>;
142 xexpression_assigner<tag>::assign_to_view(e1, e2, p);
151 template <
class E1,
class E2>
152 constexpr bool linear_static_layout()
158 select_layout<E1::static_layout, typename E1::shape_type>::value,
159 select_layout<E2::static_layout, typename E2::shape_type>::value
164 template <
class E1,
class E2>
165 inline auto is_linear_assign(
const E1& e1,
const E2& e2)
166 -> std::enable_if_t<strided_expression<E1>,
bool>
168 return (E1::contiguous_layout && E2::contiguous_layout && linear_static_layout<E1, E2>())
169 || (e1.is_contiguous() && e2.has_linear_assign(e1.strides()));
172 template <
class E1,
class E2>
173 inline auto is_linear_assign(
const E1&,
const E2&) -> std::enable_if_t<!strided_expression<E1>,
bool>
178 template <
class E1,
class E2>
179 inline bool linear_dynamic_layout(
const E1& e1,
const E2& e2)
181 return e1.is_contiguous() && e2.is_contiguous()
185 template <
class E,
class =
void>
186 struct has_step_leading : std::false_type
191 struct has_step_leading<E, void_t<decltype(std::declval<E>().step_leading())>> : std::true_type
196 struct use_strided_loop
198 static constexpr bool stepper_deref()
200 return std::is_reference<typename T::stepper::reference>::value;
203 static constexpr bool value = strided_expression<T>
204 && has_step_leading<typename T::stepper>::value && stepper_deref();
208 struct use_strided_loop<xscalar<T>>
210 static constexpr bool value =
true;
213 template <
class F,
class... CT>
214 struct use_strided_loop<xfunction<F, CT...>>
216 static constexpr bool value = std::conjunction<use_strided_loop<std::decay_t<CT>>...>::value;
220 template <
class E1,
class E2>
225 using e1_value_type =
typename E1::value_type;
226 using e2_value_type =
typename E2::value_type;
229 using is_bool = std::is_same<T, bool>;
231 static constexpr bool is_bool_conversion()
233 return is_bool<e2_value_type>::value && !is_bool<e1_value_type>::value;
236 static constexpr bool contiguous_layout()
238 return E1::contiguous_layout && E2::contiguous_layout;
241 static constexpr bool convertible_types()
243 return std::is_convertible<e2_value_type, e1_value_type>::value && !is_bool_conversion();
246 static constexpr bool use_xsimd()
248 return xt_simd::simd_traits<int8_t>::size > 1;
252 static constexpr bool simd_size_impl()
254 return xt_simd::simd_traits<T>::size > 1 || (is_bool<T>::value && use_xsimd());
257 static constexpr bool simd_size()
259 return simd_size_impl<e1_value_type>() && simd_size_impl<e2_value_type>();
262 static constexpr bool simd_interface()
264 using rvt = requested_value_type_t<E1, E2>;
273 static constexpr bool simd_assign()
275 return convertible_types() && simd_size() && simd_interface();
278 static constexpr bool linear_assign(
const E1& e1,
const E2& e2,
bool trivial)
280 return trivial && detail::is_linear_assign(e1, e2);
283 static constexpr bool strided_assign()
285 return detail::use_strided_loop<E1>::value && detail::use_strided_loop<E2>::value;
288 static constexpr bool simd_linear_assign()
290 return contiguous_layout() && simd_assign();
293 static constexpr bool simd_strided_assign()
295 return strided_assign() && simd_assign();
298 static constexpr bool simd_linear_assign(
const E1& e1,
const E2& e2)
300 return simd_assign() && detail::linear_dynamic_layout(e1, e2);
304 template <
class E1,
class E2,
class P>
316 bool linear_assign = traits::linear_assign(de1, de2, trivial);
317 constexpr bool simd_assign = traits::simd_assign();
318 constexpr bool simd_linear_assign = traits::simd_linear_assign();
319 constexpr bool simd_strided_assign = traits::simd_strided_assign();
321 parallel_assigner_t<E1, E2, typename P::parallelism> assigner;
325 if (simd_linear_assign || traits::simd_linear_assign(de1, de2))
332 assigner.template linear_assign<simd_assign>(de1, de2);
336 assigner.template linear_assign<false>(de1, de2);
341 if constexpr (simd_strided_assign)
343 assigner.strided_loop_assign(de1, de2);
347 assigner.template stepper_assign<default_assignable_layout(E1::static_layout)>(de1, de2);
353 template <
class E1,
class E2,
class P>
357 bool trivial_broadcast = resize(e1.derived_cast(), e2.derived_cast(), p);
358 base_type::assign_data(e1, e2, trivial_broadcast, p);
363 template <
class F,
class P>
364 bool get_rhs_triviality(
const F&, P)
369 template <
class F,
class R,
class... CT,
class P>
370 bool get_rhs_triviality(
const xfunction<F, R, CT...>& rhs, P)
372 if constexpr (has_broadcasting<P>)
374 using index_type = xindex_type_t<
typename xfunction<F, R, CT...>::shape_type>;
375 using size_type =
typename index_type::size_type;
376 size_type size = rhs.dimension();
377 index_type shape = uninitialized_shape<index_type>(size);
378 bool trivial_broadcast = rhs.broadcast_shape(shape,
true);
379 return trivial_broadcast;
389 template <
class E1,
class E2,
class P>
392 assert_compatible_shape(e1, e2, p);
393 base_type::assign_data(e1, e2, detail::get_rhs_triviality(e2.derived_cast(), p), p);
397 template <
class E1,
class E2,
class P>
400 using shape_type =
typename E1::shape_type;
401 using comperator_type = std::greater<typename shape_type::value_type>;
403 using size_type =
typename E1::size_type;
405 E1& de1 = e1.derived_cast();
406 const E2& de2 = e2.derived_cast();
410 size_type dim2 = de2.dimension();
411 shape_type shape = uninitialized_shape<shape_type>(dim2);
413 bool trivial_broadcast = de2.broadcast_shape(shape,
true);
415 auto&& de1_shape = de1.shape();
416 if (dim2 > de1.dimension()
417 || std::lexicographical_compare(
425 typename E1::temporary_type tmp(shape);
426 base_type::assign_data(tmp, e2, trivial_broadcast, p);
427 de1.assign_temporary(std::move(tmp));
431 base_type::assign_data(e1, e2, trivial_broadcast, p);
436 XTENSOR_ASSERT(
same_shape(de1.shape(), de2.shape()));
437 base_type::assign_data(e1, e2,
true, p);
442 template <
class E1,
class E2,
class F,
class P>
443 inline void xexpression_assigner<Tag>::scalar_computed_assign(
xexpression<E1>& e1,
const E2& e2, F&& f, P)
445 E1& d = e1.derived_cast();
446 using size_type =
typename E1::size_type;
447 auto dst = d.storage().begin();
448 for (size_type i = d.size(); i > 0; --i)
457 template <
bool B,
class... CT>
458 struct static_trivial_broadcast;
460 template <
class... CT>
461 struct static_trivial_broadcast<true, CT...>
463 static constexpr bool value = detail::promote_index<typename std::decay_t<CT>::shape_type...>::value;
466 template <
class... CT>
467 struct static_trivial_broadcast<false, CT...>
469 static constexpr bool value =
false;
474 template <
class E1,
class E2,
class P>
475 inline bool xexpression_assigner<Tag>::resize(E1& e1,
const E2& e2, P)
480 e1.resize(e2.shape());
485 template <
class S,
class E>
486 inline S xexpression_assigner<Tag>::get_nobroadcast_shape(
const E& e)
488 const auto& shape = e.shape();
489 return xtl::forward_sequence<S, decltype(shape)>(shape);
493 template <
class S,
class F,
class... CT>
496 return get_nobroadcast_shape<S, 0>(f.arguments());
500 template <
class S,
size_t I,
class... CT>
501 inline S xexpression_assigner<Tag>::get_nobroadcast_shape(
const std::tuple<CT...>& args)
503 if constexpr (I ==
sizeof...(CT))
507 else if constexpr (
xscalar_concept<std::tuple_element_t<I, std::tuple<CT...>>>)
509 return get_nobroadcast_shape<S, I + 1>(args);
513 return get_nobroadcast_shape<S>(std::get<I>(args));
518 template <
class E1,
class F,
class... CT,
class P>
521 if constexpr (detail::is_fixed<
typename xfunction<F, CT...>::shape_type>::value)
528 e1.resize(
typename xfunction<F, CT...>::shape_type{});
529 return detail::static_trivial_broadcast<
530 detail::is_fixed<
typename xfunction<F, CT...>::shape_type>::value,
537 using index_type = xindex_type_t<typename E1::shape_type>;
538 using size_type =
typename E1::size_type;
539 size_type size = e2.dimension();
540 index_type shape = uninitialized_shape<index_type>(size);
541 bool trivial_broadcast = e2.broadcast_shape(shape,
true);
542 e1.resize(std::move(shape));
543 return trivial_broadcast;
547 using shape_type =
typename E1::shape_type;
548 shape_type shape = get_nobroadcast_shape<shape_type>(e2);
549 e1.resize(std::move(shape));
556 template <
class E1,
class E2,
class P>
560 const E1& de1 = e1.derived_cast();
561 const E2& de2 = e2.derived_cast();
564 if (!broadcastable(de2.shape(), de1.shape()))
566 throw_broadcast_error(de2.shape(), de1.shape());
573 throw_broadcast_error(de2.shape(), de1.shape());
Base class for xexpressions.
derived_type & derived_cast() &noexcept
Returns a reference to the actual derived type of the xexpression.
Multidimensional function operating on xtensor expressions.
bool same_shape(const S1 &s1, const S2 &s2) noexcept
Check if two objects have the same shape.
standard mathematical functions for xexpressions
constexpr layout_type compute_layout(Args... args) noexcept
Implementation of the following logical table: