-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathutil.cpp
674 lines (636 loc) · 17.8 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/master/LICENSE
#include <sstream>
#include <set>
#include "rapidjson/document.h"
#include "awkward/cpu-kernels/identities.h"
#include "awkward/cpu-kernels/getitem.h"
#include "awkward/cpu-kernels/operations.h"
#include "awkward/cpu-kernels/reducers.h"
#include "awkward/cpu-kernels/sorting.h"
#include "awkward/util.h"
#include "awkward/Identities.h"
namespace rj = rapidjson;
namespace awkward {
namespace util {
dtype
name_to_dtype(const std::string& name) {
if (name == "bool") {
return util::dtype::boolean;
}
else if (name == "int8") {
return util::dtype::int8;
}
else if (name == "int16") {
return util::dtype::int16;
}
else if (name == "int32") {
return util::dtype::int32;
}
else if (name == "int64") {
return util::dtype::int64;
}
else if (name == "uint8") {
return util::dtype::uint8;
}
else if (name == "uint16") {
return util::dtype::uint16;
}
else if (name == "uint32") {
return util::dtype::uint32;
}
else if (name == "uint64") {
return util::dtype::uint64;
}
else if (name == "float16") {
return util::dtype::float16;
}
else if (name == "float32") {
return util::dtype::float32;
}
else if (name == "float64") {
return util::dtype::float64;
}
else if (name == "float128") {
return util::dtype::float128;
}
else if (name == "complex64") {
return util::dtype::complex64;
}
else if (name == "complex128") {
return util::dtype::complex128;
}
else if (name == "complex256") {
return util::dtype::complex256;
}
// else if (name == "datetime64") {
// return util::dtype::datetime64;
// }
// else if (name == "timedelta64") {
// return util::dtype::timedelta64;
// }
else {
return util::dtype::NOT_PRIMITIVE;
}
}
const std::string
dtype_to_name(dtype dt) {
switch (dt) {
case util::dtype::boolean:
return "bool";
case util::dtype::int8:
return "int8";
case util::dtype::int16:
return "int16";
case util::dtype::int32:
return "int32";
case util::dtype::int64:
return "int64";
case util::dtype::uint8:
return "uint8";
case util::dtype::uint16:
return "uint16";
case util::dtype::uint32:
return "uint32";
case util::dtype::uint64:
return "uint64";
case util::dtype::float16:
return "float16";
case util::dtype::float32:
return "float32";
case util::dtype::float64:
return "float64";
case util::dtype::float128:
return "float128";
case util::dtype::complex64:
return "complex64";
case util::dtype::complex128:
return "complex128";
case util::dtype::complex256:
return "complex256";
// case datetime64:
// return "datetime64";
// case timedelta64:
// return "timedelta64";
default:
return "unknown";
}
}
dtype
format_to_dtype(const std::string& format, int64_t itemsize) {
int32_t test = 1;
bool little_endian = (*(int8_t*)&test == 1);
std::string fmt = format;
if (format.length() > 1) {
std::string endianness = format.substr(0, 1);
if ((endianness == ">" && !little_endian) ||
(endianness == "<" && little_endian) ||
(endianness == "=")) {
fmt = format.substr(1, format.length() - 1);
}
else if ((endianness == ">" && little_endian) ||
(endianness == "<" && !little_endian)) {
return dtype::NOT_PRIMITIVE;
}
}
if (fmt == std::string("?")) {
return dtype::boolean;
}
else if (fmt == std::string("b") ||
fmt == std::string("h") ||
fmt == std::string("i") ||
fmt == std::string("l") ||
fmt == std::string("q")) {
if (itemsize == 1) {
return dtype::int8;
}
else if (itemsize == 2) {
return dtype::int16;
}
else if (itemsize == 4) {
return dtype::int32;
}
else if (itemsize == 8) {
return dtype::int64;
}
else {
return dtype::NOT_PRIMITIVE;
}
}
else if (fmt == std::string("c") ||
fmt == std::string("B") ||
fmt == std::string("H") ||
fmt == std::string("I") ||
fmt == std::string("L") ||
fmt == std::string("Q")) {
if (itemsize == 1) {
return dtype::uint8;
}
else if (itemsize == 2) {
return dtype::uint16;
}
else if (itemsize == 4) {
return dtype::uint32;
}
else if (itemsize == 8) {
return dtype::uint64;
}
else {
return dtype::NOT_PRIMITIVE;
}
}
else if (fmt == std::string("e")) {
return dtype::float16;
}
else if (fmt == std::string("f")) {
return dtype::float32;
}
else if (fmt == std::string("d")) {
return dtype::float64;
}
else if (fmt == std::string("g")) {
return dtype::float128;
}
else if (fmt == std::string("Zf")) {
return dtype::complex64;
}
else if (fmt == std::string("Zd")) {
return dtype::complex128;
}
else if (fmt == std::string("Zg")) {
return dtype::complex256;
}
// else if (fmt == std::string("M")) {
// return dtype::datetime64;
// }
// else if (fmt == std::string("m")) {
// return dtype::timedelta64;
// }
else {
return dtype::NOT_PRIMITIVE;
}
}
const std::string
dtype_to_format(dtype dt) {
switch (dt) {
case dtype::boolean:
return "?";
case dtype::int8:
return "b";
case dtype::int16:
return "h";
case dtype::int32:
#if defined _MSC_VER || defined __i386__
return "l";
#else
return "i";
#endif
case dtype::int64:
#if defined _MSC_VER || defined __i386__
return "q";
#else
return "l";
#endif
case dtype::uint8:
return "B";
case dtype::uint16:
return "H";
case dtype::uint32:
#if defined _MSC_VER || defined __i386__
return "L";
#else
return "I";
#endif
case dtype::uint64:
#if defined _MSC_VER || defined __i386__
return "Q";
#else
return "L";
#endif
case dtype::float16:
return "e";
case dtype::float32:
return "f";
case dtype::float64:
return "d";
case dtype::float128:
return "g";
case dtype::complex64:
return "Zf";
case dtype::complex128:
return "Zd";
case dtype::complex256:
return "Zg";
// case dtype::datetime64:
// return "M";
// case dtype::timedelta64:
// return "m";
default:
return "";
}
}
int64_t
dtype_to_itemsize(dtype dt) {
switch (dt) {
case dtype::boolean:
return 1;
case dtype::int8:
return 1;
case dtype::int16:
return 2;
case dtype::int32:
return 4;
case dtype::int64:
return 8;
case dtype::uint8:
return 1;
case dtype::uint16:
return 2;
case dtype::uint32:
return 4;
case dtype::uint64:
return 8;
case dtype::float16:
return 2;
case dtype::float32:
return 4;
case dtype::float64:
return 8;
case dtype::float128:
return 16;
case dtype::complex64:
return 8;
case dtype::complex128:
return 16;
case dtype::complex256:
return 32;
// case dtype::datetime64:
// return 8;
// case dtype::timedelta64:
// return 8;
default:
return 0;
}
}
bool
is_integer(dtype dt) {
switch (dt) {
case dtype::int8:
case dtype::int16:
case dtype::int32:
case dtype::int64:
case dtype::uint8:
case dtype::uint16:
case dtype::uint32:
case dtype::uint64:
return true;
default:
return false;
}
}
bool
is_signed(dtype dt) {
switch (dt) {
case dtype::int8:
case dtype::int16:
case dtype::int32:
case dtype::int64:
return true;
default:
return false;
}
}
bool
is_unsigned(dtype dt) {
switch (dt) {
case dtype::uint8:
case dtype::uint16:
case dtype::uint32:
case dtype::uint64:
return true;
default:
return false;
}
}
bool
is_real(dtype dt) {
switch (dt) {
case dtype::float16:
case dtype::float32:
case dtype::float64:
case dtype::float128:
return true;
default:
return false;
}
}
bool
is_complex(dtype dt) {
switch (dt) {
case dtype::complex64:
case dtype::complex128:
case dtype::complex256:
return true;
default:
return false;
}
}
void
handle_error(const struct Error& err,
const std::string& classname,
const Identities* identities) {
if(err.pass_through == true) {
throw std::invalid_argument(err.str);
}
else {
if (err.str != nullptr) {
std::stringstream out;
out << "in " << classname;
if (err.identity != kSliceNone && identities != nullptr) {
if (0 <= err.identity && err.identity < identities->length()) {
out << " with identity ["
<< identities->identity_at(err.identity) << "]";
} else {
out << " with invalid identity";
}
}
if (err.attempt != kSliceNone) {
out << " attempting to get " << err.attempt;
}
out << ", " << err.str;
throw std::invalid_argument(out.str());
}
}
}
template<typename T>
IndexOf<T> make_starts(const IndexOf<T> &offsets) {
return IndexOf<T>(offsets.ptr(),
offsets.offset(),
offsets.length() - 1,
offsets.ptr_lib());
}
template<typename T>
IndexOf<T> make_stops(const IndexOf<T> &offsets) {
return IndexOf<T>(offsets.ptr(),
offsets.offset() + 1,
offsets.length() - 1,
offsets.ptr_lib());
}
template IndexOf<int32_t> make_starts(const IndexOf<int32_t> &offsets);
template IndexOf<uint32_t> make_starts(const IndexOf<uint32_t> &offsets);
template IndexOf<int64_t> make_starts(const IndexOf<int64_t> &offsets);
template IndexOf<int32_t> make_stops(const IndexOf<int32_t> &offsets);
template IndexOf<uint32_t> make_stops(const IndexOf<uint32_t> &offsets);
template IndexOf<int64_t> make_stops(const IndexOf<int64_t> &offsets);
std::string
quote(const std::string &x, bool doublequote) {
// TODO: escape characters, possibly using RapidJSON.
if (doublequote) {
return std::string("\"") + x + std::string("\"");
} else {
return std::string("'") + x + std::string("'");
}
}
RecordLookupPtr
init_recordlookup(int64_t numfields) {
RecordLookupPtr out = std::make_shared<RecordLookup>();
for (int64_t i = 0; i < numfields; i++) {
out.get()->push_back(std::to_string(i));
}
return out;
}
int64_t
fieldindex(const RecordLookupPtr &recordlookup,
const std::string &key,
int64_t numfields) {
int64_t out = -1;
if (recordlookup.get() != nullptr) {
for (size_t i = 0; i < recordlookup.get()->size(); i++) {
if (recordlookup.get()->at(i) == key) {
out = (int64_t) i;
break;
}
}
}
if (out == -1) {
try {
out = (int64_t) std::stoi(key);
}
catch (std::invalid_argument err) {
throw std::invalid_argument(
std::string("key ") + quote(key, true)
+ std::string(" does not exist (not in record)"));
}
if (!(0 <= out && out < numfields)) {
throw std::invalid_argument(
std::string("key interpreted as fieldindex ") + key
+ std::string(" for records with only " + std::to_string(numfields)
+ std::string(" fields")));
}
}
return out;
}
const std::string
key(const RecordLookupPtr &recordlookup,
int64_t fieldindex,
int64_t numfields) {
if (fieldindex >= numfields) {
throw std::invalid_argument(
std::string("fieldindex ") + std::to_string(fieldindex)
+ std::string(" for records with only " + std::to_string(numfields)
+ std::string(" fields")));
}
if (recordlookup.get() != nullptr) {
return recordlookup.get()->at((size_t) fieldindex);
} else {
return std::to_string(fieldindex);
}
}
bool
haskey(const RecordLookupPtr &recordlookup,
const std::string &key,
int64_t numfields) {
try {
fieldindex(recordlookup, key, numfields);
}
catch (std::invalid_argument err) {
return false;
}
return true;
}
const std::vector<std::string>
keys(const RecordLookupPtr &recordlookup, int64_t numfields) {
std::vector<std::string> out;
if (recordlookup.get() != nullptr) {
out.insert(out.end(),
recordlookup.get()->begin(),
recordlookup.get()->end());
} else {
int64_t cols = numfields;
for (int64_t j = 0; j < cols; j++) {
out.push_back(std::to_string(j));
}
}
return out;
}
bool
parameter_equals(const Parameters ¶meters,
const std::string &key,
const std::string &value) {
auto item = parameters.find(key);
std::string myvalue;
if (item == parameters.end()) {
myvalue = "null";
} else {
myvalue = item->second;
}
rj::Document mine;
rj::Document yours;
mine.Parse<rj::kParseNanAndInfFlag>(myvalue.c_str());
yours.Parse<rj::kParseNanAndInfFlag>(value.c_str());
return mine == yours;
}
bool
parameters_equal(const Parameters &self, const Parameters &other) {
std::set<std::string> checked;
for (auto pair : self) {
if (!parameter_equals(other, pair.first, pair.second)) {
return false;
}
checked.insert(pair.first);
}
for (auto pair : other) {
if (checked.find(pair.first) == checked.end()) {
if (!parameter_equals(self, pair.first, pair.second)) {
return false;
}
}
}
return true;
}
bool
parameter_isstring(const Parameters ¶meters, const std::string &key) {
auto item = parameters.find(key);
if (item == parameters.end()) {
return false;
}
rj::Document mine;
mine.Parse<rj::kParseNanAndInfFlag>(item->second.c_str());
return mine.IsString();
}
bool
parameter_isname(const Parameters ¶meters, const std::string &key) {
auto item = parameters.find(key);
if (item == parameters.end()) {
return false;
}
rj::Document mine;
mine.Parse<rj::kParseNanAndInfFlag>(item->second.c_str());
if (!mine.IsString()) {
return false;
}
std::string value = mine.GetString();
if (value.empty()) {
return false;
}
if (!((value[0] >= 'a' && value[0] <= 'z') ||
(value[0] >= 'A' && value[0] <= 'Z') ||
(value[0] == '_'))) {
return false;
}
for (size_t i = 1; i < value.length(); i++) {
if (!((value[i] >= 'a' && value[i] <= 'z') ||
(value[i] >= 'A' && value[i] <= 'Z') ||
(value[i] >= '0' && value[i] <= '9') ||
(value[i] == '_'))) {
return false;
}
}
return true;
}
const std::string
parameter_asstring(const Parameters ¶meters, const std::string &key) {
auto item = parameters.find(key);
if (item == parameters.end()) {
throw std::runtime_error("parameter is null");
}
rj::Document mine;
mine.Parse<rj::kParseNanAndInfFlag>(item->second.c_str());
if (!mine.IsString()) {
throw std::runtime_error("parameter is not a string");
}
return mine.GetString();
}
std::string
gettypestr(const Parameters ¶meters, const TypeStrs &typestrs) {
auto item = parameters.find("__record__");
if (item != parameters.end()) {
std::string source = item->second;
rj::Document recname;
recname.Parse<rj::kParseNanAndInfFlag>(source.c_str());
if (recname.IsString()) {
std::string name = recname.GetString();
for (auto pair : typestrs) {
if (pair.first == name) {
return pair.second;
}
}
}
}
item = parameters.find("__array__");
if (item != parameters.end()) {
std::string source = item->second;
rj::Document recname;
recname.Parse<rj::kParseNanAndInfFlag>(source.c_str());
if (recname.IsString()) {
std::string name = recname.GetString();
for (auto pair : typestrs) {
if (pair.first == name) {
return pair.second;
}
}
}
}
return std::string();
}
}
}