mirror of
https://github.com/boostorg/hana.git
synced 2026-02-01 08:32:11 +00:00
[Map] Provide model of Foldable, improve docs and add insert method.
Fixes #41
This commit is contained in:
114
example/map.cpp
114
example/map.cpp
@@ -6,10 +6,12 @@ Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
#include <boost/hana/assert.hpp>
|
||||
#include <boost/hana/config.hpp>
|
||||
#include <boost/hana/functional/placeholder.hpp>
|
||||
#include <boost/hana/integral_constant.hpp>
|
||||
#include <boost/hana/map.hpp>
|
||||
#include <boost/hana/maybe.hpp>
|
||||
#include <boost/hana/pair.hpp>
|
||||
#include <boost/hana/set.hpp>
|
||||
#include <boost/hana/type.hpp>
|
||||
|
||||
#include <string>
|
||||
@@ -22,6 +24,7 @@ int main() {
|
||||
|
||||
//! [make<Map>]
|
||||
using namespace std::literals;
|
||||
|
||||
auto m = make<Map>(
|
||||
make<Pair>(int_<1>, "foobar"s),
|
||||
make<Pair>(type<void>, 1234)
|
||||
@@ -33,6 +36,7 @@ auto m = make<Map>(
|
||||
|
||||
//! [make_map]
|
||||
using namespace std::literals;
|
||||
|
||||
BOOST_HANA_RUNTIME_CHECK(
|
||||
make_map(
|
||||
make<Pair>(int_<1>, "foobar"s),
|
||||
@@ -48,8 +52,76 @@ BOOST_HANA_RUNTIME_CHECK(
|
||||
|
||||
}{
|
||||
|
||||
//! [comparable]
|
||||
//! [values]
|
||||
using namespace std::literals;
|
||||
|
||||
auto m = make<Map>(
|
||||
make<Pair>(int_<1>, "foobar"s),
|
||||
make<Pair>(type<void>, 1234)
|
||||
);
|
||||
|
||||
// The order of the values is unspecified.
|
||||
BOOST_HANA_RUNTIME_CHECK(values(m) ^in^ permutations(make<Tuple>("foobar"s, 1234)));
|
||||
//! [values]
|
||||
|
||||
}{
|
||||
|
||||
//! [keys]
|
||||
using namespace std::literals;
|
||||
|
||||
auto m = make<Map>(
|
||||
make<Pair>(int_<1>, "foobar"s),
|
||||
make<Pair>(type<void>, 1234)
|
||||
);
|
||||
|
||||
// The order of the keys is unspecified.
|
||||
BOOST_HANA_CONSTANT_CHECK(keys(m) ^in^ permutations(make<Tuple>(int_<1>, type<void>)));
|
||||
//! [keys]
|
||||
|
||||
}{
|
||||
|
||||
//! [from_Foldable]
|
||||
using namespace std::literals;
|
||||
|
||||
BOOST_HANA_RUNTIME_CHECK(
|
||||
to<Map>(make<Tuple>(
|
||||
make<Pair>(type<int>, "abcd"s),
|
||||
make<Pair>(type<void>, 1234),
|
||||
make<Pair>(type<int>, nullptr)
|
||||
)) == make<Map>(
|
||||
make<Pair>(type<int>, "abcd"s),
|
||||
make<Pair>(type<void>, 1234)
|
||||
)
|
||||
);
|
||||
//! [from_Foldable]
|
||||
|
||||
}{
|
||||
|
||||
//! [insert]
|
||||
using namespace std::literals;
|
||||
|
||||
auto m = make<Map>(
|
||||
make<Pair>(type<int>, "abcd"s),
|
||||
make<Pair>(type<void>, 1234)
|
||||
);
|
||||
|
||||
BOOST_HANA_RUNTIME_CHECK(
|
||||
insert(m, make<Pair>(type<float>, 'x')) ==
|
||||
make<Map>(
|
||||
make<Pair>(type<int>, "abcd"s),
|
||||
make<Pair>(type<void>, 1234),
|
||||
make<Pair>(type<float>, 'x')
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_HANA_RUNTIME_CHECK(insert(m, make<Pair>(type<void>, 'x')) == m);
|
||||
//! [insert]
|
||||
|
||||
}{
|
||||
|
||||
//! [Comparable]
|
||||
using namespace std::literals;
|
||||
|
||||
BOOST_HANA_RUNTIME_CHECK(
|
||||
make_map(
|
||||
make_pair(char_<'a'>, "foobar"s),
|
||||
@@ -61,20 +133,48 @@ BOOST_HANA_RUNTIME_CHECK(
|
||||
make_pair(char_<'a'>, "foobar"s)
|
||||
)
|
||||
);
|
||||
//! [comparable]
|
||||
//! [Comparable]
|
||||
|
||||
}{
|
||||
|
||||
//! [searchable]
|
||||
BOOST_HANA_CONSTEXPR_LAMBDA auto m = make_map(
|
||||
//! [Searchable]
|
||||
constexpr auto m = make_map(
|
||||
make_pair(type<int>, 'i'),
|
||||
make_pair(type<float>, 'f')
|
||||
);
|
||||
BOOST_HANA_CONSTEXPR_CHECK(find(m, type<int>) == just('i'));
|
||||
BOOST_HANA_CONSTEXPR_CHECK(find(m, type<float>) == just('f'));
|
||||
static_assert(find(m, type<int>) == just('i'), "");
|
||||
static_assert(find(m, type<float>) == just('f'), "");
|
||||
BOOST_HANA_CONSTANT_CHECK(find(m, type<void>) == nothing);
|
||||
BOOST_HANA_CONSTANT_CHECK(find(m, int_<3>) == nothing);
|
||||
//! [searchable]
|
||||
//! [Searchable]
|
||||
|
||||
}{
|
||||
|
||||
//! [Foldable]
|
||||
using namespace std::literals;
|
||||
|
||||
// Given a map of (key, value) pairs, returns a map of (value, key) pairs.
|
||||
// This requires both the keys and the values to be compile-time Comparable.
|
||||
auto invert = [](auto map) {
|
||||
return fold.left(map, make_map(), [](auto map, auto pair) {
|
||||
return insert(map, make_pair(second(pair), first(pair)));
|
||||
});
|
||||
};
|
||||
|
||||
auto m = make_map(
|
||||
make_pair(type<int>, int_<1>),
|
||||
make_pair(type<float>, int_<2>),
|
||||
make_pair(int_<3>, type<void>)
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(invert(m) ==
|
||||
make_map(
|
||||
make_pair(int_<1>, type<int>),
|
||||
make_pair(int_<2>, type<float>),
|
||||
make_pair(type<void>, int_<3>)
|
||||
)
|
||||
);
|
||||
//! [Foldable]
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user