-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathLayoutBuilder.h
2453 lines (2153 loc) · 82 KB
/
LayoutBuilder.h
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
#ifndef AWKWARD_LAYOUTBUILDER_H_
#define AWKWARD_LAYOUTBUILDER_H_
#include "awkward/BuilderOptions.h"
#include "awkward/GrowableBuffer.h"
#include "awkward/utils.h"
#include <map>
#include <algorithm>
#include <tuple>
#include <string>
#include <functional>
/// @brief Object of {@link BuilderOptions BuilderOptions} which sets the
/// values of the default options.
#define AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS awkward::BuilderOptions(1024, 1)
namespace awkward {
namespace LayoutBuilder {
/// @class Field
///
/// @brief Helper class for sending a pair of field names (as enum) and field
/// type as template parameters in {@link Record Record}.
///
/// @tparam ENUM Enumerated type which assigns index values to field names.
/// @tparam BUILDER The type of field.
template <std::size_t ENUM, typename BUILDER>
class Field {
public:
using Builder = BUILDER;
/// @brief Converts index as field string.
std::string
index_as_field() const {
return std::to_string(index);
}
/// @brief The index of a Record field.
const std::size_t index = ENUM;
/// @brief The content type of field in a Record.
Builder builder;
};
/// @class Numpy
///
/// @brief Builds a NumpyArray which describes multi-dimensional data
/// of `PRIMITIVE` type.
///
/// @tparam PRIMITIVE The type of data in NumpyArray.
template <typename PRIMITIVE>
class Numpy {
public:
/// @brief Creates a new Numpy layout builder by allocating a new buffer,
/// using `AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS` for initializing the buffer.
Numpy()
: data_(
awkward::GrowableBuffer<PRIMITIVE>(AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS)) {
size_t id = 0;
set_id(id);
}
/// @brief Creates a new Numpy layout builder by allocating a new buffer,
/// taking `options` from {@link BuilderOptions BuilderOptions}
/// for initializing the buffer.
///
/// @param options Initial size configuration of a buffer.
Numpy(const awkward::BuilderOptions& options)
: data_(awkward::GrowableBuffer<PRIMITIVE>(options)) {
size_t id = 0;
set_id(id);
}
/// @brief Inserts a `PRIMITIVE` type data.
void
append(PRIMITIVE x) noexcept {
data_.append(x);
}
/// @brief Inserts an entire array of `PRIMITIVE` type data.
///
/// Just an interface; not actually faster than calling append many times.
void
extend(PRIMITIVE* ptr, size_t size) noexcept {
data_.extend(ptr, size);
}
/// @brief Parameters for the builder form.
const std::string&
parameters() const noexcept {
return parameters_;
}
/// @brief Sets the form parameters.
void
set_parameters(std::string parameter) noexcept {
parameters_ = parameter;
}
/// @brief Assigns a unique ID to each node.
void
set_id(size_t& id) noexcept {
id_ = id;
id++;
}
/// @brief Discards the accumulated data in the builder.
void
clear() noexcept {
data_.clear();
}
/// @brief Current length of the data.
size_t
length() const noexcept {
return data_.length();
}
/// @brief Checks for validity and consistency.
bool
is_valid(std::string& /* error */) const noexcept {
return true;
}
/// @brief Retrieves the name and size (in bytes) of the buffer.
void
buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
noexcept {
names_nbytes["node" + std::to_string(id_) + "-data"] = data_.nbytes();
}
/// @brief Copies and concatenates all the accumulated data in the builder
/// to a user-defined pointer.
///
/// Used to fill the buffers map by allocating it with user-defined pointers
/// using the same name and size (in bytes) obtained from #buffer_nbytes.
void
to_buffers(std::map<std::string, void*>& buffers) const noexcept {
data_.concatenate(reinterpret_cast<PRIMITIVE*>(
buffers["node" + std::to_string(id_) + "-data"]));
}
/// @brief Copies and concatenates the accumulated data in the builder buffer to
/// a user-defined pointer if the given node name matches with the node associated
/// with the builder.
void
to_buffer(void* buffer, const char* name) const noexcept {
if (std::string(name) == std::string("node" + std::to_string(id_) + "-data")) {
data_.concatenate(reinterpret_cast<PRIMITIVE*>(buffer));
}
}
/// @brief Copies and concatenates all the accumulated data in the builder
/// to a map of user-allocated buffers.
///
/// The map keys and the buffer sizes are obtained from #buffer_nbytes
void
to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
data_.concatenate(reinterpret_cast<PRIMITIVE*>(
buffers["node" + std::to_string(id_) + "-data"]));
}
/// @brief Generates a unique description of the builder and its
/// contents in the form of a JSON-like string.
std::string
form() const {
std::stringstream form_key;
form_key << "node" << id_;
std::string params("");
if (parameters_ == "") {
} else {
params = std::string(", \"parameters\": { " + parameters_ + " }");
}
if (std::is_arithmetic<PRIMITIVE>::value) {
return "{ \"class\": \"NumpyArray\", \"primitive\": \"" +
type_to_name<PRIMITIVE>() + "\"" + params +
", \"form_key\": \"" + form_key.str() + "\" }";
} else if (is_specialization<PRIMITIVE, std::complex>::value) {
return "{ \"class\": \"NumpyArray\", \"primitive\": \"" +
type_to_name<PRIMITIVE>() + "\"" + params +
", \"form_key\": \"" + form_key.str() + "\" }";
} else {
throw std::runtime_error("type " +
std::string(typeid(PRIMITIVE).name()) +
"is not supported");
}
}
private:
/// @brief Buffer of `PRIMITIVE` type.
awkward::GrowableBuffer<PRIMITIVE> data_;
/// @brief Form parameters.
std::string parameters_;
/// @brief Unique form ID.
size_t id_;
};
/// @class ListOffset
///
/// @brief Builds a ListOffsetArray which describes unequal-length lists
/// (often called a "jagged" or "ragged" array). The underlying data for
/// all lists are in a BUILDER content. It is subdivided into lists according
/// to an offsets array, which specifies the starting and stopping index of each list.
///
/// The offsets must have at least length `1` (corresponding to an empty array).
///
/// The offsets values can be 64-bit signed integers `int64`, 32-bit signed integers
/// `int32` or 32-bit unsigned integers `uint32`.
///
/// @tparam PRIMITIVE The type of `offsets` buffer.
/// @tparam BUILDER The type of builder content.
template <typename PRIMITIVE, typename BUILDER>
class ListOffset {
public:
/// @brief Creates a new ListOffset layout builder by allocating a new `offset`
/// buffer, using `AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS` for initializing the buffer.
ListOffset()
: offsets_(
awkward::GrowableBuffer<PRIMITIVE>(AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS)) {
offsets_.append(0);
size_t id = 0;
set_id(id);
}
/// @brief Creates a new ListOffset layout builder by allocating a new `offset`
/// buffer, taking `options` from {@link BuilderOptions BuilderOptions} for
/// initializing the buffer.
///
/// @param options Initial size configuration of a buffer.
ListOffset(const awkward::BuilderOptions& options)
: offsets_(awkward::GrowableBuffer<PRIMITIVE>(options)) {
offsets_.append(0);
size_t id = 0;
set_id(id);
}
/// @brief Returns the reference to the builder content.
BUILDER&
content() noexcept {
return content_;
}
/// @brief Begins a list and returns the reference to the builder content.
BUILDER&
begin_list() noexcept {
return content_;
}
/// @brief Ends a list and appends the current length of the list
/// contents in the `offsets` buffer.
void
end_list() noexcept {
offsets_.append(content_.length());
}
/// @brief Parameters for the builder form.
const std::string&
parameters() const noexcept {
return parameters_;
}
/// @brief Sets the form parameters.
void
set_parameters(std::string parameter) noexcept {
parameters_ = parameter;
}
/// @brief Assigns a unique ID to each node.
void
set_id(size_t& id) noexcept {
id_ = id;
id++;
content_.set_id(id);
}
/// @brief Discards the accumulated offsets and clears the builder content.
void
clear() noexcept {
offsets_.clear();
offsets_.append(0);
content_.clear();
}
/// @brief Current length of the content.
size_t
length() const noexcept {
return offsets_.length() - 1;
}
/// @brief Checks for validity and consistency.
bool
is_valid(std::string& error) const noexcept {
if ((int64_t)content_.length() != (int64_t)offsets_.last()) {
std::stringstream out;
out << "ListOffset node" << id_ << "has content length "
<< content_.length() << "but last offset " << offsets_.last()
<< "\n";
error.append(out.str());
return false;
} else {
return content_.is_valid(error);
}
}
/// @brief Retrieves the names and sizes (in bytes) of the buffers used
/// in the builder and its contents.
void
buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
noexcept {
names_nbytes["node" + std::to_string(id_) + "-offsets"] =
offsets_.nbytes();
content_.buffer_nbytes(names_nbytes);
}
/// @brief Copies and concatenates all the accumulated data in each of the
/// buffers of the builder and its contents to user-defined pointers.
///
/// Used to fill the buffers map by allocating it with user-defined pointers
/// using the same names and sizes (in bytes) obtained from #buffer_nbytes.
void
to_buffers(std::map<std::string, void*>& buffers) const noexcept {
offsets_.concatenate(reinterpret_cast<PRIMITIVE*>(
buffers["node" + std::to_string(id_) + "-offsets"]));
content_.to_buffers(buffers);
}
/// @brief Copies and concatenates the accumulated data in the builder buffer to
/// a user-defined pointer if the given node name matches with the node associated
/// with the builder; otherwise, it searches the builder contents to locate a
/// matching node.
void
to_buffer(void* buffer, const char* name) const noexcept {
if (std::string(name) == std::string("node" + std::to_string(id_) + "-offsets")) {
offsets_.concatenate(reinterpret_cast<PRIMITIVE*>(buffer));
}
content_.to_buffer(buffer, name);
}
/// @brief Copies and concatenates all the accumulated data in the builder
/// to a map of user-allocated buffers.
///
/// The map keys and the buffer sizes are obtained from #buffer_nbytes
void
to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
offsets_.concatenate(reinterpret_cast<PRIMITIVE*>(
buffers["node" + std::to_string(id_) + "-offsets"]));
content_.to_char_buffers(buffers);
}
/// @brief Generates a unique description of the builder and its
/// contents in the form of a JSON-like string.
std::string
form() const noexcept {
std::stringstream form_key;
form_key << "node" << id_;
std::string params("");
if (parameters_ == "") {
} else {
params = std::string(", \"parameters\": { " + parameters_ + " }");
}
return "{ \"class\": \"ListOffsetArray\", \"offsets\": \"" +
type_to_numpy_like<PRIMITIVE>() +
"\", \"content\": " + content_.form() + params +
", \"form_key\": \"" + form_key.str() + "\" }";
}
private:
/// @brief Buffer of `PRIMITIVE` type.
///
/// It specifies the starting and stopping index of each list.
GrowableBuffer<PRIMITIVE> offsets_;
/// @brief The content of the ListOffsetArray.
BUILDER content_;
/// @brief Form parameters.
std::string parameters_;
/// @brief Unique form ID.
size_t id_;
};
/// @class Empty
///
/// @brief Builds an EmptyArray which has no content in it.
/// It is used whenever an array's type is not known because it is empty.
class Empty {
public:
/// @brief Creates a new Empty layout builder.
Empty() {
size_t id = 0;
set_id(id);
}
void
set_id(size_t& /* id */) noexcept {}
void
clear() noexcept {}
/// @brief Current length of the content.
size_t
length() const noexcept {
return 0;
}
/// @brief Checks for validity and consistency.
bool
is_valid(std::string& /* error */) const noexcept {
return true;
}
void
buffer_nbytes(std::map<std::string, size_t>& /* names_nbytes */) const
noexcept {}
void
to_buffers(std::map<std::string, void*>& /* buffers */) const noexcept {}
void
to_buffer(void* /* buffer */, const char* /* name */) const noexcept {}
/// @brief Copies and concatenates all the accumulated data in the builder
/// to a map of user-allocated buffers.
///
/// The map keys and the buffer sizes are obtained from #buffer_nbytes
void
to_char_buffers(std::map<std::string, uint8_t*>& /* buffers */) const noexcept {}
/// @brief Generates a unique description of the builder and its
/// contents in the form of a JSON-like string.
std::string
form() const noexcept {
return "{ \"class\": \"EmptyArray\" }";
}
private:
/// @brief Unique form ID.
size_t id_;
};
/// @class Record
///
/// @brief Builds a RecordArray which represents an array of records, which
/// can be of same or different types. Its contents is an ordered list of arrays
/// with the same length as the length of its shortest content; all are aligned
/// element-by-element, associating a field name to every content.
///
/// @tparam MAP Map of index keys and field name.
/// @tparam BUILDERS The types of builder contents.
template <class MAP = std::map<std::size_t, std::string>,
typename... BUILDERS>
class Record {
public:
using RecordContents = typename std::tuple<BUILDERS...>;
using UserDefinedMap = MAP;
template <std::size_t INDEX>
using RecordFieldType = std::tuple_element_t<INDEX, RecordContents>;
/// @brief Creates a new Record layout builder.
Record() {
size_t id = 0;
set_id(id);
map_fields(std::index_sequence_for<BUILDERS...>());
}
/// @brief Creates a new Record layout builder, taking a user-defined
/// map with enumerated type field ID as keys and field names as value
/// which sets the field names.
///
/// @param user_defined_field_id_to_name_map A user-defined field ID
/// field name map.
Record(UserDefinedMap user_defined_field_id_to_name_map)
: content_names_(user_defined_field_id_to_name_map) {
assert(content_names_.size() == fields_count_);
size_t id = 0;
set_id(id);
}
/// @brief Returns a vector of strings sontaining all the field names.
const std::vector<std::string>
fields() const noexcept {
if (content_names_.empty()) {
return fields_;
} else {
std::vector<std::string> result;
for (auto it : content_names_) {
result.emplace_back(it.second);
}
return result;
}
}
/// @brief Sets the field names.
///
/// Alternative method to set the field names besides passing the
/// user-defined map as constructor parameter.
void
set_fields(MAP user_defined_field_id_to_name_map) noexcept {
content_names_ = user_defined_field_id_to_name_map;
}
/// @brief Returns the reference to the builder contents at `INDEX`.
template <std::size_t INDEX>
typename RecordFieldType<INDEX>::Builder&
content() noexcept {
return std::get<INDEX>(contents).builder;
}
/// @brief Parameters for the builder form.
const std::string&
parameters() const noexcept {
return parameters_;
}
/// @brief Sets the form parameters.
void
set_parameters(std::string parameter) noexcept {
parameters_ = parameter;
}
/// @brief Assigns a unique ID to each node.
void
set_id(size_t& id) noexcept {
id_ = id;
id++;
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&id](auto& content) {
content.builder.set_id(id);
});
}
}
/// @brief Clears the builder contents.
///
/// Discards the accumulated data and the contents in each
/// field of the record.
void
clear() noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [](auto& content) {
content.builder.clear();
});
}
}
/// @brief Current number of records in first field.
size_t
length() const noexcept {
return (std::get<0>(contents).builder.length());
}
/// @brief Checks for validity and consistency.
bool
is_valid(std::string& error) const noexcept {
auto index_sequence((std::index_sequence_for<BUILDERS...>()));
int64_t length = -1;
std::vector<size_t> lengths = field_lengths(index_sequence);
for (size_t i = 0; i < lengths.size(); i++) {
if (length == -1) {
length = lengths[i];
}
else if (length != (int64_t)lengths[i]) {
std::stringstream out;
out << "Record node" << id_ << " has field \""
<< fields().at(i) << "\" length " << lengths[i]
<< " that differs from the first length " << length << "\n";
error.append(out.str());
return false;
}
}
std::vector<bool> valid_fields = field_is_valid(index_sequence, error);
return std::none_of(std::cbegin(valid_fields),
std::cend(valid_fields),
std::logical_not<bool>());
}
/// @brief Retrieves the names and sizes (in bytes) of the buffers used
/// in the builder and its contents.
void
buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&names_nbytes](auto& content) {
content.builder.buffer_nbytes(names_nbytes);
});
}
}
/// @brief Copies and concatenates all the accumulated data in each of the
/// buffers of the builder and its contents to user-defined pointers.
///
/// Used to fill the buffers map by allocating it with user-defined pointers
/// using the same names and sizes (in bytes) obtained from #buffer_nbytes.
void
to_buffers(std::map<std::string, void*>& buffers) const noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&buffers](auto& content) {
content.builder.to_buffers(buffers);
});
}
}
/// @brief Copies and concatenates the accumulated data in the buffers of the
/// builder contents to user-defined pointers if the given node name matches
/// with the node associated with that builder.
void
to_buffer(void* buffer, const char* name) const noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&buffer, &name](auto& content) {
content.builder.to_buffer(buffer, name);
});
}
}
/// @brief Copies and concatenates all the accumulated data in the builder
/// to a map of user-allocated buffers.
///
/// The map keys and the buffer sizes are obtained from #buffer_nbytes
void
to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&buffers](auto& content) {
content.builder.to_char_buffers(buffers);
});
}
}
/// @brief Generates a unique description of the builder and its
/// contents in the form of a JSON-like string.
std::string
form() const noexcept {
std::stringstream form_key;
form_key << "node" << id_;
std::string params("");
if (parameters_ == "") {
} else {
params = std::string("\"parameters\": { " + parameters_ + " }, ");
}
std::stringstream out;
out << "{ \"class\": \"RecordArray\", \"contents\": { ";
for (size_t i = 0; i < fields_count_; i++) {
if (i != 0) {
out << ", ";
}
auto contents_form = [&](auto& content) {
out << "\""
<< (!content_names_.empty() ? content_names_.at(content.index)
: content.index_as_field())
<< +"\": ";
out << content.builder.form();
};
visit_at(contents, i, contents_form);
}
out << " }, ";
out << params << "\"form_key\": \"" << form_key.str() << "\" }";
return out.str();
}
/// @brief The contents of the RecordArray.
RecordContents contents;
private:
/// @brief Inserts the field names of all the record fields in a vector
/// of strings.
template <std::size_t... S>
void
map_fields(std::index_sequence<S...>) noexcept {
fields_ = std::vector<std::string>(
{std::string(std::get<S>(contents).index_as_field())...});
}
/// @brief Inserts the lengths of all the record fields in a vector and
/// returns the vector.
template <std::size_t... S>
std::vector<size_t>
field_lengths(std::index_sequence<S...>) const noexcept {
return std::vector<size_t>({std::get<S>(contents).builder.length()...});
}
/// @brief Helper function of #is_valid for checking the validity.
template <std::size_t... S>
std::vector<bool>
field_is_valid(std::index_sequence<S...>, std::string& error) const
noexcept {
return std::vector<bool>(
{std::get<S>(contents).builder.is_valid(error)...});
}
/// @brief Vector of strings of field names.
std::vector<std::string> fields_;
/// @brief User-defined map of record field names.
UserDefinedMap content_names_;
/// @brief Form parameters.
std::string parameters_;
/// @brief Unique form ID.
size_t id_;
/// @brief The count of the fields in the builder.
static constexpr size_t fields_count_ = sizeof...(BUILDERS);
};
/// @class Tuple
///
/// @brief Builds a RecordArray which represents an array of tuples which can be
/// of same or different types without field names, indexed only by their order.
///
/// @tparam BUILDERS The types of builder contents.
template <typename... BUILDERS>
class Tuple {
using TupleContents = typename std::tuple<BUILDERS...>;
template <std::size_t INDEX>
using TupleContentType = std::tuple_element_t<INDEX, TupleContents>;
public:
/// @brief Creates a new Tuple layout builder.
Tuple() {
size_t id = 0;
set_id(id);
}
/// @brief Returns the reference to the builder contents at `INDEX`.
template <std::size_t INDEX>
TupleContentType<INDEX>&
content() noexcept {
return std::get<INDEX>(contents);
}
/// @brief Parameters for the builder form.
const std::string&
parameters() const noexcept {
return parameters_;
}
/// @brief Sets the form parameters.
void
set_parameters(std::string parameter) noexcept {
parameters_ = parameter;
}
/// @brief Assigns a unique ID to each node.
void
set_id(size_t& id) noexcept {
id_ = id;
id++;
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&id](auto& content) {
content.set_id(id);
});
}
}
/// @brief Clears the builder contents.
///
/// Discards the accumulated data and the contents at each tuple index.
void
clear() noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [](auto& content) {
content.clear();
});
}
}
/// @brief Current number of records in the first index of the tuple.
size_t
length() const noexcept {
return (std::get<0>(contents).length());
}
/// @brief Checks for validity and consistency.
bool
is_valid(std::string& error) const noexcept {
auto index_sequence((std::index_sequence_for<BUILDERS...>()));
int64_t length = -1;
std::vector<size_t> lengths = content_lengths(index_sequence);
for (size_t i = 0; i < lengths.size(); i++) {
if (length == -1) {
length = (int64_t)lengths[i];
}
else if (length != (int64_t)lengths[i]) {
std::stringstream out;
out << "Record node" << id_ << " has index \"" << i << "\" length "
<< lengths[i] << " that differs from the first length "
<< length << "\n";
error.append(out.str());
return false;
}
}
std::vector<bool> valid_fields =
content_is_valid(index_sequence, error);
return std::none_of(std::cbegin(valid_fields),
std::cend(valid_fields),
std::logical_not<bool>());
}
/// @brief Retrieves the names and sizes (in bytes) of the buffers used
/// in the builder and its contents.
void
buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&names_nbytes](auto& content) {
content.buffer_nbytes(names_nbytes);
});
}
}
/// @brief Copies and concatenates all the accumulated data in each of the
/// buffers of the builder and its contents to user-defined pointers.
///
/// Used to fill the buffers map by allocating it with user-defined pointers
/// using the same names and sizes (in bytes) obtained from #buffer_nbytes.
void
to_buffers(std::map<std::string, void*>& buffers) const noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&buffers](auto& content) {
content.to_buffers(buffers);
});
}
}
/// @brief Copies and concatenates the accumulated data in the buffers of the
/// builder contents to user-defined pointers if the given node name matches
/// with the node associated with that builder.
void
to_buffer(void* buffer, const char* name) const noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&buffer, &name](auto& content) {
content.to_buffer(buffer, name);
});
}
}
/// @brief Copies and concatenates all the accumulated data in the builder
/// to a map of user-allocated buffers.
///
/// The map keys and the buffer sizes are obtained from #buffer_nbytes
void
to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
for (size_t i = 0; i < fields_count_; i++) {
visit_at(contents, i, [&buffers](auto& content) {
content.to_char_buffers(buffers);
});
}
}
/// @brief Generates a unique description of the builder and its
/// contents in the form of a JSON-like string.
std::string
form() const noexcept {
std::stringstream form_key;
form_key << "node" << id_;
std::string params("");
if (parameters_ == "") {
} else {
params = std::string("\"parameters\": { " + parameters_ + " }, ");
}
std::stringstream out;
out << "{ \"class\": \"RecordArray\", \"contents\": [";
for (size_t i = 0; i < fields_count_; i++) {
if (i != 0) {
out << ", ";
}
auto contents_form = [&out](auto& content) {
out << content.form();
};
visit_at(contents, i, contents_form);
}
out << "], ";
out << params << "\"form_key\": \"" << form_key.str() << "\" }";
return out.str();
}
/// @brief The contents of the RecordArray without fields.
TupleContents contents;
private:
/// @brief Inserts the lengths of all the builder contents in a vector
/// and returns the vector.
template <std::size_t... S>
std::vector<size_t>
content_lengths(std::index_sequence<S...>) const noexcept {
return std::vector<size_t>({std::get<S>(contents).length()...});
}
/// @brief Helper function of #is_valid for checking the validity.
template <std::size_t... S>
std::vector<bool>
content_is_valid(std::index_sequence<S...>, std::string& error) const
noexcept {
return std::vector<bool>({std::get<S>(contents).is_valid(error)...});
}
/// @brief Vector of integers of field index.
std::vector<int64_t> field_index_;
/// @brief Form parameters.
std::string parameters_;
/// @brief Unique form ID.
size_t id_;
/// @brief The count of the fields in the builder.
static constexpr size_t fields_count_ = sizeof...(BUILDERS);
};
/// @class Regular
///
/// @brief Builds a RegularArray that describes lists that have the same
/// length, a single integer size. Its underlying content is a flattened
/// view of the data; that is, each list is not stored separately in memory,
/// but is inferred as a subinterval of the underlying data.
///
/// A multidimensional {@link Numpy NumpyArray} is equivalent to a one-dimensional
/// {@link Numpy NumpyArray} nested within several RegularArrays, one for each
/// dimension. However, RegularArrays can be used to make lists of any other type.
///
/// @tparam SIZE
/// @tparam BUILDER The type of builder content.
template <unsigned SIZE, typename BUILDER>
class Regular {
public:
/// @brief Creates a new Regular layout builder.
Regular() : length_(0) {
size_t id = 0;
set_id(id);
}
/// @brief Returns the reference to the builder content.
BUILDER&
content() noexcept {
return content_;
}
/// @brief Begins a list and returns the reference to the content
/// of the builder.
BUILDER&
begin_list() noexcept {
return content_;
}
/// @brief Ends a list and increments the number of lists.
void
end_list() noexcept {
length_++;
}
/// @brief Parameters for the builder form.
const std::string&
parameters() const noexcept {
return parameters_;
}
/// @brief Sets the form parameters.
void
set_parameters(std::string parameter) noexcept {
parameters_ = parameter;
}
/// @brief Assigns a unique ID to each node.
void
set_id(size_t& id) noexcept {
id_ = id;
id++;
content_.set_id(id);
}
/// @brief Clears the builder content.
void
clear() noexcept {
length_ = 0;
content_.clear();
}
/// @brief Current number of lists of length `SIZE`.
size_t
length() const noexcept {
return length_;