Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* [`atomic`](atomic): `std::atomic`
* [`bitset`](bitset): `std::bitset`
* [`fundamental`](fundamental): fundamental column types
* [`map`](map): `std::map` with all `[Split]Index{32,64}` column types
* [`multimap`](multimap): `std::multimap` with all `[Split]Index{32,64}` column types
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
* [`optional`](optional): `std::optional` with different element types
* [`pair`](pair): `std::pair` with different element types
Expand All @@ -12,6 +14,8 @@
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
* [`tuple`](tuple): `std::tuple` with different element types
* [`unique_ptr`](unique_ptr): `std::unique_ptr` with different element types
* [`unordered_map`](unordered_map): `std::unordered_map` with all `[Split]Index{32,64}` column types
* [`unordered_multimap`](unordered_multimap): `std::unordered_multimap` with all `[Split]Index{32,64}` column types
* [`unordered_multiset`](unordered_multiset): `std::unordered_multiset` with all `[Split]Index{32,64}` column types
* [`unordered_set`](unordered_set): `std::unordered_set` with all `[Split]Index{32,64}` column types
* [`user`](user): user-defined types, such as classes and enums
Expand Down Expand Up @@ -39,8 +43,8 @@ If `C` is a container type with a single item field, we test the following nesti
* `C<C<std::int32_t>>`

If `M` is an dictionary container type, we test the following nestings:
* `M<std::int32_t, std::int32_t>`
* `M<M<std::int32_t, std::int32_t>, M<std::int32_t, std::int32_t>>`
* `M<std::string, std::int32_t>`
* `M<M<std::string, std::int32_t>, M<std::string, std::int32_t>>`

### Record Fields

Expand Down
4 changes: 4 additions & 0 deletions types/map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# `std::map`

- [`fundamental`](fundamental): `std::map<std::string, ::int32_t>`
- [`nested`](nested): `std::map<std::string, std::map<std::string, std::int32_t>>`
15 changes: 15 additions & 0 deletions types/map/fundamental/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `std::map<std::string, std::int32_t>`

## Fields

- `[Split]Index{32,64}`

with the corresponding column type for the offset column of the collection parent field.
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`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? Since we are writing without compression, the defaults should be Index64 and Int32 I believe


## Entries

1. Single-element maps, with ascending values for both key and value
2. Empty maps
3. Increasing number of elements in the map:
one key-value pair in the first field, two key-value pairs in the second field, etc.
68 changes: 68 additions & 0 deletions types/map/fundamental/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <map>
#include <string>
#include <string_view>

using Map = std::map<std::string, std::int32_t>;

static void PrintMapValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
Map &item = *entry.GetPtr<Map>(name);
os << " \"" << name << "\": {";
bool first = true;
for (auto &[key, value] : item) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n \"" << key << "\": " << value;
}
if (!item.empty()) {
os << "\n ";
}
os << "}";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.map.fundamental.root",
std::string_view output = "types.map.fundamental.json") {
std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintMapValue(entry, "Index32", os);
PrintMapValue(entry, "Index64", os);
PrintMapValue(entry, "SplitIndex32", os);
PrintMapValue(entry, "SplitIndex64", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
67 changes: 67 additions & 0 deletions types/map/fundamental/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <ROOT/RField.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleUtil.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>

using ROOT::Experimental::EColumnType;
using ROOT::Experimental::RField;
using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;

#include <cstdint>
#include <memory>
#include <map>
#include <string_view>

using Map = std::map<std::string, std::int32_t>;

static std::shared_ptr<Map> MakeMapField(RNTupleModel &model,
std::string_view name,
EColumnType indexType) {
auto field = std::make_unique<RField<Map>>(name);
field->SetColumnRepresentatives({{indexType}});
model.AddField(std::move(field));
return model.GetDefaultEntry().GetPtr<Map>(name);
}

void write(std::string_view filename = "types.map.fundamental.root") {
auto model = RNTupleModel::Create();

// Non-split index encoding
auto Index32 = MakeMapField(*model, "Index32", EColumnType::kIndex32);
auto Index64 = MakeMapField(*model, "Index64", EColumnType::kIndex64);

// Split index encoding
auto SplitIndex32 =
MakeMapField(*model, "SplitIndex32", EColumnType::kSplitIndex32);
auto SplitIndex64 =
MakeMapField(*model, "SplitIndex64", EColumnType::kSplitIndex64);

RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);

// First entry: single-element maps, with ascending values
*Index32 = {{"a", 1}};
*Index64 = {{"b", 2}};
*SplitIndex32 = {{"c", 3}};
*SplitIndex64 = {{"d", 4}};
writer->Fill();

// Second entry: empty maps
*Index32 = {};
*Index64 = {};
*SplitIndex32 = {};
*SplitIndex64 = {};
writer->Fill();

// Third entry: increasing number of elements in the map
*Index32 = {{"a", 1}};
*Index64 = {{"b", 2}, {"c", 3}};
*SplitIndex32 = {{"d", 4}, {"e", 5}, {"f", 6}};
*SplitIndex64 = {{"g", 7}, {"h", 8}, {"i", 9}, {"j", 10}};
writer->Fill();
}
6 changes: 6 additions & 0 deletions types/map/nested/LinkDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <cstdint>
#include <map>

#ifdef __CLING__
#pragma link C++ class std::map<std::string, std::map<std::string, std::int32_t>>+;
#endif
26 changes: 26 additions & 0 deletions types/map/nested/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ROOT_CONFIG ?= $(shell which root-config)
ifeq ($(ROOT_CONFIG),)
$(error Could not find root-config)
endif
ROOTCLING ?= $(shell which rootcling)
ifeq ($(ROOTCLING),)
$(error Could not find rootcling)
endif

CXX := $(shell $(ROOT_CONFIG) --cxx)
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)

.PHONY: all clean

all: libNestedMap.so

NestedMap.cxx: NestedMap.hxx LinkDef.h
$(ROOTCLING) -f $@ $^

libNestedMap.so: NestedMap.cxx
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

clean:
$(RM) NestedMap.cxx NestedMap_rdict.pcm libNestedMap.so
4 changes: 4 additions & 0 deletions types/map/nested/NestedMap.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#include <cstdint>
#include <map>
18 changes: 18 additions & 0 deletions types/map/nested/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `std::map<std::string, std::map<std::string, std::int32_t>>`

## Fields

- `[Split]Index{32,64}`

with the corresponding column type for the offset column of the two collection parent fields.
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`).

## Entries

1. Single-element maps, with ascending values for both key and value
2. Empty maps
3. Increasing number of elements in the outer map, with arbitrary lengths of the inner maps

## Dictionaries

These tests require ROOT dictionaries, which can be generated with the provided `Makefile` in this directory. This will create a `libNestedMap` shared object.
88 changes: 88 additions & 0 deletions types/map/nested/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <TSystem.h>

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <map>
#include <ostream>
#include <string>
#include <string_view>

using Map = std::map<std::string, std::map<std::string, std::int32_t>>;

static void PrintNestedMapValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
Map &item = *entry.GetPtr<Map>(name);
os << " \"" << name << "\": {";
bool outerFirst = true;
for (auto &[key, inner] : item) {
if (outerFirst) {
outerFirst = false;
} else {
os << ",";
}
os << "\n \"" << key << "\": {";
bool innerFirst = true;
for (auto &[innerKey, value] : inner) {
if (innerFirst) {
innerFirst = false;
} else {
os << ",";
}
os << "\n \"" << innerKey << "\": " << value;
}
if (!inner.empty()) {
os << "\n ";
}
os << "}";
}
if (!item.empty()) {
os << "\n ";
}
os << "}";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.map.nested.root",
std::string_view output = "types.map.nested.json") {
if (gSystem->Load("libNestedMap") == -1)
throw std::runtime_error("could not find the required ROOT dictionaries, "
"please make sure to run `make` first");

std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintNestedMapValue(entry, "Index32", os);
PrintNestedMapValue(entry, "Index64", os);
PrintNestedMapValue(entry, "SplitIndex32", os);
PrintNestedMapValue(entry, "SplitIndex64", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
Loading