#ifndef SPROUT_ALGORITHM_MISMATCH_HPP #define SPROUT_ALGORITHM_MISMATCH_HPP #include #include #include #include #include #include #include #include namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR sprout::pair mismatch_impl_ra_1(RandomAccessIterator1 first1, RandomAccessIterator2 first2, RandomAccessIterator1 found) { return sprout::pair{ found, sprout::next(first2, sprout::distance(first1, found)) }; } template inline SPROUT_CONSTEXPR RandomAccessIterator1 mismatch_impl_ra( RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, BinaryPredicate pred, typename std::iterator_traits::difference_type pivot, RandomAccessIterator1 found ) { return found != first1 ? found : pivot == 0 ? (!pred(*first1, *first2) ? first1 : last1) : sprout::detail::mismatch_impl_ra( sprout::next(first1, pivot), last1, sprout::next(first2, pivot), pred, (sprout::distance(first1, last1) - pivot) / 2, sprout::detail::mismatch_impl_ra( first1, sprout::next(first1, pivot), first2, pred, pivot / 2, first1 ) ) ; } template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_constant_distance_iterator::value && sprout::is_constant_distance_iterator::value, sprout::pair >::type mismatch( RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, BinaryPredicate pred, std::random_access_iterator_tag* ) { return first1 == last1 ? sprout::pair{first1, first2} : sprout::detail::mismatch_impl_ra_1( first1, first2, sprout::detail::mismatch_impl_ra( first1, last1, first2, pred, sprout::distance(first1, last1) / 2, first1 ) ) ; } template inline SPROUT_CONSTEXPR sprout::pair mismatch_impl_check(sprout::tuples::tuple const& current) { return sprout::pair( sprout::tuples::get<0>(current), sprout::tuples::get<1>(current) ); } template inline SPROUT_CONSTEXPR sprout::tuples::tuple mismatch_impl_1( sprout::tuples::tuple const& current, InputIterator1 last1, BinaryPredicate pred, typename std::iterator_traits::difference_type n ) { typedef sprout::tuples::tuple type; return sprout::tuples::get<2>(current) || sprout::tuples::get<0>(current) == last1 ? current : n == 1 ? !pred(*sprout::tuples::get<0>(current), *sprout::tuples::get<1>(current)) ? type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), true) : type(sprout::next(sprout::tuples::get<0>(current)), sprout::next(sprout::tuples::get<1>(current)), false) : sprout::detail::mismatch_impl_1( sprout::detail::mismatch_impl_1( current, last1, pred, n / 2 ), last1, pred, n - n / 2 ) ; } template inline SPROUT_CONSTEXPR sprout::tuples::tuple mismatch_impl( sprout::tuples::tuple const& current, InputIterator1 last1, BinaryPredicate pred, typename std::iterator_traits::difference_type n ) { typedef sprout::tuples::tuple type; return sprout::tuples::get<2>(current) || sprout::tuples::get<0>(current) == last1 ? current : sprout::detail::mismatch_impl( sprout::detail::mismatch_impl_1( current, last1, pred, n ), last1, pred, n * 2 ) ; } template inline SPROUT_CONSTEXPR sprout::pair mismatch( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred, void* ) { typedef sprout::tuples::tuple type; return sprout::detail::mismatch_impl_check( sprout::detail::mismatch_impl(type(first1, first2, false), last1, pred, 1) ); } } // namespace detail // 25.2.10 Mismatch // // recursion depth: // O(log N) // template inline SPROUT_CONSTEXPR sprout::pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) { typedef typename sprout::common_iterator_category::type* category; return sprout::detail::mismatch(first1, last1, first2, pred, category()); } template inline SPROUT_CONSTEXPR sprout::pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { return sprout::mismatch(first1, last1, first2, sprout::equal_to<>()); } } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_MISMATCH_HPP