An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Poly map

[![test](https://github.com/andreiavrammsd/polymap/workflows/test/badge.svg)](https://github.com/andreiavrammsd/polymap/actions/workflows/cmake.yml)

[![bazel](https://github.com/andreiavrammsd/polymap/workflows/bazel/badge.svg)](https://github.com/andreiavrammsd/polymap/actions/workflows/bazel.yml)

[![codecov](https://codecov.io/github/andreiavrammsd/polymap/graph/badge.svg?token=CKSCACRJXC)](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).