/* @copyright Louis Dionne 2015 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #include #include #include #include #include #include #include #include #include #include using namespace boost::hana; int main() { { //! [make] using namespace std::literals; auto m = make( make(int_<1>, "foobar"s), make(type, 1234) ); //! [make] (void)m; }{ //! [make_map] using namespace std::literals; BOOST_HANA_RUNTIME_CHECK( make_map( make(int_<1>, "foobar"s), make(type, 1234) ) == make( make(int_<1>, "foobar"s), make(type, 1234) ) ); //! [make_map] }{ //! [values] using namespace std::literals; auto m = make( make(int_<1>, "foobar"s), make(type, 1234) ); // The order of the values is unspecified. BOOST_HANA_RUNTIME_CHECK(values(m) ^in^ permutations(make("foobar"s, 1234))); //! [values] }{ //! [keys] using namespace std::literals; auto m = make( make(int_<1>, "foobar"s), make(type, 1234) ); // The order of the keys is unspecified. BOOST_HANA_CONSTANT_CHECK(keys(m) ^in^ permutations(make(int_<1>, type))); //! [keys] }{ //! [from_Foldable] using namespace std::literals; BOOST_HANA_RUNTIME_CHECK( to(make( make(type, "abcd"s), make(type, 1234), make(type, nullptr) )) == make( make(type, "abcd"s), make(type, 1234) ) ); //! [from_Foldable] }{ //! [insert] using namespace std::literals; auto m = make( make(type, "abcd"s), make(type, 1234) ); BOOST_HANA_RUNTIME_CHECK( insert(m, make(type, 'x')) == make( make(type, "abcd"s), make(type, 1234), make(type, 'x') ) ); BOOST_HANA_RUNTIME_CHECK(insert(m, make(type, 'x')) == m); //! [insert] }{ //! [Comparable] using namespace std::literals; BOOST_HANA_RUNTIME_CHECK( make_map( make_pair(char_<'a'>, "foobar"s), make_pair(type, nullptr) ) == make_map( make_pair(type, (void*)0), make_pair(char_<'a'>, "foobar"s) ) ); //! [Comparable] }{ //! [Searchable] constexpr auto m = make_map( make_pair(type, 'i'), make_pair(type, 'f') ); static_assert(find(m, type) == just('i'), ""); static_assert(find(m, type) == just('f'), ""); BOOST_HANA_CONSTANT_CHECK(find(m, type) == nothing); BOOST_HANA_CONSTANT_CHECK(find(m, int_<3>) == nothing); //! [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_<1>), make_pair(type, int_<2>), make_pair(int_<3>, type) ); BOOST_HANA_CONSTANT_CHECK(invert(m) == make_map( make_pair(int_<1>, type), make_pair(int_<2>, type), make_pair(type, int_<3>) ) ); //! [Foldable] } }