forked from scikit-hep/awkward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Record.cpp
690 lines (606 loc) · 21.2 KB
/
Record.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
#define FILENAME(line) FILENAME_FOR_EXCEPTIONS("src/libawkward/array/Record.cpp", line)
#define FILENAME_C(line) FILENAME_FOR_EXCEPTIONS_C("src/libawkward/array/Record.cpp", line)
#include <sstream>
#include <memory>
#include "awkward/kernels.h"
#include "awkward/type/RecordType.h"
#include "awkward/Reducer.h"
#include "awkward/array/Record.h"
#include "awkward/io/json.h"
namespace awkward {
Record::Record(const std::shared_ptr<const RecordArray> array, int64_t at)
: Content(Identities::none(), array.get()->parameters())
, array_(array)
, at_(at) {
if (!(0 <= at && at < array.get()->length())) {
throw std::invalid_argument(
std::string("at=") + std::to_string(at)
+ std::string(" is out of range for recordarray")
+ FILENAME(__LINE__));
}
}
const std::shared_ptr<const RecordArray>
Record::array() const {
return array_;
}
int64_t
Record::at() const {
return at_;
}
const ContentPtrVec
Record::contents() const {
ContentPtrVec out;
for (auto item : array_.get()->contents()) {
out.push_back(item.get()->getitem_at_nowrap(at_));
}
return out;
}
const util::RecordLookupPtr
Record::recordlookup() const {
return array_.get()->recordlookup();
}
bool
Record::istuple() const {
return array_.get()->istuple();
}
bool
Record::isscalar() const {
return true;
}
const std::string
Record::classname() const {
return "Record";
}
const IdentitiesPtr
Record::identities() const {
IdentitiesPtr recidentities = array_.get()->identities();
if (recidentities.get() == nullptr) {
return recidentities;
}
else {
return recidentities.get()->getitem_range_nowrap(at_, at_ + 1);
}
}
void
Record::setidentities() {
throw std::runtime_error(
std::string("undefined operation: Record::setidentities")
+ FILENAME(__LINE__));
}
void
Record::setidentities(const IdentitiesPtr& identities) {
throw std::runtime_error(
std::string("undefined operation: Record::setidentities")
+ FILENAME(__LINE__));
}
const TypePtr
Record::type(const util::TypeStrs& typestrs) const {
return form(true).get()->type(typestrs);
}
const FormPtr
Record::form(bool materialize) const {
return array_.get()->form(materialize);
}
kernel::lib
Record::kernels() const {
return array_.get()->kernels();
}
void
Record::caches(std::vector<ArrayCachePtr>& out) const {
array_.get()->caches(out);
}
const std::string
Record::tostring_part(const std::string& indent,
const std::string& pre,
const std::string& post) const {
std::stringstream out;
out << indent << pre << "<" << classname() << " at=\"" << at_ << "\">\n";
if (!parameters_.empty()) {
out << parameters_tostring(indent + std::string(" "), "", "\n");
}
out << array_.get()->tostring_part(indent + std::string(" "), "", "\n");
out << indent << "</" << classname() << ">" << post;
return out.str();
}
void
Record::tojson_part(ToJson& builder, bool include_beginendlist) const {
size_t cols = (size_t)numfields();
util::RecordLookupPtr keys = array_.get()->recordlookup();
if (istuple()) {
keys = std::make_shared<util::RecordLookup>();
for (size_t j = 0; j < cols; j++) {
keys.get()->push_back(std::to_string(j));
}
}
ContentPtrVec contents = array_.get()->contents();
builder.beginrecord();
for (size_t j = 0; j < cols; j++) {
builder.field(keys.get()->at(j).c_str());
contents[j].get()->getitem_at_nowrap(at_).get()->tojson_part(builder,
true);
}
builder.endrecord();
}
void
Record::nbytes_part(std::map<size_t, int64_t>& largest) const {
return array_.get()->nbytes_part(largest);
}
int64_t
Record::length() const {
return -1; // just like NumpyArray with ndim == 0, which is also a scalar
}
const ContentPtr
Record::shallow_copy() const {
return std::make_shared<Record>(array_, at_);
}
const ContentPtr
Record::deep_copy(bool copyarrays,
bool copyindexes,
bool copyidentities) const {
ContentPtr out = array_.get()->deep_copy(copyarrays,
copyindexes,
copyidentities);
return std::make_shared<Record>(
std::dynamic_pointer_cast<RecordArray>(out), at_);
}
void
Record::check_for_iteration() const {
if (array_.get()->identities().get() != nullptr &&
array_.get()->identities().get()->length() != 1) {
util::handle_error(
failure("len(identities) != 1 for scalar Record",
kSliceNone,
kSliceNone,
FILENAME_C(__LINE__)),
array_.get()->identities().get()->classname(),
nullptr);
}
}
const ContentPtr
Record::getitem_nothing() const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_nothing")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_at(int64_t at) const {
throw std::invalid_argument(
std::string("scalar Record can only be sliced by field name (string); "
"try ") + util::quote(std::to_string(at))
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_at_nowrap(int64_t at) const {
throw std::invalid_argument(
std::string("scalar Record can only be sliced by field name (string); "
"try ") + util::quote(std::to_string(at))
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_range(int64_t start, int64_t stop) const {
throw std::invalid_argument(
std::string("scalar Record can only be sliced by field name (string)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_range_nowrap(int64_t start, int64_t stop) const {
throw std::invalid_argument(
std::string("scalar Record can only be sliced by field name (string)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_field(const std::string& key) const {
return array_.get()->field(key).get()->getitem_at_nowrap(at_);
}
const ContentPtr
Record::getitem_field(const std::string& key,
const Slice& only_fields) const {
ContentPtr recordarray = array_.get()->getitem_field(key, only_fields);
return recordarray.get()->getitem_at_nowrap(at_);
}
const ContentPtr
Record::getitem_fields(const std::vector<std::string>& keys) const {
ContentPtr recordarray = array_.get()->getitem_fields(keys);
return recordarray.get()->getitem_at_nowrap(at_);
}
const ContentPtr
Record::getitem_fields(const std::vector<std::string>& keys,
const Slice& only_fields) const {
ContentPtr recordarray = array_.get()->getitem_fields(keys, only_fields);
return recordarray.get()->getitem_at_nowrap(at_);
}
const ContentPtr
Record::carry(const Index64& carry, bool allow_lazy) const {
throw std::runtime_error(
std::string("undefined operation: Record::carry") + FILENAME(__LINE__));
}
bool
Record::dimension_optiontype() const {
return false;
}
int64_t
Record::purelist_depth() const {
return 0;
}
const std::pair<int64_t, int64_t>
Record::minmax_depth() const {
std::pair<int64_t, int64_t> out = array_.get()->minmax_depth();
return std::pair<int64_t, int64_t>(out.first - 1, out.second - 1);
}
const std::pair<bool, int64_t>
Record::branch_depth() const {
std::pair<bool, int64_t> out = array_.get()->branch_depth();
return std::pair<bool, int64_t>(out.first, out.second - 1);
}
int64_t
Record::numfields() const {
return array_.get()->numfields();
}
int64_t
Record::fieldindex(const std::string& key) const {
return array_.get()->fieldindex(key);
}
const std::string
Record::key(int64_t fieldindex) const {
return array_.get()->key(fieldindex);
}
bool
Record::haskey(const std::string& key) const {
return array_.get()->haskey(key);
}
const std::vector<std::string>
Record::keys() const {
return array_.get()->keys();
}
const std::string
Record::validityerror(const std::string& path) const {
return array_.get()->validityerror(path + std::string(".array"));
}
const ContentPtr
Record::shallow_simplify() const {
return shallow_copy();
}
const int64_t
Record::axis_wrap_if_negative(int64_t axis) const {
if (axis == 0) {
throw std::invalid_argument(
std::string("Record at axis=0 is a scalar, not an array") + FILENAME(__LINE__));
}
return array_.get()->axis_wrap_if_negative(axis);
}
const ContentPtr
Record::num(int64_t axis, int64_t depth) const {
int64_t posaxis = axis_wrap_if_negative(axis);
if (posaxis == depth) {
throw std::invalid_argument(
std::string("cannot call 'num' with an 'axis' of 0 on a Record")
+ FILENAME(__LINE__));
}
else {
ContentPtr singleton = array_.get()->getitem_range_nowrap(at_, at_ + 1);
return singleton.get()->num(posaxis, depth).get()->getitem_at_nowrap(0);
}
}
const std::pair<Index64, ContentPtr>
Record::offsets_and_flattened(int64_t axis, int64_t depth) const {
throw std::invalid_argument(
std::string("Record cannot be flattened because it is not an array")
+ FILENAME(__LINE__));
}
bool
Record::mergeable(const ContentPtr& other, bool mergebool) const {
throw std::invalid_argument(
std::string("Record cannot be merged because it is not an array")
+ FILENAME(__LINE__));
}
bool
Record::referentially_equal(const ContentPtr& other) const {
if (identities_.get() == nullptr && other.get()->identities().get() != nullptr) {
return false;
}
if (identities_.get() != nullptr && other.get()->identities().get() == nullptr) {
return false;
}
if (identities_.get() != nullptr && other.get()->identities().get() != nullptr) {
if (!identities_.get()->referentially_equal(other->identities())) {
return false;
}
}
if (Record* raw = dynamic_cast<Record*>(other.get())) {
return at_ == raw->at() &&
parameters_ == raw->parameters() &&
array_.get()->referentially_equal(raw->shallow_copy());
}
else {
return false;
}
}
const ContentPtr
Record::mergemany(const ContentPtrVec& others) const {
throw std::invalid_argument(
std::string("Record cannot be merged because it is not an array")
+ FILENAME(__LINE__));
}
const SliceItemPtr
Record::asslice() const {
throw std::invalid_argument(
std::string("cannot use a record as a slice") + FILENAME(__LINE__));
}
const ContentPtr
Record::fillna(const ContentPtr& value) const {
return array_.get() // get RecordArray
->getitem_range_nowrap(at_, at_ + 1).get() // of just this element
->fillna(value).get() // fillna
->getitem_at_nowrap(0); // turn into a scalar
}
const ContentPtr
Record::rpad(int64_t length, int64_t axis, int64_t depth) const {
throw std::invalid_argument(
std::string("Record cannot be padded because it is not an array")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::rpad_and_clip(int64_t length, int64_t axis, int64_t depth) const {
throw std::invalid_argument(
std::string("Record cannot be padded because it is not an array")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::reduce_next(const Reducer& reducer,
int64_t negaxis,
const Index64& starts,
const Index64& shifts,
const Index64& parents,
int64_t outlength,
bool mask,
bool keepdims) const {
ContentPtr trimmed = array_.get()->getitem_range_nowrap(at_, at_ + 1);
return trimmed.get()->reduce_next(reducer,
negaxis,
starts,
shifts,
parents,
outlength,
mask,
keepdims);
}
const ContentPtr
Record::localindex(int64_t axis, int64_t depth) const {
int64_t posaxis = axis_wrap_if_negative(axis);
if (posaxis == depth) {
throw std::invalid_argument(
std::string("cannot call 'localindex' with an 'axis' of 0 on a Record")
+ FILENAME(__LINE__));
}
else {
ContentPtr singleton = array_.get()->getitem_range_nowrap(at_, at_ + 1);
return singleton.get()
->localindex(posaxis, depth).get()
->getitem_at_nowrap(0);
}
}
const ContentPtr
Record::combinations(int64_t n,
bool replacement,
const util::RecordLookupPtr& recordlookup,
const util::Parameters& parameters,
int64_t axis,
int64_t depth) const {
if (n < 1) {
throw std::invalid_argument(
std::string("in combinations, 'n' must be at least 1") + FILENAME(__LINE__));
}
int64_t posaxis = axis_wrap_if_negative(axis);
if (posaxis == depth) {
throw std::invalid_argument(
std::string("cannot call 'combinations' with an 'axis' of 0 on a Record")
+ FILENAME(__LINE__));
}
else {
ContentPtr singleton = array_.get()->getitem_range_nowrap(at_, at_ + 1);
return singleton.get()->combinations(n,
replacement,
recordlookup,
parameters,
posaxis,
depth).get()->getitem_at_nowrap(0);
}
}
const ContentPtr
Record::field(int64_t fieldindex) const {
return array_.get()->field(fieldindex).get()->getitem_at_nowrap(at_);
}
const ContentPtr
Record::field(const std::string& key) const {
return array_.get()->field(key).get()->getitem_at_nowrap(at_);
}
const ContentPtrVec
Record::fields() const {
ContentPtrVec out;
int64_t cols = numfields();
for (int64_t j = 0; j < cols; j++) {
out.push_back(array_.get()->field(j).get()->getitem_at_nowrap(at_));
}
return out;
}
const std::vector<std::pair<std::string, ContentPtr>>
Record::fielditems() const {
std::vector<std::pair<std::string, ContentPtr>> out;
util::RecordLookupPtr keys = array_.get()->recordlookup();
if (istuple()) {
int64_t cols = numfields();
for (int64_t j = 0; j < cols; j++) {
out.push_back(std::pair<std::string, ContentPtr>(
std::to_string(j),
array_.get()->field(j).get()->getitem_at_nowrap(at_)));
}
}
else {
int64_t cols = numfields();
for (int64_t j = 0; j < cols; j++) {
out.push_back(std::pair<std::string, ContentPtr>(
keys.get()->at((size_t)j),
array_.get()->field(j).get()->getitem_at_nowrap(at_)));
}
}
return out;
}
const std::shared_ptr<Record>
Record::astuple() const {
return std::make_shared<Record>(array_.get()->astuple(), at_);
}
const ContentPtr
Record::sort_next(int64_t negaxis,
const Index64& starts,
const Index64& parents,
int64_t outlength,
bool ascending,
bool stable) const {
ContentPtr next = array_.get()->getitem_at_nowrap(at_);
return next.get()->sort_next(negaxis,
starts,
parents,
outlength,
ascending,
stable);
}
const ContentPtr
Record::argsort_next(int64_t negaxis,
const Index64& starts,
const Index64& shifts,
const Index64& parents,
int64_t outlength,
bool ascending,
bool stable) const {
ContentPtr next = array_.get()->getitem_at_nowrap(at_);
return next.get()->argsort_next(negaxis,
starts,
shifts,
parents,
outlength,
ascending,
stable);
}
const ContentPtr
Record::getitem(const Slice& where) const {
ContentPtr next = array_.get()->getitem_range_nowrap(at_, at_ + 1);
SliceItemPtr nexthead = where.head();
Slice nexttail = where.tail();
Index64 nextadvanced = Index64::empty_advanced();
ContentPtr out = next.get()->getitem_next(nexthead,
nexttail,
nextadvanced);
if (out.get()->length() == 0) {
return out.get()->getitem_nothing();
}
else {
return out.get()->getitem_at_nowrap(0);
}
}
const ContentPtr
Record::getitem_next(const SliceAt& at,
const Slice& tail,
const Index64& advanced) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next(at)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next(const SliceRange& range,
const Slice& tail,
const Index64& advanced) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next(range)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next(const SliceArray64& array,
const Slice& tail,
const Index64& advanced) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next(array)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next(const SliceField& field,
const Slice& tail,
const Index64& advanced) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next(field)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next(const SliceFields& fields,
const Slice& tail,
const Index64& advanced) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next(fields)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next(const SliceJagged64& jagged,
const Slice& tail,
const Index64& advanced) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next(jagged)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next_jagged(const Index64& slicestarts,
const Index64& slicestops,
const SliceArray64& slicecontent,
const Slice& tail) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next_jagged(array)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next_jagged(const Index64& slicestarts,
const Index64& slicestops,
const SliceMissing64& slicecontent,
const Slice& tail) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next_jagged(missing)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::getitem_next_jagged(const Index64& slicestarts,
const Index64& slicestops,
const SliceJagged64& slicecontent,
const Slice& tail) const {
throw std::runtime_error(
std::string("undefined operation: Record::getitem_next_jagged(jagged)")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::copy_to(kernel::lib ptr_lib) const {
ContentPtr array = array_.get()->copy_to(ptr_lib);
return std::make_shared<Record>(
std::dynamic_pointer_cast<RecordArray>(array), at_);
}
const ContentPtr
Record::numbers_to_type(const std::string& name) const {
ContentPtr out = array_.get()->numbers_to_type(name);
return std::make_shared<Record>(
std::dynamic_pointer_cast<RecordArray>(out), at_);
}
bool
Record::is_unique() const {
throw std::runtime_error(
std::string("FIXME: operation not yet implemented: Record::is_unique")
+ FILENAME(__LINE__));
}
const ContentPtr
Record::unique() const {
throw std::runtime_error(
std::string("FIXME: operation not yet implemented: Record::unique")
+ FILENAME(__LINE__));
}
bool
Record::is_subrange_equal(const Index64& start, const Index64& stop) const {
throw std::runtime_error(
std::string("FIXME: operation not yet implemented: Record::is_subrange_equal")
+ FILENAME(__LINE__));
}
}