Skip to content

Commit 57f6df6

Browse files
committed
Add test for std::pair
Closes #17
1 parent 71b59a0 commit 57f6df6

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [`fundamental`](fundamental): fundamental column types
44
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
55
* [`optional`](optional): `std::optional` with different element types
6+
* [`pair`](pair): `std::pair` with different element types
67
* [`set`](set): `std::set` with all `[Split]Index{32,64}` column types
78
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
89
* [`unique_ptr`](unique_ptr): `std::unique_ptr` with different element types

types/pair/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::pair`
2+
3+
## Fields
4+
5+
* `Int32_String`: `std::pair<std::int32_t, std::string>`
6+
* `Variant_Vector`: `std::pair<std::variant<std::int32_t, std::string>, std::vector<std::int32_t>>`
7+
* `Pair`: `std::pair<std::pair<std::int32_t, std::string>, std::int32_t>`
8+
* `VectorPair`: `std::vector<std::pair<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/pair/read.C

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

types/pair/write.C

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

0 commit comments

Comments
 (0)