From 856ae3dc6cf2be7c381d7a0aa3b883851259d350 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 15 Jul 2015 17:14:00 +0300 Subject: [PATCH] Add mp_find_index. --- include/boost/mp11/algorithm.hpp | 64 +++++++++++++++++++++++++++++++- test/Jamfile.v2 | 1 + test/mp_find_index.cpp | 63 +++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 test/mp_find_index.cpp diff --git a/include/boost/mp11/algorithm.hpp b/include/boost/mp11/algorithm.hpp index e4640f9..7b06d13 100644 --- a/include/boost/mp11/algorithm.hpp +++ b/include/boost/mp11/algorithm.hpp @@ -497,10 +497,70 @@ template class L, class T1, class... T, template cl template class P> using mp_sort = typename detail::mp_sort_impl::type; +// mp_find_index +namespace detail +{ + +template struct mp_find_index_impl; + +#if !defined( BOOST_NO_CXX11_CONSTEXPR ) + +template class L, class V> struct mp_find_index_impl, V> +{ + using type = mp_size_t<0>; +}; + +constexpr std::size_t cx_find_index( bool const * first, bool const * last ) +{ + return first == last || *first? 0: 1 + cx_find_index( first + 1, last ); +} + +template class L, class... T, class V> struct mp_find_index_impl, V> +{ + static constexpr bool _v[] = { std::is_same::value... }; + using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >; +}; + +#else + +#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1800 ) + +template class L, class... T, class V> struct mp_find_index_impl, V> +{ + static_assert( sizeof...(T) == 0, "T... must be empty" ); + using type = mp_size_t<0>; +}; + +#else + +template class L, class V> struct mp_find_index_impl, V> +{ + using type = mp_size_t<0>; +}; + +#endif + +template class L, class... T, class V> struct mp_find_index_impl, V> +{ + using type = mp_size_t<0>; +}; + +template class L, class T1, class... T, class V> struct mp_find_index_impl, V> +{ + using _r = typename mp_find_index_impl, V>::type; + using type = mp_size_t<1 + _r::value>; +}; + +#endif + +} // namespace detail + +template using mp_find_index = typename detail::mp_find_index_impl::type; + +// mp_find_index_if // mp_find // mp_find_if -// mp_find_index -// mp_find_index_if + // mp_reverse // mp_fold // mp_reverse_fold diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d344063..5deab2a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -44,6 +44,7 @@ run mp_copy_if.cpp : : : $(REQ) ; run mp_remove_if.cpp : : : $(REQ) ; run mp_partition.cpp : : : $(REQ) ; run mp_sort.cpp : : : $(REQ) ; +run mp_find_index.cpp : : : $(REQ) ; # integral run integral.cpp : : : $(REQ) ; diff --git a/test/mp_find_index.cpp b/test/mp_find_index.cpp new file mode 100644 index 0000000..570f641 --- /dev/null +++ b/test/mp_find_index.cpp @@ -0,0 +1,63 @@ + +// Copyright 2015 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 +#include + +struct X1 {}; +struct X2 {}; +struct X3 {}; + +int main() +{ + using boost::mp_list; + using boost::mp_find_index; + using boost::mp_size_t; + + { + using L1 = mp_list<>; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<0>>)); + + using L2 = mp_list; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<6>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<0>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<1>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<3>>)); + } + + { + using L3 = std::tuple<>; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<0>>)); + + using L4 = std::tuple; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<6>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<0>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<1>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<3>>)); + } + + { + using L5 = std::pair; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<2>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<0>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_size_t<1>>)); + } + + return boost::report_errors(); +}