Skip to content

Commit 2f88ff6

Browse files
committed
Add test for std::unordered_set
1 parent 1ccc287 commit 2f88ff6

File tree

11 files changed

+426
-0
lines changed

11 files changed

+426
-0
lines changed

types/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types
77
* [`set`](set): `std::set` with all `[Split]Index{32,64}` column types
88
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
9+
* [`unordered_set`](unordered_set): `std::unordered_set` with all `[Split]Index{32,64}` column types

types/unordered_set/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# `std::unordered_set`
2+
3+
* [`fundamental`](fundamental): `std::unordered_set<std::int32_t>`
4+
* [`nested`](nested): `std::unordered_set<std::unordered_set<std::int32_t>>`
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# `std::unordered_set<std::int32_t>`
2+
3+
## Fields
4+
5+
* `[Split]Index{32,64}`
6+
7+
with the corresponding column type for the offset column of the collection parent field.
8+
All child fields use the default column encoding `Int32`.
9+
10+
## Entries
11+
12+
1. Single-element sets, with ascending values
13+
2. Empty sets
14+
3. Increasing number of elements in the set:
15+
one element in the first field, two elements in the second field, etc.
16+
4. Duplicate elements passed to the set constructor
17+
5. Unordered elements passed to the set constructor
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 <unordered_set>
13+
14+
using UnorderedSet = std::unordered_set<std::int32_t>;
15+
16+
static void PrintUnorderedSetValue(const REntry &entry, std::string_view name,
17+
std::ostream &os, bool last = false) {
18+
UnorderedSet &value = *entry.GetPtr<UnorderedSet>(name);
19+
os << " \"" << name << "\": [";
20+
bool first = true;
21+
for (auto element : value) {
22+
if (first) {
23+
first = false;
24+
} else {
25+
os << ",";
26+
}
27+
os << "\n " << element;
28+
}
29+
if (!value.empty()) {
30+
os << "\n ";
31+
}
32+
os << "]";
33+
if (!last) {
34+
os << ",";
35+
}
36+
os << "\n";
37+
}
38+
39+
void read(std::string_view input = "types.unordered_set.fundamental.root",
40+
std::string_view output = "types.unordered_set.fundamental.json") {
41+
std::ofstream os(std::string{output});
42+
os << "[\n";
43+
44+
auto reader = RNTupleReader::Open("ntpl", input);
45+
auto &entry = reader->GetModel().GetDefaultEntry();
46+
bool first = true;
47+
for (auto index : *reader) {
48+
reader->LoadEntry(index);
49+
50+
if (first) {
51+
first = false;
52+
} else {
53+
os << ",\n";
54+
}
55+
os << " {\n";
56+
57+
PrintUnorderedSetValue(entry, "Index32", os);
58+
PrintUnorderedSetValue(entry, "Index64", os);
59+
PrintUnorderedSetValue(entry, "SplitIndex32", os);
60+
PrintUnorderedSetValue(entry, "SplitIndex64", os, /*last=*/true);
61+
62+
os << " }";
63+
// Newline is intentionally missing, may need to print a comma before the
64+
// next entry.
65+
}
66+
os << "\n";
67+
os << "]\n";
68+
}
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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_view>
16+
#include <unordered_set>
17+
18+
using UnorderedSet = std::unordered_set<std::int32_t>;
19+
20+
static std::shared_ptr<UnorderedSet>
21+
MakeUnorderedSetField(RNTupleModel &model, std::string_view name,
22+
EColumnType indexType) {
23+
auto field = std::make_unique<RField<UnorderedSet>>(name);
24+
field->SetColumnRepresentatives({{indexType}});
25+
model.AddField(std::move(field));
26+
return model.GetDefaultEntry().GetPtr<UnorderedSet>(name);
27+
}
28+
29+
void write(std::string_view filename = "types.unordered_set.fundamental.root") {
30+
auto model = RNTupleModel::Create();
31+
32+
// Non-split index encoding
33+
auto Index32 =
34+
MakeUnorderedSetField(*model, "Index32", EColumnType::kIndex32);
35+
auto Index64 =
36+
MakeUnorderedSetField(*model, "Index64", EColumnType::kIndex64);
37+
38+
// Split index encoding
39+
auto SplitIndex32 =
40+
MakeUnorderedSetField(*model, "SplitIndex32", EColumnType::kSplitIndex32);
41+
auto SplitIndex64 =
42+
MakeUnorderedSetField(*model, "SplitIndex64", EColumnType::kSplitIndex64);
43+
44+
RNTupleWriteOptions options;
45+
options.SetCompression(0);
46+
auto writer =
47+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
48+
49+
// First entry: single-element sets, with ascending values
50+
*Index32 = {1};
51+
*Index64 = {2};
52+
*SplitIndex32 = {3};
53+
*SplitIndex64 = {4};
54+
writer->Fill();
55+
56+
// Second entry: empty sets
57+
*Index32 = {};
58+
*Index64 = {};
59+
*SplitIndex32 = {};
60+
*SplitIndex64 = {};
61+
writer->Fill();
62+
63+
// Third entry: increasing number of elements in the set
64+
*Index32 = {1};
65+
*Index64 = {2, 3};
66+
*SplitIndex32 = {4, 5, 6};
67+
*SplitIndex64 = {7, 8, 9, 10};
68+
writer->Fill();
69+
70+
// Fourth entry: duplicate elements in the set
71+
*Index32 = {1, 1};
72+
*Index64 = {2, 2};
73+
*SplitIndex32 = {3, 3};
74+
*SplitIndex64 = {4, 4};
75+
writer->Fill();
76+
77+
// Fifth entry: unordered elements in the set
78+
*Index32 = {2, 1};
79+
*Index64 = {4, 3};
80+
*SplitIndex32 = {6, 5};
81+
*SplitIndex64 = {8, 7};
82+
writer->Fill();
83+
}

types/unordered_set/nested/LinkDef.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <cstdint>
2+
#include <unordered_set>
3+
4+
#ifdef __CLING__
5+
#pragma link off all globals;
6+
#pragma link off all classes;
7+
#pragma link off all functions;
8+
9+
#pragma link C++ nestedclasses;
10+
11+
#pragma link C++ class std::unordered_set<std::unordered_set<std::int32_t>>+;
12+
#endif

types/unordered_set/nested/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CXX=g++
2+
CXXFLAGS_ROOT=$(shell root-config --cflags)
3+
ifeq ($(CXXFLAGS_ROOT),)
4+
$(error cannot find root-config: make sure to source thisroot.sh)
5+
endif
6+
CXXFLAGS=-Wall $(CXXFLAGS_ROOT)
7+
LDFLAGS=$(shell root-config --libs)
8+
9+
.PHONY: all clean
10+
11+
all: NestedUnorderedSet.cxx libNestedUnorderedSet.so
12+
13+
NestedUnorderedSet.cxx: NestedUnorderedSet.hxx LinkDef.h
14+
rootcling -f $@ $^
15+
16+
libNestedUnorderedSet.so: NestedUnorderedSet.cxx
17+
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
18+
19+
clean:
20+
rm -f NestedUnorderedSet.cxx NestedUnorderedSet_rdict.pcm libNestedUnorderedSet.so
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <unordered_set>
5+
6+
template <> struct std::hash<std::unordered_set<std::int32_t>> {
7+
std::size_t
8+
operator()(const std::unordered_set<std::int32_t> &s) const noexcept {
9+
std::size_t h = 0;
10+
for (const auto &el : s) {
11+
h ^= std::hash<std::int32_t>{}(el);
12+
}
13+
return h;
14+
}
15+
};

types/unordered_set/nested/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# `std::unordered_set<std::unordered_set<std::int32_t>>`
2+
3+
## Fields
4+
5+
* `[Split]Index{32,64}`
6+
7+
with the corresponding column type for the offset column of the two collection parent fields.
8+
All child fields use the default column encoding `Int32`.
9+
10+
## Entries
11+
12+
1. Single-element sets, with ascending values
13+
2. Empty sets
14+
3. Increasing number of elements in the outer set, with arbitrary lengths of the inner sets
15+
4. Duplicate sets passed to the set constructor of the outer set
16+
5. Unordered sets (or arbitrary length) passed to the set constructor of the outer set
17+
18+
## Dictionaries
19+
20+
These tests require ROOT dictionaries, which can be generated with the provided `Makefile` in this directory. This will create a `libNestedUnorderedSet` shared object.

types/unordered_set/nested/read.C

+91
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 <TSystem.h>
8+
9+
#include <cstdint>
10+
#include <filesystem>
11+
#include <fstream>
12+
#include <ostream>
13+
#include <string>
14+
#include <string_view>
15+
#include <unordered_set>
16+
17+
using UnorderedSet = std::unordered_set<std::unordered_set<std::int32_t>>;
18+
19+
static void PrintNestedUnorderedSetValue(const REntry &entry,
20+
std::string_view name,
21+
std::ostream &os, bool last = false) {
22+
UnorderedSet &value = *entry.GetPtr<UnorderedSet>(name);
23+
os << " \"" << name << "\": [";
24+
bool outerFirst = true;
25+
for (auto inner : value) {
26+
if (outerFirst) {
27+
outerFirst = false;
28+
} else {
29+
os << ",";
30+
}
31+
os << "\n [";
32+
bool innerFirst = true;
33+
for (auto element : inner) {
34+
if (innerFirst) {
35+
innerFirst = false;
36+
} else {
37+
os << ",";
38+
}
39+
os << "\n " << element;
40+
}
41+
if (!inner.empty()) {
42+
os << "\n ";
43+
}
44+
os << "]";
45+
}
46+
if (!value.empty()) {
47+
os << "\n ";
48+
}
49+
os << "]";
50+
if (!last) {
51+
os << ",";
52+
}
53+
os << "\n";
54+
}
55+
56+
void read(std::string_view input = "types.unordered_set.nested.root",
57+
std::string_view output = "types.unordered_set.nested.json") {
58+
if (!std::filesystem::exists("libNestedUnorderedSet.so")) {
59+
throw std::runtime_error("could not find the required ROOT dictionaries, "
60+
"please make sure to run `make` first");
61+
}
62+
gSystem->Load("libNestedUnorderedSet");
63+
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+
PrintNestedUnorderedSetValue(entry, "Index32", os);
81+
PrintNestedUnorderedSetValue(entry, "Index64", os);
82+
PrintNestedUnorderedSetValue(entry, "SplitIndex32", os);
83+
PrintNestedUnorderedSetValue(entry, "SplitIndex64", os, /*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+
}

0 commit comments

Comments
 (0)