1
0
forked from boostorg/mp11

Add mp_map_update_q

This commit is contained in:
Peter Dimov
2017-10-22 03:19:18 +03:00
parent df8217ce97
commit cb57cb9e78
4 changed files with 73 additions and 0 deletions

View File

@ -55,6 +55,26 @@ replaces the existing element with `T`.
If the map `M` does not contain an element with a key `mp_first<T>`, inserts it (using `mp_push_back<M, T>`); otherwise,
replaces the existing element `L<X, Y...>` with `L<X, F<X, Y...>>`.
.Using mp_map_update to count the number of occurences of types in a list
```
template<class T, class U> using inc2nd = mp_int<U::value + 1>;
template<class M, class T> using count_types =
mp_map_update<M, std::pair<T, mp_int<1>>, inc2nd>;
using L1 = mp_list<float, char, float, float, float, float, char, float>;
using R1 = mp_fold<L1, std::tuple<>, count_types>;
// std::tuple<std::pair<float, mp_int<6>>, std::pair<char, mp_int<2>>>
```
## mp_map_update_q<M, T, Q>
template<class M, class T, class Q> using mp_map_update_q =
mp_map_update<M, T, Q::template fn>;
As `mp_map_update`, but takes a quoted metafunction.
## mp_map_erase<M, K>
template<class M, class K> using mp_map_erase = /*...*/;

View File

@ -66,6 +66,7 @@ template<class M, class T, template<class...> class F> struct mp_map_update_impl
} // namespace detail
template<class M, class T, template<class...> class F> using mp_map_update = typename detail::mp_map_update_impl<M, T, F>::type;
template<class M, class T, class Q> using mp_map_update_q = mp_map_update<M, T, Q::template fn>;
// mp_map_erase<M, K>
namespace detail

View File

@ -149,6 +149,7 @@ run mp_map_insert.cpp ;
run mp_map_replace.cpp ;
run mp_map_erase.cpp ;
run mp_map_update.cpp ;
run mp_map_update_q.cpp ;
run mp_map_keys.cpp ;
run mp_is_map.cpp ;

51
test/mp_map_update_q.cpp Normal file
View File

@ -0,0 +1,51 @@
// Copyright 2016, 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/map.hpp>
#include <boost/mp11/list.hpp>
#include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <tuple>
#include <utility>
using boost::mp11::mp_int;
struct Q_inc
{
template<class T, class U> using fn = mp_int<U::value + 1>;
};
int main()
{
using boost::mp11::mp_map_update_q;
using boost::mp11::mp_list;
using M1 = mp_list<>;
using M2 = mp_map_update_q<M1, std::pair<char, mp_int<0>>, Q_inc>;
BOOST_TEST_TRAIT_TRUE((std::is_same<M2, mp_list<std::pair<char, mp_int<0>>>>));
using M3 = mp_map_update_q<M2, std::pair<char, mp_int<0>>, Q_inc>;
BOOST_TEST_TRAIT_TRUE((std::is_same<M3, mp_list<std::pair<char, mp_int<1>>>>));
using M4 = mp_map_update_q<M3, std::pair<int, mp_int<0>>, Q_inc>;
BOOST_TEST_TRAIT_TRUE((std::is_same<M4, mp_list<std::pair<char, mp_int<1>>, std::pair<int, mp_int<0>>>>));
using M5 = mp_map_update_q<M4, std::pair<long, mp_int<0>>, Q_inc>;
BOOST_TEST_TRAIT_TRUE((std::is_same<M5, mp_list<std::pair<char, mp_int<1>>, std::pair<int, mp_int<0>>, std::pair<long, mp_int<0>>>>));
using M6 = mp_map_update_q<M5, std::pair<long, mp_int<0>>, Q_inc>;
BOOST_TEST_TRAIT_TRUE((std::is_same<M6, mp_list<std::pair<char, mp_int<1>>, std::pair<int, mp_int<0>>, std::pair<long, mp_int<1>>>>));
using M7 = mp_map_update_q<M6, std::pair<char, mp_int<0>>, Q_inc>;
BOOST_TEST_TRAIT_TRUE((std::is_same<M7, mp_list<std::pair<char, mp_int<2>>, std::pair<int, mp_int<0>>, std::pair<long, mp_int<1>>>>));
return boost::report_errors();
}