| 1 | // Copyright Louis Dionne 2013-2022 |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | #include <boost/hana/assert.hpp> |
| 6 | #include <boost/hana/first.hpp> |
| 7 | #include <boost/hana/fold_left.hpp> |
| 8 | #include <boost/hana/insert.hpp> |
| 9 | #include <boost/hana/integral_constant.hpp> |
| 10 | #include <boost/hana/map.hpp> |
| 11 | #include <boost/hana/pair.hpp> |
| 12 | #include <boost/hana/second.hpp> |
| 13 | #include <boost/hana/type.hpp> |
| 14 | namespace hana = boost::hana; |
| 15 | |
| 16 | |
| 17 | int main() { |
| 18 | // Given a map of (key, value) pairs, returns a map of (value, key) pairs. |
| 19 | // This requires both the keys and the values to be compile-time Comparable. |
| 20 | auto invert = [](auto map) { |
| 21 | return hana::fold_left(map, hana::make_map(), [](auto map, auto pair) { |
| 22 | return hana::insert(map, hana::make_pair(hana::second(pair), hana::first(pair))); |
| 23 | }); |
| 24 | }; |
| 25 | |
| 26 | auto m = hana::make_map( |
| 27 | hana::make_pair(hana::type_c<int>, hana::int_c<1>), |
| 28 | hana::make_pair(hana::type_c<float>, hana::int_c<2>), |
| 29 | hana::make_pair(hana::int_c<3>, hana::type_c<void>) |
| 30 | ); |
| 31 | |
| 32 | BOOST_HANA_CONSTANT_CHECK(invert(m) == |
| 33 | hana::make_map( |
| 34 | hana::make_pair(hana::int_c<1>, hana::type_c<int>), |
| 35 | hana::make_pair(hana::int_c<2>, hana::type_c<float>), |
| 36 | hana::make_pair(hana::type_c<void>, hana::int_c<3>) |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |