Sprout/sprout/algorithm/mismatch.hpp

263 lines
12 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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)
=============================================================================*/
2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_MISMATCH_HPP
#define SPROUT_ALGORITHM_MISMATCH_HPP
#include <iterator>
2013-01-03 08:38:45 +00:00
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/type_traits/common.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.hpp>
#include <sprout/functional/equal_to.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/pair/pair.hpp>
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
2012-04-01 13:15:09 +00:00
namespace sprout {
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
inline SPROUT_CONSTEXPR sprout::pair<RandomAccessIterator1, RandomAccessIterator2>
mismatch_impl_check(RandomAccessIterator1 first1, RandomAccessIterator2 first2, RandomAccessIterator1 found) {
return sprout::pair<RandomAccessIterator1, RandomAccessIterator2>(
found, sprout::next(first2, sprout::distance(first1, found))
);
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
mismatch_impl_ra(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, BinaryPredicate pred,
typename std::iterator_traits<RandomAccessIterator1>::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<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
2013-01-03 08:38:45 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value && sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
sprout::pair<RandomAccessIterator1, RandomAccessIterator2>
>::type
mismatch(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, BinaryPredicate pred,
std::random_access_iterator_tag*
)
{
return first1 == last1 ? sprout::pair<RandomAccessIterator1, RandomAccessIterator2>(first1, first2)
: sprout::detail::mismatch_impl_check(
first1, first2,
sprout::detail::mismatch_impl_ra(
first1, last1, first2, pred,
sprout::distance(first1, last1) / 2, first1
)
)
;
}
2012-04-01 13:15:09 +00:00
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch_impl_check(sprout::tuples::tuple<InputIterator1, InputIterator2, bool> const& current) {
return sprout::pair<InputIterator1, InputIterator2>(
sprout::tuples::get<0>(current),
sprout::tuples::get<1>(current)
);
}
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, bool>
mismatch_impl_1(
sprout::tuples::tuple<InputIterator1, InputIterator2, bool> const& current,
InputIterator1 last1, BinaryPredicate pred, typename std::iterator_traits<InputIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, bool> 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<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, bool>
mismatch_impl(
sprout::tuples::tuple<InputIterator1, InputIterator2, bool> const& current,
InputIterator1 last1, BinaryPredicate pred, typename std::iterator_traits<InputIterator1>::difference_type n
)
{
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<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred,
2013-02-23 06:21:27 +00:00
std::input_iterator_tag*
)
{
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, bool> type;
return sprout::detail::mismatch_impl_check(
sprout::detail::mismatch_impl(type(first1, first2, false), last1, pred, 1)
);
}
2013-01-10 17:55:19 +00:00
} // namespace detail
2012-04-01 13:15:09 +00:00
// 25.2.10 Mismatch
2012-12-12 08:46:36 +00:00
//
// recursion depth:
// O(log N)
2012-12-12 08:46:36 +00:00
//
2012-04-01 13:15:09 +00:00
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) {
typedef typename sprout::common_iterator_category<InputIterator1, InputIterator2>::type* category;
return sprout::detail::mismatch(first1, last1, first2, pred, category());
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) {
return sprout::mismatch(first1, last1, first2, sprout::equal_to<>());
2012-04-01 13:15:09 +00:00
}
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
mismatch2_impl_ra(
2013-08-27 03:53:09 +00:00
RandomAccessIterator1 first1, RandomAccessIterator2 first2, BinaryPredicate pred,
typename std::iterator_traits<RandomAccessIterator1>::difference_type size
)
{
2013-08-27 03:53:09 +00:00
return sprout::detail::mismatch_impl_ra(
first1, sprout::next(first1, size), first2, pred,
size / 2, first1
);
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value && sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
sprout::pair<RandomAccessIterator1, RandomAccessIterator2>
>::type
mismatch(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
std::random_access_iterator_tag*
)
{
return first1 == last1 || first2 == last2 ? sprout::pair<RandomAccessIterator1, RandomAccessIterator2>(first1, first2)
: sprout::detail::mismatch_impl_check(
first1, first2,
sprout::detail::mismatch2_impl_ra(
2013-08-27 03:53:09 +00:00
first1, first2, pred,
NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2))
)
)
;
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch2_impl_check(sprout::tuples::tuple<InputIterator1, InputIterator2, bool> const& current) {
return sprout::pair<InputIterator1, InputIterator2>(
sprout::tuples::get<0>(current),
sprout::tuples::get<1>(current)
);
}
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, bool>
mismatch2_impl_1(
sprout::tuples::tuple<InputIterator1, InputIterator2, bool> const& current,
InputIterator1 last1, InputIterator2 last2, BinaryPredicate pred, typename std::iterator_traits<InputIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, bool> type;
return sprout::tuples::get<2>(current) || sprout::tuples::get<0>(current) == last1 || sprout::tuples::get<1>(current) == last2 ? 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::mismatch2_impl_1(
sprout::detail::mismatch2_impl_1(
current,
last1, last2, pred, n / 2
),
last1, last2, pred, n - n / 2
)
;
}
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, bool>
mismatch2_impl(
sprout::tuples::tuple<InputIterator1, InputIterator2, bool> const& current,
InputIterator1 last1, InputIterator2 last2, BinaryPredicate pred, typename std::iterator_traits<InputIterator1>::difference_type n
)
{
return sprout::tuples::get<2>(current) || sprout::tuples::get<0>(current) == last1 || sprout::tuples::get<1>(current) == last2 ? current
: sprout::detail::mismatch2_impl(
sprout::detail::mismatch2_impl_1(
current,
last1, last2, pred, n
),
last1, last2, pred, n * 2
)
;
}
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
std::input_iterator_tag*
)
{
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, bool> type;
return sprout::detail::mismatch2_impl_check(
sprout::detail::mismatch2_impl(type(first1, first2, false), last1, last2, pred, 1)
);
}
} // namespace detail
// 25.2.10 Mismatch
//
// recursion depth:
// O(log N)
//
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred) {
typedef typename sprout::common_iterator_category<InputIterator1, InputIterator2>::type* category;
return sprout::detail::mismatch(first1, last1, first2, last2, pred, category());
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) {
return sprout::mismatch(first1, last1, first2, last2, sprout::equal_to<>());
}
2012-04-01 13:15:09 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_MISMATCH_HPP