/*============================================================================= Copyright (c) 2011-2014 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_ALGORITHM_FIND_FIRST_OF_HPP #define SPROUT_ALGORITHM_FIND_FIRST_OF_HPP #include #include #include #include #include #include #include #include #include namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR RandomAccessIterator1 find_first_of_impl_ra( RandomAccessIterator1 first1, RandomAccessIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred, typename std::iterator_traits::difference_type pivot, RandomAccessIterator1 found ) { return found != first1 ? found : pivot == 0 ? sprout::find_if(first2, last2, sprout::bind1st(pred, *first1)) != last2 ? first1 : last1 : sprout::detail::find_first_of_impl_ra( sprout::next(first1, pivot), last1, first2, last2, pred, (sprout::distance(first1, last1) - pivot) / 2, sprout::detail::find_first_of_impl_ra( first1, sprout::next(first1, pivot), first2, last2, pred, pivot / 2, first1 ) ) ; } template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_constant_distance_iterator::value, RandomAccessIterator1 >::type find_first_of( RandomAccessIterator1 first1, RandomAccessIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred, std::random_access_iterator_tag* ) { return first1 == last1 ? last1 : sprout::detail::find_first_of_impl_ra( first1, last1, first2, last2, pred, sprout::distance(first1, last1) / 2, first1 ) ; } template inline SPROUT_CONSTEXPR sprout::pair find_first_of_impl_1( sprout::pair current, InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred, typename std::iterator_traits::difference_type n ) { typedef sprout::pair type; return current.second || current.first == last1 ? current : n == 1 ? sprout::find_if(first2, last2, sprout::bind1st(pred, *current.first)) != last2 ? type(current.first, true) : type(sprout::next(current.first), false) : sprout::detail::find_first_of_impl_1( sprout::detail::find_first_of_impl_1( current, last1, first2, last2, pred, n / 2 ), last1, first2, last2, pred, n - n / 2 ) ; } template inline SPROUT_CONSTEXPR sprout::pair find_first_of_impl( sprout::pair current, InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred, typename std::iterator_traits::difference_type n ) { return current.second || current.first == last1 ? current : sprout::detail::find_first_of_impl( sprout::detail::find_first_of_impl_1( current, last1, first2, last2, pred, n ), last1, first2, last2, pred, n * 2 ) ; } template inline SPROUT_CONSTEXPR InputIterator1 find_first_of( InputIterator1 first1, InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred, std::input_iterator_tag* ) { typedef sprout::pair type; return sprout::detail::find_first_of_impl(type(first1, false), last1, first2, last2, pred, 1).first; } } // namespace detail // 25.2.7 Find first // // recursion depth: // O(log(N1+N2)) // template inline SPROUT_CONSTEXPR InputIterator1 find_first_of( InputIterator1 first1, InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred ) { typedef typename std::iterator_traits::iterator_category* category; return sprout::detail::find_first_of(first1, last1, first2, last2, pred, category()); } template inline SPROUT_CONSTEXPR InputIterator1 find_first_of( InputIterator1 first1, InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2 ) { return sprout::find_first_of(first1, last1, first2, last2, sprout::equal_to<>()); } } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_FIND_FIRST_OF_HPP