All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 <iostream>
8#include <complex>
9#include <type_traits>
10#include <cassert>
11#include <utility>
12#include <stdexcept>
13#include <stdint.h>
14
15namespace awkward {
16
18 template <typename T>
19 const std::string
21 std::cout << "Type " << typeid(T).name() << " is not recognized." << std::endl;
22 return typeid(T).name();
23 }
24
27 template <>
28 const std::string
30 return "bool";
31 }
32
35 template <>
36 const std::string
38 return "int8";
39 }
40
43 template <>
44 const std::string
46 return "int16";
47 }
48
51 template <>
52 const std::string
54 return "int32";
55 }
56
59 template <>
60 const std::string
62 return "int64";
63 }
64
67 template <>
68 const std::string
70 return "int64";
71 }
72
75 template <>
76 const std::string
78 return "uint8";
79 }
80
83 template <>
84 const std::string
86 return "uint16";
87 }
88
91 template <>
92 const std::string
94 return "uint32";
95 }
96
99 template <>
100 const std::string
102 return "uint64";
103 }
104
107 template <>
108 const std::string
110 return "float32";
111 }
112
115 template <>
116 const std::string
118 return "float64";
119 }
120
123 template <>
124 const std::string
126 return "char";
127 }
128
131 template <>
132 const std::string
133 type_to_name<std::complex<float>>() {
134 return "complex64";
135 }
136
139 template <>
140 const std::string
141 type_to_name<std::complex<double>>() {
142 return "complex128";
143 }
144
147 template <typename T>
148 const std::string
150 return type_to_name<T>();
151 }
152
155 template <>
156 const std::string
158 return "u8";
159 }
160
163 template <>
164 const std::string
166 return "i8";
167 }
168
171 template <>
172 const std::string
174 return "u32";
175 }
176
179 template <>
180 const std::string
182 return "i32";
183 }
184
187 template <>
188 const std::string
190 return "i64";
191 }
192
193 template <typename, typename = void>
194 constexpr bool is_iterable{};
195
196 // FIXME:
197 // std::void_t is part of C++17, define it ourselves until we switch to it
198 template <typename...>
199 struct voider {
200 using type = void;
201 };
202
203 template <typename... T>
204 using void_t = typename voider<T...>::type;
205
206 template <typename T>
207 constexpr bool is_iterable<T,
209 decltype(std::declval<T>().end())>> = true;
210
211 template <typename Test, template <typename...> class Ref>
212 struct is_specialization : std::false_type {};
213
214 template <template <typename...> class Ref, typename... Args>
215 struct is_specialization<Ref<Args...>, Ref> : std::true_type {};
216
222 template <typename T>
223 std::string
224 type_to_form(int64_t form_key_id) {
225 if (std::string(typeid(T).name()).find("awkward") != std::string::npos) {
226 return std::string("awkward type");
227 }
228
229 std::stringstream form_key;
230 form_key << "node" << (form_key_id++);
231
232 if (std::is_arithmetic<T>::value) {
233 std::string parameters(type_to_name<T>() + "\", ");
234 if (std::is_same<T, char>::value) {
235 parameters = std::string(
236 "uint8\", \"parameters\": { \"__array__\": \"char\" }, ");
237 }
238 return "{\"class\": \"NumpyArray\", \"primitive\": \"" + parameters +
239 "\"form_key\": \"" + form_key.str() + "\"}";
241 return "{\"class\": \"NumpyArray\", \"primitive\": \"" +
242 type_to_name<T>() + "\", \"form_key\": \"" + form_key.str() +
243 "\"}";
244 }
245
246 typedef typename T::value_type value_type;
247
248 if (is_iterable<T>) {
249 std::string parameters("");
250 if (std::is_same<value_type, char>::value) {
251 parameters =
252 std::string(" \"parameters\": { \"__array__\": \"string\" }, ");
253 }
254 return "{\"class\": \"ListOffsetArray\", \"offsets\": \"i64\", "
255 "\"content\":" +
256 type_to_form<value_type>(form_key_id) + ", " + parameters +
257 "\"form_key\": \"" + form_key.str() + "\"}";
258 }
259 return "unsupported type";
260 }
261
263 template <typename T>
264 bool
266 return (std::string(typeid(T).name()).find("awkward") != std::string::npos);
267 }
268
274 template <size_t INDEX>
275 struct visit_impl {
281 template <typename CONTENT, typename FUNCTION>
282 static void
283 visit(CONTENT& contents, size_t index, FUNCTION fun) {
284 if (index == INDEX - 1) {
285 fun(std::get<INDEX - 1>(contents));
286 } else {
287 visit_impl<INDEX - 1>::visit(contents, index, fun);
288 }
289 }
290 };
291
294 template <>
295 struct visit_impl<0> {
296 template <typename CONTENT, typename FUNCTION>
297 static void
298 visit(CONTENT& /* contents */, size_t /* index */, FUNCTION /* fun */) {
299 assert(false);
300 }
301 };
302
304 template <typename FUNCTION, typename... CONTENTs>
305 void
306 visit_at(std::tuple<CONTENTs...> const& contents, size_t index, FUNCTION fun) {
307 visit_impl<sizeof...(CONTENTs)>::visit(contents, index, fun);
308 }
309
311 template <typename FUNCTION, typename... CONTENTs>
312 void
313 visit_at(std::tuple<CONTENTs...>& contents, size_t index, FUNCTION fun) {
314 visit_impl<sizeof...(CONTENTs)>::visit(contents, index, fun);
315 }
316
317} // namespace awkward
318
319#endif // AWKWARD_CPP_HEADERS_UTILS_H_
Definition: ArrayBuilder.h:14
const std::string type_to_numpy_like< uint8_t >()
Returns numpy-like character code of a primitive type as a string.
Definition: utils.h:157
const std::string type_to_name< double >()
Returns float32 string when the primitive type is a double floating point.
Definition: utils.h:117
const std::string type_to_name< char >()
Returns char string when the primitive type is a character.
Definition: utils.h:125
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:165
const std::string type_to_name< uint64_t >()
Returns uint64 string when the primitive type is a 64-bit unsigned integer.
Definition: utils.h:101
const std::string type_to_numpy_like()
Returns char string when the primitive type is a character.
Definition: utils.h:149
const std::string type_to_name< uint32_t >()
Returns uint32 string when the primitive type is a 32-bit unsigned integer.
Definition: utils.h:93
const std::string type_to_name< uint16_t >()
Returns uint16 string when the primitive type is a 16-bit unsigned integer.
Definition: utils.h:85
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:224
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:189
bool is_awkward_type()
Check if an RDataFrame column is an Awkward Array.
Definition: utils.h:265
const std::string type_to_name< Long64_t >()
Returns int64 string when the primitive type is a 64-bit signed integer.
Definition: utils.h:69
void visit_at(std::tuple< CONTENTs... > const &contents, size_t index, FUNCTION fun)
Visits the tuple contents at index.
Definition: utils.h:306
constexpr bool is_iterable
Definition: utils.h:194
typename voider< T... >::type void_t
Definition: utils.h:204
const std::string type_to_name< float >()
Returns float32 string when the primitive type is a floating point.
Definition: utils.h:109
const std::string type_to_name< int32_t >()
Returns int32 string when the primitive type is a 32-bit signed integer.
Definition: utils.h:53
const std::string type_to_name< uint8_t >()
Returns uint8 string when the primitive type is an 8-bit unsigned integer.
Definition: utils.h:77
const std::string type_to_name< int8_t >()
Returns int8 string when the primitive type is an 8-bit signed integer.
Definition: utils.h:37
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:173
const std::string type_to_name()
Returns the name of a primitive type as a string.
Definition: utils.h:20
const std::string type_to_name< int64_t >()
Returns int64 string when the primitive type is a 64-bit signed integer.
Definition: utils.h:61
const std::string type_to_name< int16_t >()
Returns int16 string when the primitive type is a 16-bit signed integer.
Definition: utils.h:45
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:181
const std::string type_to_name< bool >()
Returns bool string when the primitive type is boolean.
Definition: utils.h:29
Definition: utils.h:212
static void visit(CONTENT &, size_t, FUNCTION)
Definition: utils.h:298
Class to index tuple at runtime.
Definition: utils.h:275
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:283
Definition: utils.h:199
void type
Definition: utils.h:200