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>
153 constexpr bool is_newaxis_v = is_newaxis<T>::value;
154
155 template <class T, class... S>
156 struct newaxis_count_impl
157 {
158 static constexpr std::size_t count(std::size_t i) noexcept
159 {
160 return i
161 ? (newaxis_count_impl<S...>::count(i - 1)
162 + (is_newaxis<std::remove_reference_t<T>>::value ? 1 : 0))
163 : 0;
164 }
165 };
166
167 template <>
168 struct newaxis_count_impl<void>
169 {
170 static constexpr std::size_t count(std::size_t /*i*/) noexcept
171 {
172 return 0;
173 }
174 };
175 }
176
177 template <class... S>
178 constexpr std::size_t newaxis_count()
179 {
180 return detail::newaxis_count_impl<S..., void>::count(sizeof...(S));
181 }
182
183 template <class... S>
184 constexpr std::size_t newaxis_count_before(std::size_t i)
185 {
186 return detail::newaxis_count_impl<S..., void>::count(i);
187 }
188
189 /**********************************
190 * index of ith non-integral type *
191 **********************************/
192
193 namespace detail
194 {
195
196 template <class T, class... S>
197 struct integral_skip_impl
198 {
199 static constexpr std::size_t count(std::size_t i) noexcept
200 {
201 return i == 0 ? count_impl() : count_impl(i);
202 }
203
204 private:
205
206 static constexpr std::size_t count_impl(std::size_t i) noexcept
207 {
208 return 1
209 + (xtl::is_integral<std::remove_reference_t<T>>::value
210 ? integral_skip_impl<S...>::count(i)
211 : integral_skip_impl<S...>::count(i - 1));
212 }
213
214 static constexpr std::size_t count_impl() noexcept
215 {
216 return xtl::is_integral<std::remove_reference_t<T>>::value
217 ? 1 + integral_skip_impl<S...>::count(0)
218 : 0;
219 }
220 };
221
222 template <>
223 struct integral_skip_impl<void>
224 {
225 static constexpr std::size_t count(std::size_t i) noexcept
226 {
227 return i;
228 }
229 };
230 }
231
232 template <class... S>
233 constexpr std::size_t integral_skip(std::size_t i)
234 {
235 return detail::integral_skip_impl<S..., void>::count(i);
236 }
237
238 /*********************************
239 * index of ith non-newaxis type *
240 *********************************/
241
242 namespace detail
243 {
244
245 template <class T, class... S>
246 struct newaxis_skip_impl
247 {
248 static constexpr std::size_t count(std::size_t i) noexcept
249 {
250 return i == 0 ? count_impl() : count_impl(i);
251 }
252
253 private:
254
255 static constexpr std::size_t count_impl(std::size_t i) noexcept
256 {
257 return 1
258 + (is_newaxis<std::remove_reference_t<T>>::value
259 ? newaxis_skip_impl<S...>::count(i)
260 : newaxis_skip_impl<S...>::count(i - 1));
261 }
262
263 static constexpr std::size_t count_impl() noexcept
264 {
265 return is_newaxis<std::remove_reference_t<T>>::value ? 1 + newaxis_skip_impl<S...>::count(0)
266 : 0;
267 }
268 };
269
270 template <>
271 struct newaxis_skip_impl<void>
272 {
273 static constexpr std::size_t count(std::size_t i) noexcept
274 {
275 return i;
276 }
277 };
278 }
279
280 template <class... S>
281 constexpr std::size_t newaxis_skip(std::size_t i)
282 {
283 return detail::newaxis_skip_impl<S..., void>::count(i);
284 }
285}
286
287#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.