diff --git a/sprout/type/algorithm/detail/find_index_result.hpp b/sprout/type/algorithm/detail/find_index_result.hpp new file mode 100644 index 00000000..68ad19db --- /dev/null +++ b/sprout/type/algorithm/detail/find_index_result.hpp @@ -0,0 +1,30 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + 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) +=============================================================================*/ +#ifndef SPROUT_TYPE_ALGORITHM_DETAIL_FIND_INDEX_RESULT_HPP +#define SPROUT_TYPE_ALGORITHM_DETAIL_FIND_INDEX_RESULT_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace types { + namespace detail { + template + struct find_index_result + : public sprout::integral_constant + { + public: + typedef sprout::bool_constant::value> found; + }; + } // namespace detail + } // namespace types +} // namespace sprout + +#endif // #ifndef SPROUT_TYPE_ALGORITHM_DETAIL_FIND_INDEX_RESULT_HPP diff --git a/sprout/type/algorithm/find_index.hpp b/sprout/type/algorithm/find_index.hpp index ba9e50fd..a021a02d 100644 --- a/sprout/type/algorithm/find_index.hpp +++ b/sprout/type/algorithm/find_index.hpp @@ -12,48 +12,56 @@ #include #include #include -#include +#include namespace sprout { namespace types { namespace detail { template< - typename Tuple, typename T, std::size_t I, - bool Valid = (I != sprout::types::tuple_size::value), - typename Enable = void + typename Tuple, typename T, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found, + bool C0 = (Found != First), bool C1 = (Pivot == 0) > struct find_index_impl; - template - struct find_index_impl< - Tuple, T, I, false, - void + template< + typename Tuple, typename T, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found, + bool C1 > - : public sprout::integral_constant - { - public: - typedef sprout::false_type found; - }; - template - struct find_index_impl< - Tuple, T, I, true, - typename std::enable_if< - std::is_same::type, T>::value - >::type + struct find_index_impl + : public sprout::types::detail::find_index_result + {}; + template< + typename Tuple, typename T, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found > - : public sprout::integral_constant - { - public: - typedef sprout::true_type found; - typedef typename sprout::types::tuple_element::type element; - }; - template - struct find_index_impl< - Tuple, T, I, true, - typename std::enable_if< - !std::is_same::type, T>::value - >::type + struct find_index_impl + : public sprout::types::detail::find_index_result< + Tuple, (std::is_same::type, T>::value ? First : Last) + > + {}; + template< + typename Tuple, typename T, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found > - : public sprout::types::detail::find_index_impl + struct find_index_impl + : public sprout::types::detail::find_index_impl< + Tuple, T, First + Pivot, Last, + (Last - First - Pivot) / 2, + sprout::types::detail::find_index_impl< + Tuple, T, First, First + Pivot, + Pivot / 2, + First + >::value + > + {}; + template::value> + struct find_index + : public sprout::types::detail::find_index_impl + {}; + template + struct find_index + : public sprout::types::detail::find_index_result {}; } // namespace detail // @@ -61,7 +69,7 @@ namespace sprout { // template struct find_index - : public sprout::types::detail::find_index_impl + : public sprout::types::detail::find_index {}; #if SPROUT_USE_TEMPLATE_ALIASES diff --git a/sprout/type/algorithm/find_index_if.hpp b/sprout/type/algorithm/find_index_if.hpp index 5707bdf1..4a108db3 100644 --- a/sprout/type/algorithm/find_index_if.hpp +++ b/sprout/type/algorithm/find_index_if.hpp @@ -13,48 +13,56 @@ #include #include #include -#include +#include namespace sprout { namespace types { namespace detail { template< - typename Tuple, typename Predicate, std::size_t I, - bool Valid = (I != sprout::types::tuple_size::value), - typename Enable = void + typename Tuple, typename Predicate, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found, + bool C0 = (Found != First), bool C1 = (Pivot == 0) > struct find_index_if_impl; - template - struct find_index_if_impl< - Tuple, Predicate, I, false, - void + template< + typename Tuple, typename Predicate, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found, + bool C1 > - : public sprout::integral_constant - { - public: - typedef sprout::false_type found; - }; - template - struct find_index_if_impl< - Tuple, Predicate, I, true, - typename std::enable_if< - sprout::types::apply::type>::type::value - >::type + struct find_index_if_impl + : public sprout::types::detail::find_index_result + {}; + template< + typename Tuple, typename Predicate, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found > - : public sprout::integral_constant - { - public: - typedef sprout::true_type found; - typedef typename sprout::types::tuple_element::type element; - }; - template - struct find_index_if_impl< - Tuple, Predicate, I, true, - typename std::enable_if< - !sprout::types::apply::type>::type::value - >::type + struct find_index_if_impl + : public sprout::types::detail::find_index_result< + Tuple, (sprout::types::apply::type>::type::value ? First : Last) + > + {}; + template< + typename Tuple, typename Predicate, std::size_t First, std::size_t Last, + std::size_t Pivot, std::size_t Found > - : public sprout::types::detail::find_index_if_impl + struct find_index_if_impl + : public sprout::types::detail::find_index_if_impl< + Tuple, Predicate, First + Pivot, Last, + (Last - First - Pivot) / 2, + sprout::types::detail::find_index_if_impl< + Tuple, Predicate, First, First + Pivot, + Pivot / 2, + First + >::value + > + {}; + template::value> + struct find_index_if + : public sprout::types::detail::find_index_if_impl + {}; + template + struct find_index_if + : public sprout::types::detail::find_index_result {}; } // namespace detail // @@ -62,7 +70,7 @@ namespace sprout { // template struct find_index_if - : public sprout::types::detail::find_index_if_impl + : public sprout::types::detail::find_index_if {}; #if SPROUT_USE_TEMPLATE_ALIASES