1
0
forked from boostorg/mp11

Improve mp_copy_if, mp_remove(_if), mp_partition

This commit is contained in:
Peter Dimov
2017-03-24 15:22:10 +02:00
parent 42b4561725
commit c1485d5d51
3 changed files with 52 additions and 77 deletions

View File

@@ -384,27 +384,15 @@ namespace detail
template<class L, template<class...> class P> struct mp_copy_if_impl;
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<template<class...> class L, class... T, template<class...> class P> struct mp_copy_if_impl<L<T...>, P>
{
static_assert( sizeof...(T) == 0, "T... must be empty" );
using type = L<>;
};
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
template<class U> struct _f { using type = mp_if<P<U>, mp_list<U>, mp_list<>>; };
using type = mp_append<L<>, typename _f<T>::type...>;
#else
template<template<class...> class L, template<class...> class P> struct mp_copy_if_impl<L<>, P>
{
using type = L<>;
};
template<class U> using _f = mp_if<P<U>, mp_list<U>, mp_list<>>;
using type = mp_append<L<>, _f<T>...>;
#endif
template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_copy_if_impl<L<T1, T...>, P>
{
using rest = typename mp_copy_if_impl<L<T...>, P>::type;
using type = mp_if<P<T1>, mp_push_front<rest, T1>, rest>;
};
} // namespace detail
@@ -417,32 +405,15 @@ namespace detail
template<class L, class V> struct mp_remove_impl;
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<template<class...> class L, class... T, class V> struct mp_remove_impl<L<T...>, V>
{
static_assert( sizeof...(T) == 0, "T... must be empty" );
using type = L<>;
};
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
template<class U> struct _f { using type = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>; };
using type = mp_append<L<>, typename _f<T>::type...>;
#else
template<template<class...> class L, class V> struct mp_remove_impl<L<>, V>
{
using type = L<>;
};
template<class U> using _f = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>;
using type = mp_append<L<>, _f<T>...>;
#endif
template<template<class...> class L, class T1, class... T> struct mp_remove_impl<L<T1, T...>, T1>
{
using type = typename mp_remove_impl<L<T...>, T1>::type;
};
template<template<class...> class L, class T1, class... T, class V> struct mp_remove_impl<L<T1, T...>, V>
{
using rest = typename mp_remove_impl<L<T...>, V>::type;
using type = mp_push_front<rest, T1>;
};
} // namespace detail
@@ -455,27 +426,15 @@ namespace detail
template<class L, template<class...> class P> struct mp_remove_if_impl;
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<template<class...> class L, class... T, template<class...> class P> struct mp_remove_if_impl<L<T...>, P>
{
static_assert( sizeof...(T) == 0, "T... must be empty" );
using type = L<>;
};
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
template<class U> struct _f { using type = mp_if<P<U>, mp_list<>, mp_list<U>>; };
using type = mp_append<L<>, typename _f<T>::type...>;
#else
template<template<class...> class L, template<class...> class P> struct mp_remove_if_impl<L<>, P>
{
using type = L<>;
};
template<class U> using _f = mp_if<P<U>, mp_list<>, mp_list<U>>;
using type = mp_append<L<>, _f<T>...>;
#endif
template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_remove_if_impl<L<T1, T...>, P>
{
using rest = typename mp_remove_if_impl<L<T...>, P>::type;
using type = mp_if<P<T1>, rest, mp_push_front<rest, T1>>;
};
} // namespace detail
@@ -488,27 +447,9 @@ namespace detail
template<class L, template<class...> class P> struct mp_partition_impl;
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<template<class...> class L, class... T, template<class...> class P> struct mp_partition_impl<L<T...>, P>
{
static_assert( sizeof...(T) == 0, "T... must be empty" );
using type = L<L<>, L<>>;
};
#else
template<template<class...> class L, template<class...> class P> struct mp_partition_impl<L<>, P>
{
using type = L<L<>, L<>>;
};
#endif
template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_partition_impl<L<T1, T...>, P>
{
using rest = typename mp_partition_impl<L<T...>, P>::type;
using type = mp_if<P<T1>, L<mp_push_front<mp_first<rest>, T1>, mp_second<rest>>, L<mp_first<rest>, mp_push_front<mp_second<rest>, T1>>>;
using type = L<mp_copy_if<L<T...>, P>, mp_remove_if<L<T...>, P>>;
};
} // namespace detail

View File

@@ -1,5 +1,5 @@
// Copyright 2015 Peter Dimov.
// Copyright 2015-2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
@@ -9,6 +9,7 @@
#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>
@@ -17,6 +18,10 @@ struct X1 {};
struct X2 {};
struct X3 {};
using boost::mp11::mp_bool;
template<class N> using is_even = mp_bool<N::value % 2 == 0>;
int main()
{
using boost::mp11::mp_list;
@@ -46,5 +51,17 @@ int main()
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_copy_if<L2, std::is_pointer>, std::tuple<X1*, X2*, X3*>>));
}
using boost::mp11::mp_iota_c;
using boost::mp11::mp_size_t;
{
int const N = 12;
using L1 = mp_iota_c<N>;
using R1 = mp_copy_if<L1, is_even>;
BOOST_TEST_TRAIT_TRUE((std::is_same<R1, mp_list<mp_size_t<0>, mp_size_t<2>, mp_size_t<4>, mp_size_t<6>, mp_size_t<8>, mp_size_t<10>>>));
}
return boost::report_errors();
}

View File

@@ -1,5 +1,5 @@
// Copyright 2015 Peter Dimov.
// Copyright 2015-2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
@@ -9,6 +9,7 @@
#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>
@@ -17,6 +18,10 @@ struct X1 {};
struct X2 {};
struct X3 {};
using boost::mp11::mp_bool;
template<class N> using is_odd = mp_bool<N::value % 2 != 0>;
int main()
{
using boost::mp11::mp_list;
@@ -46,5 +51,17 @@ int main()
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove_if<L2, std::is_pointer>, std::tuple<X1, X1 const, X2 const>>));
}
using boost::mp11::mp_iota_c;
using boost::mp11::mp_size_t;
{
int const N = 12;
using L1 = mp_iota_c<N>;
using R1 = mp_remove_if<L1, is_odd>;
BOOST_TEST_TRAIT_TRUE((std::is_same<R1, mp_list<mp_size_t<0>, mp_size_t<2>, mp_size_t<4>, mp_size_t<6>, mp_size_t<8>, mp_size_t<10>>>));
}
return boost::report_errors();
}