From 07892c8d73ee29b97a9ae47545c17533c91d618b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 3 Feb 2019 19:48:16 +0200 Subject: [PATCH] Add mp_filter --- include/boost/mp11/algorithm.hpp | 21 +++++++++ test/Jamfile | 1 + test/mp_filter.cpp | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 test/mp_filter.cpp diff --git a/include/boost/mp11/algorithm.hpp b/include/boost/mp11/algorithm.hpp index ba2b181..31d5c4e 100644 --- a/include/boost/mp11/algorithm.hpp +++ b/include/boost/mp11/algorithm.hpp @@ -174,6 +174,27 @@ template class P, template class F, class... L> str template class P, template class F, class... L> using mp_transform_if = typename detail::mp_transform_if_impl::type; template using mp_transform_if_q = typename detail::mp_transform_if_impl::type; +// mp_filter +namespace detail +{ + +template class P, class L1, class... L> struct mp_filter_impl +{ + using Qp = mp_quote

; + + template using _f = mp_if< mp_invoke_q, mp_list, mp_list<> >; + + using _t1 = mp_transform<_f, L1, L...>; + using _t2 = mp_apply; + + using type = mp_assign; +}; + +} // namespace detail + +template class P, class... L> using mp_filter = typename detail::mp_filter_impl::type; +template using mp_filter_q = typename detail::mp_filter_impl::type; + // mp_fill namespace detail { diff --git a/test/Jamfile b/test/Jamfile index b46dd24..7137c5f 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -48,6 +48,7 @@ run mp_transform_q.cpp ; run mp_transform_sf.cpp ; run mp_transform_if.cpp ; run mp_transform_if_q.cpp ; +run mp_filter.cpp ; run mp_fill.cpp ; run mp_count.cpp ; run mp_count_if.cpp ; diff --git a/test/mp_filter.cpp b/test/mp_filter.cpp new file mode 100644 index 0000000..6a2ea13 --- /dev/null +++ b/test/mp_filter.cpp @@ -0,0 +1,78 @@ + +// Copyright 2019 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 +#include +#include +#include + +using boost::mp11::mp_int; + +template using mod_2 = mp_int; +template using mod_3 = mp_int; +template using mod_6 = mp_int; + +using boost::mp11::mp_not; +using boost::mp11::mp_plus; + +template using P1 = mp_not>; +template using P2 = mp_not>; + +using boost::mp11::mp_bool; + +template struct second_is +{ + template using fn = mp_bool< T2::value == N >; +}; + +using boost::mp11::mp_first; +using boost::mp11::mp_filter_q; +using boost::mp11::mp_iota; +using boost::mp11::mp_size; + +template using at_c = mp_first< mp_filter_q< second_is, L, mp_iota> > >; + +int main() +{ + using boost::mp11::mp_iota_c; + using boost::mp11::mp_filter; + using boost::mp11::mp_list; + using boost::mp11::mp_size_t; + using boost::mp11::mp_transform; + + { + int const N = 12; + using L1 = mp_iota_c; + + using R1 = mp_filter; + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<6>>>)); + + using L2 = mp_transform; + using L3 = mp_transform; + using L6 = mp_transform; + + using R2 = mp_filter; + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<6>>>)); + + using R3 = mp_filter; + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<6>>>)); + } + + { + int const N = 64; + int const M = 17; + + using L1 = mp_iota_c; + using R1 = at_c; + + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + } + + return boost::report_errors(); +}