From 67beaa8fc99e13e4411f3e637fb1f3f6b63680e5 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 14 Oct 2017 22:05:49 +0300 Subject: [PATCH] Add mp_min_element, mp_max_element --- doc/mp11/algorithm.adoc | 16 ++++++++++ include/boost/mp11/algorithm.hpp | 26 +++++++++++++++ test/Jamfile | 2 ++ test/mp_max_element.cpp | 55 ++++++++++++++++++++++++++++++++ test/mp_min_element.cpp | 55 ++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 test/mp_max_element.cpp create mode 100644 test/mp_min_element.cpp diff --git a/doc/mp11/algorithm.adoc b/doc/mp11/algorithm.adoc index 629583b..bff181e 100644 --- a/doc/mp11/algorithm.adoc +++ b/doc/mp11/algorithm.adoc @@ -441,6 +441,22 @@ using L1 = mp_list, std::ratio<1,4>>; using R1 = mp_sort; // mp_list, ratio<1,2>> ---- +## mp_min_element + + template class P> using mp_min_element = /*...*/; + +`mp_min_element` returns the minimal element of the list `L` according to the ordering `mp_to_bool>`. + +It's equivalent to `mp_fold, mp_first, Q>`, where `Q` returns `mp_if, T, U>`. + +## mp_max_element + + template class P> using mp_max_element = /*...*/; + +`mp_max_element` returns the maximal element of the list `L` according to the ordering `mp_to_bool>`. + +It's equivalent to `mp_fold, mp_first, Q>`, where `Q` returns `mp_if, T, U>`. + ## mp_find template using mp_find = /*...*/; diff --git a/include/boost/mp11/algorithm.hpp b/include/boost/mp11/algorithm.hpp index f8215b7..6a89bc8 100644 --- a/include/boost/mp11/algorithm.hpp +++ b/include/boost/mp11/algorithm.hpp @@ -922,6 +922,32 @@ template using mp_erase = mp_append, mp // mp_erase_c template using mp_erase_c = mp_append, mp_drop_c>; +// mp_min_element +namespace detail +{ + +template class P> struct select_min +{ + template using fn = mp_if, T1, T2>; +}; + +} // namespace detail + +template class P> using mp_min_element = mp_fold_q, mp_first, detail::select_min

>; + +// mp_max_element +namespace detail +{ + +template class P> struct select_max +{ + template using fn = mp_if, T1, T2>; +}; + +} // namespace detail + +template class P> using mp_max_element = mp_fold_q, mp_first, detail::select_max

>; + } // namespace mp11 } // namespace boost diff --git a/test/Jamfile b/test/Jamfile index 812429f..a3dffcd 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -78,6 +78,8 @@ run mp_erase.cpp ; run mp_with_index.cpp ; run mp_with_index_cx.cpp ; run mp_from_sequence.cpp ; +run mp_min_element.cpp ; +run mp_max_element.cpp ; # integral run integral.cpp ; diff --git a/test/mp_max_element.cpp b/test/mp_max_element.cpp new file mode 100644 index 0000000..1855029 --- /dev/null +++ b/test/mp_max_element.cpp @@ -0,0 +1,55 @@ + +// Copyright 2017 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 +#include +#include + +using boost::mp11::mp_bool; + +template using sizeof_less = mp_bool<(sizeof(T) < sizeof(U))>; + +int main() +{ + using boost::mp11::mp_list; + using boost::mp11::mp_max_element; + + { + using L1 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, void>)); + + using L2 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[4]>)); + + using L3 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[4]>)); + } + + { + using L1 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, void>)); + + using L2 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[4]>)); + + using L3 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[4]>)); + } + + return boost::report_errors(); +} diff --git a/test/mp_min_element.cpp b/test/mp_min_element.cpp new file mode 100644 index 0000000..3a4451e --- /dev/null +++ b/test/mp_min_element.cpp @@ -0,0 +1,55 @@ + +// Copyright 2017 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 +#include +#include + +using boost::mp11::mp_bool; + +template using sizeof_less = mp_bool<(sizeof(T) < sizeof(U))>; + +int main() +{ + using boost::mp11::mp_list; + using boost::mp11::mp_min_element; + + { + using L1 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, void>)); + + using L2 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[1]>)); + + using L3 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[1]>)); + } + + { + using L1 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, void>)); + + using L2 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[1]>)); + + using L3 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, char[1]>)); + } + + return boost::report_errors(); +}