Loading...
Searching...
No Matches
LayoutBuilder.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
2
3#ifndef AWKWARD_LAYOUTBUILDER_H_
4#define AWKWARD_LAYOUTBUILDER_H_
5
8#include "awkward/utils.h"
9
10#include <map>
11#include <algorithm>
12#include <tuple>
13#include <string>
14#include <functional>
15
18#define AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS awkward::BuilderOptions(1024, 1)
19
20namespace awkward {
21
22 namespace LayoutBuilder {
23
28 public:
29 BuilderSetId(size_t& id) : id_(id) {}
30
31 template <class CONTENT>
32 void operator()(CONTENT& content) {
33 content.builder.set_id(id_);
34 }
35
36 private:
37 size_t& id_;
38 };
39
47 template <std::size_t ENUM, typename BUILDER>
48 class Field {
49 public:
50 using Builder = BUILDER;
51
53 std::string
55 return std::to_string(index);
56 }
57
59 const std::size_t index = ENUM;
62 };
63
70 template <typename PRIMITIVE>
71 class Numpy {
72 public:
76 : data_(
78 size_t id = 0;
79 set_id(id);
80 }
81
88 : data_(awkward::GrowableBuffer<PRIMITIVE>(options)) {
89 size_t id = 0;
90 set_id(id);
91 }
92
94 void
95 append(PRIMITIVE x) noexcept {
96 data_.append(x);
97 }
98
102 void
103 extend(PRIMITIVE* ptr, size_t size) noexcept {
104 data_.extend(ptr, size);
105 }
106
108 const std::string&
109 parameters() const noexcept {
110 return parameters_;
111 }
112
114 void
115 set_parameters(std::string parameter) noexcept {
116 parameters_ = parameter;
117 }
118
120 void
121 set_id(size_t& id) noexcept {
122 id_ = id;
123 id++;
124 }
125
127 void
128 clear() noexcept {
129 data_.clear();
130 }
131
133 size_t
134 length() const noexcept {
135 return data_.length();
136 }
137
139 bool
140 is_valid(std::string& /* error */) const noexcept {
141 return true;
142 }
143
145 void
146 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
147 noexcept {
148 names_nbytes["node" + std::to_string(id_) + "-data"] = data_.nbytes();
149 }
150
156 void
157 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
158 data_.concatenate(reinterpret_cast<PRIMITIVE*>(
159 buffers["node" + std::to_string(id_) + "-data"]));
160 }
161
165 void
166 to_buffer(void* buffer, const char* name) const noexcept {
167 if (std::string(name) == std::string("node" + std::to_string(id_) + "-data")) {
168 data_.concatenate(reinterpret_cast<PRIMITIVE*>(buffer));
169 }
170 }
171
176 void
177 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
178 data_.concatenate(reinterpret_cast<PRIMITIVE*>(
179 buffers["node" + std::to_string(id_) + "-data"]));
180 }
181
184 std::string
185 form() const {
186 std::stringstream form_key;
187 form_key << "node" << id_;
188
189 std::string params("");
190 if (parameters_ == "") {
191 } else {
192 params = std::string(", \"parameters\": { " + parameters_ + " }");
193 }
194
195 if (std::is_arithmetic<PRIMITIVE>::value) {
196 return "{ \"class\": \"NumpyArray\", \"primitive\": \"" +
197 type_to_name<PRIMITIVE>() + "\"" + params +
198 ", \"form_key\": \"" + form_key.str() + "\" }";
200 return "{ \"class\": \"NumpyArray\", \"primitive\": \"" +
201 type_to_name<PRIMITIVE>() + "\"" + params +
202 ", \"form_key\": \"" + form_key.str() + "\" }";
203 } else {
204 throw std::runtime_error("type " +
205 std::string(typeid(PRIMITIVE).name()) +
206 "is not supported");
207 }
208 }
209
210 private:
213
215 std::string parameters_;
216
218 size_t id_;
219 };
220
235 template <typename PRIMITIVE, typename BUILDER>
237 public:
241 : offsets_(
243 offsets_.append(0);
244 size_t id = 0;
245 set_id(id);
246 }
247
254 : offsets_(awkward::GrowableBuffer<PRIMITIVE>(options)) {
255 offsets_.append(0);
256 size_t id = 0;
257 set_id(id);
258 }
259
261 BUILDER&
262 content() noexcept {
263 return content_;
264 }
265
267 BUILDER&
268 begin_list() noexcept {
269 return content_;
270 }
271
274 void
275 end_list() noexcept {
276 offsets_.append(content_.length());
277 }
278
280 const std::string&
281 parameters() const noexcept {
282 return parameters_;
283 }
284
286 void
287 set_parameters(std::string parameter) noexcept {
288 parameters_ = parameter;
289 }
290
292 void
293 set_id(size_t& id) noexcept {
294 id_ = id;
295 id++;
296 content_.set_id(id);
297 }
298
300 void
301 clear() noexcept {
302 offsets_.clear();
303 offsets_.append(0);
304 content_.clear();
305 }
306
308 size_t
309 length() const noexcept {
310 return offsets_.length() - 1;
311 }
312
314 bool
315 is_valid(std::string& error) const noexcept {
316 if ((int64_t)content_.length() != (int64_t)offsets_.last()) {
317 std::stringstream out;
318 out << "ListOffset node" << id_ << "has content length "
319 << content_.length() << "but last offset " << offsets_.last()
320 << "\n";
321 error.append(out.str());
322
323 return false;
324 } else {
325 return content_.is_valid(error);
326 }
327 }
328
331 void
332 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
333 noexcept {
334 names_nbytes["node" + std::to_string(id_) + "-offsets"] =
335 offsets_.nbytes();
336 content_.buffer_nbytes(names_nbytes);
337 }
338
344 void
345 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
346 offsets_.concatenate(reinterpret_cast<PRIMITIVE*>(
347 buffers["node" + std::to_string(id_) + "-offsets"]));
348 content_.to_buffers(buffers);
349 }
350
355 void
356 to_buffer(void* buffer, const char* name) const noexcept {
357 if (std::string(name) == std::string("node" + std::to_string(id_) + "-offsets")) {
358 offsets_.concatenate(reinterpret_cast<PRIMITIVE*>(buffer));
359 }
360 content_.to_buffer(buffer, name);
361 }
362
367 void
368 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
369 offsets_.concatenate(reinterpret_cast<PRIMITIVE*>(
370 buffers["node" + std::to_string(id_) + "-offsets"]));
371 content_.to_char_buffers(buffers);
372 }
373
376 std::string
377 form() const noexcept {
378 std::stringstream form_key;
379 form_key << "node" << id_;
380 std::string params("");
381 if (parameters_ == "") {
382 } else {
383 params = std::string(", \"parameters\": { " + parameters_ + " }");
384 }
385 return "{ \"class\": \"ListOffsetArray\", \"offsets\": \"" +
386 type_to_numpy_like<PRIMITIVE>() +
387 "\", \"content\": " + content_.form() + params +
388 ", \"form_key\": \"" + form_key.str() + "\" }";
389 }
390
391 private:
396
398 BUILDER content_;
399
401 std::string parameters_;
402
404 size_t id_;
405 };
406
411 template<class PRIMITIVE>
412 class String : public ListOffset<PRIMITIVE, Numpy<uint8_t>> {
413 public:
414 String() : ListOffset<PRIMITIVE, Numpy<uint8_t>>() {
415 this->set_parameters(R"""("__array__": "string")""");
416 this->content().set_parameters(R"""("__array__": "char")""");
417 }
418
419 void append(const std::string& value) {
420 this->begin_list();
421 for (const auto& c: value) {
422 this->content().append(c);
423 }
424 this->end_list();
425 }
426 };
427
432 class Empty {
433 public:
436 size_t id = 0;
437 set_id(id);
438 }
439
440 void
441 set_id(size_t& /* id */) noexcept {}
442
443 void
444 clear() noexcept {}
445
447 size_t
448 length() const noexcept {
449 return 0;
450 }
451
453 bool
454 is_valid(std::string& /* error */) const noexcept {
455 return true;
456 }
457
458 void
459 buffer_nbytes(std::map<std::string, size_t>& /* names_nbytes */) const
460 noexcept {}
461
462 void
463 to_buffers(std::map<std::string, void*>& /* buffers */) const noexcept {}
464
465 void
466 to_buffer(void* /* buffer */, const char* /* name */) const noexcept {}
467
472 void
473 to_char_buffers(std::map<std::string, uint8_t*>& /* buffers */) const noexcept {}
474
477 std::string
478 form() const noexcept {
479 return "{ \"class\": \"EmptyArray\" }";
480 }
481
482 private:
484 size_t id_;
485 };
486
487
497 template <class MAP = std::map<std::size_t, std::string>,
498 typename... BUILDERS>
499 class Record {
500 public:
501 using RecordContents = typename std::tuple<BUILDERS...>;
502 using UserDefinedMap = MAP;
503
504 template <std::size_t INDEX>
505 using RecordFieldType = std::tuple_element_t<INDEX, RecordContents>;
506
509 size_t id = 0;
510 set_id(id);
511 map_fields(std::index_sequence_for<BUILDERS...>());
512 }
513
520 Record(UserDefinedMap user_defined_field_id_to_name_map)
521 : content_names_(user_defined_field_id_to_name_map) {
522 assert(content_names_.size() == fields_count_);
523 size_t id = 0;
524 set_id(id);
525 }
526
528 const std::vector<std::string>
529 fields() const noexcept {
530 if (content_names_.empty()) {
531 return fields_;
532 } else {
533 std::vector<std::string> result;
534 for (auto it : content_names_) {
535 result.emplace_back(it.second);
536 }
537 return result;
538 }
539 }
540
545 void
546 set_fields(MAP user_defined_field_id_to_name_map) noexcept {
547 content_names_ = user_defined_field_id_to_name_map;
548 }
549
551 template <std::size_t INDEX>
552 typename RecordFieldType<INDEX>::Builder&
553 content() noexcept {
554 return std::get<INDEX>(contents).builder;
555 }
556
558 const std::string&
559 parameters() const noexcept {
560 return parameters_;
561 }
562
564 void
565 set_parameters(std::string parameter) noexcept {
566 parameters_ = parameter;
567 }
568
570 void set_id(size_t& id) noexcept {
571
572 id_ = id;
573 id++;
574
575 BuilderSetId bsi(id);
576 for (size_t i = 0; i < fields_count_; i++) {
577
578 visit_at(contents, i, bsi); // Here the functor will propagate `id`
579 }
580 }
581
587 public:
588 template <class CONTENT>
589 void operator()(CONTENT& content) const {
590 content.builder.clear();
591 }
592 };
593
594 void
595 clear() noexcept {
596 ClearBuilder clearBuilder; // instantiate the functor
597 for (size_t i = 0; i < fields_count_; i++) {
598 visit_at(contents, i, clearBuilder); // pass the functor instead of a lambda
599 }
600 }
602 size_t
603 length() const noexcept {
604 return (std::get<0>(contents).builder.length());
605 }
606
608 bool
609 is_valid(std::string& error) const noexcept {
610 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
611
612 int64_t length = -1;
613 std::vector<size_t> lengths = field_lengths(index_sequence);
614 for (size_t i = 0; i < lengths.size(); i++) {
615 if (length == -1) {
616 length = lengths[i];
617 }
618 else if (length != (int64_t)lengths[i]) {
619 std::stringstream out;
620 out << "Record node" << id_ << " has field \""
621 << fields().at(i) << "\" length " << lengths[i]
622 << " that differs from the first length " << length << "\n";
623 error.append(out.str());
624
625 return false;
626 }
627 }
628
629 std::vector<bool> valid_fields = field_is_valid(index_sequence, error);
630 return std::none_of(std::cbegin(valid_fields),
631 std::cend(valid_fields),
632 std::logical_not<bool>());
633 }
634
638 public:
639 // Constructor to capture names_nbytes by reference
640 BufferNBytesFunctor(std::map<std::string, size_t>& names_nbytes)
641 : names_nbytes_(names_nbytes) { }
642
643 // Template operator() to handle the content
644 template <class CONTENT>
645 void operator()(CONTENT& content) const {
646 content.builder.buffer_nbytes(names_nbytes_);
647 }
648
649 private:
650 std::map<std::string, size_t>& names_nbytes_; // reference to the map
651 };
652
653 void
654 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const noexcept {
655 BufferNBytesFunctor bufferNBytesFunctor(names_nbytes); // instantiate the functor
656 for (size_t i = 0; i < fields_count_; i++) {
657 visit_at(contents, i, bufferNBytesFunctor); // pass the functor instead of the lambda
658 }
659 }
660
667 public:
668 // Constructor to capture buffers by reference
669 ToBuffersFunctor(std::map<std::string, void*>& buffers)
670 : buffers_(buffers) { }
671
672 // Template operator() to handle the content
673 template <class CONTENT>
674 void operator()(CONTENT& content) const {
675 content.builder.to_buffers(buffers_);
676 }
677
678 private:
679 std::map<std::string, void*>& buffers_; // reference to the map
680 };
681
682 void
683 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
684 ToBuffersFunctor toBuffersFunctor(buffers); // instantiate the functor
685 for (size_t i = 0; i < fields_count_; i++) {
686 visit_at(contents, i, toBuffersFunctor); // pass the functor instead of the lambda
687 }
688 }
693 public:
694 // Constructor to capture buffer and name by reference
695 ToBufferFunctor(void* buffer, const char* name)
696 : buffer_(buffer), name_(name) { }
697
698 // Template operator() to handle the content
699 template <class CONTENT>
700 void operator()(CONTENT& content) const {
701 content.builder.to_buffer(buffer_, name_);
702 }
703
704 private:
705 void* buffer_; // reference to buffer
706 const char* name_; // reference to name
707 };
708
709 void
710 to_buffer(void* buffer, const char* name) const noexcept {
711 ToBufferFunctor toBufferFunctor(buffer, name); // instantiate the functor
712 for (size_t i = 0; i < fields_count_; i++) {
713 visit_at(contents, i, toBufferFunctor); // pass the functor instead of the lambda
714 }
715 }
716
717
723 public:
724 // Constructor to capture buffers by reference
725 ToCharBuffersFunctor(std::map<std::string, uint8_t*>& buffers)
726 : buffers_(buffers) { }
727
728 // Template operator() to handle the content
729 template <class CONTENT>
730 void operator()(CONTENT& content) const {
731 content.builder.to_char_buffers(buffers_);
732 }
733
734 private:
735 std::map<std::string, uint8_t*>& buffers_; // reference to the buffers map
736 };
737
738 void
739 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
740 ToCharBuffersFunctor toCharBuffersFunctor(buffers); // instantiate the functor
741 for (size_t i = 0; i < fields_count_; i++) {
742 visit_at(contents, i, toCharBuffersFunctor); // pass the functor instead of the lambda
743 }
744 }
745
746
750 public:
751 // Modify the constructor to accept a std::map instead of a std::vector
752 ContentsFormFunctor(std::stringstream& out, const std::map<size_t, std::string>& content_names)
753 : out_(out), content_names_(content_names) {}
754
755 // Template operator() to handle the content
756 template <class CONTENT>
757 void operator()(CONTENT& content) const {
758 size_t index = content.index; // Assuming CONTENT has an index
759 auto it = content_names_.find(index); // Lookup content name in the map
760
761 if (it != content_names_.end()) {
762 out_ << "\"" << it->second << "\": ";
763 } else {
764 out_ << "\"" << index << "\": "; // Fallback to index if not found
765 }
766
767 out_ << content.builder.form();
768 }
769
770 private:
771 std::stringstream& out_;
772 const std::map<size_t, std::string>& content_names_; // Store the map by reference
773 };
774
775
776 std::string form() const noexcept {
777 std::stringstream form_key;
778 form_key << "node" << id_;
779
780 std::string params("");
781 if (!parameters_.empty()) {
782 params = "\"parameters\": { " + parameters_ + " }, ";
783 }
784
785 std::stringstream out;
786 out << "{ \"class\": \"RecordArray\", \"contents\": { ";
787
788 for (size_t i = 0; i < fields_count_; i++) {
789 if (i != 0) {
790 out << ", ";
791 }
792 ContentsFormFunctor contentsFormFunctor(out, content_names_);
793 visit_at(contents, i, contentsFormFunctor);
794 }
795
796 out << " }, ";
797 out << params << "\"form_key\": \"" << form_key.str() << "\" }";
798 return out.str();
799 }
800
803
804 private:
807 template <std::size_t... S>
808 void
809 map_fields(std::index_sequence<S...>) noexcept {
810 fields_ = std::vector<std::string>(
811 {std::string(std::get<S>(contents).index_as_field())...});
812 }
813
816 template <std::size_t... S>
817 std::vector<size_t>
818 field_lengths(std::index_sequence<S...>) const noexcept {
819 return std::vector<size_t>({std::get<S>(contents).builder.length()...});
820 }
821
823 template <std::size_t... S>
824 std::vector<bool>
825 field_is_valid(std::index_sequence<S...>, std::string& error) const
826 noexcept {
827 return std::vector<bool>(
828 {std::get<S>(contents).builder.is_valid(error)...});
829 }
830
832 std::vector<std::string> fields_;
833
835 UserDefinedMap content_names_;
836
838 std::string parameters_;
839
841 size_t id_;
842
844 static constexpr size_t fields_count_ = sizeof...(BUILDERS);
845 };
846
853 template <typename... BUILDERS>
854 class Tuple {
855 using TupleContents = typename std::tuple<BUILDERS...>;
856
857 template <std::size_t INDEX>
858 using TupleContentType = std::tuple_element_t<INDEX, TupleContents>;
859
860 public:
863 size_t id = 0;
864 set_id(id);
865 }
866
868 template <std::size_t INDEX>
869 TupleContentType<INDEX>&
870 content() noexcept {
871 return std::get<INDEX>(contents);
872 }
873
875 const std::string&
876 parameters() const noexcept {
877 return parameters_;
878 }
879
881 void
882 set_parameters(std::string parameter) noexcept {
883 parameters_ = parameter;
884 }
885
888 public:
889 // Constructor to capture id by reference
890 SetIdFunctor(size_t& id) : id_(id) { }
891
892 // Template operator() to handle the content
893 template <class CONTENT>
894 void operator()(CONTENT& content) const {
895 content.set_id(id_);
896 }
897
898 private:
899 size_t& id_; // reference to the id
900 };
901
902 void
903 set_id(size_t& id) noexcept {
904 id_ = id;
905 id++;
906 SetIdFunctor setIdFunctor(id); // instantiate the functor with id reference
907 for (size_t i = 0; i < fields_count_; i++) {
908 visit_at(contents, i, setIdFunctor); // pass the functor instead of the lambda
909 }
910 }
911
912
917 public:
918 // Template operator() to handle the content
919 template <class CONTENT>
920 void operator()(CONTENT& content) const {
921 content.clear();
922 }
923 };
924 void
925 clear() noexcept {
926 ClearFunctor clearFunctor; // instantiate the functor
927 for (size_t i = 0; i < fields_count_; i++) {
928 visit_at(contents, i, clearFunctor); // pass the functor instead of the lambda
929 }
930 }
931
933 size_t
934 length() const noexcept {
935 return (std::get<0>(contents).length());
936 }
937
939 bool
940 is_valid(std::string& error) const noexcept {
941 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
942
943 int64_t length = -1;
944 std::vector<size_t> lengths = content_lengths(index_sequence);
945 for (size_t i = 0; i < lengths.size(); i++) {
946 if (length == -1) {
947 length = (int64_t)lengths[i];
948 }
949 else if (length != (int64_t)lengths[i]) {
950 std::stringstream out;
951 out << "Record node" << id_ << " has index \"" << i << "\" length "
952 << lengths[i] << " that differs from the first length "
953 << length << "\n";
954 error.append(out.str());
955
956 return false;
957 }
958 }
959
960 std::vector<bool> valid_fields =
961 content_is_valid(index_sequence, error);
962 return std::none_of(std::cbegin(valid_fields),
963 std::cend(valid_fields),
964 std::logical_not<bool>());
965 }
966
970 public:
971 // Constructor to capture names_nbytes by reference
972 BufferNBytesFunctor(std::map<std::string, size_t>& names_nbytes)
973 : names_nbytes_(names_nbytes) { }
974
975 // Template operator() to handle the content
976 template <class CONTENT>
977 void operator()(CONTENT& content) const {
978 content.buffer_nbytes(names_nbytes_);
979 }
980
981 private:
982 std::map<std::string, size_t>& names_nbytes_; // reference to the map
983 };
984
985 void
986 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const noexcept {
987 BufferNBytesFunctor bufferNBytesFunctor(names_nbytes); // instantiate the functor
988 for (size_t i = 0; i < fields_count_; i++) {
989 visit_at(contents, i, bufferNBytesFunctor); // pass the functor instead of the lambda
990 }
991 }
992
993
1000 public:
1001 // Constructor to capture buffers by reference
1002 ToBuffersFunctor(std::map<std::string, void*>& buffers)
1003 : buffers_(buffers) { }
1004
1005 // Template operator() to handle the content
1006 template <class CONTENT>
1007 void operator()(CONTENT& content) const {
1008 content.to_buffers(buffers_);
1009 }
1010
1011 private:
1012 std::map<std::string, void*>& buffers_; // reference to the buffers map
1013 };
1014
1015 void
1016 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
1017 ToBuffersFunctor toBuffersFunctor(buffers); // instantiate the functor
1018 for (size_t i = 0; i < fields_count_; i++) {
1019 visit_at(contents, i, toBuffersFunctor); // pass the functor instead of the lambda
1020 }
1021 }
1022
1023
1028 public:
1029 // Constructor to capture buffer and name by reference
1030 ToBufferFunctor(void* buffer, const char* name)
1031 : buffer_(buffer), name_(name) { }
1032
1033 // Template operator() to handle the content
1034 template <class CONTENT>
1035 void operator()(CONTENT& content) const {
1036 content.to_buffer(buffer_, name_);
1037 }
1038
1039 private:
1040 void* buffer_; // reference to the buffer
1041 const char* name_; // reference to the name
1042 };
1043
1044 void
1045 to_buffer(void* buffer, const char* name) const noexcept {
1046 ToBufferFunctor toBufferFunctor(buffer, name); // instantiate the functor
1047 for (size_t i = 0; i < fields_count_; i++) {
1048 visit_at(contents, i, toBufferFunctor); // pass the functor instead of the lambda
1049 }
1050 }
1051
1052
1058 public:
1059 // Constructor to capture buffers by reference
1060 ToCharBuffersFunctor(std::map<std::string, uint8_t*>& buffers)
1061 : buffers_(buffers) { }
1062
1063 // Template operator() to handle the content
1064 template <class CONTENT>
1065 void operator()(CONTENT& content) const {
1066 content.to_char_buffers(buffers_);
1067 }
1068
1069 private:
1070 std::map<std::string, uint8_t*>& buffers_; // reference to the buffers map
1071 };
1072
1073 void
1074 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
1075 ToCharBuffersFunctor toCharBuffersFunctor(buffers); // instantiate the functor
1076 for (size_t i = 0; i < fields_count_; i++) {
1077 visit_at(contents, i, toCharBuffersFunctor); // pass the functor instead of the lambda
1078 }
1079 }
1080
1084 public:
1085 ContentsFormFunctor(std::stringstream& out)
1086 : out_(out) {}
1087
1088 // Template operator() to handle each content
1089 template <class CONTENT>
1090 void operator()(CONTENT& content) const {
1091 out_ << content.form();
1092 }
1093
1094 private:
1095 std::stringstream& out_; // Reference to the output stringstream
1096 };
1097
1098
1099 std::string
1100 form() const noexcept {
1101 std::stringstream form_key;
1102 form_key << "node" << id_;
1103 std::string params("");
1104 if (!parameters_.empty()) {
1105 params = "\"parameters\": { " + parameters_ + " }, ";
1106 }
1107 std::stringstream out;
1108 out << "{ \"class\": \"RecordArray\", \"contents\": [";
1109 for (size_t i = 0; i < fields_count_; i++) {
1110 if (i != 0) {
1111 out << ", ";
1112 }
1113 ContentsFormFunctor contentsFormFunctor(out);
1114 visit_at(contents, i, contentsFormFunctor);
1115 }
1116 out << "], ";
1117 out << params << "\"form_key\": \"" << form_key.str() << "\" }";
1118 return out.str();
1119 }
1120
1122 TupleContents contents;
1123
1124 private:
1127 template <std::size_t... S>
1128 std::vector<size_t>
1129 content_lengths(std::index_sequence<S...>) const noexcept {
1130 return std::vector<size_t>({std::get<S>(contents).length()...});
1131 }
1132
1134 template <std::size_t... S>
1135 std::vector<bool>
1136 content_is_valid(std::index_sequence<S...>, std::string& error) const
1137 noexcept {
1138 return std::vector<bool>({std::get<S>(contents).is_valid(error)...});
1139 }
1140
1142 std::vector<int64_t> field_index_;
1143
1145 std::string parameters_;
1146
1148 size_t id_;
1149
1151 static constexpr size_t fields_count_ = sizeof...(BUILDERS);
1152 };
1153
1167 template <unsigned SIZE, typename BUILDER>
1168 class Regular {
1169 public:
1171 Regular() : length_(0) {
1172 size_t id = 0;
1173 set_id(id);
1174 }
1175
1177 BUILDER&
1178 content() noexcept {
1179 return content_;
1180 }
1181
1184 BUILDER&
1185 begin_list() noexcept {
1186 return content_;
1187 }
1188
1190 void
1191 end_list() noexcept {
1192 length_++;
1193 }
1194
1196 const std::string&
1197 parameters() const noexcept {
1198 return parameters_;
1199 }
1200
1202 void
1203 set_parameters(std::string parameter) noexcept {
1204 parameters_ = parameter;
1205 }
1206
1208 void
1209 set_id(size_t& id) noexcept {
1210 id_ = id;
1211 id++;
1212 content_.set_id(id);
1213 }
1214
1216 void
1217 clear() noexcept {
1218 length_ = 0;
1219 content_.clear();
1220 }
1221
1223 size_t
1224 length() const noexcept {
1225 return length_;
1226 }
1227
1229 bool
1230 is_valid(std::string& error) const noexcept {
1231 if (content_.length() != length_ * size_) {
1232 std::stringstream out;
1233 out << "Regular node" << id_ << "has content length "
1234 << content_.length() << ", but length " << length_ << " and size "
1235 << size_ << "\n";
1236 error.append(out.str());
1237
1238 return false;
1239 } else {
1240 return content_.is_valid(error);
1241 }
1242 }
1243
1246 void
1247 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
1248 noexcept {
1249 content_.buffer_nbytes(names_nbytes);
1250 }
1251
1257 void
1258 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
1259 content_.to_buffers(buffers);
1260 }
1261
1265 void
1266 to_buffer(void* buffer, const char* name) const noexcept {
1267 content_.to_buffer(buffer, name);
1268 }
1269
1274 void
1275 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
1276 content_.to_char_buffers(buffers);
1277 }
1278
1281 std::string
1282 form() const noexcept {
1283 std::stringstream form_key;
1284 form_key << "node" << id_;
1285 std::string params("");
1286 if (parameters_ == "") {
1287 } else {
1288 params = std::string(", \"parameters\": { " + parameters_ + " }");
1289 }
1290 return "{ \"class\": \"RegularArray\", \"content\": " +
1291 content_.form() + ", \"size\": " + std::to_string(size_) +
1292 params + ", \"form_key\": \"" + form_key.str() + "\" }";
1293 }
1294
1295 private:
1297 BUILDER content_;
1298
1300 std::string parameters_;
1301
1303 size_t id_;
1304
1306 size_t length_;
1307
1309 size_t size_ = SIZE;
1310 };
1311
1312
1322 template <typename PRIMITIVE, typename BUILDER>
1323 class Indexed {
1324 public:
1328 : index_(
1330 max_index_(0) {
1331 size_t id = 0;
1332 set_id(id);
1333 }
1334
1341 : index_(awkward::GrowableBuffer<PRIMITIVE>(options)),
1342 max_index_(0) {
1343 size_t id = 0;
1344 set_id(id);
1345 }
1346
1348 BUILDER&
1349 content() noexcept {
1350 return content_;
1351 }
1352
1355 BUILDER&
1356 append_index() noexcept {
1357 return append_index(content_.length());
1358 }
1359
1362 BUILDER&
1363 append_index(size_t i) noexcept {
1364 index_.append(i);
1365 if (i > max_index_) {
1366 max_index_ = i;
1367 } else if (i < 0) {
1368 max_index_ = UINTMAX_MAX;
1369 }
1370 return content_;
1371 }
1372
1377 BUILDER&
1378 extend_index(size_t size) noexcept {
1379 size_t start = content_.length();
1380 size_t stop = start + size;
1381 if (stop - 1 > max_index_) {
1382 max_index_ = stop - 1;
1383 }
1384 for (size_t i = start; i < stop; i++) {
1385 index_.append(i);
1386 }
1387 return content_;
1388 }
1389
1391 const std::string&
1392 parameters() const noexcept {
1393 return parameters_;
1394 }
1395
1397 void
1398 set_parameters(std::string parameter) noexcept {
1399 parameters_ = parameter;
1400 }
1401
1403 void
1404 set_id(size_t& id) noexcept {
1405 id_ = id;
1406 id++;
1407 content_.set_id(id);
1408 }
1409
1412 void
1413 clear() noexcept {
1414 max_index_ = 0;
1415 index_.clear();
1416 content_.clear();
1417 }
1418
1420 size_t
1421 length() const noexcept {
1422 return index_.length();
1423 }
1424
1427 void
1428 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
1429 noexcept {
1430 names_nbytes["node" + std::to_string(id_) + "-index"] = index_.nbytes();
1431 content_.buffer_nbytes(names_nbytes);
1432 }
1433
1435 bool
1436 is_valid(std::string& error) const noexcept {
1437
1438 if (max_index_ == UINTMAX_MAX) {
1439 std::stringstream out;
1440 out << "Indexed node" << id_ << " has a negative index\n";
1441 error.append(out.str());
1442 return false;
1443 } else if ((content_.length() == 0) && (max_index_ == 0)) { // catches empty object
1444 } else if (max_index_ >= content_.length()) {
1445 std::stringstream out;
1446 out << "Indexed node" << id_ << " has index " << max_index_
1447 << " but content has length "
1448 << content_.length() << "\n";
1449 error.append(out.str());
1450 return false;
1451 }
1452 return content_.is_valid(error);
1453 }
1454
1460 void
1461 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
1462 index_.concatenate(reinterpret_cast<PRIMITIVE*>(
1463 buffers["node" + std::to_string(id_) + "-index"]));
1464 content_.to_buffers(buffers);
1465 }
1466
1471 void
1472 to_buffer(void* buffer, const char* name) const noexcept {
1473 if (std::string(name) == std::string("node" + std::to_string(id_) + "-index")) {
1474 index_.concatenate(reinterpret_cast<PRIMITIVE*>(buffer));
1475 }
1476 content_.to_buffer(buffer, name);
1477 }
1478
1483 void
1484 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
1485 index_.concatenate(reinterpret_cast<PRIMITIVE*>(
1486 buffers["node" + std::to_string(id_) + "-index"]));
1487 content_.to_char_buffers(buffers);
1488 }
1489
1492 std::string
1493 form() const noexcept {
1494 std::stringstream form_key;
1495 form_key << "node" << id_;
1496 std::string params("");
1497 if (parameters_ == "") {
1498 } else {
1499 params = std::string(", \"parameters\": { " + parameters_ + " }");
1500 }
1501 return "{ \"class\": \"IndexedArray\", \"index\": \"" +
1502 type_to_numpy_like<PRIMITIVE>() +
1503 "\", \"content\": " + content_.form() + params +
1504 ", \"form_key\": \"" + form_key.str() + "\" }";
1505 }
1506
1507 private:
1512
1514 BUILDER content_;
1515
1517 std::string parameters_;
1518
1520 size_t id_;
1521
1523 size_t max_index_;
1524 };
1525
1536 template <typename PRIMITIVE, typename BUILDER>
1538 public:
1542 : index_(
1544 last_valid_(-1),
1545 max_index_(0) {
1546 size_t id = 0;
1547 set_id(id);
1548 }
1549
1556 : index_(awkward::GrowableBuffer<PRIMITIVE>(options)),
1557 last_valid_(-1),
1558 max_index_(0) {
1559 size_t id = 0;
1560 set_id(id);
1561 }
1562
1564 BUILDER&
1565 content() noexcept {
1566 return content_;
1567 }
1568
1571 BUILDER&
1572 append_valid() noexcept {
1573 last_valid_ = content_.length();
1574 return append_valid(last_valid_);
1575 }
1576
1579 BUILDER&
1580 append_valid(size_t i) noexcept {
1581 last_valid_ = content_.length();
1582 index_.append(i);
1583 if (i > max_index_) {
1584 max_index_ = i;
1585 }
1586 return content_;
1587 }
1588
1593 BUILDER&
1594 extend_valid(size_t size) noexcept {
1595 size_t start = content_.length();
1596 size_t stop = start + size;
1597 last_valid_ = stop - 1;
1598 if (last_valid_ > max_index_) {
1599 max_index_ = last_valid_;
1600 }
1601 for (size_t i = start; i < stop; i++) {
1602 index_.append(i);
1603 }
1604 return content_;
1605 }
1606
1608 void
1609 append_invalid() noexcept {
1610 index_.append(-1);
1611 }
1612
1616 void
1617 extend_invalid(size_t size) noexcept {
1618 for (size_t i = 0; i < size; i++) {
1619 index_.append(-1);
1620 }
1621 }
1622
1624 const std::string&
1625 parameters() const noexcept {
1626 return parameters_;
1627 }
1628
1630 void
1631 set_parameters(std::string parameter) noexcept {
1632 parameters_ = parameter;
1633 }
1634
1636 void
1637 set_id(size_t& id) noexcept {
1638 id_ = id;
1639 id++;
1640 content_.set_id(id);
1641 }
1642
1645 void
1646 clear() noexcept {
1647 last_valid_ = -1;
1648 index_.clear();
1649 content_.clear();
1650 }
1651
1653 size_t
1654 length() const noexcept {
1655 return index_.length();
1656 }
1657
1659 bool
1660 is_valid(std::string& error) const noexcept {
1661 if ((content_.length() == 0) && (max_index_ == 0)) { // catches empty object
1662 } else if (max_index_ >= content_.length()) {
1663 std::stringstream out;
1664 out << "IndexedOption node" << id_ << " has index " << max_index_
1665 << " but content has length "
1666 << content_.length() << "\n";
1667 error.append(out.str());
1668
1669 return false;
1670 }
1671 return content_.is_valid(error);
1672 }
1673
1676 void
1677 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
1678 noexcept {
1679 names_nbytes["node" + std::to_string(id_) + "-index"] = index_.nbytes();
1680 content_.buffer_nbytes(names_nbytes);
1681 }
1682
1688 void
1689 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
1690 index_.concatenate(reinterpret_cast<PRIMITIVE*>(
1691 buffers["node" + std::to_string(id_) + "-index"]));
1692 content_.to_buffers(buffers);
1693 }
1694
1699 void
1700 to_buffer(void* buffer, const char* name) const noexcept {
1701 if (std::string(name) == std::string("node" + std::to_string(id_) + "-index")) {
1702 index_.concatenate(reinterpret_cast<PRIMITIVE*>(buffer));
1703 }
1704 content_.to_buffer(buffer, name);
1705 }
1706
1711 void
1712 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
1713 index_.concatenate(reinterpret_cast<PRIMITIVE*>(
1714 buffers["node" + std::to_string(id_) + "-index"]));
1715 content_.to_char_buffers(buffers);
1716 }
1717
1720 std::string
1721 form() const noexcept {
1722 std::stringstream form_key;
1723 form_key << "node" << id_;
1724 std::string params("");
1725 if (parameters_ == "") {
1726 } else {
1727 params = std::string(", \"parameters\": { " + parameters_ + " }");
1728 }
1729 return "{ \"class\": \"IndexedOptionArray\", \"index\": \"" +
1730 type_to_numpy_like<PRIMITIVE>() +
1731 "\", \"content\": " + content_.form() + params +
1732 ", \"form_key\": \"" + form_key.str() + "\" }";
1733 }
1734
1735 private:
1740
1742 BUILDER content_;
1743
1745 std::string parameters_;
1746
1748 size_t id_;
1749
1751 size_t last_valid_;
1752
1754 size_t max_index_;
1755 };
1756
1768 template <typename BUILDER>
1769 class Unmasked {
1770 public:
1773 size_t id = 0;
1774 set_id(id);
1775 }
1776
1778 BUILDER&
1779 content() noexcept {
1780 return content_;
1781 }
1782
1784 const std::string&
1785 parameters() const noexcept {
1786 return parameters_;
1787 }
1788
1790 void
1791 set_parameters(std::string parameter) noexcept {
1792 parameters_ = parameter;
1793 }
1794
1796 void
1797 set_id(size_t& id) noexcept {
1798 id_ = id;
1799 id++;
1800 content_.set_id(id);
1801 }
1802
1804 void
1805 clear() noexcept {
1806 content_.clear();
1807 }
1808
1810 size_t
1811 length() const noexcept {
1812 return content_.length();
1813 }
1814
1816 bool
1817 is_valid(std::string& error) const noexcept {
1818 return content_.is_valid(error);
1819 }
1820
1823 void
1824 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
1825 noexcept {
1826 content_.buffer_nbytes(names_nbytes);
1827 }
1828
1834 void
1835 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
1836 content_.to_buffers(buffers);
1837 }
1838
1842 void
1843 to_buffer(void* buffer, const char* name) const noexcept {
1844 content_.to_buffer(buffer, name);
1845 }
1846
1851 void
1852 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
1853 content_.to_char_buffers(buffers);
1854 }
1855
1858 std::string
1859 form() const noexcept {
1860 std::stringstream form_key;
1861 form_key << "node" << id_;
1862 std::string params("");
1863 if (parameters_ == "") {
1864 } else {
1865 params = std::string(", \"parameters\": { " + parameters_ + " }");
1866 }
1867 return "{ \"class\": \"UnmaskedArray\", \"content\": " +
1868 content_.form() + params + ", \"form_key\": \"" +
1869 form_key.str() + "\" }";
1870 }
1871
1872 private:
1874 BUILDER content_;
1875
1877 std::string parameters_;
1878
1880 size_t id_;
1881 };
1882
1899 template <bool VALID_WHEN, typename BUILDER>
1901 public:
1906 size_t id = 0;
1907 set_id(id);
1908 }
1909
1916 : mask_(awkward::GrowableBuffer<int8_t>(options)) {
1917 size_t id = 0;
1918 set_id(id);
1919 }
1920
1922 BUILDER&
1923 content() noexcept {
1924 return content_;
1925 }
1926
1928 bool
1929 valid_when() const noexcept {
1930 return valid_when_;
1931 }
1932
1936 BUILDER&
1937 append_valid() noexcept {
1938 mask_.append(valid_when_);
1939 return content_;
1940 }
1941
1947 BUILDER&
1948 extend_valid(size_t size) noexcept {
1949 for (size_t i = 0; i < size; i++) {
1950 mask_.append(valid_when_);
1951 }
1952 return content_;
1953 }
1954
1958 BUILDER&
1959 append_invalid() noexcept {
1960 mask_.append(!valid_when_);
1961 return content_;
1962 }
1963
1969 BUILDER&
1970 extend_invalid(size_t size) noexcept {
1971 for (size_t i = 0; i < size; i++) {
1972 mask_.append(!valid_when_);
1973 }
1974 return content_;
1975 }
1976
1978 const std::string&
1979 parameters() const noexcept {
1980 return parameters_;
1981 }
1982
1984 void
1985 set_parameters(std::string parameter) noexcept {
1986 parameters_ = parameter;
1987 }
1988
1990 void
1991 set_id(size_t& id) noexcept {
1992 id_ = id;
1993 id++;
1994 content_.set_id(id);
1995 }
1996
1999 void
2000 clear() noexcept {
2001 mask_.clear();
2002 content_.clear();
2003 }
2004
2006 size_t
2007 length() const noexcept {
2008 return mask_.length();
2009 }
2010
2012 bool
2013 is_valid(std::string& error) const noexcept {
2014 if (content_.length() != mask_.length()) {
2015 std::stringstream out;
2016 out << "ByteMasked node" << id_ << "has content length "
2017 << content_.length() << "but mask length " << mask_.length()
2018 << "\n";
2019 error.append(out.str());
2020
2021 return false;
2022 } else {
2023 return content_.is_valid(error);
2024 }
2025 }
2026
2029 void
2030 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
2031 noexcept {
2032 names_nbytes["node" + std::to_string(id_) + "-mask"] = mask_.nbytes();
2033 content_.buffer_nbytes(names_nbytes);
2034 }
2035
2041 void
2042 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
2043 mask_.concatenate(reinterpret_cast<int8_t*>(
2044 buffers["node" + std::to_string(id_) + "-mask"]));
2045 content_.to_buffers(buffers);
2046 }
2047
2052 void
2053 to_buffer(void* buffer, const char* name) const noexcept {
2054 if (std::string(name) == std::string("node" + std::to_string(id_) + "-mask")) {
2055 mask_.concatenate(reinterpret_cast<int8_t*>(buffer));
2056 }
2057 content_.to_buffer(buffer, name);
2058 }
2059
2064 void
2065 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
2066 mask_.concatenate(reinterpret_cast<int8_t*>(
2067 buffers["node" + std::to_string(id_) + "-mask"]));
2068 content_.to_char_buffers(buffers);
2069 }
2070
2073 std::string
2074 form() const noexcept {
2075 std::stringstream form_key, form_valid_when;
2076 form_key << "node" << id_;
2077 form_valid_when << std::boolalpha << valid_when_;
2078 std::string params("");
2079 if (parameters_ == "") {
2080 } else {
2081 params = std::string(", \"parameters\": { " + parameters_ + " }");
2082 }
2083 return "{ \"class\": \"ByteMaskedArray\", \"mask\": \"i8\", "
2084 "\"content\": " +
2085 content_.form() + ", \"valid_when\": " + form_valid_when.str() +
2086 params + ", \"form_key\": \"" + form_key.str() + "\" }";
2087 }
2088
2089 private:
2094
2096 BUILDER content_;
2097
2099 std::string parameters_;
2100
2102 size_t id_;
2103
2105 bool valid_when_ = VALID_WHEN;
2106 };
2107
2124 template <bool VALID_WHEN, bool LSB_ORDER, typename BUILDER>
2126 public:
2131 current_byte_(uint8_t(0)),
2132 current_byte_ref_(mask_.append_and_get_ref(current_byte_)),
2133 current_index_(0) {
2134 size_t id = 0;
2135 set_id(id);
2136 if (lsb_order_) {
2137 for (size_t i = 0; i < 8; i++) {
2138 cast_[i] = 1 << i;
2139 }
2140 } else {
2141 for (size_t i = 0; i < 8; i++) {
2142 cast_[i] = 128 >> i;
2143 }
2144 }
2145 }
2146
2153 : mask_(awkward::GrowableBuffer<uint8_t>(options)),
2154 current_byte_(uint8_t(0)),
2155 current_byte_ref_(mask_.append_and_get_ref(current_byte_)),
2156 current_index_(0) {
2157 size_t id = 0;
2158 set_id(id);
2159 if (lsb_order_) {
2160 for (size_t i = 0; i < 8; i++) {
2161 cast_[i] = 1 << i;
2162 }
2163 } else {
2164 for (size_t i = 0; i < 8; i++) {
2165 cast_[i] = 128 >> i;
2166 }
2167 }
2168 }
2169
2171 BUILDER&
2172 content() noexcept {
2173 return content_;
2174 }
2175
2177 bool
2178 valid_when() const noexcept {
2179 return valid_when_;
2180 }
2181
2184 bool
2185 lsb_order() const noexcept {
2186 return lsb_order_;
2187 }
2188
2193 BUILDER&
2194 append_valid() noexcept {
2195 append_begin();
2196 current_byte_ |= cast_[current_index_];
2197 append_end();
2198 return content_;
2199 }
2200
2207 BUILDER&
2208 extend_valid(size_t size) noexcept {
2209 for (size_t i = 0; i < size; i++) {
2210 append_valid();
2211 }
2212 return content_;
2213 }
2214
2218 BUILDER&
2219 append_invalid() noexcept {
2220 append_begin();
2221 append_end();
2222 return content_;
2223 }
2224
2230 BUILDER&
2231 extend_invalid(size_t size) noexcept {
2232 for (size_t i = 0; i < size; i++) {
2234 }
2235 return content_;
2236 }
2237
2239 const std::string&
2240 parameters() const noexcept {
2241 return parameters_;
2242 }
2243
2245 void
2246 set_parameters(std::string parameter) noexcept {
2247 parameters_ = parameter;
2248 }
2249
2251 void
2252 set_id(size_t& id) noexcept {
2253 id_ = id;
2254 id++;
2255 content_.set_id(id);
2256 }
2257
2260 void
2261 clear() noexcept {
2262 mask_.clear();
2263 content_.clear();
2264 current_byte_ = 0;
2265 current_byte_ref_ = mask_.append_and_get_ref(current_byte_);
2266 current_index_ = 0;
2267 }
2268
2270 size_t
2271 length() const noexcept {
2272 return mask_.length() > 0 ?
2273 (mask_.length() - 1) * 8 + current_index_ : current_index_;
2274 }
2275
2277 bool
2278 is_valid(std::string& error) const noexcept {
2279 if (content_.length() != length()) {
2280 std::stringstream out;
2281 out << "BitMasked node" << id_ << "has content length "
2282 << content_.length() << "but bit mask length " << mask_.length()
2283 << "\n";
2284 error.append(out.str());
2285
2286 return false;
2287 } else {
2288 return content_.is_valid(error);
2289 }
2290 }
2291
2294 void
2295 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const
2296 noexcept {
2297 names_nbytes["node" + std::to_string(id_) + "-mask"] = mask_.nbytes();
2298 content_.buffer_nbytes(names_nbytes);
2299 }
2300
2306 void
2307 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
2308 mask_.concatenate_from(reinterpret_cast<uint8_t*>(
2309 buffers["node" + std::to_string(id_) + "-mask"]), 0, 1);
2310 mask_.append(reinterpret_cast<uint8_t*>(
2311 buffers["node" + std::to_string(id_) + "-mask"]), mask_.length() - 1, 0, 1);
2312 content_.to_buffers(buffers);
2313 }
2314
2319 void
2320 to_buffer(void* buffer, const char* name) const noexcept {
2321 if (std::string(name) == std::string("node" + std::to_string(id_) + "-mask")) {
2322 mask_.concatenate_from(reinterpret_cast<uint8_t*>(buffer), 0, 1);
2323 mask_.append(reinterpret_cast<uint8_t*>(buffer), mask_.length() - 1, 0, 1);
2324 }
2325 content_.to_buffer(buffer, name);
2326 }
2327
2332 void
2333 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
2334 mask_.concatenate_from(reinterpret_cast<uint8_t*>(
2335 buffers["node" + std::to_string(id_) + "-mask"]), 0, 1);
2336 mask_.append(reinterpret_cast<uint8_t*>(
2337 buffers["node" + std::to_string(id_) + "-mask"]), mask_.length() - 1, 0, 1);
2338 content_.to_char_buffers(buffers);
2339 }
2340
2343 std::string
2344 form() const noexcept {
2345 std::stringstream form_key, form_valid_when, form_lsb_order;
2346 form_key << "node" << id_;
2347 form_valid_when << std::boolalpha << valid_when_;
2348 form_lsb_order << std::boolalpha << lsb_order_;
2349 std::string params("");
2350 if (parameters_ == "") {
2351 } else {
2352 params = std::string(", \"parameters\": { " + parameters_ + " }");
2353 }
2354 return "{ \"class\": \"BitMaskedArray\", \"mask\": \"u8\", "
2355 "\"content\": " +
2356 content_.form() + ", \"valid_when\": " + form_valid_when.str() +
2357 ", \"lsb_order\": " + form_lsb_order.str() + params +
2358 ", \"form_key\": \"" + form_key.str() + "\" }";
2359 }
2360
2361 private:
2365 void
2366 append_begin() {
2367 if (current_index_ == 8) {
2368 current_byte_ref_ = mask_.append_and_get_ref(current_byte_);
2369 current_byte_ = uint8_t(0);
2370 current_index_ = 0;
2371 }
2372 }
2373
2379 void
2380 append_end() {
2381 current_index_ += 1;
2382 if (valid_when_) {
2383 current_byte_ref_ = current_byte_;
2384 } else {
2385 current_byte_ref_ = ~current_byte_;
2386 }
2387 }
2388
2392 GrowableBuffer<uint8_t> mask_;
2393
2395 BUILDER content_;
2396
2398 std::string parameters_;
2399
2401 size_t id_;
2402
2404 uint8_t current_byte_;
2405
2407 uint8_t& current_byte_ref_;
2408
2410 size_t current_index_;
2411
2413 uint8_t cast_[8];
2414
2416 bool valid_when_ = VALID_WHEN;
2417
2420 bool lsb_order_ = LSB_ORDER;
2421 };
2422
2438 template <typename TAGS, typename INDEX, typename... BUILDERS>
2439 class Union {
2440 public:
2441 using Contents = typename std::tuple<BUILDERS...>;
2442
2443 template <std::size_t I>
2444 using ContentType = std::tuple_element_t<I, Contents>;
2445
2451 size_t id = 0;
2452 set_id(id);
2453 for (size_t i = 0; i < contents_count_; i++) {
2454 last_valid_index_[i] = -1;
2455 }
2456 }
2457
2464 : tags_(awkward::GrowableBuffer<TAGS>(options)),
2465 index_(awkward::GrowableBuffer<INDEX>(options)) {
2466 size_t id = 0;
2467 set_id(id);
2468 for (size_t i = 0; i < contents_count_; i++) {
2469 last_valid_index_[i] = -1;
2470 }
2471 }
2472
2473 template <std::size_t I>
2474 ContentType<I>&
2475 content() noexcept {
2476 return std::get<I>(contents_);
2477 }
2478
2481 template <std::size_t TAG>
2482 ContentType<TAG>&
2483 append_content() noexcept {
2484 auto& which_content = std::get<TAG>(contents_);
2485 INDEX next_index = which_content.length();
2486
2487 TAGS tag = (TAGS)TAG;
2488 last_valid_index_[tag] = next_index;
2489 tags_.append(tag);
2490 index_.append(next_index);
2491
2492 return which_content;
2493 }
2494
2496 const std::string&
2497 parameters() const noexcept {
2498 return parameters_;
2499 }
2500
2502 void
2503 set_parameters(std::string parameter) noexcept {
2504 parameters_ = parameter;
2505 }
2506
2509 public:
2510 // Constructor to capture id by reference
2511 SetIdFunctor(size_t& id) : id_(id) { }
2512
2513 // Template operator() to handle the content
2514 template <class CONTENT>
2515 void operator()(CONTENT& content) const {
2516 content.set_id(id_);
2517 }
2518
2519 private:
2520 size_t& id_; // reference to the id
2521 };
2522
2523 void
2524 set_id(size_t& id) noexcept {
2525 id_ = id;
2526 id++;
2527 SetIdFunctor setIdFunctor(id); // instantiate the functor
2528 for (size_t i = 0; i < contents_count_; i++) {
2529 visit_at(contents_, i, setIdFunctor); // pass the functor instead of the lambda
2530 }
2531 }
2532
2533
2539 public:
2540 // Template operator() to handle the content
2541 template <class CONTENT>
2542 void operator()(CONTENT& content) const {
2543 content.clear();
2544 }
2545 };
2546
2547 void
2548 clear() noexcept {
2549 for (size_t i = 0; i < contents_count_; i++) {
2550 last_valid_index_[i] = -1;
2551 }
2552 tags_.clear();
2553 index_.clear();
2554
2555 ClearContentsFunctor clearContentsFunctor; // instantiate the functor
2556 for (size_t i = 0; i < contents_count_; i++) {
2557 visit_at(contents_, i, clearContentsFunctor); // pass the functor instead of the lambda
2558 }
2559 }
2560
2561
2563 size_t
2564 length() const noexcept {
2565 return tags_.length();
2566 }
2567
2569 bool
2570 is_valid(std::string& error) const noexcept {
2571 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
2572
2573 std::vector<size_t> lengths = content_lengths(index_sequence);
2574 std::unique_ptr<INDEX[]> index_ptr(new INDEX[index_.length()]);
2575 index_.concatenate(index_ptr.get());
2576 std::unique_ptr<TAGS[]> tags_ptr(new TAGS[tags_.length()]);
2577 tags_.concatenate(tags_ptr.get());
2578 for (size_t i = 0; i < index_.length(); i++) {
2579 if (index_ptr.get()[i] < 0 || index_ptr.get()[i] >= lengths[tags_ptr.get()[i]]) {
2580 std::stringstream out;
2581 out << "Union node" << id_ << " has index " << index_ptr.get()[i]
2582 << " at position " << i << " but content has length "
2583 << lengths[tags_ptr.get()[i]] << "\n";
2584 error.append(out.str());
2585
2586 return false;
2587 }
2588 }
2589
2590 std::vector<bool> valid_contents =
2591 content_is_valid(index_sequence, error);
2592 return std::none_of(std::cbegin(valid_contents),
2593 std::cend(valid_contents),
2594 std::logical_not<bool>());
2595 }
2596
2600 public:
2601 // Constructor to capture names_nbytes by reference
2602 BufferNBytesFunctor(std::map<std::string, size_t>& names_nbytes)
2603 : names_nbytes_(names_nbytes) {}
2604
2605 // Template operator() to handle the content
2606 template <class CONTENT>
2607 void operator()(CONTENT& content) const {
2608 content.buffer_nbytes(names_nbytes_);
2609 }
2610
2611 private:
2612 std::map<std::string, size_t>& names_nbytes_; // reference to the map
2613 };
2614
2615 void
2616 buffer_nbytes(std::map<std::string, size_t>& names_nbytes) const noexcept {
2617 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
2618
2619 // Store the size of tags and index buffers
2620 names_nbytes["node" + std::to_string(id_) + "-tags"] = tags_.nbytes();
2621 names_nbytes["node" + std::to_string(id_) + "-index"] = index_.nbytes();
2622
2623 // Instantiate the functor to handle each content's buffer_nbytes call
2624 BufferNBytesFunctor bufferNBytesFunctor(names_nbytes);
2625
2626 for (size_t i = 0; i < contents_count_; i++) {
2627 visit_at(contents_, i, bufferNBytesFunctor); // pass the functor instead of the lambda
2628 }
2629 }
2630
2631
2638 public:
2639 // Constructor to capture buffers by reference
2640 ToBuffersFunctor(std::map<std::string, void*>& buffers)
2641 : buffers_(buffers) { }
2642
2643 // Template operator() to handle the content
2644 template <class CONTENT>
2645 void operator()(CONTENT& content) const {
2646 content.to_buffers(buffers_);
2647 }
2648
2649 private:
2650 std::map<std::string, void*>& buffers_; // reference to the buffers map
2651 };
2652
2653 void
2654 to_buffers(std::map<std::string, void*>& buffers) const noexcept {
2655 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
2656
2657 // Concatenate tags and index buffers
2658 tags_.concatenate(reinterpret_cast<TAGS*>(
2659 buffers["node" + std::to_string(id_) + "-tags"]));
2660 index_.concatenate(reinterpret_cast<INDEX*>(
2661 buffers["node" + std::to_string(id_) + "-index"]));
2662
2663 // Create the functor to handle each content's to_buffers call
2664 ToBuffersFunctor toBuffersFunctor(buffers);
2665
2666 for (size_t i = 0; i < contents_count_; i++) {
2667 visit_at(contents_, i, toBuffersFunctor); // pass the functor instead of the lambda
2668 }
2669 }
2670
2671
2677 public:
2678 // Constructor to capture buffer and name by reference
2679 ToBufferFunctor(void* buffer, const char* name)
2680 : buffer_(buffer), name_(name) { }
2681
2682 // Template operator() to handle the content
2683 template <class CONTENT>
2684 void operator()(CONTENT& content) const {
2685 content.to_buffer(buffer_, name_);
2686 }
2687
2688 private:
2689 void* buffer_; // reference to the buffer
2690 const char* name_; // reference to the name
2691 };
2692
2693 void
2694 to_buffer(void* buffer, const char* name) const noexcept {
2695 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
2696
2697 if (std::string(name) == std::string("node" + std::to_string(id_) + "-tags")) {
2698 tags_.concatenate(reinterpret_cast<TAGS*>(buffer));
2699 }
2700 else if (std::string(name) == std::string("node" + std::to_string(id_) + "-index")) {
2701 index_.concatenate(reinterpret_cast<INDEX*>(buffer));
2702 }
2703
2704 // Instantiate the functor to handle each content's to_buffer call
2705 ToBufferFunctor toBufferFunctor(buffer, name);
2706
2707 for (size_t i = 0; i < contents_count_; i++) {
2708 visit_at(contents_, i, toBufferFunctor); // pass the functor instead of the lambda
2709 }
2710 }
2711
2712
2718 public:
2719 // Constructor to capture buffers by reference
2720 ToCharBuffersFunctor(std::map<std::string, uint8_t*>& buffers)
2721 : buffers_(buffers) { }
2722
2723 // Template operator() to handle the content
2724 template <class CONTENT>
2725 void operator()(CONTENT& content) const {
2726 content.to_char_buffers(buffers_);
2727 }
2728
2729 private:
2730 std::map<std::string, uint8_t*>& buffers_; // reference to the buffers map
2731 };
2732
2733 void
2734 to_char_buffers(std::map<std::string, uint8_t*>& buffers) const noexcept {
2735 auto index_sequence((std::index_sequence_for<BUILDERS...>()));
2736
2737 // Concatenate tags and index buffers
2738 tags_.concatenate(reinterpret_cast<TAGS*>(
2739 buffers["node" + std::to_string(id_) + "-tags"]));
2740 index_.concatenate(reinterpret_cast<INDEX*>(
2741 buffers["node" + std::to_string(id_) + "-index"]));
2742
2743 // Instantiate the functor to handle each content's to_char_buffers call
2744 ToCharBuffersFunctor toCharBuffersFunctor(buffers);
2745
2746 for (size_t i = 0; i < contents_count_; i++) {
2747 visit_at(contents_, i, toCharBuffersFunctor); // pass the functor instead of the lambda
2748 }
2749 }
2750
2751
2755 public:
2756 ContentsFormFunctor(std::stringstream& out)
2757 : out_(out) {}
2758
2759 // Template operator() to handle each content
2760 template <class CONTENT>
2761 void operator()(CONTENT& content) const {
2762 out_ << content.form();
2763 }
2764
2765 private:
2766 std::stringstream& out_; // Reference to the output stringstream
2767 };
2768
2769
2770 std::string
2771 form() const noexcept {
2772 std::stringstream form_key;
2773 form_key << "node" << id_;
2774 std::string params("");
2775 if (!parameters_.empty()) {
2776 params = ", \"parameters\": { " + parameters_ + " }";
2777 }
2778 std::stringstream out;
2779 out << "{ \"class\": \"UnionArray\", \"tags\": \"" +
2780 type_to_numpy_like<TAGS>() + "\", \"index\": \"" +
2781 type_to_numpy_like<INDEX>() + "\", \"contents\": [";
2782 for (size_t i = 0; i < contents_count_; i++) {
2783 if (i != 0) {
2784 out << ", ";
2785 }
2786 ContentsFormFunctor contentsFormFunctor(out);
2787 visit_at(contents_, i, contentsFormFunctor);
2788 }
2789 out << "], ";
2790 out << params << "\"form_key\": \"" << form_key.str() << "\" }";
2791 return out.str();
2792 }
2793
2794
2795 private:
2798 template <std::size_t... S>
2799 std::vector<size_t>
2800 content_lengths(std::index_sequence<S...>) const {
2801 return std::vector<size_t>({std::get<S>(contents_).length()...});
2802 }
2803
2805 template <std::size_t... S>
2806 std::vector<bool>
2807 content_is_valid(std::index_sequence<S...>, std::string& error) const {
2808 return std::vector<bool>({std::get<S>(contents_).is_valid(error)...});
2809 }
2810
2814 GrowableBuffer<TAGS> tags_;
2815
2819 GrowableBuffer<INDEX> index_;
2820
2822 Contents contents_;
2823
2825 std::string parameters_;
2826
2828 size_t id_;
2829
2831 size_t last_valid_index_[sizeof...(BUILDERS)];
2832
2834 static constexpr size_t contents_count_ = sizeof...(BUILDERS);
2835 };
2836
2837 } // namespace LayoutBuilder
2838} // namespace awkward
2839
2840#endif // AWKWARD_LAYOUTBUILDER_H_
#define AWKWARD_LAYOUTBUILDER_DEFAULT_OPTIONS
Object of BuilderOptions which sets the values of the default options.
Definition LayoutBuilder.h:18
virtual const std::string to_buffers(BuffersContainer &container, int64_t &form_key_id) const =0
Copy the current snapshot into the BuffersContainer and return a Form as a std::string (JSON).
virtual void clear()=0
Removes all accumulated data without resetting the type knowledge.
virtual const BuilderPtr index(int64_t index)=0
Sets the pointer to a given tuple field index; the next command will fill that slot.
Discontiguous, one-dimensional buffer (which consists of multiple contiguous, one-dimensional panels)...
Definition GrowableBuffer.h:233
void concatenate_from(PRIMITIVE *external_pointer, size_t to, size_t from) const noexcept
Copies and concatenates all accumulated data from multiple panels to one contiguously allocated exter...
Definition GrowableBuffer.h:517
size_t nbytes() const
Currently used number of bytes.
Definition GrowableBuffer.h:440
void concatenate(PRIMITIVE *external_pointer) const noexcept
Copies and concatenates all accumulated data from multiple panels to one contiguously allocated exter...
Definition GrowableBuffer.h:492
PRIMITIVE & append_and_get_ref(PRIMITIVE datum)
Like append, but the type signature returns the reference to PRIMITIVE.
Definition GrowableBuffer.h:484
size_t length() const
Currently used number of elements.
Definition GrowableBuffer.h:408
void append(PRIMITIVE datum)
Inserts one datum into the panel, possibly triggering allocation of a new panel.
Definition GrowableBuffer.h:450
void clear()
Discards accumulated data, the #reserved returns to options.initial(), and a new #ptr is allocated.
Definition GrowableBuffer.h:421
Builds a BitMaskedArray in which mask values are packed into a bitmap.
Definition LayoutBuilder.h:2125
bool valid_when() const noexcept
Determines when the builder content are valid.
Definition LayoutBuilder.h:2178
void clear() noexcept
Discards the accumulated mask and clears the content of the builder.
Definition LayoutBuilder.h:2261
bool lsb_order() const noexcept
Determines whether the position of each bit is in Least-Significant Bit order (LSB) or not.
Definition LayoutBuilder.h:2185
BUILDER & append_invalid() noexcept
Sets current_byte_ and cast_ default to null, no change in current_byte_.
Definition LayoutBuilder.h:2219
BUILDER & extend_valid(size_t size) noexcept
Sets size number of bits in the mask. If current_byte_ and cast_: 0 indicates null,...
Definition LayoutBuilder.h:2208
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:2344
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:2240
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:2246
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:2172
size_t length() const noexcept
Current length of the mask buffer.
Definition LayoutBuilder.h:2271
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:2333
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:2252
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:2278
BitMasked(const awkward::BuilderOptions &options)
Creates a new BitMasked layout builder by allocating a new mask buffer, taking options from BuilderOp...
Definition LayoutBuilder.h:2152
BUILDER & append_valid() noexcept
Sets a bit in the mask. If current_byte_ and cast_: 0 indicates null, 1 indicates valid and vice vers...
Definition LayoutBuilder.h:2194
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:2295
BUILDER & extend_invalid(size_t size) noexcept
Sets current_byte_ and cast_ default to null, no change in current_byte_.
Definition LayoutBuilder.h:2231
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the builder buffer to a user-defined pointer if the g...
Definition LayoutBuilder.h:2320
BitMasked()
Creates a new BitMasked layout builder by allocating a new mask buffer, using AWKWARD_LAYOUTBUILDER_D...
Definition LayoutBuilder.h:2129
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:2307
Functor for setting the ids of all nodes in a layout tree.
Definition LayoutBuilder.h:27
BuilderSetId(size_t &id)
Definition LayoutBuilder.h:29
void operator()(CONTENT &content)
Definition LayoutBuilder.h:32
Builds a ByteMaskedArray using a mask which is an array of booleans that determines whether the corre...
Definition LayoutBuilder.h:1900
bool valid_when() const noexcept
Determines when the builder content are valid.
Definition LayoutBuilder.h:1929
void clear() noexcept
Discards the accumulated mask and clears the content of the builder.
Definition LayoutBuilder.h:2000
BUILDER & append_invalid() noexcept
Inserts !valid_when in the mask.
Definition LayoutBuilder.h:1959
BUILDER & extend_valid(size_t size) noexcept
Inserts size number of valid_when in the mask.
Definition LayoutBuilder.h:1948
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:2074
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:1979
ByteMasked()
Creates a new ByteMasked layout builder by allocating a new mask buffer, using AWKWARD_LAYOUTBUILDER_...
Definition LayoutBuilder.h:1904
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:1985
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:1923
ByteMasked(const awkward::BuilderOptions &options)
Creates a new ByteMasked layout builder by allocating a new mask buffer, taking options from BuilderO...
Definition LayoutBuilder.h:1915
size_t length() const noexcept
Current length of the mask buffer.
Definition LayoutBuilder.h:2007
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:2065
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:1991
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:2013
BUILDER & append_valid() noexcept
Inserts valid_when in the mask.
Definition LayoutBuilder.h:1937
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:2030
BUILDER & extend_invalid(size_t size) noexcept
Inserts size number of !valid_when in the mask.
Definition LayoutBuilder.h:1970
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the builder buffer to a user-defined pointer if the g...
Definition LayoutBuilder.h:2053
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:2042
Builds an EmptyArray which has no content in it. It is used whenever an array's type is not known bec...
Definition LayoutBuilder.h:432
void clear() noexcept
Definition LayoutBuilder.h:444
bool is_valid(std::string &) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:454
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:478
void to_buffers(std::map< std::string, void * > &) const noexcept
Definition LayoutBuilder.h:463
void to_buffer(void *, const char *) const noexcept
Definition LayoutBuilder.h:466
Empty()
Creates a new Empty layout builder.
Definition LayoutBuilder.h:435
void set_id(size_t &) noexcept
Definition LayoutBuilder.h:441
size_t length() const noexcept
Current length of the content.
Definition LayoutBuilder.h:448
void buffer_nbytes(std::map< std::string, size_t > &) const noexcept
Definition LayoutBuilder.h:459
void to_char_buffers(std::map< std::string, uint8_t * > &) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:473
Helper class for sending a pair of field names (as enum) and field type as template parameters in Rec...
Definition LayoutBuilder.h:48
std::string index_as_field() const
Converts index as field string.
Definition LayoutBuilder.h:54
BUILDER Builder
Definition LayoutBuilder.h:50
const std::size_t index
The index of a Record field.
Definition LayoutBuilder.h:59
Builder builder
The content type of field in a Record.
Definition LayoutBuilder.h:61
Builds an IndexedOptionArray which consists of an index buffer. The negative values in the index are ...
Definition LayoutBuilder.h:1537
void clear() noexcept
Discards the accumulated index and clears the content of the builder. Also, last valid returns to -1.
Definition LayoutBuilder.h:1646
void extend_invalid(size_t size) noexcept
Inserts -1 in the index buffer size number of times.
Definition LayoutBuilder.h:1617
BUILDER & extend_valid(size_t size) noexcept
Inserts size number of valid index in the index buffer and returns the reference to the builder conte...
Definition LayoutBuilder.h:1594
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:1721
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:1625
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:1631
IndexedOption(const awkward::BuilderOptions &options)
Creates a new IndexedOption layout builder by allocating a new index buffer, taking options from Buil...
Definition LayoutBuilder.h:1555
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:1565
BUILDER & append_valid(size_t i) noexcept
Inserts an explicit value in the index buffer and returns the reference to the builder content.
Definition LayoutBuilder.h:1580
size_t length() const noexcept
Current length of the index buffer.
Definition LayoutBuilder.h:1654
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:1712
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:1637
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:1660
IndexedOption()
Creates a new IndexedOption layout builder by allocating a new index buffer, using AWKWARD_LAYOUTBUIL...
Definition LayoutBuilder.h:1541
BUILDER & append_valid() noexcept
Inserts the last valid index in the index buffer and returns the reference to the builder content.
Definition LayoutBuilder.h:1572
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:1677
void append_invalid() noexcept
Inserts -1 in the index buffer.
Definition LayoutBuilder.h:1609
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the builder buffer to a user-defined pointer if the g...
Definition LayoutBuilder.h:1700
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:1689
Builds an IndexedArray which consists of an index buffer.
Definition LayoutBuilder.h:1323
void clear() noexcept
Discards the accumulated index and clears the content of the builder.
Definition LayoutBuilder.h:1413
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:1493
Indexed(const awkward::BuilderOptions &options)
Creates a new Indexed layout builder by allocating a new index buffer, taking options from BuilderOpt...
Definition LayoutBuilder.h:1340
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:1392
Indexed()
Creates a new Indexed layout builder by allocating a new index buffer, using AWKWARD_LAYOUTBUILDER_DE...
Definition LayoutBuilder.h:1327
BUILDER & append_index() noexcept
Inserts the last valid index in the index buffer and returns the reference to the builder content.
Definition LayoutBuilder.h:1356
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:1398
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:1349
BUILDER & extend_index(size_t size) noexcept
Inserts size number indices in the index buffer and returns the reference to the builder content.
Definition LayoutBuilder.h:1378
size_t length() const noexcept
Current length of the index buffer.
Definition LayoutBuilder.h:1421
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:1484
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:1404
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:1436
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:1428
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the builder buffer to a user-defined pointer if the g...
Definition LayoutBuilder.h:1472
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:1461
BUILDER & append_index(size_t i) noexcept
Inserts an explicit value in the index buffer and returns the reference to the builder content.
Definition LayoutBuilder.h:1363
Builds a ListOffsetArray which describes unequal-length lists (often called a "jagged" or "ragged" ar...
Definition LayoutBuilder.h:236
void clear() noexcept
Discards the accumulated offsets and clears the builder content.
Definition LayoutBuilder.h:301
BUILDER & begin_list() noexcept
Begins a list and returns the reference to the builder content.
Definition LayoutBuilder.h:268
ListOffset()
Creates a new ListOffset layout builder by allocating a new offset buffer, using AWKWARD_LAYOUTBUILDE...
Definition LayoutBuilder.h:240
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:377
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:281
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:287
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:262
size_t length() const noexcept
Current length of the content.
Definition LayoutBuilder.h:309
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:368
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:293
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:315
void end_list() noexcept
Ends a list and appends the current length of the list contents in the offsets buffer.
Definition LayoutBuilder.h:275
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:332
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the builder buffer to a user-defined pointer if the g...
Definition LayoutBuilder.h:356
ListOffset(const awkward::BuilderOptions &options)
Creates a new ListOffset layout builder by allocating a new offset buffer, taking options from Builde...
Definition LayoutBuilder.h:253
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:345
Builds a NumpyArray which describes multi-dimensional data of PRIMITIVE type.
Definition LayoutBuilder.h:71
void clear() noexcept
Discards the accumulated data in the builder.
Definition LayoutBuilder.h:128
bool is_valid(std::string &) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:140
Numpy(const awkward::BuilderOptions &options)
Creates a new Numpy layout builder by allocating a new buffer, taking options from BuilderOptions for...
Definition LayoutBuilder.h:87
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:109
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:115
size_t length() const noexcept
Current length of the data.
Definition LayoutBuilder.h:134
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:177
void append(PRIMITIVE x) noexcept
Inserts a PRIMITIVE type data.
Definition LayoutBuilder.h:95
Numpy()
Creates a new Numpy layout builder by allocating a new buffer, using AWKWARD_LAYOUTBUILDER_DEFAULT_OP...
Definition LayoutBuilder.h:75
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:121
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the name and size (in bytes) of the buffer.
Definition LayoutBuilder.h:146
void extend(PRIMITIVE *ptr, size_t size) noexcept
Inserts an entire array of PRIMITIVE type data.
Definition LayoutBuilder.h:103
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the builder buffer to a user-defined pointer if the g...
Definition LayoutBuilder.h:166
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a user-defined pointer.
Definition LayoutBuilder.h:157
std::string form() const
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:185
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:637
BufferNBytesFunctor(std::map< std::string, size_t > &names_nbytes)
Definition LayoutBuilder.h:640
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:645
Clears the builder contents.
Definition LayoutBuilder.h:586
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:589
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:749
ContentsFormFunctor(std::stringstream &out, const std::map< size_t, std::string > &content_names)
Definition LayoutBuilder.h:752
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:757
Copies and concatenates the accumulated data in the buffers of the builder contents to user-defined p...
Definition LayoutBuilder.h:692
ToBufferFunctor(void *buffer, const char *name)
Definition LayoutBuilder.h:695
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:700
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:666
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:674
ToBuffersFunctor(std::map< std::string, void * > &buffers)
Definition LayoutBuilder.h:669
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:722
ToCharBuffersFunctor(std::map< std::string, uint8_t * > &buffers)
Definition LayoutBuilder.h:725
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:730
Builds a RecordArray which represents an array of records, which can be of same or different types....
Definition LayoutBuilder.h:499
void clear() noexcept
Definition LayoutBuilder.h:595
Record()
Creates a new Record layout builder.
Definition LayoutBuilder.h:508
MAP UserDefinedMap
Definition LayoutBuilder.h:502
std::string form() const noexcept
Definition LayoutBuilder.h:776
RecordFieldType< INDEX >::Builder & content() noexcept
Returns the reference to the builder contents at INDEX.
Definition LayoutBuilder.h:553
std::tuple_element_t< INDEX, RecordContents > RecordFieldType
Definition LayoutBuilder.h:505
Record(UserDefinedMap user_defined_field_id_to_name_map)
Creates a new Record layout builder, taking a user-defined map with enumerated type field ID as keys ...
Definition LayoutBuilder.h:520
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:559
typename std::tuple< BUILDERS... > RecordContents
Definition LayoutBuilder.h:501
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:565
size_t length() const noexcept
Current number of records in first field.
Definition LayoutBuilder.h:603
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Definition LayoutBuilder.h:739
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:570
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:609
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Definition LayoutBuilder.h:654
void set_fields(MAP user_defined_field_id_to_name_map) noexcept
Sets the field names.
Definition LayoutBuilder.h:546
void to_buffer(void *buffer, const char *name) const noexcept
Definition LayoutBuilder.h:710
const std::vector< std::string > fields() const noexcept
Returns a vector of strings sontaining all the field names.
Definition LayoutBuilder.h:529
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Definition LayoutBuilder.h:683
RecordContents contents
The contents of the RecordArray.
Definition LayoutBuilder.h:802
Builds a RegularArray that describes lists that have the same length, a single integer size....
Definition LayoutBuilder.h:1168
void clear() noexcept
Clears the builder content.
Definition LayoutBuilder.h:1217
BUILDER & begin_list() noexcept
Begins a list and returns the reference to the content of the builder.
Definition LayoutBuilder.h:1185
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:1282
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:1197
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:1203
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:1178
size_t length() const noexcept
Current number of lists of length SIZE.
Definition LayoutBuilder.h:1224
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:1275
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:1209
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:1230
void end_list() noexcept
Ends a list and increments the number of lists.
Definition LayoutBuilder.h:1191
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:1247
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the buffers of the builder content to user-defined po...
Definition LayoutBuilder.h:1266
Regular()
Creates a new Regular layout builder.
Definition LayoutBuilder.h:1171
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:1258
Helper for building an array of strings with a similar API as a Numpy builder.
Definition LayoutBuilder.h:412
void append(const std::string &value)
Definition LayoutBuilder.h:419
String()
Definition LayoutBuilder.h:414
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:969
BufferNBytesFunctor(std::map< std::string, size_t > &names_nbytes)
Definition LayoutBuilder.h:972
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:977
Clears the builder contents.
Definition LayoutBuilder.h:916
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:920
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:1083
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:1090
ContentsFormFunctor(std::stringstream &out)
Definition LayoutBuilder.h:1085
Assigns a unique ID to each node.
Definition LayoutBuilder.h:887
SetIdFunctor(size_t &id)
Definition LayoutBuilder.h:890
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:894
Copies and concatenates the accumulated data in the buffers of the builder contents to user-defined p...
Definition LayoutBuilder.h:1027
ToBufferFunctor(void *buffer, const char *name)
Definition LayoutBuilder.h:1030
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:1035
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:999
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:1007
ToBuffersFunctor(std::map< std::string, void * > &buffers)
Definition LayoutBuilder.h:1002
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:1057
ToCharBuffersFunctor(std::map< std::string, uint8_t * > &buffers)
Definition LayoutBuilder.h:1060
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:1065
Builds a RecordArray which represents an array of tuples which can be of same or different types with...
Definition LayoutBuilder.h:854
void clear() noexcept
Definition LayoutBuilder.h:925
TupleContents contents
The contents of the RecordArray without fields.
Definition LayoutBuilder.h:1122
std::string form() const noexcept
Definition LayoutBuilder.h:1100
Tuple()
Creates a new Tuple layout builder.
Definition LayoutBuilder.h:862
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:876
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:882
size_t length() const noexcept
Current number of records in the first index of the tuple.
Definition LayoutBuilder.h:934
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Definition LayoutBuilder.h:1074
TupleContentType< INDEX > & content() noexcept
Returns the reference to the builder contents at INDEX.
Definition LayoutBuilder.h:870
void set_id(size_t &id) noexcept
Definition LayoutBuilder.h:903
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:940
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Definition LayoutBuilder.h:986
void to_buffer(void *buffer, const char *name) const noexcept
Definition LayoutBuilder.h:1045
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Definition LayoutBuilder.h:1016
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:2599
BufferNBytesFunctor(std::map< std::string, size_t > &names_nbytes)
Definition LayoutBuilder.h:2602
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2607
Discards the accumulated tags and index, and clears the builder contents.
Definition LayoutBuilder.h:2538
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2542
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:2754
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2761
ContentsFormFunctor(std::stringstream &out)
Definition LayoutBuilder.h:2756
Assigns a unique ID to each node.
Definition LayoutBuilder.h:2508
SetIdFunctor(size_t &id)
Definition LayoutBuilder.h:2511
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2515
Copies and concatenates the accumulated data in the builder buffers to user-defined pointers if the g...
Definition LayoutBuilder.h:2676
ToBufferFunctor(void *buffer, const char *name)
Definition LayoutBuilder.h:2679
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2684
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:2637
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2645
ToBuffersFunctor(std::map< std::string, void * > &buffers)
Definition LayoutBuilder.h:2640
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:2717
ToCharBuffersFunctor(std::map< std::string, uint8_t * > &buffers)
Definition LayoutBuilder.h:2720
void operator()(CONTENT &content) const
Definition LayoutBuilder.h:2725
Builds a UnionArray which represents data drawn from an ordered list of contents, which can have diff...
Definition LayoutBuilder.h:2439
void clear() noexcept
Definition LayoutBuilder.h:2548
Union(const awkward::BuilderOptions &options)
Creates a new Union layout builder by allocating new tags and index buffers, taking options from Buil...
Definition LayoutBuilder.h:2463
typename std::tuple< BUILDERS... > Contents
Definition LayoutBuilder.h:2441
std::tuple_element_t< I, Contents > ContentType
Definition LayoutBuilder.h:2444
ContentType< TAG > & append_content() noexcept
Inserts the current tag in the tags buffer and the next index in the index buffer and returns the ref...
Definition LayoutBuilder.h:2483
std::string form() const noexcept
Definition LayoutBuilder.h:2771
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:2497
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:2503
size_t length() const noexcept
Current length of the tags buffer.
Definition LayoutBuilder.h:2564
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Definition LayoutBuilder.h:2734
Union()
Creates a new Union layout builder by allocating new tags and index buffers, using AWKWARD_LAYOUTBUIL...
Definition LayoutBuilder.h:2448
ContentType< I > & content() noexcept
Definition LayoutBuilder.h:2475
void set_id(size_t &id) noexcept
Definition LayoutBuilder.h:2524
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:2570
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Definition LayoutBuilder.h:2616
void to_buffer(void *buffer, const char *name) const noexcept
Definition LayoutBuilder.h:2694
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Definition LayoutBuilder.h:2654
Builds an UnmaskedArray which the values are never, in fact, missing. It exists to satisfy systems th...
Definition LayoutBuilder.h:1769
void clear() noexcept
Clears the builder content.
Definition LayoutBuilder.h:1805
Unmasked()
Creates a new Unmasked layout builder.
Definition LayoutBuilder.h:1772
std::string form() const noexcept
Generates a unique description of the builder and its contents in the form of a JSON-like string.
Definition LayoutBuilder.h:1859
const std::string & parameters() const noexcept
Parameters for the builder form.
Definition LayoutBuilder.h:1785
void set_parameters(std::string parameter) noexcept
Sets the form parameters.
Definition LayoutBuilder.h:1791
BUILDER & content() noexcept
Returns the reference to the builder content.
Definition LayoutBuilder.h:1779
size_t length() const noexcept
Current length of the content.
Definition LayoutBuilder.h:1811
void to_char_buffers(std::map< std::string, uint8_t * > &buffers) const noexcept
Copies and concatenates all the accumulated data in the builder to a map of user-allocated buffers.
Definition LayoutBuilder.h:1852
void set_id(size_t &id) noexcept
Assigns a unique ID to each node.
Definition LayoutBuilder.h:1797
bool is_valid(std::string &error) const noexcept
Checks for validity and consistency.
Definition LayoutBuilder.h:1817
void buffer_nbytes(std::map< std::string, size_t > &names_nbytes) const noexcept
Retrieves the names and sizes (in bytes) of the buffers used in the builder and its contents.
Definition LayoutBuilder.h:1824
void to_buffer(void *buffer, const char *name) const noexcept
Copies and concatenates the accumulated data in the buffers of the builder content to user-defined po...
Definition LayoutBuilder.h:1843
void to_buffers(std::map< std::string, void * > &buffers) const noexcept
Copies and concatenates all the accumulated data in each of the buffers of the builder and its conten...
Definition LayoutBuilder.h:1835
Definition ArrayBuilder.h:14
void visit_at(std::tuple< CONTENTs... > const &contents, size_t index, FUNCTION fun)
Visits the tuple contents at index.
Definition utils.h:263
Container for all configuration options needed by ArrayBuilder, GrowableBuffer, LayoutBuilder and the...
Definition BuilderOptions.h:20
Definition utils.h:168
std::map< std::size_t, std::string > UserDefinedMap
Definition test_1494-layout-builder.cpp:39