-
Notifications
You must be signed in to change notification settings - Fork 4
Add tests for std::[unordered_][multi]map
#62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
enirolf
wants to merge
4
commits into
root-project:main
Choose a base branch
from
enirolf:types-maps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>>` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`). | ||
|
|
||
| ## 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <map> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Index64andInt32I believe