xtensor
 
Loading...
Searching...
No Matches
xview_utils.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_VIEW_UTILS_HPP
11#define XTENSOR_VIEW_UTILS_HPP
12
13#include <array>
14
15#include "../core/xlayout.hpp"
16#include "../core/xtensor_forward.hpp"
17#include "../views/xslice.hpp"
18
19namespace xt
20{
21
22 /********************************
23 * helper functions declaration *
24 ********************************/
25
26 // number of integral types in the specified sequence of types
27 template <class... S>
28 constexpr std::size_t integral_count();
29
30 // number of integral types in the specified sequence of types before specified index
31 template <class... S>
32 constexpr std::size_t integral_count_before(std::size_t i);
33
34 // index in the specified sequence of types of the ith non-integral type
35 template <class... S>
36 constexpr std::size_t integral_skip(std::size_t i);
37
38 // number of newaxis types in the specified sequence of types
39 template <class... S>
40 constexpr std::size_t newaxis_count();
41
42 // number of newaxis types in the specified sequence of types before specified index
43 template <class... S>
44 constexpr std::size_t newaxis_count_before(std::size_t i);
45
46 // index in the specified sequence of types of the ith non-newaxis type
47 template <class... S>
48 constexpr std::size_t newaxis_skip(std::size_t i);
49
50 template <class S, class It>
51 inline auto get_slice_value(const S& slice, It& it) noexcept
52 {
53 if constexpr (is_xslice<S>::value)
54 {
55 return slice(typename S::size_type(*it));
56 }
57 else
58 {
59 return static_cast<std::size_t>(slice);
60 }
61 }
62
63 /***********************
64 * view_temporary_type *
65 ***********************/
66
67 namespace detail
68 {
69 template <class T, class S, layout_type L, class... SL>
70 struct view_temporary_type_impl
71 {
72 using type = xt::xarray<T, L>;
73 };
74
75 template <class T, class I, std::size_t N, layout_type L, class... SL>
76 struct view_temporary_type_impl<T, std::array<I, N>, L, SL...>
77 {
78 using type = xt::xtensor<T, N + newaxis_count<SL...>() - integral_count<SL...>(), L>;
79 };
80 }
81
82 template <class E, class... SL>
84 {
85 using type = typename detail::view_temporary_type_impl<
86 std::decay_t<typename E::value_type>,
87 typename E::shape_type,
88 E::static_layout,
89 SL...>::type;
90 };
91
92 template <class E, class... SL>
93 using view_temporary_type_t = typename view_temporary_type<E, SL...>::type;
94
95 /************************
96 * count integral types *
97 ************************/
98
99 namespace detail
100 {
101
102 template <class T, class... S>
103 struct integral_count_impl
104 {
105 static constexpr std::size_t count(std::size_t i) noexcept
106 {
107 return i
108 ? (integral_count_impl<S...>::count(i - 1)
109 + (xtl::is_integral<std::remove_reference_t<T>>::value ? 1 : 0))
110 : 0;
111 }
112 };
113
114 template <>
115 struct integral_count_impl<void>
116 {
117 static constexpr std::size_t count(std::size_t /*i*/) noexcept
118 {
119 return 0;
120 }
121 };
122 }
123
124 template <class... S>
125 constexpr std::size_t integral_count()
126 {
127 return detail::integral_count_impl<S..., void>::count(sizeof...(S));
128 }
129
130 template <class... S>
131 constexpr std::size_t integral_count_before(std::size_t i)
132 {
133 return detail::integral_count_impl<S..., void>::count(i);
134 }
135
136 /***********************
137 * count newaxis types *
138 ***********************/
139
140 namespace detail
141 {
142 template <class T>
143 struct is_newaxis : std::false_type
144 {
145 };
146
147 template <class T>
148 struct is_newaxis<xnewaxis<T>> : public std::true_type
149 {
150 };
151
152 template <class T, class... S>
153 struct newaxis_count_impl
154 {
155 static constexpr std::size_t count(std::size_t i) noexcept
156 {
157 return i
158 ? (newaxis_count_impl<S...>::count(i - 1)
159 + (is_newaxis<std::remove_reference_t<T>>::value ? 1 : 0))
160 : 0;
161 }
162 };
163
164 template <>
165 struct newaxis_count_impl<void>
166 {
167 static constexpr std::size_t count(std::size_t /*i*/) noexcept
168 {
169 return 0;
170 }
171 };
172 }
173
174 template <class... S>
175 constexpr std::size_t newaxis_count()
176 {
177 return detail::newaxis_count_impl<S..., void>::count(sizeof...(S));
178 }
179
180 template <class... S>
181 constexpr std::size_t newaxis_count_before(std::size_t i)
182 {
183 return detail::newaxis_count_impl<S..., void>::count(i);
184 }
185
186 /**********************************
187 * index of ith non-integral type *
188 **********************************/
189
190 namespace detail
191 {
192
193 template <class T, class... S>
194 struct integral_skip_impl
195 {
196 static constexpr std::size_t count(std::size_t i) noexcept
197 {
198 return i == 0 ? count_impl() : count_impl(i);
199 }
200
201 private:
202
203 static constexpr std::size_t count_impl(std::size_t i) noexcept
204 {
205 return 1
206 + (xtl::is_integral<std::remove_reference_t<T>>::value
207 ? integral_skip_impl<S...>::count(i)
208 : integral_skip_impl<S...>::count(i - 1));
209 }
210
211 static constexpr std::size_t count_impl() noexcept
212 {
213 return xtl::is_integral<std::remove_reference_t<T>>::value
214 ? 1 + integral_skip_impl<S...>::count(0)
215 : 0;
216 }
217 };
218
219 template <>
220 struct integral_skip_impl<void>
221 {
222 static constexpr std::size_t count(std::size_t i) noexcept
223 {
224 return i;
225 }
226 };
227 }
228
229 template <class... S>
230 constexpr std::size_t integral_skip(std::size_t i)
231 {
232 return detail::integral_skip_impl<S..., void>::count(i);
233 }
234
235 /*********************************
236 * index of ith non-newaxis type *
237 *********************************/
238
239 namespace detail
240 {
241
242 template <class T, class... S>
243 struct newaxis_skip_impl
244 {
245 static constexpr std::size_t count(std::size_t i) noexcept
246 {
247 return i == 0 ? count_impl() : count_impl(i);
248 }
249
250 private:
251
252 static constexpr std::size_t count_impl(std::size_t i) noexcept
253 {
254 return 1
255 + (is_newaxis<std::remove_reference_t<T>>::value
256 ? newaxis_skip_impl<S...>::count(i)
257 : newaxis_skip_impl<S...>::count(i - 1));
258 }
259
260 static constexpr std::size_t count_impl() noexcept
261 {
262 return is_newaxis<std::remove_reference_t<T>>::value ? 1 + newaxis_skip_impl<S...>::count(0)
263 : 0;
264 }
265 };
266
267 template <>
268 struct newaxis_skip_impl<void>
269 {
270 static constexpr std::size_t count(std::size_t i) noexcept
271 {
272 return i;
273 }
274 };
275 }
276
277 template <class... S>
278 constexpr std::size_t newaxis_skip(std::size_t i)
279 {
280 return detail::newaxis_skip_impl<S..., void>::count(i);
281 }
282}
283
284#endif
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...
layout_type
Definition xlayout.hpp:24
xtensor_container< uvector< T, A >, N, L > xtensor
Alias template on xtensor_container with default parameters for data container type.