/*! @file Defines `boost::hana::map`. @copyright Louis Dionne 2013-2016 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_MAP_HPP #define BOOST_HANA_MAP_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN ////////////////////////////////////////////////////////////////////////// // operators ////////////////////////////////////////////////////////////////////////// namespace detail { template <> struct comparable_operators { static constexpr bool value = true; }; } ////////////////////////////////////////////////////////////////////////// // map ////////////////////////////////////////////////////////////////////////// //! @cond namespace detail { template struct map_impl : detail::searchable_operators> , detail::operators::adl> { using hash_table_type = HashTable; using storage_type = Storage; Storage storage; using hana_tag = map_tag; template ::type...> >::value >::type> explicit constexpr map_impl(P&& ...pairs) : storage{static_cast(pairs)...} { } explicit constexpr map_impl(Storage&& xs) : storage(static_cast(xs)) { } constexpr map_impl() = default; constexpr map_impl(map_impl const& other) = default; constexpr map_impl(map_impl&& other) = default; }; //! @endcond template struct KeyAtIndex { template using apply = decltype(hana::first(hana::get_impl(std::declval()))); }; } ////////////////////////////////////////////////////////////////////////// // make ////////////////////////////////////////////////////////////////////////// template <> struct make_impl { template static constexpr auto apply(Pairs&& ...pairs) { #if defined(BOOST_HANA_CONFIG_ENABLE_DEBUG_MODE) static_assert(detail::fast_and::value...>::value, "hana::make_map(pairs...) requires all the 'pairs' to be Products"); static_assert(detail::fast_and< hana::Comparable::value... >::value, "hana::make_map(pairs...) requires all the keys to be Comparable"); static_assert(detail::fast_and< hana::Constant< decltype(hana::equal(hana::first(pairs), hana::first(pairs))) >::value... >::value, "hana::make_map(pairs...) requires all the keys to be " "Comparable at compile-time"); //! @todo //! This can be implemented more efficiently by doing the check //! inside each bucket instead. static_assert(!detail::has_duplicates::value, "hana::make_map({keys, values}...) requires all the keys to be unique"); static_assert(!detail::has_duplicates::value, "hana::make_map({keys, values}...) requires all the keys to have different hashes"); #endif using Storage = hana::basic_tuple::type...>; using HashTable = typename detail::make_hash_table< detail::KeyAtIndex::template apply, sizeof...(Pairs) >::type; return detail::map_impl{ hana::make_basic_tuple(static_cast(pairs)...) }; } }; ////////////////////////////////////////////////////////////////////////// // keys ////////////////////////////////////////////////////////////////////////// template <> struct keys_impl { template static constexpr decltype(auto) apply(Map&& map) { return hana::transform(static_cast(map).storage, hana::first); } }; ////////////////////////////////////////////////////////////////////////// // values ////////////////////////////////////////////////////////////////////////// //! @cond template constexpr decltype(auto) values_t::operator()(Map&& map) const { return hana::transform(static_cast(map).storage, hana::second); } //! @endcond ////////////////////////////////////////////////////////////////////////// // insert ////////////////////////////////////////////////////////////////////////// template <> struct insert_impl { template static constexpr auto helper(Map&& map, Pair&& pair, ...) { using RawMap = typename std::remove_reference::type; using HashTable = typename RawMap::hash_table_type; using NewHashTable = typename detail::bucket_insert< HashTable, decltype(hana::first(pair)), decltype(hana::length(map.storage))::value >::type; using NewStorage = decltype( hana::append(static_cast(map).storage, static_cast(pair)) ); return detail::map_impl( hana::append(static_cast(map).storage, static_cast(pair)) ); } template static constexpr auto helper(Map&& map, Pair&&, hana::optional>) { return static_cast(map); } //! @todo //! Here, we insert only if the key is not already in the map. //! This should be handled by `bucket_insert`, and that would also //! be more efficient. template static constexpr auto apply(Map&& map, Pair&& pair) { using RawMap = typename std::remove_reference::type; using Storage = typename RawMap::storage_type; using HashTable = typename RawMap::hash_table_type; using Key = decltype(hana::first(pair)); using MaybeIndex = typename detail::find_index< HashTable, Key, detail::KeyAtIndex::template apply >::type; return helper(static_cast(map), static_cast(pair), MaybeIndex{}); } }; ////////////////////////////////////////////////////////////////////////// // erase_key ////////////////////////////////////////////////////////////////////////// template <> struct erase_key_impl { //! @todo //! We could implement some kind of `bucket_erase` metafunction //! that would be much more efficient than this. template static constexpr auto erase_key_helper(Map&& map, Key const&, hana::false_) { return static_cast(map); } template static constexpr auto erase_key_helper(Map&& map, Key const& key, hana::true_) { return hana::unpack( hana::remove_if(static_cast(map).storage, hana::on(hana::equal.to(key), hana::first)), hana::make_map ); } template static constexpr auto apply(Map&& map, Key const& key) { constexpr bool contains = hana::value(); return erase_key_helper(static_cast(map), key, hana::bool_c); } }; ////////////////////////////////////////////////////////////////////////// // Comparable ////////////////////////////////////////////////////////////////////////// template <> struct equal_impl { template static constexpr auto equal_helper(M1 const&, M2 const&, hana::false_) { return hana::false_c; } template static constexpr auto equal_helper(M1 const& m1, M2 const& m2, hana::true_) { return hana::all_of(hana::keys(m1), hana::demux(equal)( hana::partial(hana::find, m1), hana::partial(hana::find, m2) )); } template static constexpr auto apply(M1 const& m1, M2 const& m2) { return equal_impl::equal_helper(m1, m2, hana::bool_c< decltype(hana::length(m1.storage))::value == decltype(hana::length(m2.storage))::value >); } }; ////////////////////////////////////////////////////////////////////////// // Searchable ////////////////////////////////////////////////////////////////////////// template <> struct find_impl { template static constexpr auto find_helper(Map&&, ...) { return hana::nothing; } template static constexpr auto find_helper(Map&& map, hana::optional>) { return hana::just(hana::second(hana::at_c(static_cast(map).storage))); } template static constexpr auto apply(Map&& map, Key const&) { using RawMap = typename std::remove_reference::type; using Storage = typename RawMap::storage_type; using HashTable = typename RawMap::hash_table_type; using MaybeIndex = typename detail::find_index< HashTable, Key, detail::KeyAtIndex::template apply >::type; return find_helper(static_cast(map), MaybeIndex{}); } }; template <> struct find_if_impl { template static constexpr auto apply(M&& map, Pred&& pred) { return hana::transform( hana::find_if(static_cast(map).storage, hana::compose(static_cast(pred), hana::first)), hana::second ); } }; template <> struct any_of_impl { template static constexpr auto apply(M const& map, Pred const& pred) { return hana::any_of(hana::keys(map), pred); } }; template <> struct is_subset_impl { template struct all_contained { Ys const& ys; template constexpr auto operator()(X const& ...x) const { return hana::bool_c()... >::value>; } }; template static constexpr auto apply(Xs const& xs, Ys const& ys) { auto ys_keys = hana::keys(ys); return hana::unpack(hana::keys(xs), all_contained{ys_keys}); } }; template <> struct at_key_impl { template static constexpr decltype(auto) apply(Map&& map, Key const&) { using RawMap = typename std::remove_reference::type; using HashTable = typename RawMap::hash_table_type; using Storage = typename RawMap::storage_type; using MaybeIndex = typename detail::find_index< HashTable, Key, detail::KeyAtIndex::template apply >::type; static_assert(!decltype(hana::is_nothing(MaybeIndex{}))::value, "hana::at_key(map, key) requires the 'key' to be present in the 'map'"); constexpr std::size_t index = decltype(*MaybeIndex{}){}(); return hana::second(hana::at_c(static_cast(map).storage)); } }; ////////////////////////////////////////////////////////////////////////// // Foldable ////////////////////////////////////////////////////////////////////////// template <> struct unpack_impl { template static constexpr decltype(auto) apply(M&& map, F&& f) { return hana::unpack(static_cast(map).storage, static_cast(f)); } }; ////////////////////////////////////////////////////////////////////////// // Construction from a Foldable ////////////////////////////////////////////////////////////////////////// template struct to_impl::value>> { template static constexpr decltype(auto) apply(Xs&& xs) { return hana::fold_left( static_cast(xs), hana::make_map(), hana::insert ); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_MAP_HPP