Skip to content

Commit 7a27276

Browse files
committed
Add test for std::array
Closes #7
1 parent cdba96f commit 7a27276

4 files changed

Lines changed: 211 additions & 0 deletions

File tree

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Types
22

3+
* [`array`](array): `std::array`
34
* [`fundamental`](fundamental): fundamental column types
45
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
56
* [`optional`](optional): `std::optional` with different element types

types/array/README.md

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

types/array/read.C

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

types/array/write.C

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 <array>
10+
#include <cstdint>
11+
#include <memory>
12+
#include <string>
13+
#include <string_view>
14+
#include <utility>
15+
#include <variant>
16+
#include <vector>
17+
18+
using ArrayInt32 = std::array<std::int32_t, 2>;
19+
using Variant = std::variant<std::int32_t, std::string>;
20+
using VectorInt32 = std::vector<std::int32_t>;
21+
22+
void write(std::string_view filename = "types.array.root") {
23+
auto model = RNTupleModel::Create();
24+
25+
auto Array_Int32 = model->MakeField<ArrayInt32>("Array_Int32");
26+
auto Array_Array = model->MakeField<std::array<ArrayInt32, 3>>("Array_Array");
27+
auto Array_String =
28+
model->MakeField<std::array<std::string, 2>>("Array_String");
29+
auto Array_Variant =
30+
model->MakeField<std::array<Variant, 2>>("Array_Variant");
31+
auto Array_Vector =
32+
model->MakeField<std::array<VectorInt32, 2>>("Array_Vector");
33+
34+
RNTupleWriteOptions options;
35+
options.SetCompression(0);
36+
auto writer =
37+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
38+
39+
// First entry: simple values
40+
*Array_Int32 = {1, 2};
41+
*Array_Array = {ArrayInt32{3, 4}, ArrayInt32{5, 6}, ArrayInt32{7, 8}};
42+
*Array_String = {"ijk", "lmn"};
43+
*Array_Vector = {VectorInt32{15, 16, 17}, VectorInt32{17, 18}};
44+
*Array_Variant = {19, "uvw"};
45+
writer->Fill();
46+
47+
// Second entry: zero / empty values
48+
*Array_Int32 = {0, 0};
49+
*Array_Array = {ArrayInt32{0, 0}, ArrayInt32{0, 0}, ArrayInt32{0, 0}};
50+
*Array_String = {"", ""};
51+
*Array_Vector = {VectorInt32{}, VectorInt32{}};
52+
*Array_Variant = {0, ""};
53+
writer->Fill();
54+
}

0 commit comments

Comments
 (0)