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