1
0
forked from boostorg/mp11

Add mp_min_element, mp_max_element

This commit is contained in:
Peter Dimov
2017-10-14 22:05:49 +03:00
parent e5b4200d24
commit 67beaa8fc9
5 changed files with 154 additions and 0 deletions

View File

@ -441,6 +441,22 @@ using L1 = mp_list<std::ratio<1,2>, std::ratio<1,4>>;
using R1 = mp_sort<L1, std::ratio_less>; // mp_list<ratio<1,4>, ratio<1,2>>
----
## mp_min_element<L, P>
template<class L, template<class...> class P> using mp_min_element = /*...*/;
`mp_min_element<L, P>` returns the minimal element of the list `L` according to the ordering `mp_to_bool<P<T, U>>`.
It's equivalent to `mp_fold<mp_rest<L>, mp_first<L>, Q>`, where `Q<T, U>` returns `mp_if<P<T, U>, T, U>`.
## mp_max_element<L, P>
template<class L, template<class...> class P> using mp_max_element = /*...*/;
`mp_max_element<L, P>` returns the maximal element of the list `L` according to the ordering `mp_to_bool<P<T, U>>`.
It's equivalent to `mp_fold<mp_rest<L>, mp_first<L>, Q>`, where `Q<T, U>` returns `mp_if<P<U, T>, T, U>`.
## mp_find<L, V>
template<class L, class V> using mp_find = /*...*/;

View File

@ -922,6 +922,32 @@ template<class L, class I, class J> using mp_erase = mp_append<mp_take<L, I>, mp
// mp_erase_c<L, I, J>
template<class L, std::size_t I, std::size_t J> using mp_erase_c = mp_append<mp_take_c<L, I>, mp_drop_c<L, J>>;
// mp_min_element<L, P>
namespace detail
{
template<template<class...> class P> struct select_min
{
template<class T1, class T2> using fn = mp_if<P<T1, T2>, T1, T2>;
};
} // namespace detail
template<class L, template<class...> class P> using mp_min_element = mp_fold_q<mp_rest<L>, mp_first<L>, detail::select_min<P>>;
// mp_max_element<L, P>
namespace detail
{
template<template<class...> class P> struct select_max
{
template<class T1, class T2> using fn = mp_if<P<T2, T1>, T1, T2>;
};
} // namespace detail
template<class L, template<class...> class P> using mp_max_element = mp_fold_q<mp_rest<L>, mp_first<L>, detail::select_max<P>>;
} // namespace mp11
} // namespace boost

View File

@ -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 ;

55
test/mp_max_element.cpp Normal file
View File

@ -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 <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>
using boost::mp11::mp_bool;
template<class T, class U> 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<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element<L1, sizeof_less>, void>));
using L2 = mp_list<char[2], char[4], char[3], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element<L2, sizeof_less>, char[4]>));
using L3 = mp_list<char[2], char[4], char[2], char[3], char[1], char[2], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element<L3, sizeof_less>, char[4]>));
}
{
using L1 = std::tuple<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element<L1, sizeof_less>, void>));
using L2 = std::tuple<char[2], char[4], char[3], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element<L2, sizeof_less>, char[4]>));
using L3 = std::tuple<char[2], char[4], char[2], char[3], char[1], char[2], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element<L3, sizeof_less>, char[4]>));
}
return boost::report_errors();
}

55
test/mp_min_element.cpp Normal file
View File

@ -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 <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>
using boost::mp11::mp_bool;
template<class T, class U> 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<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element<L1, sizeof_less>, void>));
using L2 = mp_list<char[2], char[4], char[3], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element<L2, sizeof_less>, char[1]>));
using L3 = mp_list<char[2], char[4], char[2], char[3], char[1], char[2], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element<L3, sizeof_less>, char[1]>));
}
{
using L1 = std::tuple<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element<L1, sizeof_less>, void>));
using L2 = std::tuple<char[2], char[4], char[3], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element<L2, sizeof_less>, char[1]>));
using L3 = std::tuple<char[2], char[4], char[2], char[3], char[1], char[2], char[1]>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element<L3, sizeof_less>, char[1]>));
}
return boost::report_errors();
}