Skip to content

Commit 2c882bb

Browse files
committed
Add test for std::multimap
1 parent a61ae51 commit 2c882bb

11 files changed

Lines changed: 379 additions & 0 deletions

File tree

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [`bitset`](bitset): `std::bitset`
66
* [`fundamental`](fundamental): fundamental column types
77
* [`map`](map): `std::map` with all `[Split]Index{32,64}` column types
8+
* [`multimap`](multimap): `std::multimap` with all `[Split]Index{32,64}` column types
89
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
910
* [`optional`](optional): `std::optional` with different element types
1011
* [`pair`](pair): `std::pair` with different element types

types/multimap/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# `std::multimap`
2+
3+
- [`fundamental`](fundamental): `std::multimap<std::string, ::int32_t>`
4+
- [`nested`](nested): `std::multimap<std::string, std::multimap<std::string, std::int32_t>>`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::map<std::string, 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 types for `std::string` (`SplitIndex64` for the principal column, `Char` for the second column) and `std::int32_t` (`SplitInt32`).
9+
10+
## Entries
11+
12+
1. Single-element maps, with ascending values for both key and value
13+
2. Empty maps
14+
3. Increasing number of elements in the map:
15+
one key-value pair in the first field, two key-value pairs in the second field, etc.

types/multimap/fundamental/read.C

Lines changed: 68 additions & 0 deletions
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 <map>
11+
#include <string>
12+
#include <string_view>
13+
14+
using Multimap = std::multimap<std::string, std::int32_t>;
15+
16+
static void PrintMultimapValue(const REntry &entry, std::string_view name,
17+
std::ostream &os, bool last = false) {
18+
Multimap &item = *entry.GetPtr<Multimap>(name);
19+
os << " \"" << name << "\": {";
20+
bool first = true;
21+
for (auto &[key, value] : item) {
22+
if (first) {
23+
first = false;
24+
} else {
25+
os << ",";
26+
}
27+
os << "\n \"" << key << "\": " << value;
28+
}
29+
if (!item.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.multimap.fundamental.root",
40+
std::string_view output = "types.multimap.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+
PrintMultimapValue(entry, "Index32", os);
58+
PrintMultimapValue(entry, "Index64", os);
59+
PrintMultimapValue(entry, "SplitIndex32", os);
60+
PrintMultimapValue(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+
}

types/multimap/fundamental/write.C

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 <map>
15+
#include <memory>
16+
#include <string_view>
17+
18+
using Multimap = std::multimap<std::string, std::int32_t>;
19+
20+
static std::shared_ptr<Multimap> MakeMultimapField(RNTupleModel &model,
21+
std::string_view name,
22+
EColumnType indexType) {
23+
auto field = std::make_unique<RField<Multimap>>(name);
24+
field->SetColumnRepresentatives({{indexType}});
25+
model.AddField(std::move(field));
26+
return model.GetDefaultEntry().GetPtr<Multimap>(name);
27+
}
28+
29+
void write(std::string_view filename = "types.multimap.fundamental.root") {
30+
auto model = RNTupleModel::Create();
31+
32+
// Non-split index encoding
33+
auto Index32 = MakeMultimapField(*model, "Index32", EColumnType::kIndex32);
34+
auto Index64 = MakeMultimapField(*model, "Index64", EColumnType::kIndex64);
35+
36+
// Split index encoding
37+
auto SplitIndex32 =
38+
MakeMultimapField(*model, "SplitIndex32", EColumnType::kSplitIndex32);
39+
auto SplitIndex64 =
40+
MakeMultimapField(*model, "SplitIndex64", EColumnType::kSplitIndex64);
41+
42+
RNTupleWriteOptions options;
43+
options.SetCompression(0);
44+
auto writer =
45+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
46+
47+
// First entry: single-element maps, with ascending values
48+
*Index32 = {{"a", 1}};
49+
*Index64 = {{"b", 2}};
50+
*SplitIndex32 = {{"c", 3}};
51+
*SplitIndex64 = {{"d", 4}};
52+
writer->Fill();
53+
54+
// Second entry: empty maps
55+
*Index32 = {};
56+
*Index64 = {};
57+
*SplitIndex32 = {};
58+
*SplitIndex64 = {};
59+
writer->Fill();
60+
61+
// Third entry: increasing number of elements in the map
62+
*Index32 = {{"a", 1}};
63+
*Index64 = {{"b", 2}, {"c", 3}};
64+
*SplitIndex32 = {{"d", 4}, {"e", 5}, {"f", 6}};
65+
*SplitIndex64 = {{"g", 7}, {"h", 8}, {"i", 9}, {"j", 10}};
66+
writer->Fill();
67+
}

types/multimap/nested/LinkDef.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <cstdint>
2+
#include <map>
3+
4+
#ifdef __CLING__
5+
#pragma link C++ class std::multimap<std::string, std::multimap<std::string, std::int32_t>>+;
6+
#endif

types/multimap/nested/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
ROOT_CONFIG ?= $(shell which root-config)
2+
ifeq ($(ROOT_CONFIG),)
3+
$(error Could not find root-config)
4+
endif
5+
ROOTCLING ?= $(shell which rootcling)
6+
ifeq ($(ROOTCLING),)
7+
$(error Could not find rootcling)
8+
endif
9+
10+
CXX := $(shell $(ROOT_CONFIG) --cxx)
11+
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
12+
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
13+
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)
14+
15+
.PHONY: all clean
16+
17+
all: libNestedMultimap.so
18+
19+
NestedMultimap.cxx: NestedMultimap.hxx LinkDef.h
20+
$(ROOTCLING) -f $@ $^
21+
22+
libNestedMultimap.so: NestedMultimap.cxx
23+
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
24+
25+
clean:
26+
$(RM) NestedMultimap.cxx NestedMultimap_rdict.pcm libNestedMultimap.so
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <map>

types/multimap/nested/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `std::multimap<std::string, std::multimap<std::string, 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 types for `std::string` (`SplitIndex64` for the principal column, `Char` for the second column) and `std::int32_t` (`SplitInt32`).
9+
10+
## Entries
11+
12+
1. Single-element maps, with ascending values for both key and value
13+
2. Empty maps
14+
3. Increasing number of elements in the outer map, with arbitrary lengths of the inner maps
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 `libNestedMultimap` shared object.

types/multimap/nested/read.C

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 <map>
13+
#include <ostream>
14+
#include <string>
15+
#include <string_view>
16+
17+
using Multimap =
18+
std::multimap<std::string, std::multimap<std::string, std::int32_t>>;
19+
20+
static void PrintNestedMultimapValue(const REntry &entry, std::string_view name,
21+
std::ostream &os, bool last = false) {
22+
Multimap &item = *entry.GetPtr<Multimap>(name);
23+
os << " \"" << name << "\": {";
24+
bool outerFirst = true;
25+
for (auto &[key, inner] : item) {
26+
if (outerFirst) {
27+
outerFirst = false;
28+
} else {
29+
os << ",";
30+
}
31+
os << "\n \"" << key << "\": {";
32+
bool innerFirst = true;
33+
for (auto &[innerKey, value] : inner) {
34+
if (innerFirst) {
35+
innerFirst = false;
36+
} else {
37+
os << ",";
38+
}
39+
os << "\n \"" << innerKey << "\": " << value;
40+
}
41+
if (!inner.empty()) {
42+
os << "\n ";
43+
}
44+
os << "}";
45+
}
46+
if (!item.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.multimap.nested.root",
57+
std::string_view output = "types.multimap.nested.json") {
58+
if (gSystem->Load("libNestedMultimap") == -1)
59+
throw std::runtime_error("could not find the required ROOT dictionaries, "
60+
"please make sure to run `make` first");
61+
62+
std::ofstream os(std::string{output});
63+
os << "[\n";
64+
65+
auto reader = RNTupleReader::Open("ntpl", input);
66+
auto &entry = reader->GetModel().GetDefaultEntry();
67+
bool first = true;
68+
for (auto index : *reader) {
69+
reader->LoadEntry(index);
70+
71+
if (first) {
72+
first = false;
73+
} else {
74+
os << ",\n";
75+
}
76+
os << " {\n";
77+
78+
PrintNestedMultimapValue(entry, "Index32", os);
79+
PrintNestedMultimapValue(entry, "Index64", os);
80+
PrintNestedMultimapValue(entry, "SplitIndex32", os);
81+
PrintNestedMultimapValue(entry, "SplitIndex64", os, /*last=*/true);
82+
83+
os << " }";
84+
// Newline is intentionally missing, may need to print a comma before the
85+
// next entry.
86+
}
87+
os << "\n";
88+
os << "]\n";
89+
}

0 commit comments

Comments
 (0)