1
0
forked from boostorg/mp11

Add mp_min_element_q, mp_max_element_q

This commit is contained in:
Peter Dimov
2017-10-22 02:00:56 +03:00
parent 806a96e42c
commit aae8d4dfba
5 changed files with 134 additions and 2 deletions

View File

@ -504,7 +504,13 @@ Like `mp_nth_element`, but takes a quoted metafunction.
`mp_min_element<L, P>` returns the minimal element of the list `L` according to the ordering `mp_to_bool<P<T, U>>`. `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>`. It's equivalent to `mp_fold<mp_rest<L>, mp_first<L>, F>`, where `F<T, U>` returns `mp_if<P<T, U>, T, U>`.
## mp_min_element_q<L, Q>
template<class L, class Q> using mp_min_element_q = mp_min_element<L, Q::template fn>;
As `mp_min_element`, but takes a quoted metafunction.
## mp_max_element<L, P> ## mp_max_element<L, P>
@ -512,7 +518,13 @@ It's equivalent to `mp_fold<mp_rest<L>, mp_first<L>, Q>`, where `Q<T, U>` return
`mp_max_element<L, P>` returns the maximal element of the list `L` according to the ordering `mp_to_bool<P<T, U>>`. `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>`. It's equivalent to `mp_fold<mp_rest<L>, mp_first<L>, F>`, where `F<T, U>` returns `mp_if<P<U, T>, T, U>`.
## mp_max_element_q<L, Q>
template<class L, class Q> using mp_max_element_q = mp_max_element<L, Q::template fn>;
As `mp_max_element`, but takes a quoted metafunction.
## mp_find<L, V> ## mp_find<L, V>

View File

@ -29,6 +29,7 @@ template<template<class...> class P> struct select_min
} // namespace detail } // 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>>; template<class L, template<class...> class P> using mp_min_element = mp_fold_q<mp_rest<L>, mp_first<L>, detail::select_min<P>>;
template<class L, class Q> using mp_min_element_q = mp_min_element<L, Q::template fn>;
// mp_max_element<L, P> // mp_max_element<L, P>
namespace detail namespace detail
@ -42,6 +43,7 @@ template<template<class...> class P> struct select_max
} // namespace detail } // 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>>; template<class L, template<class...> class P> using mp_max_element = mp_fold_q<mp_rest<L>, mp_first<L>, detail::select_max<P>>;
template<class L, class Q> using mp_max_element_q = mp_max_element<L, Q::template fn>;
} // namespace mp11 } // namespace mp11
} // namespace boost } // namespace boost

View File

@ -85,7 +85,9 @@ run mp_with_index.cpp ;
run mp_with_index_cx.cpp ; run mp_with_index_cx.cpp ;
run mp_from_sequence.cpp ; run mp_from_sequence.cpp ;
run mp_min_element.cpp ; run mp_min_element.cpp ;
run mp_min_element_q.cpp ;
run mp_max_element.cpp ; run mp_max_element.cpp ;
run mp_max_element_q.cpp ;
run mp_nth_element.cpp ; run mp_nth_element.cpp ;
run mp_nth_element_q.cpp ; run mp_nth_element_q.cpp ;

58
test/mp_max_element_q.cpp Normal file
View File

@ -0,0 +1,58 @@
// 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;
struct Q_sizeof_less
{
template<class T, class U> using fn = mp_bool<(sizeof(T) < sizeof(U))>;
};
int main()
{
using boost::mp11::mp_list;
using boost::mp11::mp_max_element_q;
{
using L1 = mp_list<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element_q<L1, Q_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_q<L2, Q_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_q<L3, Q_sizeof_less>, char[4]>));
}
{
using L1 = std::tuple<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_max_element_q<L1, Q_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_q<L2, Q_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_q<L3, Q_sizeof_less>, char[4]>));
}
return boost::report_errors();
}

58
test/mp_min_element_q.cpp Normal file
View File

@ -0,0 +1,58 @@
// 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;
struct Q_sizeof_less
{
template<class T, class U> using fn = mp_bool<(sizeof(T) < sizeof(U))>;
};
int main()
{
using boost::mp11::mp_list;
using boost::mp11::mp_min_element_q;
{
using L1 = mp_list<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element_q<L1, Q_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_q<L2, Q_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_q<L3, Q_sizeof_less>, char[1]>));
}
{
using L1 = std::tuple<void>;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_min_element_q<L1, Q_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_q<L2, Q_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_q<L3, Q_sizeof_less>, char[1]>));
}
return boost::report_errors();
}