Skip to content

Commit f868583

Browse files
committed
Add test for std::tuple
Closes #18
1 parent 82c8722 commit f868583

File tree

4 files changed

+227
-0
lines changed

4 files changed

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

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)