xtensor
Loading...
Searching...
No Matches
xsemantic.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_SEMANTIC_HPP
11#define XTENSOR_SEMANTIC_HPP
12
13#include <functional>
14#include <utility>
15
16#include "../core/xassign.hpp"
17#include "../core/xassign_proxy.hpp"
18#include "../core/xexpression_traits.hpp"
19
20namespace xt
21{
22 namespace detail
23 {
24 template <class D>
25 struct is_sharable
26 {
27 static constexpr bool value = true;
28 };
29
30 template <class ET, class S, layout_type L, bool SH, class Tag>
31 struct is_sharable<xfixed_container<ET, S, L, SH, Tag>>
32 {
33 static constexpr bool value = SH;
34 };
35
36 template <class ET, class S, layout_type L, bool SH, class Tag>
37 struct is_sharable<xfixed_adaptor<ET, S, L, SH, Tag>>
38 {
39 static constexpr bool value = SH;
40 };
41 }
42
43 template <class D>
44 using select_expression_base_t = std::
45 conditional_t<detail::is_sharable<D>::value, xsharable_expression<D>, xexpression<D>>;
46
57 template <class D>
58 class xsemantic_base : public select_expression_base_t<D>
59 {
60 public:
61
62 using base_type = select_expression_base_t<D>;
63 using derived_type = typename base_type::derived_type;
64
65 using temporary_type = typename xcontainer_inner_types<D>::temporary_type;
66
67 template <class E>
68 disable_xexpression<E, derived_type&> operator+=(const E&);
69
70 template <class E>
71 disable_xexpression<E, derived_type&> operator-=(const E&);
72
73 template <class E>
74 disable_xexpression<E, derived_type&> operator*=(const E&);
75
76 template <class E>
77 disable_xexpression<E, derived_type&> operator/=(const E&);
78
79 template <class E>
80 disable_xexpression<E, derived_type&> operator%=(const E&);
81
82 template <class E>
83 disable_xexpression<E, derived_type&> operator&=(const E&);
84
85 template <class E>
86 disable_xexpression<E, derived_type&> operator|=(const E&);
87
88 template <class E>
89 disable_xexpression<E, derived_type&> operator^=(const E&);
90
91 template <class E>
92 derived_type& operator+=(const xexpression<E>&);
93
94 template <class E>
95 derived_type& operator-=(const xexpression<E>&);
96
97 template <class E>
98 derived_type& operator*=(const xexpression<E>&);
99
100 template <class E>
101 derived_type& operator/=(const xexpression<E>&);
102
103 template <class E>
104 derived_type& operator%=(const xexpression<E>&);
105
106 template <class E>
107 derived_type& operator&=(const xexpression<E>&);
108
109 template <class E>
110 derived_type& operator|=(const xexpression<E>&);
111
112 template <class E>
113 derived_type& operator^=(const xexpression<E>&);
114
115 template <class E, class P>
116 derived_type& assign(const xexpression<E>& e, P p);
117
118 template <class E, class P>
119 derived_type& plus_assign(const xexpression<E>& e, P p);
120
121 template <class E, class P>
122 derived_type& minus_assign(const xexpression<E>& e, P p);
123
124 template <class E, class P>
125 derived_type& multiplies_assign(const xexpression<E>& e, P p);
126
127 template <class E, class P>
128 derived_type& divides_assign(const xexpression<E>& e, P p);
129
130 template <class E, class P>
131 derived_type& modulus_assign(const xexpression<E>& e, P p);
132
133 template <class E, class P>
134 derived_type& bit_and_assign(const xexpression<E>& e, P p);
135
136 template <class E, class P>
137 derived_type& bit_or_assign(const xexpression<E>& e, P p);
138
139 template <class E, class P>
140 derived_type& bit_xor_assign(const xexpression<E>& e, P p);
141
142 protected:
143
144 xsemantic_base() = default;
145 ~xsemantic_base() = default;
146
147 xsemantic_base(const xsemantic_base&) = default;
148 xsemantic_base& operator=(const xsemantic_base&) = default;
149
150 xsemantic_base(xsemantic_base&&) = default;
151 xsemantic_base& operator=(xsemantic_base&&) = default;
152
153 template <class E>
154 derived_type& operator=(const xexpression<E>&);
155
156 private:
157
158 template <class E, class P>
159 derived_type& computed_assign(const xexpression<E>& e, P p);
160 };
161
162 template <class E>
163 using is_assignable = is_crtp_base_of<xsemantic_base, E>;
164
165 template <class E, class R = void>
166 using enable_assignable = typename std::enable_if<is_assignable<E>::value, R>::type;
167
168 template <class E, class R = void>
169 using disable_assignable = typename std::enable_if<!is_assignable<E>::value, R>::type;
170
182 template <class D>
183 class xcontainer_semantic : public xsemantic_base<D>
184 {
185 public:
186
187 using base_type = xsemantic_base<D>;
188 using derived_type = D;
189 using temporary_type = typename base_type::temporary_type;
190
191 derived_type& assign_temporary(temporary_type&&);
192
193 template <class E, class P>
194 derived_type& assign_xexpression(const xexpression<E>& e, P p);
195
196 template <class E, class P>
197 derived_type& eval_computed_assign(const xexpression<E>& e, P p);
198
199 template <class E, class F, class P = assign_policy<>>
200 derived_type& scalar_computed_assign(const E& e, F&& f, P p = P{});
201
202 protected:
203
204 xcontainer_semantic() = default;
205 ~xcontainer_semantic() = default;
206
208 xcontainer_semantic& operator=(const xcontainer_semantic&) = default;
209
211 xcontainer_semantic& operator=(xcontainer_semantic&&) = default;
212
213 template <class E>
214 derived_type& operator=(const xexpression<E>&);
215 };
216
217 template <class E>
218 using has_container_semantics = is_crtp_base_of<xcontainer_semantic, E>;
219
220 template <class E, class R = void>
221 using enable_xcontainer_semantics = typename std::enable_if<has_container_semantics<E>::value, R>::type;
222
223 template <class E, class R = void>
224 using disable_xcontainer_semantics = typename std::enable_if<!has_container_semantics<E>::value, R>::type;
225
226
227 template <class D>
228 class xview_semantic;
229
230 template <class E>
232 E,
233 std::enable_if_t<!addressable_to_expression<E> && is_crtp_base_of<xview_semantic, E>::value>>
234 {
235 static bool check_overlap(const E& expr, const memory_range& dst_range)
236 {
237 if (expr.size() == 0)
238 {
239 return false;
240 }
241 else
242 {
243 using ChildE = std::decay_t<decltype(expr.expression())>;
244 return overlapping_memory_checker_traits<ChildE>::check_overlap(expr.expression(), dst_range);
245 }
246 }
247 };
248
259 template <class D>
260 class xview_semantic : public xsemantic_base<D>
261 {
262 public:
263
264 using base_type = xsemantic_base<D>;
265 using derived_type = D;
266 using temporary_type = typename base_type::temporary_type;
267
268 derived_type& assign_temporary(temporary_type&&);
269
270 template <class E, class P>
271 derived_type& assign_xexpression(const xexpression<E>& e, P p);
272
273 template <class E, class P>
274 derived_type& eval_computed_assign(const xexpression<E>& e, P p);
275
276 template <class E, class F, class P = assign_policy<>>
277 derived_type& scalar_computed_assign(const E& e, F&& f, P p = P{});
278
279 protected:
280
281 xview_semantic() = default;
282 ~xview_semantic() = default;
283
284 xview_semantic(const xview_semantic&) = default;
285 xview_semantic& operator=(const xview_semantic&) = default;
286
287 xview_semantic(xview_semantic&&) = default;
288 xview_semantic& operator=(xview_semantic&&) = default;
289
290 template <class E>
291 derived_type& operator=(const xexpression<E>&);
292 };
293
294 template <class E>
295 using has_view_semantics = is_crtp_base_of<xview_semantic, E>;
296
297 template <class E, class R = void>
298 using enable_xview_semantics = typename std::enable_if<has_view_semantics<E>::value, R>::type;
299
300 template <class E, class R = void>
301 using disable_xview_semantics = typename std::enable_if<!has_view_semantics<E>::value, R>::type;
302
303 /*********************************
304 * xsemantic_base implementation *
305 *********************************/
306
311
316 template <class D>
317 template <class E>
318 inline auto xsemantic_base<D>::operator+=(const E& e) -> disable_xexpression<E, derived_type&>
319 {
320 return this->derived_cast().scalar_computed_assign(e, std::plus<>());
321 }
322
328 template <class D>
329 template <class E>
330 inline auto xsemantic_base<D>::operator-=(const E& e) -> disable_xexpression<E, derived_type&>
331 {
332 return this->derived_cast().scalar_computed_assign(e, std::minus<>());
333 }
334
340 template <class D>
341 template <class E>
342 inline auto xsemantic_base<D>::operator*=(const E& e) -> disable_xexpression<E, derived_type&>
343 {
344 return this->derived_cast().scalar_computed_assign(e, std::multiplies<>());
345 }
346
352 template <class D>
353 template <class E>
354 inline auto xsemantic_base<D>::operator/=(const E& e) -> disable_xexpression<E, derived_type&>
355 {
356 return this->derived_cast().scalar_computed_assign(e, std::divides<>());
357 }
358
364 template <class D>
365 template <class E>
366 inline auto xsemantic_base<D>::operator%=(const E& e) -> disable_xexpression<E, derived_type&>
367 {
368 return this->derived_cast().scalar_computed_assign(e, std::modulus<>());
369 }
370
376 template <class D>
377 template <class E>
378 inline auto xsemantic_base<D>::operator&=(const E& e) -> disable_xexpression<E, derived_type&>
379 {
380 return this->derived_cast().scalar_computed_assign(e, std::bit_and<>());
381 }
382
388 template <class D>
389 template <class E>
390 inline auto xsemantic_base<D>::operator|=(const E& e) -> disable_xexpression<E, derived_type&>
391 {
392 return this->derived_cast().scalar_computed_assign(e, std::bit_or<>());
393 }
394
400 template <class D>
401 template <class E>
402 inline auto xsemantic_base<D>::operator^=(const E& e) -> disable_xexpression<E, derived_type&>
403 {
404 return this->derived_cast().scalar_computed_assign(e, std::bit_xor<>());
405 }
406
412 template <class D>
413 template <class E>
414 inline auto xsemantic_base<D>::operator+=(const xexpression<E>& e) -> derived_type&
415 {
416 return this->derived_cast().computed_assign(this->derived_cast() + e.derived_cast(), default_assign);
417 }
418
424 template <class D>
425 template <class E>
426 inline auto xsemantic_base<D>::operator-=(const xexpression<E>& e) -> derived_type&
427 {
428 return this->derived_cast().computed_assign(this->derived_cast() - e.derived_cast(), default_assign);
429 }
430
436 template <class D>
437 template <class E>
438 inline auto xsemantic_base<D>::operator*=(const xexpression<E>& e) -> derived_type&
439 {
440 return this->derived_cast().computed_assign(this->derived_cast() * e.derived_cast(), default_assign);
441 }
442
448 template <class D>
449 template <class E>
450 inline auto xsemantic_base<D>::operator/=(const xexpression<E>& e) -> derived_type&
451 {
452 return this->derived_cast().computed_assign(this->derived_cast() / e.derived_cast(), default_assign);
453 }
454
460 template <class D>
461 template <class E>
462 inline auto xsemantic_base<D>::operator%=(const xexpression<E>& e) -> derived_type&
463 {
464 return this->derived_cast().computed_assign(this->derived_cast() % e.derived_cast(), default_assign);
465 }
466
472 template <class D>
473 template <class E>
474 inline auto xsemantic_base<D>::operator&=(const xexpression<E>& e) -> derived_type&
475 {
476 return this->derived_cast().computed_assign(this->derived_cast() & e.derived_cast(), default_assign);
477 }
478
484 template <class D>
485 template <class E>
486 inline auto xsemantic_base<D>::operator|=(const xexpression<E>& e) -> derived_type&
487 {
488 return this->derived_cast().computed_assign(this->derived_cast() | e.derived_cast(), default_assign);
489 }
490
496 template <class D>
497 template <class E>
498 inline auto xsemantic_base<D>::operator^=(const xexpression<E>& e) -> derived_type&
499 {
500 return this->derived_cast().computed_assign(this->derived_cast() ^ e.derived_cast(), default_assign);
501 }
502
504
508
515 template <class D>
516 template <class E, class P>
517 inline auto xsemantic_base<D>::assign(const xexpression<E>& e, P p) -> derived_type&
518 {
519 if constexpr (has_aliasing<P>)
520 {
521#ifdef XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS
522 temporary_type tmp(e);
523 return this->derived_cast().assign_temporary(std::move(tmp));
524#else
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()))
528 {
529 temporary_type tmp(e);
530 return this_derived.assign_temporary(std::move(tmp));
531 }
532 else
533 {
535 return this->derived_cast().assign_xexpression(e, new_policy{});
536 }
537#endif
538 }
539 else
540 {
541 return this->derived_cast().assign_xexpression(e, p);
542 }
543 }
544
551 template <class D>
552 template <class E, class P>
553 inline auto xsemantic_base<D>::plus_assign(const xexpression<E>& e, P p) -> derived_type&
554 {
555 return this->derived_cast().computed_assign(this->derived_cast() + e.derived_cast(), p);
556 }
557
564 template <class D>
565 template <class E, class P>
566 inline auto xsemantic_base<D>::minus_assign(const xexpression<E>& e, P p) -> derived_type&
567 {
568 return this->derived_cast().computed_assign(this->derived_cast() - e.derived_cast(), p);
569 }
570
577 template <class D>
578 template <class E, class P>
579 inline auto xsemantic_base<D>::multiplies_assign(const xexpression<E>& e, P p) -> derived_type&
580 {
581 return this->derived_cast().computed_assign(this->derived_cast() * e.derived_cast(), p);
582 }
583
590 template <class D>
591 template <class E, class P>
592 inline auto xsemantic_base<D>::divides_assign(const xexpression<E>& e, P p) -> derived_type&
593 {
594 return this->derived_cast().computed_assign(this->derived_cast() / e.derived_cast(), p);
595 }
596
603 template <class D>
604 template <class E, class P>
605 inline auto xsemantic_base<D>::modulus_assign(const xexpression<E>& e, P p) -> derived_type&
606 {
607 return this->derived_cast().computed_assign(this->derived_cast() % e.derived_cast(), p);
608 }
609
616 template <class D>
617 template <class E, class P>
618 inline auto xsemantic_base<D>::bit_and_assign(const xexpression<E>& e, P p) -> derived_type&
619 {
620 return this->derived_cast().computed_assign(this->derived_cast() & e.derived_cast(), p);
621 }
622
629 template <class D>
630 template <class E, class P>
631 inline auto xsemantic_base<D>::bit_or_assign(const xexpression<E>& e, P p) -> derived_type&
632 {
633 return this->derived_cast().computed_assign(this->derived_cast() | e.derived_cast(), p);
634 }
635
642 template <class D>
643 template <class E, class P>
644 inline auto xsemantic_base<D>::bit_xor_assign(const xexpression<E>& e, P p) -> derived_type&
645 {
646 return this->derived_cast().computed_assign(this->derived_cast() ^ e.derived_cast(), p);
647 }
648
649 template <class D>
650 template <class E>
651 inline auto xsemantic_base<D>::operator=(const xexpression<E>& e) -> derived_type&
652 {
653 return this->assign(e, default_assign);
654 }
655
656 template <class D>
657 template <class E, class P>
658 inline auto xsemantic_base<D>::computed_assign(const xexpression<E>& e, P p) -> derived_type&
659 {
660 if constexpr (has_aliasing<P>)
661 {
662 temporary_type tmp(e);
663 return this->derived_cast().assign_temporary(std::move(tmp));
664 }
665 else
666 {
667 return this->derived_cast().eval_computed_assign(e, p);
668 }
669 }
670
671 /**************************************
672 * xcontainer_semantic implementation *
673 **************************************/
674
680 template <class D>
681 inline auto xcontainer_semantic<D>::assign_temporary(temporary_type&& tmp) -> derived_type&
682 {
683 return (this->derived_cast() = std::move(tmp));
684 }
685
686 template <class D>
687 template <class E, class P>
688 inline auto xcontainer_semantic<D>::assign_xexpression(const xexpression<E>& e, P p) -> derived_type&
689 {
690 xt::assign_to_container(*this, e, p);
691 return this->derived_cast();
692 }
693
694 template <class D>
695 template <class E, class P>
696 inline auto xcontainer_semantic<D>::eval_computed_assign(const xexpression<E>& e, P p) -> derived_type&
697 {
698 xt::computed_assign(*this, e, p);
699 return this->derived_cast();
700 }
701
702 template <class D>
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&
705 {
706 xt::scalar_computed_assign(*this, e, std::forward<F>(f), p);
707 return this->derived_cast();
708 }
709
710 template <class D>
711 template <class E>
712 inline auto xcontainer_semantic<D>::operator=(const xexpression<E>& e) -> derived_type&
713 {
714 return base_type::operator=(e);
715 }
716
717 /*********************************
718 * xview_semantic implementation *
719 *********************************/
720
726 template <class D>
727 inline auto xview_semantic<D>::assign_temporary(temporary_type&& tmp) -> derived_type&
728 {
729 this->derived_cast().assign_temporary_impl(std::move(tmp));
730 return this->derived_cast();
731 }
732
733 template <class D>
734 template <class E, class P>
735 inline auto xview_semantic<D>::assign_xexpression(const xexpression<E>& e, P p) -> derived_type&
736 {
737 xt::assign_to_view(*this, e, p);
738 return this->derived_cast();
739 }
740
741 template <class D>
742 template <class E, class P>
743 inline auto xview_semantic<D>::eval_computed_assign(const xexpression<E>& e, P p) -> derived_type&
744 {
745 xt::assign_to_view(*this, e, p);
746 return this->derived_cast();
747 }
748
749 namespace xview_semantic_detail
750 {
751 template <class D>
752 auto get_begin(D&& lhs, std::true_type)
753 {
754 return lhs.linear_begin();
755 }
756
757 template <class D>
758 auto get_begin(D&& lhs, std::false_type)
759 {
760 return lhs.begin();
761 }
762 }
763
764 template <class D>
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&
767 {
768 D& d = this->derived_cast();
769
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)
773 {
774 *dst = f(*dst, e);
775 ++dst;
776 }
777 return this->derived_cast();
778 }
779
780 template <class D>
781 template <class E>
782 inline auto xview_semantic<D>::operator=(const xexpression<E>& rhs) -> derived_type&
783 {
784 bool cond = (rhs.derived_cast().shape().size() == this->derived_cast().dimension())
785 && std::equal(
786 this->derived_cast().shape().begin(),
787 this->derived_cast().shape().end(),
788 rhs.derived_cast().shape().begin()
789 );
790
791 if (!cond)
792 {
793 base_type::operator=(broadcast(rhs.derived_cast(), this->derived_cast().shape()));
794 }
795 else
796 {
797 base_type::operator=(rhs);
798 }
799 return this->derived_cast();
800 }
801}
802
803#endif
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.