Skip to content

Commit 0cd7d4d

Browse files
committed
Add test for std::unordered_multiset
1 parent 797711c commit 0cd7d4d

File tree

11 files changed

+411
-0
lines changed

11 files changed

+411
-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
* [`set`](set): `std::set` with all `[Split]Index{32,64}` column types
77
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
88
* [`unique_ptr`](unique_ptr): `std::unique_ptr` with different element types
9+
* [`unordered_multiset`](unordered_multiset): `std::unordered_multiset` with all `[Split]Index{32,64}` column types
910
* [`unordered_set`](unordered_set): `std::unordered_set` with all `[Split]Index{32,64}` column types
1011
* [`variant`](variant): `std::variant` with `Switch` column type
1112
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

types/unordered_multiset/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# `std::unordered_multiset`
2+
3+
* [`fundamental`](fundamental): `std::unordered_multiset<std::int32_t>`
4+
* [`nested`](nested): `std::unordered_multiset<std::unordered_multiset<std::int32_t>>`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::unordered_multiset<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.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 UnorderedMultiSet = std::unordered_multiset<std::int32_t>;
15+
16+
static void PrintUnorderedMultiSetValue(const REntry &entry,
17+
std::string_view name, std::ostream &os,
18+
bool last = false) {
19+
UnorderedMultiSet &value = *entry.GetPtr<UnorderedMultiSet>(name);
20+
21+
std::vector<UnorderedMultiSet::value_type> valueSorted(value.begin(), value.end());
22+
std::sort(valueSorted.begin(), valueSorted.end());
23+
24+
os << " \"" << name << "\": [";
25+
bool first = true;
26+
for (auto element : valueSorted) {
27+
if (first) {
28+
first = false;
29+
} else {
30+
os << ",";
31+
}
32+
os << "\n " << element;
33+
}
34+
if (!valueSorted.empty()) {
35+
os << "\n ";
36+
}
37+
os << "]";
38+
if (!last) {
39+
os << ",";
40+
}
41+
os << "\n";
42+
}
43+
44+
void read(
45+
std::string_view input = "types.unordered_multiset.fundamental.root",
46+
std::string_view output = "types.unordered_multiset.fundamental.json") {
47+
std::ofstream os(std::string{output});
48+
os << "[\n";
49+
50+
auto reader = RNTupleReader::Open("ntpl", input);
51+
auto &entry = reader->GetModel().GetDefaultEntry();
52+
bool first = true;
53+
for (auto index : *reader) {
54+
reader->LoadEntry(index);
55+
56+
if (first) {
57+
first = false;
58+
} else {
59+
os << ",\n";
60+
}
61+
os << " {\n";
62+
63+
PrintUnorderedMultiSetValue(entry, "Index32", os);
64+
PrintUnorderedMultiSetValue(entry, "Index64", os);
65+
PrintUnorderedMultiSetValue(entry, "SplitIndex32", os);
66+
PrintUnorderedMultiSetValue(entry, "SplitIndex64", os, /*last=*/true);
67+
68+
os << " }";
69+
// Newline is intentionally missing, may need to print a comma before the
70+
// next entry.
71+
}
72+
os << "\n";
73+
os << "]\n";
74+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 UnorderedMultiSet = std::unordered_multiset<std::int32_t>;
19+
20+
static std::shared_ptr<UnorderedMultiSet>
21+
MakeUnorderedMultiSetField(RNTupleModel &model, std::string_view name,
22+
EColumnType indexType) {
23+
auto field = std::make_unique<RField<UnorderedMultiSet>>(name);
24+
field->SetColumnRepresentatives({{indexType}});
25+
model.AddField(std::move(field));
26+
return model.GetDefaultEntry().GetPtr<UnorderedMultiSet>(name);
27+
}
28+
29+
void write(
30+
std::string_view filename = "types.unordered_multiset.fundamental.root") {
31+
auto model = RNTupleModel::Create();
32+
33+
// Non-split index encoding
34+
auto Index32 =
35+
MakeUnorderedMultiSetField(*model, "Index32", EColumnType::kIndex32);
36+
auto Index64 =
37+
MakeUnorderedMultiSetField(*model, "Index64", EColumnType::kIndex64);
38+
39+
// Split index encoding
40+
auto SplitIndex32 = MakeUnorderedMultiSetField(*model, "SplitIndex32",
41+
EColumnType::kSplitIndex32);
42+
auto SplitIndex64 = MakeUnorderedMultiSetField(*model, "SplitIndex64",
43+
EColumnType::kSplitIndex64);
44+
45+
RNTupleWriteOptions options;
46+
options.SetCompression(0);
47+
auto writer =
48+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
49+
50+
// First entry: single-element sets, with ascending values
51+
*Index32 = {1};
52+
*Index64 = {2};
53+
*SplitIndex32 = {3};
54+
*SplitIndex64 = {4};
55+
writer->Fill();
56+
57+
// Second entry: empty sets
58+
*Index32 = {};
59+
*Index64 = {};
60+
*SplitIndex32 = {};
61+
*SplitIndex64 = {};
62+
writer->Fill();
63+
64+
// Third entry: increasing number of elements in the set
65+
*Index32 = {1};
66+
*Index64 = {2, 3};
67+
*SplitIndex32 = {4, 5, 6};
68+
*SplitIndex64 = {7, 8, 9, 10};
69+
writer->Fill();
70+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <cstdint>
2+
#include <unordered_set>
3+
4+
#ifdef __CLING__
5+
#pragma link C++ class std::unordered_multiset<std::unordered_multiset<std::int32_t>>+;
6+
#endif
Lines changed: 20 additions & 0 deletions
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: NestedUnorderedMultiset.cxx libNestedUnorderedMultiset.so
12+
13+
NestedUnorderedMultiset.cxx: NestedUnorderedMultiset.hxx LinkDef.h
14+
rootcling -f $@ $^
15+
16+
libNestedUnorderedMultiset.so: NestedUnorderedMultiset.cxx
17+
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
18+
19+
clean:
20+
rm -f NestedUnorderedMultiset.cxx NestedUnorderedMultiset_rdict.pcm libNestedUnorderedMultiset.so
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <unordered_set>
5+
6+
template<>
7+
struct std::hash<std::unordered_multiset<std::int32_t>> {
8+
std::size_t
9+
operator()(const std::unordered_multiset<std::int32_t> &s) const noexcept {
10+
std::size_t h = 0;
11+
for (const auto &el : s) {
12+
h ^= std::hash<std::int32_t>{}(el);
13+
}
14+
return h;
15+
}
16+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `std::unordered_multiset<std::unordered_multiset<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+
16+
## Dictionaries
17+
18+
These tests require ROOT dictionaries, which can be generated with the provided `Makefile` in this directory. This will create a `libNestedUnorderedMultiset` shared object.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 UnorderedMultiset =
18+
std::unordered_multiset<std::unordered_multiset<std::int32_t>>;
19+
20+
static void PrintNestedUnorderedMultisetValue(const REntry &entry,
21+
std::string_view name,
22+
std::ostream &os,
23+
bool last = false) {
24+
UnorderedMultiset &value = *entry.GetPtr<UnorderedMultiset>(name);
25+
26+
std::vector<UnorderedMultiset::value_type> valueInnerSorted(value.begin(),
27+
value.end());
28+
std::vector<std::vector<UnorderedMultiset::value_type::value_type>>
29+
valueSorted;
30+
31+
for (auto inner : value) {
32+
std::vector innerSorted(inner.begin(), inner.end());
33+
std::sort(innerSorted.begin(), innerSorted.end());
34+
valueSorted.push_back(innerSorted);
35+
}
36+
37+
os << " \"" << name << "\": [";
38+
bool outerFirst = true;
39+
for (auto inner : valueSorted) {
40+
if (outerFirst) {
41+
outerFirst = false;
42+
} else {
43+
os << ",";
44+
}
45+
os << "\n [";
46+
bool innerFirst = true;
47+
for (auto element : inner) {
48+
if (innerFirst) {
49+
innerFirst = false;
50+
} else {
51+
os << ",";
52+
}
53+
os << "\n " << element;
54+
}
55+
if (!inner.empty()) {
56+
os << "\n ";
57+
}
58+
os << "]";
59+
}
60+
if (!valueSorted.empty()) {
61+
os << "\n ";
62+
}
63+
os << "]";
64+
if (!last) {
65+
os << ",";
66+
}
67+
os << "\n";
68+
}
69+
70+
void read(std::string_view input = "types.unordered_multiset.nested.root",
71+
std::string_view output = "types.unordered_multiset.nested.json") {
72+
if (!std::filesystem::exists("libNestedUnorderedMultiset.so")) {
73+
throw std::runtime_error("could not find the required ROOT dictionaries, "
74+
"please make sure to run `make` first");
75+
}
76+
gSystem->Load("libNestedUnorderedMultiset");
77+
78+
std::ofstream os(std::string{output});
79+
os << "[\n";
80+
81+
auto reader = RNTupleReader::Open("ntpl", input);
82+
auto &entry = reader->GetModel().GetDefaultEntry();
83+
bool first = true;
84+
for (auto index : *reader) {
85+
reader->LoadEntry(index);
86+
87+
if (first) {
88+
first = false;
89+
} else {
90+
os << ",\n";
91+
}
92+
os << " {\n";
93+
94+
PrintNestedUnorderedMultisetValue(entry, "Index32", os);
95+
PrintNestedUnorderedMultisetValue(entry, "Index64", os);
96+
PrintNestedUnorderedMultisetValue(entry, "SplitIndex32", os);
97+
PrintNestedUnorderedMultisetValue(entry, "SplitIndex64", os, /*last=*/true);
98+
99+
os << " }";
100+
// Newline is intentionally missing, may need to print a comma before the
101+
// next entry.
102+
}
103+
os << "\n";
104+
os << "]\n";
105+
}

0 commit comments

Comments
 (0)