xtensor
Loading...
Searching...
No Matches
xeval.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_EVAL_HPP
11#define XTENSOR_EVAL_HPP
12
13#include "xexpression_traits.hpp"
14#include "xshape.hpp"
15#include "xtensor_forward.hpp"
16
17namespace xt
18{
19
27 namespace detail
28 {
29 template <class T>
30 using is_container = std::is_base_of<xcontainer<std::remove_const_t<T>>, T>;
31 }
32
45 template <class T>
46 inline auto eval(T&& t) -> std::enable_if_t<detail::is_container<std::decay_t<T>>::value, T&&>
47 {
48 return std::forward<T>(t);
49 }
50
52 template <class T>
53 inline auto eval(T&& t)
54 -> std::enable_if_t<!detail::is_container<std::decay_t<T>>::value, temporary_type_t<T>>
55 {
56 return std::forward<T>(t);
57 }
58
60
61 namespace detail
62 {
63 /**********************************
64 * has_same_layout implementation *
65 **********************************/
66
67 template <layout_type L = layout_type::any, class E>
68 constexpr bool has_same_layout()
69 {
70 return (std::decay_t<E>::static_layout == L) || (L == layout_type::any);
71 }
72
73 template <layout_type L = layout_type::any, class E>
74 constexpr bool has_same_layout(E&&)
75 {
76 return has_same_layout<L, E>();
77 }
78
79 template <class E1, class E2>
80 constexpr bool has_same_layout(E1&&, E2&&)
81 {
82 return has_same_layout<std::decay_t<E1>::static_layout, E2>();
83 }
84
85 /*********************************
86 * has_fixed_dims implementation *
87 *********************************/
88
89 template <class E>
90 constexpr bool has_fixed_dims()
91 {
92 return detail::is_array<typename std::decay_t<E>::shape_type>::value;
93 }
94
95 template <class E>
96 constexpr bool has_fixed_dims(E&&)
97 {
98 return has_fixed_dims<E>();
99 }
100
101 /****************************************
102 * as_xarray_container_t implementation *
103 ****************************************/
104
105 template <class E, layout_type L>
106 using as_xarray_container_t = xarray<typename std::decay_t<E>::value_type, layout_remove_any(L)>;
107
108 /*****************************************
109 * as_xtensor_container_t implementation *
110 *****************************************/
111
112 template <class E, layout_type L>
113 using as_xtensor_container_t = xtensor<
114 typename std::decay_t<E>::value_type,
115 std::tuple_size<typename std::decay_t<E>::shape_type>::value,
116 layout_remove_any(L)>;
117 }
118
148 template <layout_type L = layout_type::any, class E>
149 inline auto as_strided(E&& e)
150 -> std::enable_if_t<has_data_interface<std::decay_t<E>>::value && detail::has_same_layout<L, E>(), E&&>
151 {
152 return std::forward<E>(e);
153 }
154
156 template <layout_type L = layout_type::any, class E>
157 inline auto as_strided(E&& e) -> std::enable_if_t<
158 (!(has_data_interface<std::decay_t<E>>::value && detail::has_same_layout<L, E>()))
159 && detail::has_fixed_dims<E>(),
160 detail::as_xtensor_container_t<E, L>>
161 {
162 return e;
163 }
164
165 template <layout_type L = layout_type::any, class E>
166 inline auto as_strided(E&& e) -> std::enable_if_t<
167 (!(has_data_interface<std::decay_t<E>>::value && detail::has_same_layout<L, E>()))
168 && (!detail::has_fixed_dims<E>()),
169 detail::as_xarray_container_t<E, L>>
170 {
171 return e;
172 }
173
175}
176
177#endif
auto eval(T &&t) -> std::enable_if_t< detail::is_container< std::decay_t< T > >::value, T && >
Force evaluation of xexpression.
Definition xeval.hpp:46
auto as_strided(E &&e) -> std::enable_if_t< has_data_interface< std::decay_t< E > >::value &&detail::has_same_layout< L, E >(), E && >
Force evaluation of xexpression not providing a data interface and convert to the required layout.
Definition xeval.hpp:149
standard mathematical functions for xexpressions
xtensor_container< uvector< T, A >, N, L > xtensor
Alias template on xtensor_container with default parameters for data container type.