Skip to content

Commit 1dd3efb

Browse files
committed
Add test for std::tuple
Closes #18
1 parent 595cb6b commit 1dd3efb

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [`pair`](pair): `std::pair` with different element types
77
* [`set`](set): `std::set` with all `[Split]Index{32,64}` column types
88
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
9+
* [`tuple`](tuple): `std::tuple` with different element types
910
* [`unique_ptr`](unique_ptr): `std::unique_ptr` with different element types
1011
* [`unordered_multiset`](unordered_multiset): `std::unordered_multiset` with all `[Split]Index{32,64}` column types
1112
* [`unordered_set`](unordered_set): `std::unordered_set` with all `[Split]Index{32,64}` column types

types/tuple/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::tuple`
2+
3+
## Fields
4+
5+
* `Int32_String_Vector`: `std::tuple<std::int32_t, std::string, std::vector<std::int32_t>>`
6+
* `Variant`: `std::tuple<std::variant<std::int32_t, std::string>>`
7+
* `Tuple`: `std::tuple<std::tuple<std::int32_t, std::string>>`
8+
* `VectorTuple`: `std::vector<std::tuple<std::int32_t, std::string>>`
9+
10+
with the default column types.
11+
12+
## Entries
13+
14+
1. Simple values
15+
2. Zero / empty values

types/tuple/read.C

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
4+
using ROOT::Experimental::REntry;
5+
using ROOT::Experimental::RNTupleReader;
6+
7+
#include <cstdint>
8+
#include <fstream>
9+
#include <ostream>
10+
#include <string>
11+
#include <string_view>
12+
#include <tuple>
13+
#include <variant>
14+
#include <vector>
15+
16+
using Tuple_Int32_String = std::tuple<std::int32_t, std::string>;
17+
using VectorInt32 = std::vector<std::int32_t>;
18+
19+
template <typename T> static void PrintValue(const T &value, std::ostream &os);
20+
21+
template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
22+
os << value;
23+
}
24+
25+
template <> void PrintValue(const std::string &value, std::ostream &os) {
26+
os << "\"" << value << "\"";
27+
}
28+
29+
template <> void PrintValue(const VectorInt32 &value, std::ostream &os) {
30+
os << "[";
31+
bool first = true;
32+
for (auto element : value) {
33+
if (first) {
34+
first = false;
35+
} else {
36+
os << ",";
37+
}
38+
os << "\n " << element;
39+
}
40+
if (!value.empty()) {
41+
os << "\n ";
42+
}
43+
os << "]";
44+
}
45+
46+
template <> void PrintValue(const Tuple_Int32_String &value, std::ostream &os) {
47+
os << "[\n";
48+
os << " " << std::get<0>(value) << ",\n";
49+
os << " \"" << std::get<1>(value) << "\"\n";
50+
os << " ]";
51+
}
52+
53+
static void PrintTupleValue(const REntry &entry, std::string_view name,
54+
std::ostream &os, bool last = false) {
55+
auto &value =
56+
*entry.GetPtr<std::tuple<std::int32_t, std::string, VectorInt32>>(name);
57+
os << " \"" << name << "\": [\n";
58+
os << " ";
59+
PrintValue(std::get<0>(value), os);
60+
os << ",\n";
61+
os << " ";
62+
PrintValue(std::get<1>(value), os);
63+
os << ",\n";
64+
os << " ";
65+
PrintValue(std::get<2>(value), os);
66+
os << "\n";
67+
os << " ]";
68+
if (!last) {
69+
os << ",";
70+
}
71+
os << "\n";
72+
}
73+
74+
static void PrintVariantValue(const REntry &entry, std::string_view name,
75+
std::ostream &os, bool last = false) {
76+
auto &value =
77+
*entry.GetPtr<std::tuple<std::variant<std::int32_t, std::string>>>(name);
78+
os << " \"" << name << "\": [\n";
79+
os << " ";
80+
auto &variant = std::get<0>(value);
81+
if (variant.index() == 0) {
82+
PrintValue(std::get<std::int32_t>(variant), os);
83+
} else if (variant.index() == 1) {
84+
PrintValue(std::get<std::string>(variant), os);
85+
}
86+
os << "\n";
87+
os << " ]";
88+
if (!last) {
89+
os << ",";
90+
}
91+
os << "\n";
92+
}
93+
94+
static void PrintNestedValue(const REntry &entry, std::string_view name,
95+
std::ostream &os, bool last = false) {
96+
auto &value = *entry.GetPtr<std::tuple<Tuple_Int32_String>>(name);
97+
os << " \"" << name << "\": [\n";
98+
os << " ";
99+
PrintValue(std::get<0>(value), os);
100+
os << "\n";
101+
os << " ]";
102+
if (!last) {
103+
os << ",";
104+
}
105+
os << "\n";
106+
}
107+
108+
static void PrintVectorTupleValue(const REntry &entry, std::string_view name,
109+
std::ostream &os, bool last = false) {
110+
auto &value = *entry.GetPtr<std::vector<Tuple_Int32_String>>(name);
111+
os << " \"" << name << "\": [";
112+
bool first = true;
113+
for (auto &&element : value) {
114+
if (first) {
115+
first = false;
116+
} else {
117+
os << ",";
118+
}
119+
os << "\n ";
120+
PrintValue(element, os);
121+
}
122+
if (!value.empty()) {
123+
os << "\n ";
124+
}
125+
os << "]";
126+
if (!last) {
127+
os << ",";
128+
}
129+
os << "\n";
130+
}
131+
132+
void read(std::string_view input = "types.tuple.root",
133+
std::string_view output = "types.tuple.json") {
134+
std::ofstream os(std::string{output});
135+
os << "[\n";
136+
137+
auto reader = RNTupleReader::Open("ntpl", input);
138+
auto &entry = reader->GetModel().GetDefaultEntry();
139+
bool first = true;
140+
for (auto index : *reader) {
141+
reader->LoadEntry(index);
142+
143+
if (first) {
144+
first = false;
145+
} else {
146+
os << ",\n";
147+
}
148+
os << " {\n";
149+
150+
PrintTupleValue(entry, "Int32_String_Vector", os);
151+
PrintVariantValue(entry, "Variant", os);
152+
PrintNestedValue(entry, "Tuple", os);
153+
PrintVectorTupleValue(entry, "VectorTuple", os, /*last=*/true);
154+
155+
os << " }";
156+
// Newline is intentionally missing, may need to print a comma before the
157+
// next entry.
158+
}
159+
os << "\n";
160+
os << "]\n";
161+
}

types/tuple/write.C

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <ROOT/RNTupleModel.hxx>
2+
#include <ROOT/RNTupleWriteOptions.hxx>
3+
#include <ROOT/RNTupleWriter.hxx>
4+
5+
using ROOT::Experimental::RNTupleModel;
6+
using ROOT::Experimental::RNTupleWriteOptions;
7+
using ROOT::Experimental::RNTupleWriter;
8+
9+
#include <cstdint>
10+
#include <memory>
11+
#include <string>
12+
#include <string_view>
13+
#include <tuple>
14+
#include <variant>
15+
#include <vector>
16+
17+
using Tuple_Int32_String = std::tuple<std::int32_t, std::string>;
18+
using VectorInt32 = std::vector<std::int32_t>;
19+
20+
void write(std::string_view filename = "types.tuple.root") {
21+
auto model = RNTupleModel::Create();
22+
23+
auto Int32_String_Vector =
24+
model->MakeField<std::tuple<std::int32_t, std::string, VectorInt32>>(
25+
"Int32_String_Vector");
26+
auto Variant =
27+
model->MakeField<std::tuple<std::variant<std::int32_t, std::string>>>(
28+
"Variant");
29+
auto Tuple = model->MakeField<std::tuple<Tuple_Int32_String>>("Tuple");
30+
auto VectorTuple =
31+
model->MakeField<std::vector<Tuple_Int32_String>>("VectorTuple");
32+
33+
RNTupleWriteOptions options;
34+
options.SetCompression(0);
35+
auto writer =
36+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
37+
38+
// First entry: simple values
39+
*Int32_String_Vector = {1, "abc", {2, 3, 4}};
40+
*Variant = {"def"};
41+
*Tuple = {{5, "ghi"}};
42+
*VectorTuple = {{6, "jkl"}, {7, "mno"}};
43+
writer->Fill();
44+
45+
// Second entry: zero / empty values
46+
*Int32_String_Vector = {0, "", {}};
47+
*Variant = {0};
48+
*Tuple = {{0, ""}};
49+
*VectorTuple = {};
50+
writer->Fill();
51+
}

0 commit comments

Comments
 (0)