1
0
forked from boostorg/mp11

Add mp_replace, mp_replace_if.

This commit is contained in:
Peter Dimov
2015-06-23 16:26:56 +03:00
parent 4094f2f73f
commit 4a33810f9b
4 changed files with 165 additions and 2 deletions

View File

@@ -309,6 +309,51 @@ template<class L, std::size_t N> using mp_take_c = typename detail::mp_take_impl
template<class L, class N> using mp_take = typename detail::mp_take_impl<L, N>::type;
// mp_replace<L, V, W>
namespace detail
{
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<class L, class V, class W> struct mp_replace_impl;
template<template<class...> class L, class... T, class V, class W> struct mp_replace_impl<L<T...>, V, W>
{
template<class A> struct _f { using type = mp_if<std::is_same<A, V>, W, A>; };
using type = L<typename _f<T>::type...>;
};
#else
template<class L, class V, class W> struct mp_replace_impl
{
template<class A> using _f = mp_if<std::is_same<A, V>, W, A>;
using type = mp_transform<_f, L>;
};
#endif
} // namespace detail
template<class L, class V, class W> using mp_replace = typename detail::mp_replace_impl<L, V, W>::type;
// mp_replace_if<L, P, W>
namespace detail
{
template<class L, template<class...> class P, class W> struct mp_replace_if_impl
{
template<class T> using _f = mp_if<P<T>, W, T>;
using type = mp_transform<_f, L>;
};
} // namespace detail
template<class L, template<class...> class P, class W> using mp_replace_if = typename detail::mp_replace_if_impl<L, P, W>::type;
// mp_find<L, V>
// mp_find_if<L, P>
// mp_find_index<L, V>
@@ -318,8 +363,6 @@ template<class L, class N> using mp_take = typename detail::mp_take_impl<L, N>::
// mp_remove_if<L, P>
// mp_fold<L, V, F>
// mp_reverse_fold<L, V, F>
// mp_replace<L, V1, V2>?
// mp_replace_if<L, P, V2>?
// mp_partition<L, P>
// mp_sort<L>

View File

@@ -38,6 +38,8 @@ run mp_drop.cpp : : : $(REQ) ;
run mp_iota.cpp : : : $(REQ) ;
run mp_at.cpp : : : $(REQ) ;
run mp_take.cpp : : : $(REQ) ;
run mp_replace.cpp : : : $(REQ) ;
run mp_replace_if.cpp : : : $(REQ) ;
# integral
run integral.cpp : : : $(REQ) ;

61
test/mp_replace.cpp Normal file
View File

@@ -0,0 +1,61 @@
// Copyright 2015 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/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <tuple>
#include <utility>
struct X1 {};
struct X2 {};
struct X3 {};
int main()
{
using boost::mp_list;
using boost::mp_replace;
{
using L1 = mp_list<>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L1, void, int[]>, L1>));
using L2 = mp_list<X1, X2, X3, X2, X3, X3>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, void, int[]>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X1, int[]>, mp_list<int[], X2, X3, X2, X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X2, int[]>, mp_list<X1, int[], X3, int[], X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X3, int[]>, mp_list<X1, X2, int[], X2, int[], int[]>>));
}
{
using L1 = std::tuple<>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L1, void, int[]>, L1>));
using L2 = std::tuple<X1, X2, X3, X2, X3, X3>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, void, int[]>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X1, int[]>, std::tuple<int[], X2, X3, X2, X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X2, int[]>, std::tuple<X1, int[], X3, int[], X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X3, int[]>, std::tuple<X1, X2, int[], X2, int[], int[]>>));
}
{
using L2 = std::pair<X1, X2>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, void, int[]>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X1, int[]>, std::pair<int[], X2>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace<L2, X2, int[]>, std::pair<X1, int[]>>));
}
return boost::report_errors();
}

57
test/mp_replace_if.cpp Normal file
View File

@@ -0,0 +1,57 @@
// Copyright 2015 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/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <tuple>
#include <utility>
struct X1 {};
int main()
{
using boost::mp_list;
using boost::mp_replace_if;
{
using L1 = mp_list<>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L1, std::is_const, void>, L1>));
using L2 = mp_list<X1, X1 const, X1*, X1 const, X1*, X1*>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_volatile, void>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_const, void>, mp_list<X1, void, X1*, void, X1*, X1*>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_pointer, void>, mp_list<X1, X1 const, void, X1 const, void, void>>));
}
{
using L1 = std::tuple<>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L1, std::is_const, void>, L1>));
using L2 = std::tuple<X1, X1 const, X1*, X1 const, X1*, X1*>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_volatile, void>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_const, void>, std::tuple<X1, void, X1*, void, X1*, X1*>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_pointer, void>, std::tuple<X1, X1 const, void, X1 const, void, void>>));
}
{
using L2 = std::pair<X1 const, X1*>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_volatile, void>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_const, void>, std::pair<void, X1*>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_replace_if<L2, std::is_pointer, void>, std::pair<X1 const, void>>));
}
return boost::report_errors();
}