1
0
forked from boostorg/mp11

Add mp_remove, mp_reverse.

This commit is contained in:
Peter Dimov
2015-07-24 01:17:16 +03:00
parent b5d9b95709
commit 67ef9ab3fb
4 changed files with 210 additions and 1 deletions

View File

@@ -387,6 +387,44 @@ template<template<class...> class L, class T1, class... T, template<class...> cl
template<class L, template<class...> class P> using mp_copy_if = typename detail::mp_copy_if_impl<L, P>::type; template<class L, template<class...> class P> using mp_copy_if = typename detail::mp_copy_if_impl<L, P>::type;
// mp_remove<L, V>
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<>;
};
#else
template<template<class...> class L, class V> struct mp_remove_impl<L<>, V>
{
using type = L<>;
};
#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
template<class L, class V> using mp_remove = typename detail::mp_remove_impl<L, V>::type;
// mp_remove_if<L, P> // mp_remove_if<L, P>
namespace detail namespace detail
{ {
@@ -619,10 +657,65 @@ template<class L, class V> using mp_find = mp_drop<L, mp_find_index<L, V>>;
template<class L, template<class...> class P> using mp_find_if = mp_drop<L, mp_find_index_if<L, P>>; template<class L, template<class...> class P> using mp_find_if = mp_drop<L, mp_find_index_if<L, P>>;
// mp_reverse<L> // mp_reverse<L>
namespace detail
{
template<class L> struct mp_reverse_impl;
#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<template<class...> class L, class... T> struct mp_reverse_impl<L<T...>>
{
static_assert( sizeof...(T) == 0, "T... must be empty" );
using type = L<>;
};
#else
template<template<class...> class L> struct mp_reverse_impl<L<>>
{
using type = L<>;
};
#endif
template<template<class...> class L, class T1> struct mp_reverse_impl<L<T1>>
{
using type = L<T1>;
};
template<template<class...> class L, class T1, class T2> struct mp_reverse_impl<L<T1, T2>>
{
using type = L<T2, T1>;
};
template<template<class...> class L, class T1, class T2, class T3> struct mp_reverse_impl<L<T1, T2, T3>>
{
using type = L<T3, T2, T1>;
};
template<template<class...> class L, class T1, class T2, class T3, class T4> struct mp_reverse_impl<L<T1, T2, T3, T4>>
{
using type = L<T4, T3, T2, T1>;
};
template<template<class...> class L, class T1, class T2, class T3, class T4, class T5> struct mp_reverse_impl<L<T1, T2, T3, T4, T5>>
{
using type = L<T5, T4, T3, T2, T1>;
};
template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class... T> struct mp_reverse_impl<L<T1, T2, T3, T4, T5, T6, T...>>
{
using type = mp_push_back<typename mp_reverse_impl<L<T...>>::type, T6, T5, T4, T3, T2, T1>;
};
} // namespace detail
template<class L> using mp_reverse = typename detail::mp_reverse_impl<L>::type;
// mp_fold<L, V, F> // mp_fold<L, V, F>
// mp_reverse_fold<L, V, F> // mp_reverse_fold<L, V, F>
// mp_unique<L> // mp_unique<L>
// mp_remove<L, V>
} // namespace boost } // namespace boost

View File

@@ -41,6 +41,7 @@ run mp_take.cpp : : : $(REQ) ;
run mp_replace.cpp : : : $(REQ) ; run mp_replace.cpp : : : $(REQ) ;
run mp_replace_if.cpp : : : $(REQ) ; run mp_replace_if.cpp : : : $(REQ) ;
run mp_copy_if.cpp : : : $(REQ) ; run mp_copy_if.cpp : : : $(REQ) ;
run mp_remove.cpp : : : $(REQ) ;
run mp_remove_if.cpp : : : $(REQ) ; run mp_remove_if.cpp : : : $(REQ) ;
run mp_partition.cpp : : : $(REQ) ; run mp_partition.cpp : : : $(REQ) ;
run mp_sort.cpp : : : $(REQ) ; run mp_sort.cpp : : : $(REQ) ;
@@ -48,6 +49,7 @@ run mp_find_index.cpp : : : $(REQ) ;
run mp_find_index_if.cpp : : : $(REQ) ; run mp_find_index_if.cpp : : : $(REQ) ;
run mp_find.cpp : : : $(REQ) ; run mp_find.cpp : : : $(REQ) ;
run mp_find_if.cpp : : : $(REQ) ; run mp_find_if.cpp : : : $(REQ) ;
run mp_reverse.cpp : : : $(REQ) ;
# integral # integral
run integral.cpp : : : $(REQ) ; run integral.cpp : : : $(REQ) ;

52
test/mp_remove.cpp Normal file
View File

@@ -0,0 +1,52 @@
// 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>
struct X1 {};
struct X2 {};
struct X3 {};
int main()
{
using boost::mp_list;
using boost::mp_remove;
{
using L1 = mp_list<>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L1, void>, L1>));
using L2 = mp_list<X1, X2, X3, X2, X3, X3>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, void>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, X1>, mp_list<X2, X3, X2, X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, X2>, mp_list<X1, X3, X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, X3>, mp_list<X1, X2, X2>>));
}
{
using L1 = std::tuple<>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L1, void>, L1>));
using L2 = std::tuple<X1, X2, X3, X2, X3, X3>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, void>, L2>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, X1>, std::tuple<X2, X3, X2, X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, X2>, std::tuple<X1, X3, X3, X3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_remove<L2, X3>, std::tuple<X1, X2, X2>>));
}
return boost::report_errors();
}

62
test/mp_reverse.cpp Normal file
View File

@@ -0,0 +1,62 @@
// 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>
struct X1 {};
struct X2 {};
struct X3 {};
struct X4 {};
struct X5 {};
struct X6 {};
struct X7 {};
struct X8 {};
struct X9 {};
int main()
{
using boost::mp_list;
using boost::mp_reverse;
{
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<>>, mp_list<>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1>>, mp_list<X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2>>, mp_list<X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3>>, mp_list<X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3, X4>>, mp_list<X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3, X4, X5>>, mp_list<X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3, X4, X5, X6>>, mp_list<X6, X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3, X4, X5, X6, X7>>, mp_list<X7, X6, X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3, X4, X5, X6, X7, X8>>, mp_list<X8, X7, X6, X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<mp_list<X1, X2, X3, X4, X5, X6, X7, X8, X9>>, mp_list<X9, X8, X7, X6, X5, X4, X3, X2, X1>>));
}
{
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<>>, std::tuple<>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1>>, std::tuple<X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2>>, std::tuple<X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3>>, std::tuple<X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3, X4>>, std::tuple<X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3, X4, X5>>, std::tuple<X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3, X4, X5, X6>>, std::tuple<X6, X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3, X4, X5, X6, X7>>, std::tuple<X7, X6, X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3, X4, X5, X6, X7, X8>>, std::tuple<X8, X7, X6, X5, X4, X3, X2, X1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::tuple<X1, X2, X3, X4, X5, X6, X7, X8, X9>>, std::tuple<X9, X8, X7, X6, X5, X4, X3, X2, X1>>));
}
{
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_reverse<std::pair<X1, X2>>, std::pair<X2, X1>>));
}
return boost::report_errors();
}