1
0
forked from boostorg/mp11

Add mp_less, mp_min, mp_max

This commit is contained in:
Peter Dimov
2017-10-15 15:52:32 +03:00
parent 66b7128253
commit a5e74b2951
6 changed files with 150 additions and 0 deletions

View File

@@ -106,3 +106,25 @@ using R4 = mp_any<void, mp_true>; // compile-time error
`mp_plus<T...>` is an integral constant type with a value that is the sum of `U::value` for all types `U` in `T...`. `mp_plus<T...>` is an integral constant type with a value that is the sum of `U::value` for all types `U` in `T...`.
`mp_plus<>` is `mp_int<0>`. `mp_plus<>` is `mp_int<0>`.
## mp_less<T1, T2>
template<class T1, class T2> using mp_less = /*...*/;
`mp_less<T1, T2>` is `mp_true` when the numeric value of `T1::value` is less than the numeric value of `T2::value`,
`mp_false` otherwise.
(Note that this is not necessarily the same as `T1::value < T2::value` when comparing between signed and unsigned types;
`-1 < 1u` is `false`, but `mp_less<mp_int\<-1>, mp_size_t<1>>` is `mp_true`.)
## mp_min<T1, T...>
template<class T1, class... T> using mp_min = mp_min_element<mp_list<T1, T...>, mp_less>;
`mp_min<T...>` returns the type `U` in `T...` with the lowest `U::value`.
## mp_max<T1, T...>
template<class T1, class... T> using mp_max = mp_max_element<mp_list<T1, T...>, mp_less>;
`mp_max<T...>` returns the type `U` in `T...` with the highest `U::value`.

View File

@@ -13,6 +13,7 @@
#include <boost/mp11/detail/mp_list.hpp> #include <boost/mp11/detail/mp_list.hpp>
#include <boost/mp11/detail/mp_count.hpp> #include <boost/mp11/detail/mp_count.hpp>
#include <boost/mp11/detail/mp_plus.hpp> #include <boost/mp11/detail/mp_plus.hpp>
#include <boost/mp11/detail/mp_min_element.hpp>
#include <type_traits> #include <type_traits>
namespace boost namespace boost
@@ -162,6 +163,15 @@ template<class T1, class... T> struct mp_same_impl<T1, T...>
template<class... T> using mp_same = typename detail::mp_same_impl<T...>::type; template<class... T> using mp_same = typename detail::mp_same_impl<T...>::type;
// mp_less<T1, T2>
template<class T1, class T2> using mp_less = mp_bool<(T1::value < 0 && T2::value >= 0) || ((T1::value < T2::value) && !(T1::value >= 0 && T2::value < 0))>;
// mp_min<T...>
template<class T1, class... T> using mp_min = mp_min_element<mp_list<T1, T...>, mp_less>;
// mp_max<T...>
template<class T1, class... T> using mp_max = mp_max_element<mp_list<T1, T...>, mp_less>;
} // namespace mp11 } // namespace mp11
} // namespace boost } // namespace boost

View File

@@ -124,6 +124,9 @@ run mp_any.cpp ;
run mp_or.cpp ; run mp_or.cpp ;
run mp_same.cpp ; run mp_same.cpp ;
run mp_plus.cpp ; run mp_plus.cpp ;
run mp_less.cpp ;
run mp_min.cpp ;
run mp_max.cpp ;
# map # map
run mp_map_find.cpp ; run mp_map_find.cpp ;

63
test/mp_less.cpp Normal file
View File

@@ -0,0 +1,63 @@
// 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/function.hpp>
#include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
int main()
{
using boost::mp11::mp_less;
using boost::mp11::mp_true;
using boost::mp11::mp_false;
using boost::mp11::mp_int;
using boost::mp11::mp_size_t;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<0>, mp_int<-1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<0>, mp_int<0>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<0>, mp_int<1>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<0>, mp_int<2>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<-1>, mp_int<-2>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<-1>, mp_int<-1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<-1>, mp_int<0>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<-1>, mp_int<1>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<1>, mp_int<-1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<1>, mp_int<0>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<1>, mp_int<1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<1>, mp_int<2>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_size_t<0>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_size_t<1>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_size_t<2>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_size_t<3>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<1>, mp_size_t<0>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<1>, mp_size_t<1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<1>, mp_size_t<2>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<1>, mp_size_t<3>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<2>, mp_size_t<0>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<2>, mp_size_t<1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<2>, mp_size_t<2>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<2>, mp_size_t<3>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_int<-1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_int<0>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_size_t<0>, mp_int<1>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<-1>, mp_size_t<1>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<0>, mp_size_t<1>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<1>, mp_size_t<1>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_less<mp_int<2>, mp_size_t<1>>, mp_false>));
return boost::report_errors();
}

26
test/mp_max.cpp Normal file
View File

@@ -0,0 +1,26 @@
// 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/function.hpp>
#include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
int main()
{
using boost::mp11::mp_max;
using boost::mp11::mp_int;
using boost::mp11::mp_size_t;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max<mp_int<1>>, mp_int<1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max<mp_int<2>, mp_int<1>, mp_int<2>, mp_int<3>>, mp_int<3>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max<mp_int<-1>, mp_size_t<1>, mp_int<-2>, mp_size_t<2>>, mp_size_t<2>>));
return boost::report_errors();
}

26
test/mp_min.cpp Normal file
View File

@@ -0,0 +1,26 @@
// 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/function.hpp>
#include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
int main()
{
using boost::mp11::mp_min;
using boost::mp11::mp_int;
using boost::mp11::mp_size_t;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min<mp_int<1>>, mp_int<1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min<mp_int<2>, mp_int<1>, mp_int<2>, mp_int<3>>, mp_int<1>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min<mp_int<-1>, mp_size_t<1>, mp_int<-2>, mp_size_t<2>>, mp_int<-2>>));
return boost::report_errors();
}