Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
2
3#ifndef AWKWARD_CPP_HEADERS_UTILS_H_
4#define AWKWARD_CPP_HEADERS_UTILS_H_
5
6#include <iterator>
7#include <complex>
8#include <type_traits>
9#include <cassert>
10#include <utility>
11#include <stdexcept>
12#include <stdint.h>
13
14namespace awkward {
15
17 template <typename T>
18 const std::string
20 std::cout << "Type " << typeid(T).name() << " is not recognized." << std::endl;
21 return typeid(T).name();
22 }
23
26 template <>
27 const std::string
29 return "bool";
30 }
31
34 template <>
35 const std::string
37 return "int8";
38 }
39
42 template <>
43 const std::string
45 return "int16";
46 }
47
50 template <>
51 const std::string
53 return "int32";
54 }
55
58 template <>
59 const std::string
61 return "int64";
62 }
63
66 template <>
67 const std::string
69 return "uint8";
70 }
71
74 template <>
75 const std::string
77 return "uint16";
78 }
79
82 template <>
83 const std::string
85 return "uint32";
86 }
87
90 template <>
91 const std::string
93 return "uint64";
94 }
95
98 template <>
99 const std::string
101 return "float32";
102 }
103
106 template <>
107 const std::string
109 return "float64";
110 }
111
114 template <>
115 const std::string
117 return "char";
118 }
119
122 template <>
123 const std::string
124 type_to_name<std::complex<float>>() {
125 return "complex64";
126 }
127
130 template <>
131 const std::string
132 type_to_name<std::complex<double>>() {
133 return "complex128";
134 }
135
138 template <typename T>
139 const std::string
141 return type_to_name<T>();
142 }
143
146 template <>
147 const std::string
149 return "u8";
150 }
151
154 template <>
155 const std::string
157 return "i8";
158 }
159
162 template <>
163 const std::string
165 return "u32";
166 }
167
170 template <>
171 const std::string
173 return "i32";
174 }
175
178 template <>
179 const std::string
181 return "i64";
182 }
183
184 template <typename, typename = void>
185 constexpr bool is_iterable{};
186
187 // FIXME:
188 // std::void_t is part of C++17, define it ourselves until we switch to it
189 template <typename...>
190 struct voider {
191 using type = void;
192 };
193
194 template <typename... T>
195 using void_t = typename voider<T...>::type;
196
197 template <typename T>
198 constexpr bool is_iterable<T,
200 decltype(std::declval<T>().end())>> = true;
201
202 template <typename Test, template <typename...> class Ref>
203 struct is_specialization : std::false_type {};
204
205 template <template <typename...> class Ref, typename... Args>
206 struct is_specialization<Ref<Args...>, Ref> : std::true_type {};
207
213 template <typename T>
214 std::string
215 type_to_form(int64_t form_key_id) {
216 if (std::string(typeid(T).name()).find("awkward") != std::string::npos) {
217 return std::string("awkward type");
218 }
219
220 std::stringstream form_key;
221 form_key << "node" << (form_key_id++);
222
223 if (std::is_arithmetic<T>::value) {
224 std::string parameters(type_to_name<T>() + "\", ");
225 if (std::is_same<T, char>::value) {
226 parameters = std::string(
227 "uint8\", \"parameters\": { \"__array__\": \"char\" }, ");
228 }
229 return "{\"class\": \"NumpyArray\", \"primitive\": \"" + parameters +
230 "\"form_key\": \"" + form_key.str() + "\"}";
232 return "{\"class\": \"NumpyArray\", \"primitive\": \"" +
233 type_to_name<T>() + "\", \"form_key\": \"" + form_key.str() +
234 "\"}";
235 }
236
237 typedef typename T::value_type value_type;
238
239 if (is_iterable<T>) {
240 std::string parameters("");
241 if (std::is_same<value_type, char>::value) {
242 parameters =
243 std::string(" \"parameters\": { \"__array__\": \"string\" }, ");
244 }
245 return "{\"class\": \"ListOffsetArray\", \"offsets\": \"i64\", "
246 "\"content\":" +
247 type_to_form<value_type>(form_key_id) + ", " + parameters +
248 "\"form_key\": \"" + form_key.str() + "\"}";
249 }
250 return "unsupported type";
251 }
252
254 template <typename T>
255 bool
257 return (std::string(typeid(T).name()).find("awkward") != std::string::npos);
258 }
259
265 template <size_t INDEX>
266 struct visit_impl {
272 template <typename CONTENT, typename FUNCTION>
273 static void
274 visit(CONTENT& contents, size_t index, FUNCTION fun) {
275 if (index == INDEX - 1) {
276 fun(std::get<INDEX - 1>(contents));
277 } else {
278 visit_impl<INDEX - 1>::visit(contents, index, fun);
279 }
280 }
281 };
282
285 template <>
286 struct visit_impl<0> {
287 template <typename CONTENT, typename FUNCTION>
288 static void
289 visit(CONTENT& /* contents */, size_t /* index */, FUNCTION /* fun */) {
290 assert(false);
291 }
292 };
293
295 template <typename FUNCTION, typename... CONTENTs>
296 void
297 visit_at(std::tuple<CONTENTs...> const& contents, size_t index, FUNCTION fun) {
298 visit_impl<sizeof...(CONTENTs)>::visit(contents, index, fun);
299 }
300
302 template <typename FUNCTION, typename... CONTENTs>
303 void
304 visit_at(std::tuple<CONTENTs...>& contents, size_t index, FUNCTION fun) {
305 visit_impl<sizeof...(CONTENTs)>::visit(contents, index, fun);
306 }
307
308} // namespace awkward
309
310#endif // AWKWARD_CPP_HEADERS_UTILS_H_
Definition: BitMaskedArray.h:15
const std::string type_to_numpy_like< uint8_t >()
Returns numpy-like character code of a primitive type as a string.
Definition: utils.h:148
const std::string type_to_name< double >()
Returns float32 string when the primitive type is a double floating point.
Definition: utils.h:108
const std::string type_to_name< char >()
Returns char string when the primitive type is a character.
Definition: utils.h:116
const std::string type_to_numpy_like< int8_t >()
Returns numpy-like character code i8, when the primitive type is an 8-bit signed integer.
Definition: utils.h:156
const std::string type_to_name< uint64_t >()
Returns uint64 string when the primitive type is a 64-bit unsigned integer.
Definition: utils.h:92
const std::string type_to_numpy_like()
Returns char string when the primitive type is a character.
Definition: utils.h:140
const std::string type_to_name< uint32_t >()
Returns uint32 string when the primitive type is a 32-bit unsigned integer.
Definition: utils.h:84
const std::string type_to_name< uint16_t >()
Returns uint16 string when the primitive type is a 16-bit unsigned integer.
Definition: utils.h:76
std::string type_to_form(int64_t form_key_id)
Generates a Form, which is a unique description of the Layout Builder and its contents in the form of...
Definition: utils.h:215
const std::string type_to_numpy_like< int64_t >()
Returns numpy-like character code i64, when the primitive type is a 64-bit signed integer.
Definition: utils.h:180
bool is_awkward_type()
Check if an RDataFrame column is an Awkward Array.
Definition: utils.h:256
void visit_at(std::tuple< CONTENTs... > const &contents, size_t index, FUNCTION fun)
Visits the tuple contents at index.
Definition: utils.h:297
constexpr bool is_iterable
Definition: utils.h:185
typename voider< T... >::type void_t
Definition: utils.h:195
const std::string type_to_name< float >()
Returns float32 string when the primitive type is a floating point.
Definition: utils.h:100
const std::string type_to_name< int32_t >()
Returns int32 string when the primitive type is a 32-bit signed integer.
Definition: utils.h:52
const std::string type_to_name< uint8_t >()
Returns uint8 string when the primitive type is an 8-bit unsigned integer.
Definition: utils.h:68
const std::string type_to_name< int8_t >()
Returns int8 string when the primitive type is an 8-bit signed integer.
Definition: utils.h:36
const std::string type_to_numpy_like< uint32_t >()
Returns numpy-like character code u32, when the primitive type is a 32-bit unsigned integer.
Definition: utils.h:164
const std::string type_to_name()
Returns the name of a primitive type as a string.
Definition: utils.h:19
const std::string type_to_name< int64_t >()
Returns int64 string when the primitive type is a 64-bit signed integer.
Definition: utils.h:60
const std::string type_to_name< int16_t >()
Returns int16 string when the primitive type is a 16-bit signed integer.
Definition: utils.h:44
const std::string type_to_numpy_like< int32_t >()
Returns numpy-like character code i32, when the primitive type is a 32-bit signed integer.
Definition: utils.h:172
const std::string type_to_name< bool >()
Returns bool string when the primitive type is boolean.
Definition: utils.h:28
Definition: utils.h:203
static void visit(CONTENT &, size_t, FUNCTION)
Definition: utils.h:289
Class to index tuple at runtime.
Definition: utils.h:266
static void visit(CONTENT &contents, size_t index, FUNCTION fun)
Accesses the tuple contents at INDEX and calls the given function on it.
Definition: utils.h:274
Definition: utils.h:190
void type
Definition: utils.h:191