forked from boostorg/mp11
Add mp_nth_element
This commit is contained in:
@@ -441,6 +441,18 @@ 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>>
|
using R1 = mp_sort<L1, std::ratio_less>; // mp_list<ratio<1,4>, ratio<1,2>>
|
||||||
----
|
----
|
||||||
|
|
||||||
|
## mp_nth_element_c<L, I, P>
|
||||||
|
|
||||||
|
template<class L, std::size_t I, template<class...> class P> using mp_nth_element_c = /*...*/;
|
||||||
|
|
||||||
|
Returns the element at position `I` in `mp_sort<L, P>`.
|
||||||
|
|
||||||
|
## mp_nth_element<L, I, P>
|
||||||
|
|
||||||
|
template<class L, class I, template<class...> class P> using mp_nth_element = /*...*/;
|
||||||
|
|
||||||
|
Like `mp_nth_element_c`, but with a type argument `I`. `I::value` must be a nonnegative number.
|
||||||
|
|
||||||
## mp_min_element<L, P>
|
## mp_min_element<L, P>
|
||||||
|
|
||||||
template<class L, template<class...> class P> using mp_min_element = /*...*/;
|
template<class L, template<class...> class P> using mp_min_element = /*...*/;
|
||||||
|
@@ -508,6 +508,45 @@ template<template<class...> class L, class T1, class... T, template<class...> cl
|
|||||||
|
|
||||||
template<class L, template<class...> class P> using mp_sort = typename detail::mp_sort_impl<L, P>::type;
|
template<class L, template<class...> class P> using mp_sort = typename detail::mp_sort_impl<L, P>::type;
|
||||||
|
|
||||||
|
// mp_nth_element(_c)<L, I, P>
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class L, std::size_t I, template<class...> class P> struct mp_nth_element_impl;
|
||||||
|
|
||||||
|
template<template<class...> class L, class T1, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1>, I, P>
|
||||||
|
{
|
||||||
|
static_assert( I == 0, "mp_nth_element index out of range" );
|
||||||
|
using type = T1;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<template<class...> class L, class T1, class... T, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1, T...>, I, P>
|
||||||
|
{
|
||||||
|
static_assert( I < 1 + sizeof...(T), "mp_nth_element index out of range" );
|
||||||
|
|
||||||
|
template<class U> using F = P<U, T1>;
|
||||||
|
|
||||||
|
using part = mp_partition<L<T...>, F>;
|
||||||
|
|
||||||
|
using L1 = mp_first<part>;
|
||||||
|
static std::size_t const N1 = mp_size<L1>::value;
|
||||||
|
|
||||||
|
using L2 = mp_second<part>;
|
||||||
|
|
||||||
|
using type = typename mp_cond<
|
||||||
|
|
||||||
|
mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
|
||||||
|
mp_bool<(I == N1)>, mp_identity<T1>,
|
||||||
|
mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
|
||||||
|
|
||||||
|
>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<class L, std::size_t I, template<class...> class P> using mp_nth_element_c = typename detail::mp_nth_element_impl<L, I, P>::type;
|
||||||
|
template<class L, class I, template<class...> class P> using mp_nth_element = typename detail::mp_nth_element_impl<L, std::size_t{ I::value }, P>::type;
|
||||||
|
|
||||||
// mp_find<L, V>
|
// mp_find<L, V>
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
@@ -80,6 +80,7 @@ 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_max_element.cpp ;
|
run mp_max_element.cpp ;
|
||||||
|
run mp_nth_element.cpp ;
|
||||||
|
|
||||||
# integral
|
# integral
|
||||||
run integral.cpp ;
|
run integral.cpp ;
|
||||||
|
72
test/mp_nth_element.cpp
Normal file
72
test/mp_nth_element.cpp
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
|
||||||
|
// 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/mp11/function.hpp>
|
||||||
|
#include <boost/core/lightweight_test_trait.hpp>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using boost::mp11::mp_nth_element;
|
||||||
|
using boost::mp11::mp_nth_element_c;
|
||||||
|
using boost::mp11::mp_list_c;
|
||||||
|
using boost::mp11::mp_sort;
|
||||||
|
using boost::mp11::mp_less;
|
||||||
|
using boost::mp11::mp_at_c;
|
||||||
|
using boost::mp11::mp_size_t;
|
||||||
|
using boost::mp11::mp_rename;
|
||||||
|
|
||||||
|
{
|
||||||
|
using L1 = mp_list_c<int, 7, 1, 11, 3, 2, 2, 4>;
|
||||||
|
using L2 = mp_sort<L1, mp_less>;
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 0, mp_less>, mp_at_c<L2, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 1, mp_less>, mp_at_c<L2, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 2, mp_less>, mp_at_c<L2, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 3, mp_less>, mp_at_c<L2, 3>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 4, mp_less>, mp_at_c<L2, 4>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 5, mp_less>, mp_at_c<L2, 5>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 6, mp_less>, mp_at_c<L2, 6>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<0>, mp_less>, mp_at_c<L2, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<1>, mp_less>, mp_at_c<L2, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<2>, mp_less>, mp_at_c<L2, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<3>, mp_less>, mp_at_c<L2, 3>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<4>, mp_less>, mp_at_c<L2, 4>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<5>, mp_less>, mp_at_c<L2, 5>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<6>, mp_less>, mp_at_c<L2, 6>>));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
using L1 = mp_rename<mp_list_c<int, 7, 1, 11, 3, 2, 2, 4>, std::tuple>;
|
||||||
|
using L2 = mp_sort<L1, mp_less>;
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 0, mp_less>, mp_at_c<L2, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 1, mp_less>, mp_at_c<L2, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 2, mp_less>, mp_at_c<L2, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 3, mp_less>, mp_at_c<L2, 3>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 4, mp_less>, mp_at_c<L2, 4>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 5, mp_less>, mp_at_c<L2, 5>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element_c<L1, 6, mp_less>, mp_at_c<L2, 6>>));
|
||||||
|
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<0>, mp_less>, mp_at_c<L2, 0>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<1>, mp_less>, mp_at_c<L2, 1>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<2>, mp_less>, mp_at_c<L2, 2>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<3>, mp_less>, mp_at_c<L2, 3>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<4>, mp_less>, mp_at_c<L2, 4>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<5>, mp_less>, mp_at_c<L2, 5>>));
|
||||||
|
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_nth_element<L1, mp_size_t<6>, mp_less>, mp_at_c<L2, 6>>));
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user