1
0
forked from boostorg/mp11

Add mp_transform_if, mp_map_update

This commit is contained in:
Peter Dimov
2017-03-14 16:32:17 +02:00
parent c054b2b2fe
commit 6fb4cee7b2
6 changed files with 143 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
#ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED #ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED
#define BOOST_MP11_ALGORITHM_HPP_INCLUDED #define BOOST_MP11_ALGORITHM_HPP_INCLUDED
// Copyright 2015, 2016 Peter Dimov. // Copyright 2015-2017 Peter Dimov.
// //
// Distributed under the Boost Software License, Version 1.0. // Distributed under the Boost Software License, Version 1.0.
// //
@@ -69,6 +69,31 @@ template<template<class...> class F, template<class...> class L1, class... T1, t
template<template<class...> class F, class... L> using mp_transform = typename detail::mp_transform_impl<F, L...>::type; template<template<class...> class F, class... L> using mp_transform = typename detail::mp_transform_impl<F, L...>::type;
// mp_transform_if<P, F, L>
namespace detail
{
template<template<class...> class P, template<class...> class F, class L> struct mp_transform_if_impl;
template<template<class...> class P, template<class...> class F, template<class...> class L, class... T> struct mp_transform_if_impl<P, F, L<T...>>
{
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
template<class U> struct _f { using type = mp_eval_if<mp_not<P<U>>, U, F, U>; };
using type = L<typename _f<T>::type...>;
#else
template<class U> using _f = mp_eval_if<mp_not<P<U>>, U, F, U>;
using type = L<_f<T>...>;
#endif
};
} // namespace detail
template<template<class...> class P, template<class...> class F, class L> using mp_transform_if = typename detail::mp_transform_if_impl<P, F, L>::type;
// mp_fill<L, V> // mp_fill<L, V>
namespace detail namespace detail
{ {

View File

@@ -1,7 +1,7 @@
#ifndef BOOST_MP11_MAP_HPP_INCLUDED #ifndef BOOST_MP11_MAP_HPP_INCLUDED
#define BOOST_MP11_MAP_HPP_INCLUDED #define BOOST_MP11_MAP_HPP_INCLUDED
// Copyright 2015, 2016 Peter Dimov. // Copyright 2015-2017 Peter Dimov.
// //
// Distributed under the Boost Software License, Version 1.0. // Distributed under the Boost Software License, Version 1.0.
// //
@@ -46,6 +46,26 @@ template<template<class...> class M, class... U, class T> struct mp_map_replace_
template<class M, class T> using mp_map_replace = typename detail::mp_map_replace_impl<M, T>::type; template<class M, class T> using mp_map_replace = typename detail::mp_map_replace_impl<M, T>::type;
// mp_map_update<M, T, F> // mp_map_update<M, T, F>
namespace detail
{
template<class M, class T, template<class...> class F> struct mp_map_update_impl
{
template<class U> using _f = std::is_same<mp_first<T>, mp_first<U>>;
// _f2<L<X, Y...>> -> F<Y...>
// _f2<pair<X, Y>> -> F<Y>
template<class L> using _f2 = mp_rename<mp_rest<mp_rename<L, mp_list>>, F>;
// _f3<L<X, Y...>> -> L<X, F<Y...>>
template<class L> using _f3 = mp_assign<L, mp_list<mp_first<L>, _f2<L>>>;
using type = mp_if< mp_map_contains<M, mp_first<T>>, mp_transform_if<_f, _f3, M>, mp_push_back<M, T> >;
};
} // 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;
// mp_map_erase<M, K> // mp_map_erase<M, K>
namespace detail namespace detail

View File

@@ -30,6 +30,7 @@ run mp_append.cpp : : : $(REQ) ;
run mp_assign.cpp : : : $(REQ) ; run mp_assign.cpp : : : $(REQ) ;
run mp_clear.cpp : : : $(REQ) ; run mp_clear.cpp : : : $(REQ) ;
run mp_transform.cpp : : : $(REQ) ; run mp_transform.cpp : : : $(REQ) ;
run mp_transform_if.cpp : : : $(REQ) ;
run mp_fill.cpp : : : $(REQ) ; run mp_fill.cpp : : : $(REQ) ;
run mp_count.cpp : : : $(REQ) ; run mp_count.cpp : : : $(REQ) ;
run mp_count_if.cpp : : : $(REQ) ; run mp_count_if.cpp : : : $(REQ) ;
@@ -95,3 +96,4 @@ run mp_map_contains.cpp : : : $(REQ) ;
run mp_map_insert.cpp : : : $(REQ) ; run mp_map_insert.cpp : : : $(REQ) ;
run mp_map_replace.cpp : : : $(REQ) ; run mp_map_replace.cpp : : : $(REQ) ;
run mp_map_erase.cpp : : : $(REQ) ; run mp_map_erase.cpp : : : $(REQ) ;
run mp_map_update.cpp : : : $(REQ) ;

View File

@@ -18,7 +18,6 @@ int main()
{ {
using boost::mp_map_erase; using boost::mp_map_erase;
using boost::mp_list; using boost::mp_list;
using boost::mp_push_back;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_map_erase<mp_list<>, void>, mp_list<>>)); BOOST_TEST_TRAIT_TRUE((std::is_same<mp_map_erase<mp_list<>, void>, mp_list<>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_map_erase<std::tuple<>, int>, std::tuple<>>)); BOOST_TEST_TRAIT_TRUE((std::is_same<mp_map_erase<std::tuple<>, int>, std::tuple<>>));

48
test/mp_map_update.cpp Normal file
View File

@@ -0,0 +1,48 @@
// 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::mp_int;
template<class T> using inc = mp_int<T::value + 1>;
int main()
{
using boost::mp_map_update;
using boost::mp_list;
using M1 = mp_list<>;
using M2 = mp_map_update< M1, std::pair<char, mp_int<0>>, inc >;
BOOST_TEST_TRAIT_TRUE((std::is_same<M2, mp_list<std::pair<char, mp_int<0>>>>));
using M3 = mp_map_update< M2, std::pair<char, mp_int<0>>, inc >;
BOOST_TEST_TRAIT_TRUE((std::is_same<M3, mp_list<std::pair<char, mp_int<1>>>>));
using M4 = mp_map_update< M3, std::pair<int, mp_int<0>>, 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< M4, std::pair<long, mp_int<0>>, 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< M5, std::pair<long, mp_int<0>>, 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< M6, std::pair<char, mp_int<0>>, 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();
}

46
test/mp_transform_if.cpp Normal file
View File

@@ -0,0 +1,46 @@
// Copyright 2015, 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/algorithm.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>
struct X1 {};
struct X2 {};
struct X3 {};
struct X4 {};
template<class T> using add_pointer = T*;
template<class T> using is_not_ref = boost::mp_not<std::is_reference<T>>;
int main()
{
using boost::mp_list;
using boost::mp_transform_if;
using L1 = mp_list<X1, X2&, X3 const, X4 const&>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_transform_if<is_not_ref, add_pointer, L1>, mp_list<X1*, X2&, X3 const*, X4 const&>>));
using L2 = std::tuple<X1, X2&, X3 const, X4 const&>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_transform_if<is_not_ref, add_pointer, L2>, std::tuple<X1*, X2&, X3 const*, X4 const&>>));
using L3 = std::pair<X1 const, X2&>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_transform_if<is_not_ref, add_pointer, L3>, std::pair<X1 const*, X2&>>));
//
return boost::report_errors();
}