Skip to content

Commit 70538e6

Browse files
committed
Add test for std::pair
Closes #17
1 parent ce8a39a commit 70538e6

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

types/README.md

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

33
* [`fundamental`](fundamental): fundamental column types
4+
* [`pair`](pair): `std::pair` with different element types
45
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
56
* [`variant`](variant): `std::variant` with `Switch` column type
67
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

types/pair/README.md

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

types/pair/read.C

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 <vector>
14+
15+
using Vector = std::vector<std::int32_t>;
16+
17+
template <typename T> static void PrintValue(const T &value, std::ostream &os);
18+
19+
template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
20+
os << value;
21+
}
22+
23+
template <> void PrintValue(const std::string &value, std::ostream &os) {
24+
os << "\"" << value << "\"";
25+
}
26+
27+
template <> void PrintValue(const Vector &value, std::ostream &os) {
28+
os << "[";
29+
bool first = true;
30+
for (auto element : value) {
31+
if (first) {
32+
first = false;
33+
} else {
34+
os << ",";
35+
}
36+
os << "\n " << element;
37+
}
38+
if (!value.empty()) {
39+
os << "\n ";
40+
}
41+
os << "]";
42+
}
43+
44+
template <typename T, typename U>
45+
static void PrintPairValue(const REntry &entry, std::string_view name,
46+
std::ostream &os, bool last = false) {
47+
auto &value = *entry.GetPtr<std::pair<T, U>>(name);
48+
os << " \"" << name << "\": [\n";
49+
os << " ";
50+
PrintValue(value.first, os);
51+
os << ",\n";
52+
os << " ";
53+
PrintValue(value.second, os);
54+
os << "\n";
55+
os << " ]";
56+
if (!last) {
57+
os << ",";
58+
}
59+
os << "\n";
60+
}
61+
62+
void read(std::string_view input = "types.pair.root",
63+
std::string_view output = "types.pair.json") {
64+
std::ofstream os(std::string{output});
65+
os << "[\n";
66+
67+
auto reader = RNTupleReader::Open("ntpl", input);
68+
auto &entry = reader->GetModel().GetDefaultEntry();
69+
bool first = true;
70+
for (auto index : *reader) {
71+
reader->LoadEntry(index);
72+
73+
if (first) {
74+
first = false;
75+
} else {
76+
os << ",\n";
77+
}
78+
os << " {\n";
79+
80+
PrintPairValue<std::int32_t, std::string>(entry, "Int32_String", os);
81+
PrintPairValue<std::int32_t, Vector>(entry, "Int32_Vector", os);
82+
PrintPairValue<std::string, Vector>(entry, "String_Vector", os,
83+
/*last=*/true);
84+
85+
os << " }";
86+
// Newline is intentionally missing, may need to print a comma before the
87+
// next entry.
88+
}
89+
os << "\n";
90+
os << "]\n";
91+
}

types/pair/write.C

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <ROOT/RField.hxx>
2+
#include <ROOT/RNTupleModel.hxx>
3+
#include <ROOT/RNTupleUtil.hxx>
4+
#include <ROOT/RNTupleWriteOptions.hxx>
5+
#include <ROOT/RNTupleWriter.hxx>
6+
7+
using ROOT::Experimental::EColumnType;
8+
using ROOT::Experimental::RField;
9+
using ROOT::Experimental::RNTupleModel;
10+
using ROOT::Experimental::RNTupleWriteOptions;
11+
using ROOT::Experimental::RNTupleWriter;
12+
13+
#include <cstdint>
14+
#include <memory>
15+
#include <string>
16+
#include <string_view>
17+
#include <utility>
18+
#include <vector>
19+
20+
using Vector = std::vector<std::int32_t>;
21+
22+
void write(std::string_view filename = "types.pair.root") {
23+
auto model = RNTupleModel::Create();
24+
25+
auto Int32_String =
26+
model->MakeField<std::pair<std::int32_t, std::string>>("Int32_String");
27+
auto Int32_Vector =
28+
model->MakeField<std::pair<std::int32_t, Vector>>("Int32_Vector");
29+
auto String_Vector =
30+
model->MakeField<std::pair<std::string, Vector>>("String_Vector");
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+
*Int32_Vector = {2, {1, 2, 3}};
40+
*String_Vector = {"def", {4, 5, 6}};
41+
writer->Fill();
42+
43+
// Second entry: zero / empty values
44+
*Int32_String = {0, ""};
45+
*Int32_Vector = {0, {}};
46+
*String_Vector = {"", {}};
47+
writer->Fill();
48+
}

0 commit comments

Comments
 (0)