From 984da7f1a144a8dd178711a24dba14bd83e18f53 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 22 Mar 2020 18:50:55 +0200 Subject: [PATCH] Add mp_iterate --- include/boost/mp11/algorithm.hpp | 28 +++++++++++++ test/Jamfile | 1 + test/mp_iterate.cpp | 68 ++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 test/mp_iterate.cpp diff --git a/include/boost/mp11/algorithm.hpp b/include/boost/mp11/algorithm.hpp index 168cfdf..f0a700d 100644 --- a/include/boost/mp11/algorithm.hpp +++ b/include/boost/mp11/algorithm.hpp @@ -1192,6 +1192,34 @@ template class F> struct mp_partial_sum_impl_f template class F> using mp_partial_sum = mp_second>, detail::mp_partial_sum_impl_f> >; template using mp_partial_sum_q = mp_partial_sum; +// mp_iterate +namespace detail +{ + +template class F, template class R, class N> struct mp_iterate_impl; + +} // namespace detail + +template class F, template class R> using mp_iterate = typename detail::mp_iterate_impl>::type; + +namespace detail +{ + +template class F, template class R> struct mp_iterate_impl +{ + template using _f = mp_list>; + using type = mp_eval_or, _f, V>; +}; + +template class F, template class R> struct mp_iterate_impl +{ + using type = mp_push_front, F, R>, F>; +}; + +} // namespace detail + +template using mp_iterate_q = mp_iterate; + } // namespace mp11 } // namespace boost diff --git a/test/Jamfile b/test/Jamfile index 09afb45..dc2d062 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -121,6 +121,7 @@ run mp_rotate_left.cpp ; run mp_rotate_right.cpp ; run mp_power_set.cpp ; run mp_partial_sum.cpp ; +run mp_iterate.cpp ; # integral run integral.cpp ; diff --git a/test/mp_iterate.cpp b/test/mp_iterate.cpp new file mode 100644 index 0000000..a0d1cb2 --- /dev/null +++ b/test/mp_iterate.cpp @@ -0,0 +1,68 @@ +// Copyright 2020 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include +#include + +struct X1 {}; +struct X2 { using first_type = double; using next_type = X1; }; +struct X3 { using first_type = float; using next_type = X2; }; +struct X4 { using first_type = int; using next_type = X3; }; + +template using first_type = typename T::first_type; +template using next_type = typename T::next_type; + +template struct cons {}; + +template struct cons2 +{ + using first_type = T1; + using next_type = T2; +}; + +using boost::mp11::mp_reverse_fold; +using boost::mp11::mp_iterate; +using boost::mp11::mp_first; +using boost::mp11::mp_second; +using boost::mp11::mp_rest; + +template void test() +{ + using R1 = mp_iterate; + BOOST_TEST_TRAIT_TRUE((std::is_same)); + + using V2 = mp_reverse_fold; + using R2 = mp_iterate; + BOOST_TEST_TRAIT_TRUE((std::is_same)); + +#if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 ) + + using V3 = mp_reverse_fold; + using R3 = mp_iterate; + BOOST_TEST_TRAIT_TRUE((std::is_same)); + +#endif +} + +int main() +{ + using boost::mp11::mp_list; + + test< mp_list<> >(); + test< mp_list >(); + test< mp_list >(); + test< mp_list >(); + test< mp_list >(); + + using boost::mp11::mp_identity_t; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list>)); + + return boost::report_errors(); +}