xtensor
 
Loading...
Searching...
No Matches
xfunction.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_FUNCTION_HPP
11#define XTENSOR_FUNCTION_HPP
12
13#include <algorithm>
14#include <cstddef>
15#include <iterator>
16#include <numeric>
17#include <tuple>
18#include <type_traits>
19#include <utility>
20
21#include <xtl/xsequence.hpp>
22#include <xtl/xtype_traits.hpp>
23
24#include "../containers/xscalar.hpp"
25#include "../core/xaccessible.hpp"
26#include "../core/xexpression_traits.hpp"
27#include "../core/xiterable.hpp"
28#include "../core/xiterator.hpp"
29#include "../core/xlayout.hpp"
30#include "../core/xshape.hpp"
31#include "../core/xstrides.hpp"
32#include "../utils/xtensor_simd.hpp"
33#include "../utils/xutils.hpp"
34
35namespace xt
36{
37 namespace detail
38 {
39
40 template <bool... B>
41 using conjunction_c = std::conjunction<std::integral_constant<bool, B>...>;
42
43 /************************
44 * xfunction_cache_impl *
45 ************************/
46
47 template <class S, class is_shape_trivial>
48 struct xfunction_cache_impl
49 {
50 S shape;
51 bool is_trivial;
52 bool is_initialized;
53
54 xfunction_cache_impl()
55 : shape(xtl::make_sequence<S>(0, std::size_t(0)))
56 , is_trivial(false)
57 , is_initialized(false)
58 {
59 }
60 };
61
62 template <std::size_t... N, class is_shape_trivial>
63 struct xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>
64 {
65 XTENSOR_CONSTEXPR_ENHANCED_STATIC fixed_shape<N...> shape = fixed_shape<N...>();
66 XTENSOR_CONSTEXPR_ENHANCED_STATIC bool is_trivial = is_shape_trivial::value;
67 XTENSOR_CONSTEXPR_ENHANCED_STATIC bool is_initialized = true;
68 };
69
70#ifdef XTENSOR_HAS_CONSTEXPR_ENHANCED
71 // Out of line definitions to prevent linker errors prior to C++17
72 template <std::size_t... N, class is_shape_trivial>
73 constexpr fixed_shape<N...> xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>::shape;
74
75 template <std::size_t... N, class is_shape_trivial>
76 constexpr bool xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>::is_trivial;
77
78 template <std::size_t... N, class is_shape_trivial>
79 constexpr bool xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>::is_initialized;
80#endif
81
82 template <class... CT>
83 struct xfunction_bool_load_type
84 {
85 using type = xtl::promote_type_t<typename std::decay_t<CT>::bool_load_type...>;
86 };
87
88 template <class CT>
89 struct xfunction_bool_load_type<CT>
90 {
91 using type = typename std::decay_t<CT>::bool_load_type;
92 };
93
94 template <class... CT>
95 using xfunction_bool_load_type_t = typename xfunction_bool_load_type<CT...>::type;
96 }
97
98 /************************
99 * xfunction extensions *
100 ************************/
101
102 namespace extension
103 {
104
105 template <class Tag, class F, class... CT>
107
108 template <class F, class... CT>
110 {
111 using type = xtensor_empty_base;
112 };
113
114 template <class F, class... CT>
115 struct xfunction_base : xfunction_base_impl<xexpression_tag_t<CT...>, F, CT...>
116 {
117 };
118
119 template <class F, class... CT>
120 using xfunction_base_t = typename xfunction_base<F, CT...>::type;
121 }
122
123 template <class promote>
124 struct xfunction_cache : detail::xfunction_cache_impl<typename promote::type, promote>
125 {
126 };
127
128 template <class F, class... CT>
129 class xfunction_iterator;
130
131 template <class F, class... CT>
132 class xfunction_stepper;
133
134 template <class F, class... CT>
135 class xfunction;
136
137 template <class F, class... CT>
139 {
140 using inner_shape_type = promote_shape_t<typename std::decay_t<CT>::shape_type...>;
141 using const_stepper = xfunction_stepper<F, CT...>;
142 using stepper = const_stepper;
143 };
144
145 template <class F, class... CT>
147 {
148 // Added indirection for MSVC 2017 bug with the operator value_type()
149 using func_return_type = typename meta_identity<
150 decltype(std::declval<F>()(std::declval<xvalue_type_t<std::decay_t<CT>>>()...))>::type;
151 using value_type = std::decay_t<func_return_type>;
152 using reference = func_return_type;
153 using const_reference = reference;
154 using size_type = common_size_type_t<std::decay_t<CT>...>;
155 };
156
157 template <class T, class F, class... CT>
158 struct has_simd_interface<xfunction<F, CT...>, T> : std::conjunction<
159 has_simd_type<T>,
160 has_simd_apply<F, xt_simd::simd_type<T>>,
161 has_simd_interface<std::decay_t<CT>, T>...>
162 {
163 };
164
165 /*************************************
166 * overlapping_memory_checker_traits *
167 *************************************/
168
169 template <class E>
171 E,
172 std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xfunction, E>::value>>
173 {
174 template <std::size_t I = 0, class... T, std::enable_if_t<(I == sizeof...(T)), int> = 0>
175 static bool check_tuple(const std::tuple<T...>&, const memory_range&)
176 {
177 return false;
178 }
179
180 template <std::size_t I = 0, class... T, std::enable_if_t<(I < sizeof...(T)), int> = 0>
181 static bool check_tuple(const std::tuple<T...>& t, const memory_range& dst_range)
182 {
183 using ChildE = std::decay_t<decltype(std::get<I>(t))>;
184 return overlapping_memory_checker_traits<ChildE>::check_overlap(std::get<I>(t), dst_range)
185 || check_tuple<I + 1>(t, dst_range);
186 }
187
188 static bool check_overlap(const E& expr, const memory_range& dst_range)
189 {
190 if (expr.size() == 0)
191 {
192 return false;
193 }
194 else
195 {
196 return check_tuple(expr.arguments(), dst_range);
197 }
198 }
199 };
200
201 /*************
202 * xfunction *
203 *************/
204
216 template <class F, class... CT>
217 class xfunction : private xconst_iterable<xfunction<F, CT...>>,
218 public xsharable_expression<xfunction<F, CT...>>,
219 private xconst_accessible<xfunction<F, CT...>>,
220 public extension::xfunction_base_t<F, CT...>
221 {
222 public:
223
224 using self_type = xfunction<F, CT...>;
225 using accessible_base = xconst_accessible<self_type>;
226 using extension_base = extension::xfunction_base_t<F, CT...>;
227 using expression_tag = typename extension_base::expression_tag;
228 using only_scalar = all_xscalar<CT...>;
229 using functor_type = typename std::remove_reference<F>::type;
230 using tuple_type = std::tuple<CT...>;
231
232 using inner_types = xcontainer_inner_types<self_type>;
233 using value_type = typename inner_types::value_type;
234 using reference = typename inner_types::reference;
235 using const_reference = typename inner_types::const_reference;
236 using pointer = value_type*;
237 using const_pointer = const value_type*;
238 using size_type = typename inner_types::size_type;
239 using difference_type = common_difference_type_t<std::decay_t<CT>...>;
240
241 using simd_value_type = xt_simd::simd_type<value_type>;
242
243 // xtl::promote_type_t<typename std::decay_t<CT>::bool_load_type...>;
244 using bool_load_type = detail::xfunction_bool_load_type_t<CT...>;
245
246 template <class requested_type>
247 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
248
249 using iterable_base = xconst_iterable<xfunction<F, CT...>>;
250 using inner_shape_type = typename iterable_base::inner_shape_type;
251 using shape_type = inner_shape_type;
252
253 using stepper = typename iterable_base::stepper;
254 using const_stepper = typename iterable_base::const_stepper;
255
256 static constexpr layout_type static_layout = compute_layout(std::decay_t<CT>::static_layout...);
257 static constexpr bool contiguous_layout = static_layout != layout_type::dynamic;
258
259 template <layout_type L>
260 using layout_iterator = typename iterable_base::template layout_iterator<L>;
261 template <layout_type L>
262 using const_layout_iterator = typename iterable_base::template const_layout_iterator<L>;
263 template <layout_type L>
264 using reverse_layout_iterator = typename iterable_base::template reverse_layout_iterator<L>;
265 template <layout_type L>
266 using const_reverse_layout_iterator = typename iterable_base::template const_reverse_layout_iterator<L>;
267
268 template <class S, layout_type L>
269 using broadcast_iterator = typename iterable_base::template broadcast_iterator<S, L>;
270 template <class S, layout_type L>
271 using const_broadcast_iterator = typename iterable_base::template const_broadcast_iterator<S, L>;
272 template <class S, layout_type L>
273 using reverse_broadcast_iterator = typename iterable_base::template reverse_broadcast_iterator<S, L>;
274 template <class S, layout_type L>
275 using const_reverse_broadcast_iterator = typename iterable_base::template const_reverse_broadcast_iterator<S, L>;
276
277 using const_linear_iterator = xfunction_iterator<F, CT...>;
278 using linear_iterator = const_linear_iterator;
279 using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
280 using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
281
282 using iterator = typename iterable_base::iterator;
283 using const_iterator = typename iterable_base::const_iterator;
284 using reverse_iterator = typename iterable_base::reverse_iterator;
285 using const_reverse_iterator = typename iterable_base::const_reverse_iterator;
286
287 template <class Func, class... CTA, class U = std::enable_if_t<!std::is_base_of<std::decay_t<Func>, self_type>::value>>
288 xfunction(Func&& f, CTA&&... e) noexcept;
289
290 template <class FA, class... CTA>
292
293 ~xfunction() = default;
294
295 xfunction(const xfunction&) = default;
296 xfunction& operator=(const xfunction&) = default;
297
298 xfunction(xfunction&&) = default;
299 xfunction& operator=(xfunction&&) = default;
300
302 size_type dimension() const noexcept;
303 const inner_shape_type& shape() const;
304 layout_type layout() const noexcept;
305 bool is_contiguous() const noexcept;
306 using accessible_base::shape;
307
308 template <class... Args>
309 const_reference operator()(Args... args) const;
310
311 template <class... Args>
312 const_reference unchecked(Args... args) const;
313
314 using accessible_base::at;
315 using accessible_base::operator[];
316 using accessible_base::back;
317 using accessible_base::front;
318 using accessible_base::in_bounds;
319 using accessible_base::periodic;
320
321 template <class It>
322 const_reference element(It first, It last) const;
323
324 template <class S>
325 bool broadcast_shape(S& shape, bool reuse_cache = false) const;
326
327 template <class S>
328 bool has_linear_assign(const S& strides) const noexcept;
329
330 using iterable_base::begin;
331 using iterable_base::cbegin;
332 using iterable_base::cend;
333 using iterable_base::crbegin;
334 using iterable_base::crend;
335 using iterable_base::end;
336 using iterable_base::rbegin;
337 using iterable_base::rend;
338
339 const_linear_iterator linear_begin() const noexcept;
340 const_linear_iterator linear_end() const noexcept;
341 const_linear_iterator linear_cbegin() const noexcept;
342 const_linear_iterator linear_cend() const noexcept;
343
344 const_reverse_linear_iterator linear_rbegin() const noexcept;
345 const_reverse_linear_iterator linear_rend() const noexcept;
346 const_reverse_linear_iterator linear_crbegin() const noexcept;
347 const_reverse_linear_iterator linear_crend() const noexcept;
348
349 template <class S>
350 const_stepper stepper_begin(const S& shape) const noexcept;
351 template <class S>
352 const_stepper stepper_end(const S& shape, layout_type l) const noexcept;
353
354 const_reference data_element(size_type i) const;
355
356 const_reference flat(size_type i) const;
357
358 template <class UT = self_type, class = typename std::enable_if<UT::only_scalar::value>::type>
359 operator value_type() const;
360
361 template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
362 simd_return_type<requested_type> load_simd(size_type i) const;
363
364 const tuple_type& arguments() const noexcept;
365
366 const functor_type& functor() const noexcept;
367
368 private:
369
370 template <std::size_t... I>
371 layout_type layout_impl(std::index_sequence<I...>) const noexcept;
372
373 template <std::size_t... I, class... Args>
374 const_reference access_impl(std::index_sequence<I...>, Args... args) const;
375
376 template <std::size_t... I, class... Args>
377 const_reference unchecked_impl(std::index_sequence<I...>, Args... args) const;
378
379 template <std::size_t... I, class It>
380 const_reference element_access_impl(std::index_sequence<I...>, It first, It last) const;
381
382 template <std::size_t... I>
383 const_reference data_element_impl(std::index_sequence<I...>, size_type i) const;
384
385 template <class align, class requested_type, std::size_t N, std::size_t... I>
386 auto load_simd_impl(std::index_sequence<I...>, size_type i) const;
387
388 template <class Func, std::size_t... I>
389 const_stepper build_stepper(Func&& f, std::index_sequence<I...>) const noexcept;
390
391 template <class Func, std::size_t... I>
392 auto build_iterator(Func&& f, std::index_sequence<I...>) const noexcept;
393
394 size_type compute_dimension() const noexcept;
395
396 void compute_cached_shape() const;
397
398 tuple_type m_e;
399 functor_type m_f;
400 mutable xfunction_cache<detail::promote_index<typename std::decay_t<CT>::shape_type...>> m_cache;
401
402 friend class xfunction_iterator<F, CT...>;
403 friend class xfunction_stepper<F, CT...>;
404 friend class xconst_iterable<self_type>;
405 friend class xconst_accessible<self_type>;
406 };
407
408 /**********************
409 * xfunction_iterator *
410 **********************/
411
412 template <class F, class... CT>
413 class xfunction_iterator : public xtl::xrandom_access_iterator_base<
414 xfunction_iterator<F, CT...>,
415 typename xfunction<F, CT...>::value_type,
416 typename xfunction<F, CT...>::difference_type,
417 typename xfunction<F, CT...>::pointer,
418 typename xfunction<F, CT...>::reference>
419 {
420 public:
421
422 using self_type = xfunction_iterator<F, CT...>;
423 using functor_type = typename std::remove_reference<F>::type;
424 using xfunction_type = xfunction<F, CT...>;
425
426 using value_type = typename xfunction_type::value_type;
427 using reference = typename xfunction_type::value_type;
428 using pointer = typename xfunction_type::const_pointer;
429 using difference_type = typename xfunction_type::difference_type;
430 using iterator_category = std::random_access_iterator_tag;
431
432 template <class... It>
433 xfunction_iterator(const xfunction_type* func, It&&... it) noexcept;
434
435 self_type& operator++();
436 self_type& operator--();
437
438 self_type& operator+=(difference_type n);
439 self_type& operator-=(difference_type n);
440
441 difference_type operator-(const self_type& rhs) const;
442
443 reference operator*() const;
444
445 bool equal(const self_type& rhs) const;
446 bool less_than(const self_type& rhs) const;
447
448 private:
449
450 using data_type = std::tuple<decltype(xt::linear_begin(std::declval<const std::decay_t<CT>>()))...>;
451
452 template <std::size_t... I>
453 reference deref_impl(std::index_sequence<I...>) const;
454
455 template <std::size_t... I>
456 difference_type
457 tuple_max_diff(std::index_sequence<I...>, const data_type& lhs, const data_type& rhs) const;
458
459 const xfunction_type* p_f;
460 data_type m_it;
461 };
462
463 template <class F, class... CT>
464 bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
465
466 template <class F, class... CT>
467 bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
468
469 /*********************
470 * xfunction_stepper *
471 *********************/
472
473 template <class F, class... CT>
474 class xfunction_stepper
475 {
476 public:
477
478 using self_type = xfunction_stepper<F, CT...>;
479 using functor_type = typename std::remove_reference<F>::type;
480 using xfunction_type = xfunction<F, CT...>;
481
482 using value_type = typename xfunction_type::value_type;
483 using reference = typename xfunction_type::reference;
484 using pointer = typename xfunction_type::const_pointer;
485 using size_type = typename xfunction_type::size_type;
486 using difference_type = typename xfunction_type::difference_type;
487
488 using shape_type = typename xfunction_type::shape_type;
489
490 template <class requested_type>
491 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
492
493 template <class... St>
494 xfunction_stepper(const xfunction_type* func, St&&... st) noexcept;
495
496 void step(size_type dim);
497 void step_back(size_type dim);
498 void step(size_type dim, size_type n);
499 void step_back(size_type dim, size_type n);
500 void reset(size_type dim);
501 void reset_back(size_type dim);
502
503 void to_begin();
504 void to_end(layout_type l);
505
506 reference operator*() const;
507
508 template <class T>
509 simd_return_type<T> step_simd();
510
511 void step_leading();
512
513 private:
514
515 template <std::size_t... I>
516 reference deref_impl(std::index_sequence<I...>) const;
517
518 template <class T, std::size_t... I>
519 simd_return_type<T> step_simd_impl(std::index_sequence<I...>);
520
521 const xfunction_type* p_f;
522 std::tuple<typename std::decay_t<CT>::const_stepper...> m_st;
523 };
524
525 /*********************************
526 * xfunction implementation *
527 *********************************/
528
533
539 template <class F, class... CT>
540 template <class Func, class... CTA, class U>
541 inline xfunction<F, CT...>::xfunction(Func&& f, CTA&&... e) noexcept
542 : m_e(std::forward<CTA>(e)...)
543 , m_f(std::forward<Func>(f))
544 {
545 }
546
552 template <class F, class... CT>
553 template <class FA, class... CTA>
555 : m_e(xf.arguments())
556 , m_f(xf.functor())
557 {
558 }
559
561
566
569 template <class F, class... CT>
570 inline auto xfunction<F, CT...>::dimension() const noexcept -> size_type
571 {
572 size_type dimension = m_cache.is_initialized ? m_cache.shape.size() : compute_dimension();
573 return dimension;
574 }
575
576 template <class F, class... CT>
577 inline void xfunction<F, CT...>::compute_cached_shape() const
578 {
579 static_assert(!detail::is_fixed<shape_type>::value, "Calling compute_cached_shape on fixed!");
580
581 m_cache.shape = uninitialized_shape<xindex_type_t<inner_shape_type>>(compute_dimension());
582 m_cache.is_trivial = broadcast_shape(m_cache.shape, false);
583 m_cache.is_initialized = true;
584 }
585
589 template <class F, class... CT>
590 inline auto xfunction<F, CT...>::shape() const -> const inner_shape_type&
591 {
592 if constexpr (!detail::is_fixed<inner_shape_type>::value)
593 {
594 if (!m_cache.is_initialized)
595 {
596 compute_cached_shape();
597 }
598 }
599 return m_cache.shape;
600 }
601
605 template <class F, class... CT>
607 {
608 return layout_impl(std::make_index_sequence<sizeof...(CT)>());
609 }
610
611 template <class F, class... CT>
612 inline bool xfunction<F, CT...>::is_contiguous() const noexcept
613 {
614 return layout() != layout_type::dynamic
615 && accumulate(
616 [](bool r, const auto& exp)
617 {
618 return r && exp.is_contiguous();
619 },
620 true,
621 m_e
622 );
623 }
624
626
630
637 template <class F, class... CT>
638 template <class... Args>
639 inline auto xfunction<F, CT...>::operator()(Args... args) const -> const_reference
640 {
641 // The static cast prevents the compiler from instantiating the template methods with signed integers,
642 // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
643 return access_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
644 }
645
649
655 template <class F, class... CT>
656 inline auto xfunction<F, CT...>::flat(size_type index) const -> const_reference
657 {
658 return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), index);
659 }
660
680 template <class F, class... CT>
681 template <class... Args>
682 inline auto xfunction<F, CT...>::unchecked(Args... args) const -> const_reference
683 {
684 // The static cast prevents the compiler from instantiating the template methods with signed integers,
685 // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
686 return unchecked_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
687 }
688
696 template <class F, class... CT>
697 template <class It>
698 inline auto xfunction<F, CT...>::element(It first, It last) const -> const_reference
699 {
700 return element_access_impl(std::make_index_sequence<sizeof...(CT)>(), first, last);
701 }
702
704
709
715 template <class F, class... CT>
716 template <class S>
717 inline bool xfunction<F, CT...>::broadcast_shape(S& shape, bool reuse_cache) const
718 {
719 if (m_cache.is_initialized && reuse_cache)
720 {
721 std::copy(m_cache.shape.cbegin(), m_cache.shape.cend(), shape.begin());
722 return m_cache.is_trivial;
723 }
724 else
725 {
726 // e.broadcast_shape must be evaluated even if b is false
727 auto func = [&shape](bool b, auto&& e)
728 {
729 return e.broadcast_shape(shape) && b;
730 };
731 return accumulate(func, true, m_e);
732 }
733 }
734
740 template <class F, class... CT>
741 template <class S>
742 inline bool xfunction<F, CT...>::has_linear_assign(const S& strides) const noexcept
743 {
744 auto func = [&strides](bool b, auto&& e)
745 {
746 return b && e.has_linear_assign(strides);
747 };
748 return accumulate(func, true, m_e);
749 }
750
752
753 template <class F, class... CT>
754 inline auto xfunction<F, CT...>::linear_begin() const noexcept -> const_linear_iterator
755 {
756 return linear_cbegin();
757 }
758
759 template <class F, class... CT>
760 inline auto xfunction<F, CT...>::linear_end() const noexcept -> const_linear_iterator
761 {
762 return linear_cend();
763 }
764
765 template <class F, class... CT>
766 inline auto xfunction<F, CT...>::linear_cbegin() const noexcept -> const_linear_iterator
767 {
768 auto f = [](const auto& e) noexcept
769 {
770 return xt::linear_begin(e);
771 };
772 return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
773 }
774
775 template <class F, class... CT>
776 inline auto xfunction<F, CT...>::linear_cend() const noexcept -> const_linear_iterator
777 {
778 auto f = [](const auto& e) noexcept
779 {
780 return xt::linear_end(e);
781 };
782 return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
783 }
784
785 template <class F, class... CT>
786 inline auto xfunction<F, CT...>::linear_rbegin() const noexcept -> const_reverse_linear_iterator
787 {
788 return linear_crbegin();
789 }
790
791 template <class F, class... CT>
792 inline auto xfunction<F, CT...>::linear_rend() const noexcept -> const_reverse_linear_iterator
793 {
794 return linear_crend();
795 }
796
797 template <class F, class... CT>
798 inline auto xfunction<F, CT...>::linear_crbegin() const noexcept -> const_reverse_linear_iterator
799 {
800 return const_reverse_linear_iterator(linear_cend());
801 }
802
803 template <class F, class... CT>
804 inline auto xfunction<F, CT...>::linear_crend() const noexcept -> const_reverse_linear_iterator
805 {
806 return const_reverse_linear_iterator(linear_cbegin());
807 }
808
809 template <class F, class... CT>
810 template <class S>
811 inline auto xfunction<F, CT...>::stepper_begin(const S& shape) const noexcept -> const_stepper
812 {
813 auto f = [&shape](const auto& e) noexcept
814 {
815 return e.stepper_begin(shape);
816 };
817 return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
818 }
819
820 template <class F, class... CT>
821 template <class S>
822 inline auto xfunction<F, CT...>::stepper_end(const S& shape, layout_type l) const noexcept -> const_stepper
823 {
824 auto f = [&shape, l](const auto& e) noexcept
825 {
826 return e.stepper_end(shape, l);
827 };
828 return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
829 }
830
831 template <class F, class... CT>
832 inline auto xfunction<F, CT...>::data_element(size_type i) const -> const_reference
833 {
834 return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), i);
835 }
836
837 template <class F, class... CT>
838 template <class UT, class>
839 inline xfunction<F, CT...>::operator value_type() const
840 {
841 return operator()();
842 }
843
844 template <class F, class... CT>
845 template <class align, class requested_type, std::size_t N>
846 inline auto xfunction<F, CT...>::load_simd(size_type i) const -> simd_return_type<requested_type>
847 {
848 return load_simd_impl<align, requested_type, N>(std::make_index_sequence<sizeof...(CT)>(), i);
849 }
850
851 template <class F, class... CT>
852 inline auto xfunction<F, CT...>::arguments() const noexcept -> const tuple_type&
853 {
854 return m_e;
855 }
856
857 template <class F, class... CT>
858 inline auto xfunction<F, CT...>::functor() const noexcept -> const functor_type&
859 {
860 return m_f;
861 }
862
863 template <class F, class... CT>
864 template <std::size_t... I>
865 inline layout_type xfunction<F, CT...>::layout_impl(std::index_sequence<I...>) const noexcept
866 {
867 return compute_layout(std::get<I>(m_e).layout()...);
868 }
869
870 template <class F, class... CT>
871 template <std::size_t... I, class... Args>
872 inline auto xfunction<F, CT...>::access_impl(std::index_sequence<I...>, Args... args) const
873 -> const_reference
874 {
875 XTENSOR_TRY(check_index(shape(), args...));
876 XTENSOR_CHECK_DIMENSION(shape(), args...);
877 return m_f(std::get<I>(m_e)(args...)...);
878 }
879
880 template <class F, class... CT>
881 template <std::size_t... I, class... Args>
882 inline auto xfunction<F, CT...>::unchecked_impl(std::index_sequence<I...>, Args... args) const
883 -> const_reference
884 {
885 return m_f(std::get<I>(m_e).unchecked(args...)...);
886 }
887
888 template <class F, class... CT>
889 template <std::size_t... I, class It>
890 inline auto xfunction<F, CT...>::element_access_impl(std::index_sequence<I...>, It first, It last) const
891 -> const_reference
892 {
893 XTENSOR_TRY(check_element_index(shape(), first, last));
894 return m_f((std::get<I>(m_e).element(first, last))...);
895 }
896
897 template <class F, class... CT>
898 template <std::size_t... I>
899 inline auto xfunction<F, CT...>::data_element_impl(std::index_sequence<I...>, size_type i) const
900 -> const_reference
901 {
902 return m_f((std::get<I>(m_e).data_element(i))...);
903 }
904
905 template <class F, class... CT>
906 template <class align, class requested_type, std::size_t N, std::size_t... I>
907 inline auto xfunction<F, CT...>::load_simd_impl(std::index_sequence<I...>, size_type i) const
908 {
909 return m_f.simd_apply((std::get<I>(m_e).template load_simd<align, requested_type>(i))...);
910 }
911
912 template <class F, class... CT>
913 template <class Func, std::size_t... I>
914 inline auto xfunction<F, CT...>::build_stepper(Func&& f, std::index_sequence<I...>) const noexcept
915 -> const_stepper
916 {
917 return const_stepper(this, f(std::get<I>(m_e))...);
918 }
919
920 template <class F, class... CT>
921 template <class Func, std::size_t... I>
922 inline auto xfunction<F, CT...>::build_iterator(Func&& f, std::index_sequence<I...>) const noexcept
923 {
924 return const_linear_iterator(this, f(std::get<I>(m_e))...);
925 }
926
927 template <class F, class... CT>
928 inline auto xfunction<F, CT...>::compute_dimension() const noexcept -> size_type
929 {
930 auto func = [](size_type d, auto&& e) noexcept
931 {
932 return (std::max)(d, e.dimension());
933 };
934 return accumulate(func, size_type(0), m_e);
935 }
936
937 /*************************************
938 * xfunction_iterator implementation *
939 *************************************/
940
941 template <class F, class... CT>
942 template <class... It>
943 inline xfunction_iterator<F, CT...>::xfunction_iterator(const xfunction_type* func, It&&... it) noexcept
944 : p_f(func)
945 , m_it(std::forward<It>(it)...)
946 {
947 }
948
949 template <class F, class... CT>
950 inline auto xfunction_iterator<F, CT...>::operator++() -> self_type&
951 {
952 auto f = [](auto& it)
953 {
954 ++it;
955 };
956 for_each(f, m_it);
957 return *this;
958 }
959
960 template <class F, class... CT>
961 inline auto xfunction_iterator<F, CT...>::operator--() -> self_type&
962 {
963 auto f = [](auto& it)
964 {
965 return --it;
966 };
967 for_each(f, m_it);
968 return *this;
969 }
970
971 template <class F, class... CT>
972 inline auto xfunction_iterator<F, CT...>::operator+=(difference_type n) -> self_type&
973 {
974 auto f = [n](auto& it)
975 {
976 it += n;
977 };
978 for_each(f, m_it);
979 return *this;
980 }
981
982 template <class F, class... CT>
983 inline auto xfunction_iterator<F, CT...>::operator-=(difference_type n) -> self_type&
984 {
985 auto f = [n](auto& it)
986 {
987 it -= n;
988 };
989 for_each(f, m_it);
990 return *this;
991 }
992
993 template <class F, class... CT>
994 inline auto xfunction_iterator<F, CT...>::operator-(const self_type& rhs) const -> difference_type
995 {
996 return tuple_max_diff(std::make_index_sequence<sizeof...(CT)>(), m_it, rhs.m_it);
997 }
998
999 template <class F, class... CT>
1000 inline auto xfunction_iterator<F, CT...>::operator*() const -> reference
1001 {
1002 return deref_impl(std::make_index_sequence<sizeof...(CT)>());
1003 }
1004
1005 template <class F, class... CT>
1006 inline bool xfunction_iterator<F, CT...>::equal(const self_type& rhs) const
1007 {
1008 // Optimization: no need to compare each subiterator since they all
1009 // are incremented decremented together.
1010 constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
1011 constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
1012 return std::get<index>(m_it) == std::get<index>(rhs.m_it);
1013 }
1014
1015 template <class F, class... CT>
1016 inline bool xfunction_iterator<F, CT...>::less_than(const self_type& rhs) const
1017 {
1018 // Optimization: no need to compare each subiterator since they all
1019 // are incremented decremented together.
1020 constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
1021 constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
1022 return std::get<index>(m_it) < std::get<index>(rhs.m_it);
1023 }
1024
1025 template <class F, class... CT>
1026 template <std::size_t... I>
1027 inline auto xfunction_iterator<F, CT...>::deref_impl(std::index_sequence<I...>) const -> reference
1028 {
1029 return (p_f->m_f)(*std::get<I>(m_it)...);
1030 }
1031
1032 template <class F, class... CT>
1033 template <std::size_t... I>
1034 inline auto xfunction_iterator<F, CT...>::tuple_max_diff(
1035 std::index_sequence<I...>,
1036 const data_type& lhs,
1037 const data_type& rhs
1038 ) const -> difference_type
1039 {
1040 auto diff = std::make_tuple((std::get<I>(lhs) - std::get<I>(rhs))...);
1041 auto func = [](difference_type n, auto&& v)
1042 {
1043 return (std::max)(n, v);
1044 };
1045 return accumulate(func, difference_type(0), diff);
1046 }
1047
1048 template <class F, class... CT>
1049 inline bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
1050 {
1051 return it1.equal(it2);
1052 }
1053
1054 template <class F, class... CT>
1055 inline bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
1056 {
1057 return it1.less_than(it2);
1058 }
1059
1060 /************************************
1061 * xfunction_stepper implementation *
1062 ************************************/
1063
1064 template <class F, class... CT>
1065 template <class... St>
1066 inline xfunction_stepper<F, CT...>::xfunction_stepper(const xfunction_type* func, St&&... st) noexcept
1067 : p_f(func)
1068 , m_st(std::forward<St>(st)...)
1069 {
1070 }
1071
1072 template <class F, class... CT>
1073 inline void xfunction_stepper<F, CT...>::step(size_type dim)
1074 {
1075 auto f = [dim](auto& st)
1076 {
1077 st.step(dim);
1078 };
1079 for_each(f, m_st);
1080 }
1081
1082 template <class F, class... CT>
1083 inline void xfunction_stepper<F, CT...>::step_back(size_type dim)
1084 {
1085 auto f = [dim](auto& st)
1086 {
1087 st.step_back(dim);
1088 };
1089 for_each(f, m_st);
1090 }
1091
1092 template <class F, class... CT>
1093 inline void xfunction_stepper<F, CT...>::step(size_type dim, size_type n)
1094 {
1095 auto f = [dim, n](auto& st)
1096 {
1097 st.step(dim, n);
1098 };
1099 for_each(f, m_st);
1100 }
1101
1102 template <class F, class... CT>
1103 inline void xfunction_stepper<F, CT...>::step_back(size_type dim, size_type n)
1104 {
1105 auto f = [dim, n](auto& st)
1106 {
1107 st.step_back(dim, n);
1108 };
1109 for_each(f, m_st);
1110 }
1111
1112 template <class F, class... CT>
1113 inline void xfunction_stepper<F, CT...>::reset(size_type dim)
1114 {
1115 auto f = [dim](auto& st)
1116 {
1117 st.reset(dim);
1118 };
1119 for_each(f, m_st);
1120 }
1121
1122 template <class F, class... CT>
1123 inline void xfunction_stepper<F, CT...>::reset_back(size_type dim)
1124 {
1125 auto f = [dim](auto& st)
1126 {
1127 st.reset_back(dim);
1128 };
1129 for_each(f, m_st);
1130 }
1131
1132 template <class F, class... CT>
1133 inline void xfunction_stepper<F, CT...>::to_begin()
1134 {
1135 auto f = [](auto& st)
1136 {
1137 st.to_begin();
1138 };
1139 for_each(f, m_st);
1140 }
1141
1142 template <class F, class... CT>
1143 inline void xfunction_stepper<F, CT...>::to_end(layout_type l)
1144 {
1145 auto f = [l](auto& st)
1146 {
1147 st.to_end(l);
1148 };
1149 for_each(f, m_st);
1150 }
1151
1152 template <class F, class... CT>
1153 inline auto xfunction_stepper<F, CT...>::operator*() const -> reference
1154 {
1155 return deref_impl(std::make_index_sequence<sizeof...(CT)>());
1156 }
1157
1158 template <class F, class... CT>
1159 template <std::size_t... I>
1160 inline auto xfunction_stepper<F, CT...>::deref_impl(std::index_sequence<I...>) const -> reference
1161 {
1162 return (p_f->m_f)(*std::get<I>(m_st)...);
1163 }
1164
1165 template <class F, class... CT>
1166 template <class T, std::size_t... I>
1167 inline auto xfunction_stepper<F, CT...>::step_simd_impl(std::index_sequence<I...>) -> simd_return_type<T>
1168 {
1169 return (p_f->m_f.simd_apply)(std::get<I>(m_st).template step_simd<T>()...);
1170 }
1171
1172 template <class F, class... CT>
1173 template <class T>
1174 inline auto xfunction_stepper<F, CT...>::step_simd() -> simd_return_type<T>
1175 {
1176 return step_simd_impl<T>(std::make_index_sequence<sizeof...(CT)>());
1177 }
1178
1179 template <class F, class... CT>
1180 inline void xfunction_stepper<F, CT...>::step_leading()
1181 {
1182 auto step_leading_lambda = [](auto&& st)
1183 {
1184 st.step_leading();
1185 };
1186 for_each(step_leading_lambda, m_st);
1187 }
1188}
1189
1190#endif
size_type size() const noexcept
Base class for multidimensional iterable constant expressions.
Definition xiterable.hpp:37
Multidimensional function operating on xtensor expressions.
const_reference back() const
xfunction(xfunction< FA, CTA... > xf) noexcept
Constructs an xfunction applying the specified function given by another xfunction with its arguments...
bool in_bounds(Args... args) const
size_type size() const noexcept
bool broadcast_shape(S &shape, bool reuse_cache=false) const
layout_type layout() const noexcept
const inner_shape_type & shape() const
size_type dimension() const noexcept
Returns the number of dimensions of the function.
xfunction(Func &&f, CTA &&... e) noexcept
Constructs an xfunction applying the specified function to the given arguments.
bool has_linear_assign(const S &strides) const noexcept
const_reference flat(size_type i) const
const_reference front() const
auto operator-(E &&e) noexcept -> detail::xfunction_type_t< detail::negate, E >
Opposite.
auto operator*(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< detail::multiplies, E1, E2 >
Multiplication.
auto equal(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< detail::equal_to, E1, E2 >
Element-wise equality.
auto exp(E &&e) noexcept -> detail::xfunction_type_t< math::exp_fun, E >
Natural exponential function.
Definition xmath.hpp:900
auto diff(const xexpression< T > &a, std::size_t n=1, std::ptrdiff_t axis=-1)
Calculate the n-th discrete difference along the given axis.
Definition xmath.hpp:2908
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
Definition xstrides.hpp:248
standard mathematical functions for xexpressions
constexpr layout_type compute_layout(Args... args) noexcept
Implementation of the following logical table:
Definition xlayout.hpp:88
layout_type
Definition xlayout.hpp:24
auto accumulate(F &&f, E &&e, EVS evaluation_strategy=EVS())
Accumulate and flatten array NOTE This function is not lazy!