https://github.com/andreiavrammsd/polymap
Map with infinite levels and multiple types for keys and values.
https://github.com/andreiavrammsd/polymap
cpp map polymorphic recursion tree
Last synced: 9 months ago
JSON representation
Map with infinite levels and multiple types for keys and values.
- Host: GitHub
- URL: https://github.com/andreiavrammsd/polymap
- Owner: andreiavrammsd
- License: mit
- Created: 2021-01-23T19:11:21.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-07T10:15:26.000Z (over 1 year ago)
- Last Synced: 2025-05-07T04:45:45.946Z (9 months ago)
- Topics: cpp, map, polymorphic, recursion, tree
- Language: C++
- Homepage:
- Size: 63.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Poly map
[](https://github.com/andreiavrammsd/polymap/actions/workflows/cmake.yml)
[](https://github.com/andreiavrammsd/polymap/actions/workflows/bazel.yml)
[](https://codecov.io/github/andreiavrammsd/polymap)
### Polymorphic map container.
A recursive map that can have any shape and can hold multiple types for keys and values.
* Header-only library.
* Key types are provided at construct time as template arguments. They must meet the type conditions
for [std::variant](https://en.cppreference.com/w/cpp/utility/variant).
* Value types must be copy constructible as the map takes ownership on the objects assigned.
* Can be visualised as a tree.
## Requirements
* C++17
* CMake 3.17+ or Bazel
## Usage
```c++
#include
#include
#include
#include
#include
struct visitor {
template
bool operator()(const double key, V&, M&)
{
std::cout << "double = " << key << "\n";
return true;
}
template
bool operator()(K& key, V&, M&)
{
std::cout << "other = " << key << "\n";
return true;
}
};
int main() {
msd::poly_map map;
map[1] = 22;
map.at(1) = 23;
map[1] = std::string{"a"};
map[1][2] = 9;
map.at(1, 2) = 8;
map[1][2][3.1] = 1;
map[1][2][3.1]["f"] = 199;
map[1][2][3.1][4.2]["g"] = std::make_pair(1, 2);
assert(map[1][2].get() == 8);
map.for_each(visitor{});
assert(!map[1].empty());
assert(map.size() == 6);
assert(map[1].size() == 5);
assert(map.contains(1, 2, 3.1));
map[1][2].clear();
map.clear();
}
```
See [tests](tests/poly_map_test.cpp).