xtensor
Loading...
Searching...
No Matches
xoptional.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_OPTIONAL_HPP
11#define XTENSOR_OPTIONAL_HPP
12
13#include <type_traits>
14#include <utility>
15
16#include <xtl/xoptional.hpp>
17#include <xtl/xoptional_sequence.hpp>
18
19#include "../containers/xarray.hpp"
20#include "../containers/xscalar.hpp"
21#include "../containers/xtensor.hpp"
22#include "../generators/xgenerator.hpp"
23#include "../reducers/xreducer.hpp"
24#include "../views/xbroadcast.hpp"
25#include "../views/xdynamic_view.hpp"
26#include "../views/xfunctor_view.hpp"
27#include "../views/xindex_view.hpp"
28#include "../views/xrepeat.hpp"
29#include "../views/xstrided_view.hpp"
30#include "../views/xview.hpp"
31
32namespace xt
33{
34
35 /****************************************************
36 * Metafunction for splitting xoptional expressions *
37 ****************************************************/
38
39 namespace extension
40 {
41
42 /**************************************
43 * get_expression_tag specializations *
44 **************************************/
45
46 template <class T, class B>
47 struct get_expression_tag<xtl::xoptional<T, B>>
48 {
49 using type = xoptional_expression_tag;
50 };
51
52 /************************
53 * xoptional_empty_base *
54 ************************/
55
56 template <class D>
58 {
59 public:
60
61 using expression_tag = xoptional_expression_tag;
62
63 protected:
64
65 D& derived_cast() noexcept;
66 const D& derived_cast() const noexcept;
67 };
68 }
69
70 namespace detail
71 {
72 /*****************************
73 * split_optional_expression *
74 *****************************/
75
76 template <class T, class Tag>
77 struct split_optional_expression_impl
78 {
79 using value_expression = T;
80 using flag_expression = decltype(ones<bool>(std::declval<T>().shape()));
81
82 template <class U>
83 inline static U&& value(U&& arg)
84 {
85 return std::forward<U>(arg);
86 }
87
88 template <class U>
89 inline static flag_expression has_value(U&& arg)
90 {
91 return ones<bool>(arg.shape());
92 }
93 };
94
95 template <class T, class Tag>
96 struct split_optional_expression_impl<xscalar<T>, Tag>
97 {
98 using value_expression = xscalar<T>;
99 using flag_expression = xscalar<bool>;
100
101 template <class U>
102 inline static U&& value(U&& arg)
103 {
104 return std::forward<U>(arg);
105 }
106
107 template <class U>
108 inline static flag_expression has_value(U&&)
109 {
110 return xscalar<bool>(true);
111 }
112 };
113
114 template <class T>
115 struct split_optional_expression_impl_base
116 {
117 static constexpr bool is_const = std::is_const<std::remove_reference_t<T>>::value;
118 using decay_type = std::decay_t<T>;
119
120 using value_expression = std::conditional_t<
121 is_const,
122 typename decay_type::const_value_expression,
123 typename decay_type::value_expression>;
124 using flag_expression = std::
125 conditional_t<is_const, typename decay_type::const_flag_expression, typename decay_type::flag_expression>;
126
127 template <class U>
128 inline static value_expression value(U&& arg)
129 {
130 return arg.value();
131 }
132
133 template <class U>
134 inline static flag_expression has_value(U&& arg)
135 {
136 return arg.has_value();
137 }
138 };
139
140 template <class T>
141 struct split_optional_expression_impl<T, xoptional_expression_tag>
142 : split_optional_expression_impl_base<T>
143 {
144 };
145
146 template <class T>
147 struct split_optional_expression_impl<xscalar<T>, xoptional_expression_tag>
148 : split_optional_expression_impl_base<xscalar<T>>
149 {
150 };
151
152 template <class T>
153 struct split_optional_expression
154 : split_optional_expression_impl<T, xexpression_tag_t<std::decay_t<T>>>
155 {
156 };
157
158 template <class T>
159 using value_expression_t = typename split_optional_expression<T>::value_expression;
160
161 template <class T>
162 using flag_expression_t = typename split_optional_expression<T>::flag_expression;
163
164 /********************
165 * optional_bitwise *
166 ********************/
167
168 template <class T = bool>
169 class optional_bitwise
170 {
171 public:
172
173 using return_type = T;
174 using first_argument_type = T;
175 using second_argument_type = T;
176 using result_type = T;
177 using simd_value_type = bool;
178 using simd_result_type = bool;
179
180 template <class... Args>
181 constexpr result_type operator()(const Args&... args) const
182 {
183 return apply_impl(args...);
184 }
185
186 template <class B, class... Args>
187 constexpr B simd_apply(const B& b, const Args&... args) const
188 {
189 return simd_apply_impl(b, args...);
190 }
191
192 private:
193
194 constexpr result_type apply_impl() const
195 {
196 return true;
197 }
198
199 template <class U, class... Args>
200 constexpr result_type apply_impl(const U& t, const Args&... args) const
201 {
202 return t && apply_impl(args...);
203 }
204
205 template <class B>
206 constexpr B simd_apply_impl(const B& b) const
207 {
208 return b;
209 }
210
211 template <class B1, class B2, class... Args>
212 constexpr B1 simd_apply_impl(const B1& b1, const B2& b2, const Args&... args) const
213 {
214 return b1 & simd_apply_impl(b2, args...);
215 }
216 };
217
218 /*********************************
219 * optional const_value rebinder *
220 *********************************/
221
222 template <class T, class B>
223 struct const_value_rebinder<xtl::xoptional<T, B>, T>
224 {
225 static const_value<T> run(const const_value<xtl::xoptional<T, B>>& src)
226 {
227 return const_value<T>(src.m_value.value());
228 }
229 };
230
231 /**************************
232 * xreducer types helpers *
233 **************************/
234
235 template <class T, class B>
236 struct xreducer_size_type<xtl::xoptional<T, B>>
237 {
238 using type = xtl::xoptional<std::size_t, bool>;
239 };
240
241 template <class T, class B>
242 struct xreducer_temporary_type<xtl::xoptional<T, B>>
243 {
244 using type = xtl::xoptional<std::decay_t<T>, bool>;
245 };
246 }
247
248 /**********************
249 * optional functions *
250 **********************/
251
252 template <class T, class B>
253 auto sign(const xtl::xoptional<T, B>& e);
254
255 /*template <class E, XTL_REQUIRES(is_xexpression<E>)>
256 detail::value_expression_t<E> value(E&&);
257
258 template <class E, XTL_REQUIRES(is_xexpression<E>)>
259 detail::flag_expression_t<E> has_value(E&&);*/
260
261 template <>
263 {
264 public:
265
266 template <class E1, class E2, class P>
267 static void assign_data(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P);
268 };
269
270 /**********************************
271 * xscalar extension for optional *
272 **********************************/
273
274 namespace extension
275 {
276 template <class CT>
278 {
279 using closure_type = CT;
280 static constexpr bool is_ref = std::is_reference<closure_type>::value;
281 using unref_closure_type = std::remove_reference_t<closure_type>;
282 static constexpr bool is_const = std::is_const<unref_closure_type>::value;
283 using raw_closure_type = std::decay_t<CT>;
284
285 using raw_value_closure = typename raw_closure_type::value_closure;
286 using raw_flag_closure = typename raw_closure_type::flag_closure;
287 using const_raw_value_closure = std::add_const_t<raw_value_closure>;
288 using const_raw_flag_closure = std::add_const_t<raw_flag_closure>;
289
290 using value_closure = std::conditional_t<is_ref, std::add_lvalue_reference_t<raw_value_closure>, raw_value_closure>;
291 using flag_closure = std::conditional_t<is_ref, std::add_lvalue_reference_t<raw_flag_closure>, raw_flag_closure>;
292 using const_value_closure = std::
293 conditional_t<is_ref, std::add_lvalue_reference_t<const_raw_value_closure>, raw_value_closure>;
294 using const_flag_closure = std::
295 conditional_t<is_ref, std::add_lvalue_reference_t<const_raw_flag_closure>, raw_flag_closure>;
296
299 using const_value_expression = xscalar<const_value_closure>;
300 using const_flag_expression = xscalar<const_flag_closure>;
301 };
302
303 template <class CT>
304 class xscalar_optional_base : public xoptional_empty_base<xscalar<CT>>
305 {
306 public:
307
308 using traits = xscalar_optional_traits<CT>;
309 using value_expression = typename traits::value_expression;
310 using flag_expression = typename traits::flag_expression;
311 using const_value_expression = typename traits::const_value_expression;
312 using const_flag_expression = typename traits::const_flag_expression;
313 using expression_tag = xoptional_expression_tag;
314
315 value_expression value();
316 const_value_expression value() const;
317
318 flag_expression has_value();
319 const_flag_expression has_value() const;
320 };
321
322 template <class CT>
324 {
325 using type = xscalar_optional_base<CT>;
326 };
327 }
328
329 /*************************************
330 * xcontainer extension for optional *
331 *************************************/
332
333 namespace extension
334 {
335 template <class T>
336 class xcontainer_optional_base : public xoptional_empty_base<typename T::derived_type>
337 {
338 public:
339
340 using traits = T;
341 using value_expression = typename traits::value_expression;
342 using flag_expression = typename traits::flag_expression;
343 using const_value_expression = typename traits::const_value_expression;
344 using const_flag_expression = typename traits::const_flag_expression;
345 using expression_tag = xoptional_expression_tag;
346
347 value_expression value();
348 const_value_expression value() const;
349
350 flag_expression has_value();
351 const_flag_expression has_value() const;
352 };
353 }
354
355 /*******************************************
356 * xarray_container extension for optional *
357 *******************************************/
358
359 namespace extension
360 {
361 template <class EC, layout_type L, class SC>
363 {
364 using value_container = typename std::remove_reference_t<EC>::base_container_type;
365 using flag_container = typename std::remove_reference_t<EC>::flag_container_type;
366 using value_expression = xarray_adaptor<value_container&, L, SC>;
367 using flag_expression = xarray_adaptor<flag_container&, L, SC>;
368 using const_value_expression = xarray_adaptor<const value_container&, L, SC>;
369 using const_flag_expression = xarray_adaptor<const flag_container&, L, SC>;
370 };
371
372 template <class EC, layout_type L, class SC>
377
378 template <class EC, layout_type L, class SC>
384 }
385
386 /*****************************************
387 * xarray_adaptor extension for optional *
388 *****************************************/
389
390 namespace extension
391 {
392 template <class EC, layout_type L, class SC>
397
398 template <class EC, layout_type L, class SC>
404 }
405
406 /********************************************
407 * xtensor_container extension for optional *
408 ********************************************/
409
410 namespace extension
411 {
412 template <class EC, std::size_t N, layout_type L>
414 {
415 using value_container = typename std::remove_reference_t<EC>::base_container_type;
416 using flag_container = typename std::remove_reference_t<EC>::flag_container_type;
417 using value_expression = xtensor_adaptor<value_container&, N, L>;
418 using flag_expression = xtensor_adaptor<flag_container&, N, L>;
419 using const_value_expression = xtensor_adaptor<const value_container&, N, L>;
420 using const_flag_expression = xtensor_adaptor<const flag_container&, N, L>;
421 };
422
423 template <class EC, std::size_t N, layout_type L>
428
429 template <class EC, std::size_t N, layout_type L>
435 }
436
437 /******************************************
438 * xtensor_adaptor extension for optional *
439 ******************************************/
440
441 namespace extension
442 {
443 template <class EC, std::size_t N, layout_type L>
448
449 template <class EC, std::size_t N, layout_type L>
455 }
456
457 /***************************************
458 * xtensor_view extension for optional *
459 ***************************************/
460
461 namespace extension
462 {
463 template <class EC, std::size_t N, layout_type L>
468
469 template <class EC, std::size_t N, layout_type L>
475 }
476
477 /************************************************
478 * xfunction extension for optional expressions *
479 ************************************************/
480
481 namespace extension
482 {
483 template <class F, class... CT>
484 class xfunction_optional_base : public xoptional_empty_base<xfunction<F, CT...>>
485 {
486 public:
487
488 using expression_tag = xoptional_expression_tag;
489 using value_functor = F;
490 using flag_functor = xt::detail::optional_bitwise<bool>;
491
494 using const_value_expression = value_expression;
495 using const_flag_expression = flag_expression;
496
497 const_value_expression value() const;
498 const_flag_expression has_value() const;
499
500 private:
501
502 template <std::size_t... I>
503 const_value_expression value_impl(std::index_sequence<I...>) const;
504
505 template <std::size_t... I>
506 const_flag_expression has_value_impl(std::index_sequence<I...>) const;
507 };
508
509 template <class F, class... CT>
511 {
512 using type = xfunction_optional_base<F, CT...>;
513 };
514 }
515
516 /****************************************************
517 * xdynamic_view extension for optional expressions *
518 ****************************************************/
519
520 namespace extension
521 {
522 template <class CT, class S, layout_type L, class FST>
523 class xdynamic_view_optional : public xoptional_empty_base<xdynamic_view<CT, S, L, FST>>
524 {
525 public:
526
527 using expression_tag = xoptional_expression_tag;
528 using uvt = typename std::decay_t<CT>::value_expression;
529 using uft = typename std::decay_t<CT>::flag_expression;
530 using ucvt = typename std::decay_t<CT>::const_value_expression;
531 using ucft = typename std::decay_t<CT>::const_flag_expression;
536
537 value_expression value();
538 const_value_expression value() const;
539
540 flag_expression has_value();
541 const_flag_expression has_value() const;
542 };
543
544 template <class CT, class S, layout_type L, class FST>
549 }
550
551 /*************************************************
552 * xbroadcast extension for optional expressions *
553 *************************************************/
554
555 namespace extension
556 {
557 template <class CT, class X>
558 class xbroadcast_optional : public xoptional_empty_base<xbroadcast<CT, X>>
559 {
560 public:
561
562 using expression_tag = xoptional_expression_tag;
563 using value_expression = xbroadcast<xt::detail::value_expression_t<CT>, X>;
564 using flag_expression = xbroadcast<xt::detail::flag_expression_t<CT>, X>;
565 using const_value_expression = value_expression;
566 using const_flag_expression = flag_expression;
567
568 const_value_expression value() const;
569 const_flag_expression has_value() const;
570 };
571
572 template <class CT, class X>
574 {
575 using type = xbroadcast_optional<CT, X>;
576 };
577 }
578
579 /***************************************************
580 * xfunctor_view extension for optional expression *
581 ***************************************************/
582
583 namespace extension
584 {
585 template <class F, class CT>
586 class xfunctor_view_optional : public xoptional_empty_base<xfunctor_view<F, CT>>
587 {
588 public:
589
590 using expression_tag = xoptional_expression_tag;
591 using uvt = typename std::decay_t<CT>::value_expression;
592 using uft = typename std::decay_t<CT>::flag_expression;
593 using ucvt = typename std::decay_t<CT>::const_value_expression;
594 using ucft = typename std::decay_t<CT>::const_flag_expression;
595 using value_expression = xfunctor_view<F, uvt>;
596 using flag_expression = uft;
597 using const_value_expression = xfunctor_view<F, ucvt>;
598 using const_flag_expression = ucft;
599
600 value_expression value();
601 const_value_expression value() const;
602
603 flag_expression has_value();
604 const_flag_expression has_value() const;
605 };
606
607 template <class F, class CT>
612 }
613
614 /**************************************************
615 * xindex_view extension for optional expressions *
616 **************************************************/
617
618 namespace extension
619 {
620 template <class CT, class I>
621 class xindex_view_optional : public xoptional_empty_base<xindex_view<CT, I>>
622 {
623 public:
624
625 using expression_tag = xoptional_expression_tag;
626 using uvt = typename std::decay_t<CT>::value_expression;
627 using ucvt = typename std::decay_t<CT>::const_value_expression;
628 using uft = typename std::decay_t<CT>::flag_expression;
629 using ucft = typename std::decay_t<CT>::const_flag_expression;
630 using value_expression = xindex_view<uvt, I>;
631 using flag_expression = xindex_view<uft, I>;
632 using const_value_expression = xindex_view<ucvt, I>;
633 using const_flag_expression = xindex_view<ucft, I>;
634
635 value_expression value();
636 const_value_expression value() const;
637
638 flag_expression has_value();
639 const_flag_expression has_value() const;
640 };
641
642 template <class CT, class I>
647 }
648
649 /***********************************************
650 * xreducer extension for optional expressions *
651 ***********************************************/
652
653 namespace extension
654 {
655 template <class F, class CT, class X, class O>
656 class xreducer_optional : public xoptional_empty_base<xreducer<F, CT, X, O>>
657 {
658 public:
659
660 using expression_tag = xoptional_expression_tag;
661 using result_type = typename F::init_value_type;
662
663 using rebound_result_type = typename result_type::value_type;
664 using rebound_functors_type = typename F::template rebind_t<rebound_result_type>;
665 using rebound_reduce_options_values = typename O::template rebind_t<rebound_result_type>;
666 using rebound_reduce_options_flag = typename O::template rebind_t<bool>;
667
669 using flag_expression = xreducer<flag_reducer, xt::detail::flag_expression_t<CT>, X, rebound_reduce_options_flag>;
670 using value_expression = xreducer<rebound_functors_type, xt::detail::value_expression_t<CT>, X, rebound_reduce_options_values>;
671
672 using const_value_expression = value_expression;
673 using const_flag_expression = flag_expression;
674
675 const_value_expression value() const;
676 const_flag_expression has_value() const;
677 };
678
679 template <class F, class CT, class X, class O>
681 {
683 };
684 }
685
686 /**********************************************
687 * xrepeat extension for optional expressions *
688 **********************************************/
689
690 namespace extension
691 {
692 template <class CT, class X>
693 class xrepeat_optional : public xoptional_empty_base<xrepeat<CT, X>>
694 {
695 public:
696
697 using expression_tag = xoptional_expression_tag;
698 using value_expression = xbroadcast<xt::detail::value_expression_t<CT>, X>;
699 using flag_expression = xbroadcast<xt::detail::flag_expression_t<CT>, X>;
700 using const_value_expression = value_expression;
701 using const_flag_expression = flag_expression;
702
703 const_value_expression value() const;
704 const_flag_expression has_value() const;
705 };
706
707 template <class CT, class X>
709 {
710 using type = xrepeat_optional<CT, X>;
711 };
712 }
713
714 /****************************************************
715 * xstrided_view extension for optional expressions *
716 ****************************************************/
717
718 namespace extension
719 {
720 template <class CT, class S, layout_type L, class FST>
721 class xstrided_view_optional : public xoptional_empty_base<xstrided_view<CT, S, L, FST>>
722 {
723 public:
724
725 using expression_tag = xoptional_expression_tag;
726 using uvt = typename std::decay_t<CT>::value_expression;
727 using uft = typename std::decay_t<CT>::flag_expression;
728 using ucvt = typename std::decay_t<CT>::const_value_expression;
729 using ucft = typename std::decay_t<CT>::const_flag_expression;
734
735 value_expression value();
736 const_value_expression value() const;
737
738 flag_expression has_value();
739 const_flag_expression has_value() const;
740 };
741
742 template <class CT, class S, layout_type L, class FST>
747 }
748
749 /********************************************
750 * xview extension for optional expressions *
751 ********************************************/
752
753 namespace extension
754 {
755 template <class CT, class... S>
756 class xview_optional : public xoptional_empty_base<xview<CT, S...>>
757 {
758 public:
759
760 using expression_tag = xoptional_expression_tag;
761 using uvt = typename std::decay_t<CT>::value_expression;
762 using uft = typename std::decay_t<CT>::flag_expression;
763 using ucvt = typename std::decay_t<CT>::const_value_expression;
764 using ucft = typename std::decay_t<CT>::const_flag_expression;
765 using value_expression = xview<uvt, S...>;
766 using flag_expression = xview<uft, S...>;
767 using const_value_expression = xview<ucvt, S...>;
768 using const_flag_expression = xview<ucft, S...>;
769
770 value_expression value();
771 const_value_expression value() const;
772
773 flag_expression has_value();
774 const_flag_expression has_value() const;
775 };
776
777 template <class CT, class... S>
779 {
780 using type = xview_optional<CT, S...>;
781 };
782 }
783
784 /*************************************************
785 * xgenerator extension for generator expression *
786 *************************************************/
787
788 namespace extension
789 {
790 namespace detail
791 {
792 template <class F, class = void_t<int>>
793 struct value_functor
794 {
795 using type = F;
796
797 static type get(const F& f)
798 {
799 return f;
800 }
801 };
802
803 template <class F>
804 struct value_functor<F, void_t<typename F::value_functor_type>>
805 {
806 using type = typename F::value_functor_type;
807
808 static type get(const F& f)
809 {
810 return f.value_functor();
811 }
812 };
813
814 template <class F>
815 using value_functor_t = typename value_functor<F>::type;
816
817 struct always_true
818 {
819 template <class... T>
820 bool operator()(T...) const
821 {
822 return true;
823 }
824 };
825
826 template <class F, class = void_t<int>>
827 struct flag_functor
828 {
829 using type = always_true;
830
831 static type get(const F&)
832 {
833 return type();
834 }
835 };
836
837 template <class F>
838 struct flag_functor<F, void_t<typename F::flag_functor_type>>
839 {
840 using type = typename F::flag_functor_type;
841
842 static type get(const F& f)
843 {
844 return f.flag_functor();
845 }
846 };
847
848 template <class F>
849 using flag_functor_t = typename flag_functor<F>::type;
850 }
851
852 template <class F, class R, class S>
853 class xgenerator_optional : public xoptional_empty_base<xgenerator<F, R, S>>
854 {
855 public:
856
857 using expression_tag = xoptional_expression_tag;
858 using value_closure = typename R::value_closure;
859 using flag_closure = typename R::flag_closure;
860 using value_functor = detail::value_functor_t<F>;
861 using flag_functor = detail::flag_functor_t<F>;
862 using value_expression = xgenerator<value_functor, value_closure, S>;
863 using flag_expression = xgenerator<flag_functor, flag_closure, S>;
864 using const_value_expression = value_expression;
865 using const_flag_expression = flag_expression;
866
867 const_value_expression value() const;
868 const_flag_expression has_value() const;
869 };
870
871 template <class F, class R, class S>
873 {
874 using type = xgenerator_optional<F, R, S>;
875 };
876 }
877
878 /***************************************
879 * xoptional_empty_base implementation *
880 ***************************************/
881
882 namespace extension
883 {
884 template <class D>
885 inline D& xoptional_empty_base<D>::derived_cast() noexcept
886 {
887 return *static_cast<D*>(this);
888 }
889
890 template <class D>
891 inline const D& xoptional_empty_base<D>::derived_cast() const noexcept
892 {
893 return *static_cast<const D*>(this);
894 }
895 }
896
897 /****************************************
898 * xscalar_optional_base implementation *
899 ****************************************/
900
901 namespace extension
902 {
903 template <class CT>
904 inline auto xscalar_optional_base<CT>::value() -> value_expression
905 {
906 return this->derived_cast().expression().value();
907 }
908
909 template <class CT>
910 inline auto xscalar_optional_base<CT>::value() const -> const_value_expression
911 {
912 return this->derived_cast().expression().value();
913 }
914
915 template <class CT>
916 inline auto xscalar_optional_base<CT>::has_value() -> flag_expression
917 {
918 return this->derived_cast().expression().has_value();
919 }
920
921 template <class CT>
922 inline auto xscalar_optional_base<CT>::has_value() const -> const_flag_expression
923 {
924 return this->derived_cast().expression().has_value();
925 }
926 }
927
928 /*******************************************
929 * xcontainer_optional_base implementation *
930 *******************************************/
931
932 namespace extension
933 {
934 template <class T>
935 inline auto xcontainer_optional_base<T>::value() -> value_expression
936 {
937 return value_expression(this->derived_cast().storage().value(), this->derived_cast().shape());
938 }
939
940 template <class T>
941 inline auto xcontainer_optional_base<T>::value() const -> const_value_expression
942 {
943 return const_value_expression(this->derived_cast().storage().value(), this->derived_cast().shape());
944 }
945
946 template <class T>
947 inline auto xcontainer_optional_base<T>::has_value() -> flag_expression
948 {
949 return flag_expression(this->derived_cast().storage().has_value(), this->derived_cast().shape());
950 }
951
952 template <class T>
953 inline auto xcontainer_optional_base<T>::has_value() const -> const_flag_expression
954 {
955 return const_flag_expression(this->derived_cast().storage().has_value(), this->derived_cast().shape());
956 }
957 }
958
959 /******************************************
960 * xfunction_optional_base implementation *
961 ******************************************/
962
963 namespace extension
964 {
965 template <class F, class... CT>
966 inline auto xfunction_optional_base<F, CT...>::value() const -> const_value_expression
967 {
968 return value_impl(std::make_index_sequence<sizeof...(CT)>());
969 }
970
971 template <class F, class... CT>
972 inline auto xfunction_optional_base<F, CT...>::has_value() const -> const_flag_expression
973 {
974 return has_value_impl(std::make_index_sequence<sizeof...(CT)>());
975 }
976
977 template <class F, class... CT>
978 template <std::size_t... I>
979 inline auto xfunction_optional_base<F, CT...>::value_impl(std::index_sequence<I...>) const
980 -> const_value_expression
981 {
982 return value_expression(
983 value_functor(),
984 xt::detail::split_optional_expression<CT>::value(std::get<I>(this->derived_cast().arguments()))...
985 );
986 }
987
988 template <class F, class... CT>
989 template <std::size_t... I>
990 inline auto xfunction_optional_base<F, CT...>::has_value_impl(std::index_sequence<I...>) const
991 -> const_flag_expression
992 {
993 return flag_expression(
994 flag_functor(),
995 xt::detail::split_optional_expression<CT>::has_value(
996 std::get<I>(this->derived_cast().arguments())
997 )...
998 );
999 }
1000 }
1001
1002 /*****************************************
1003 * xdynamic_view_optional implementation *
1004 *****************************************/
1005
1006 namespace extension
1007 {
1008 template <class CT, class S, layout_type L, class FST>
1009 inline auto xdynamic_view_optional<CT, S, L, FST>::value() -> value_expression
1010 {
1011 return this->derived_cast().build_view(this->derived_cast().expression().value());
1012 }
1013
1014 template <class CT, class S, layout_type L, class FST>
1015 inline auto xdynamic_view_optional<CT, S, L, FST>::value() const -> const_value_expression
1016 {
1017 return this->derived_cast().build_view(this->derived_cast().expression().value());
1018 }
1019
1020 template <class CT, class S, layout_type L, class FST>
1021 inline auto xdynamic_view_optional<CT, S, L, FST>::has_value() -> flag_expression
1022 {
1023 return this->derived_cast().build_view(this->derived_cast().expression().has_value());
1024 }
1025
1026 template <class CT, class S, layout_type L, class FST>
1027 inline auto xdynamic_view_optional<CT, S, L, FST>::has_value() const -> const_flag_expression
1028 {
1029 return this->derived_cast().build_view(this->derived_cast().expression().has_value());
1030 }
1031 }
1032
1033 /**************************************
1034 * xbroadcast_optional implementation *
1035 **************************************/
1036
1037 namespace extension
1038 {
1039 template <class CT, class X>
1040 inline auto xbroadcast_optional<CT, X>::value() const -> const_value_expression
1041 {
1042 return this->derived_cast().build_broadcast(this->derived_cast().expression().value());
1043 }
1044
1045 template <class CT, class X>
1046 inline auto xbroadcast_optional<CT, X>::has_value() const -> const_flag_expression
1047 {
1048 return this->derived_cast().build_broadcast(this->derived_cast().expression().has_value());
1049 }
1050 }
1051
1052 /*****************************************
1053 * xfunctor_view_optional implementation *
1054 *****************************************/
1055
1056 namespace extension
1057 {
1058 template <class F, class CT>
1059 inline auto xfunctor_view_optional<F, CT>::value() -> value_expression
1060 {
1061 return this->derived_cast().build_functor_view(this->derived_cast().expression().value());
1062 }
1063
1064 template <class F, class CT>
1065 inline auto xfunctor_view_optional<F, CT>::value() const -> const_value_expression
1066 {
1067 return this->derived_cast().build_functor_view(this->derived_cast().expression().value());
1068 }
1069
1070 template <class F, class CT>
1071 inline auto xfunctor_view_optional<F, CT>::has_value() -> flag_expression
1072 {
1073 return this->derived_cast().expression().has_value();
1074 }
1075
1076 template <class F, class CT>
1077 inline auto xfunctor_view_optional<F, CT>::has_value() const -> const_flag_expression
1078 {
1079 return this->derived_cast().expression().has_value();
1080 }
1081 }
1082
1083 /***************************************
1084 * xindex_view_optional implementation *
1085 ***************************************/
1086
1087 namespace extension
1088 {
1089 template <class CT, class I>
1090 inline auto xindex_view_optional<CT, I>::value() -> value_expression
1091 {
1092 return this->derived_cast().build_index_view(this->derived_cast().expression().value());
1093 };
1094
1095 template <class CT, class I>
1096 inline auto xindex_view_optional<CT, I>::value() const -> const_value_expression
1097 {
1098 return this->derived_cast().build_index_view(this->derived_cast().expression().value());
1099 };
1100
1101 template <class CT, class I>
1102 inline auto xindex_view_optional<CT, I>::has_value() -> flag_expression
1103 {
1104 return this->derived_cast().build_index_view(this->derived_cast().expression().has_value());
1105 };
1106
1107 template <class CT, class I>
1108 inline auto xindex_view_optional<CT, I>::has_value() const -> const_flag_expression
1109 {
1110 return this->derived_cast().build_index_view(this->derived_cast().expression().has_value());
1111 };
1112 }
1113
1114 /************************************
1115 * xreducer_optional implementation *
1116 ************************************/
1117
1118 namespace extension
1119 {
1120 template <class F, class CT, class X, class O>
1121 inline auto xreducer_optional<F, CT, X, O>::value() const -> const_value_expression
1122 {
1123 auto func = this->derived_cast().functors();
1124 auto opts = this->derived_cast().options().template rebind<rebound_result_type>(
1125 this->derived_cast().options().initial_value.value(),
1126 this->derived_cast().options()
1127 );
1128
1129 return this->derived_cast().build_reducer(
1130 this->derived_cast().expression().value(),
1131 func.template rebind<rebound_result_type>(),
1132 std::move(opts)
1133 );
1134 }
1135
1136 template <class F, class CT, class X, class O>
1137 inline auto xreducer_optional<F, CT, X, O>::has_value() const -> const_flag_expression
1138 {
1139 auto opts = this->derived_cast().options().rebind(
1140 this->derived_cast().options().initial_value.has_value(),
1141 this->derived_cast().options()
1142 );
1143
1144 return this->derived_cast().build_reducer(
1145 this->derived_cast().expression().has_value(),
1146 make_xreducer_functor(xt::detail::optional_bitwise<bool>(), xt::const_value<bool>(true)),
1147 std::move(opts)
1148 );
1149 }
1150 }
1151
1152 /*****************************************
1153 * xstrided_view_optional implementation *
1154 *****************************************/
1155
1156 namespace extension
1157 {
1158 template <class CT, class S, layout_type L, class FST>
1159 inline auto xstrided_view_optional<CT, S, L, FST>::value() -> value_expression
1160 {
1161 return this->derived_cast().build_view(this->derived_cast().expression().value());
1162 }
1163
1164 template <class CT, class S, layout_type L, class FST>
1165 inline auto xstrided_view_optional<CT, S, L, FST>::value() const -> const_value_expression
1166 {
1167 return this->derived_cast().build_view(this->derived_cast().expression().value());
1168 }
1169
1170 template <class CT, class S, layout_type L, class FST>
1171 inline auto xstrided_view_optional<CT, S, L, FST>::has_value() -> flag_expression
1172 {
1173 return this->derived_cast().build_view(this->derived_cast().expression().has_value());
1174 }
1175
1176 template <class CT, class S, layout_type L, class FST>
1177 inline auto xstrided_view_optional<CT, S, L, FST>::has_value() const -> const_flag_expression
1178 {
1179 return this->derived_cast().build_view(this->derived_cast().expression().has_value());
1180 }
1181 }
1182
1183 /*********************************
1184 * xview_optional implementation *
1185 *********************************/
1186
1187 namespace extension
1188 {
1189 template <class CT, class... S>
1190 inline auto xview_optional<CT, S...>::value() -> value_expression
1191 {
1192 return this->derived_cast().build_view(this->derived_cast().expression().value());
1193 }
1194
1195 template <class CT, class... S>
1196 inline auto xview_optional<CT, S...>::value() const -> const_value_expression
1197 {
1198 return this->derived_cast().build_view(this->derived_cast().expression().value());
1199 }
1200
1201 template <class CT, class... S>
1202 inline auto xview_optional<CT, S...>::has_value() -> flag_expression
1203 {
1204 return this->derived_cast().build_view(this->derived_cast().expression().has_value());
1205 }
1206
1207 template <class CT, class... S>
1208 inline auto xview_optional<CT, S...>::has_value() const -> const_flag_expression
1209 {
1210 return this->derived_cast().build_view(this->derived_cast().expression().has_value());
1211 }
1212 }
1213
1214 /**************************************
1215 * xgenerator_optional implementation *
1216 **************************************/
1217
1218 namespace extension
1219 {
1220 template <class F, class R, class S>
1221 inline auto xgenerator_optional<F, R, S>::value() const -> const_value_expression
1222 {
1223 return this->derived_cast().template build_generator<value_closure>(
1224 detail::value_functor<F>::get(this->derived_cast().functor())
1225 );
1226 }
1227
1228 template <class F, class R, class S>
1229 inline auto xgenerator_optional<F, R, S>::has_value() const -> const_flag_expression
1230 {
1231 return this->derived_cast().template build_generator<flag_closure>(
1232 detail::flag_functor<F>::get(this->derived_cast().functor())
1233 );
1234 }
1235 }
1236
1237 /********************************
1238 * sign function implementation *
1239 ********************************/
1240
1241 namespace math
1242 {
1243 template <class T, class B>
1244 struct sign_impl<xtl::xoptional<T, B>>
1245 {
1246 static constexpr auto run(const xtl::xoptional<T, B>& x)
1247 {
1248 return sign(x); // use overload declared above
1249 }
1250 };
1251 }
1252
1253 template <class T, class B>
1254 inline auto sign(const xtl::xoptional<T, B>& e)
1255 {
1256 using value_type = std::decay_t<T>;
1257 return e.has_value() ? math::sign_impl<value_type>::run(e.value()) : xtl::missing<value_type>();
1258 }
1259
1260 /******************************************
1261 * value() and has_value() implementation *
1262 ******************************************/
1263
1264 template <class E, XTL_REQUIRES(is_xexpression<E>)>
1265 inline auto value(E&& e) -> detail::value_expression_t<E>
1266 {
1267 return detail::split_optional_expression<E>::value(std::forward<E>(e));
1268 }
1269
1270 template <class E, XTL_REQUIRES(is_xexpression<E>)>
1271 inline auto has_value(E&& e) -> detail::flag_expression_t<E>
1272 {
1273 return detail::split_optional_expression<E>::has_value(std::forward<E>(e));
1274 }
1275
1276 namespace detail
1277 {
1278 template <class T1, class T2>
1279 struct assign_data_impl
1280 {
1281 template <class E1, class E2, class P>
1282 static void run(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P p)
1283 {
1284 E1& de1 = e1.derived_cast();
1285 const E2& de2 = e2.derived_cast();
1286
1287 decltype(auto) bde1 = xt::value(de1);
1288 decltype(auto) hde1 = xt::has_value(de1);
1289 xexpression_assigner_base<xtensor_expression_tag>::assign_data(bde1, xt::value(de2), trivial, p);
1290 xexpression_assigner_base<xtensor_expression_tag>::assign_data(hde1, xt::has_value(de2), trivial, p);
1291 }
1292 };
1293
1294 template <class T>
1295 struct xarray_assigner
1296 {
1297 template <class E1, class E2, class P>
1298 static void assign(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P p)
1299 {
1300 E1& de1 = e1.derived_cast();
1301 const E2& de2 = e2.derived_cast();
1302 xarray<bool> mask = xt::full_like(de2, true);
1303
1304 decltype(auto) bde1 = xt::value(de1);
1305 decltype(auto) hde1 = xt::has_value(de1);
1306 xexpression_assigner_base<xtensor_expression_tag>::assign_data(bde1, e2, trivial, p);
1307 xexpression_assigner_base<xtensor_expression_tag>::assign_data(hde1, mask, trivial, p);
1308 }
1309 };
1310
1311 template <class T>
1312 struct xarray_assigner<xtl::xoptional<T>>
1313 {
1314 template <class E1, class E2, class P>
1315 static void assign(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P p)
1316 {
1317 xexpression_assigner_base<xtensor_expression_tag>::assign_data(e1, e2, trivial, p);
1318 }
1319 };
1320
1321 template <>
1322 struct assign_data_impl<xoptional_expression_tag, xtensor_expression_tag>
1323 {
1324 template <class E1, class E2, class P>
1325 static void run(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P p)
1326 {
1327 xarray_assigner<typename E2::value_type>::assign(e1, e2, trivial, p);
1328 }
1329 };
1330
1331 template <>
1332 struct assign_data_impl<xtensor_expression_tag, xoptional_expression_tag>
1333 {
1334 template <class E1, class E2, class P>
1335 static void run(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P p)
1336 {
1337 xexpression_assigner_base<xtensor_expression_tag>::assign_data(e1, e2, trivial, p);
1338 }
1339 };
1340 }
1341
1342 template <class E1, class E2, class P>
1344 xexpression<E1>& e1,
1345 const xexpression<E2>& e2,
1346 bool trivial,
1347 P p
1348 )
1349 {
1350 detail::assign_data_impl<typename E1::expression_tag, typename E2::expression_tag>::run(e1, e2, trivial, p);
1351 }
1352}
1353
1354#endif
Dense multidimensional container adaptor with tensor semantic.
Definition xarray.hpp:216
Dense multidimensional container with tensor semantic.
Definition xarray.hpp:84
Broadcasted xexpression to a specified shape.
Base class for xexpressions.
Multidimensional function operating on xtensor expressions.
View of an xexpression .
Multidimensional function operating on indices.
View of an xexpression from vector of indices.
Reducing function operating over specified axes.
Definition xreducer.hpp:808
View of an xexpression using strides.
Dense multidimensional container adaptor with tensor semantics and fixed dimension.
Definition xtensor.hpp:212
Dense multidimensional container with tensor semantic and fixed dimension.
Definition xtensor.hpp:86
Dense multidimensional container adaptor with view semantics and fixed dimension.
Definition xtensor.hpp:329
Multidimensional view with tensor semantic.
Definition xview.hpp:364
auto sign(E &&e) noexcept -> detail::xfunction_type_t< math::sign_fun, E >
Returns an element-wise indication of the sign of a number.
Definition xmath.hpp:878
auto arg(E &&e) noexcept
Calculates the phase angle (in radians) elementwise for the complex numbers in e.
Definition xcomplex.hpp:221
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 ones(S shape) noexcept
Returns an xexpression containing ones of the specified shape.
Definition xbuilder.hpp:46
auto full_like(const xexpression< E > &e, typename E::value_type fill_value)
Create a xcontainer (xarray, xtensor or xtensor_fixed), filled with fill_value and of the same shape,...
Definition xbuilder.hpp:136