2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2018-12-30 08:25:21 +00:00
|
|
|
Copyright (c) 2011-2018 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_FIND_END_HPP
|
|
|
|
#define SPROUT_ALGORITHM_FIND_END_HPP
|
|
|
|
|
2013-01-03 08:38:45 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2013-02-26 07:14:04 +00:00
|
|
|
#include <sprout/iterator/type_traits/category.hpp>
|
2012-12-11 16:47:14 +00:00
|
|
|
#include <sprout/functional/equal_to.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2012-12-13 04:03:18 +00:00
|
|
|
#include <sprout/detail/algorithm/search_one.hpp>
|
2013-08-27 23:34:20 +00:00
|
|
|
#include <sprout/detail/algorithm/search_one_rev.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-12-11 03:59:36 +00:00
|
|
|
namespace detail {
|
2013-08-27 23:34:20 +00:00
|
|
|
template<typename RandomAccessIterator1>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
|
|
|
find_end_impl_check_ra(RandomAccessIterator1 first1, RandomAccessIterator1 last1_, RandomAccessIterator1 searched) {
|
|
|
|
return searched == first1 ? searched
|
|
|
|
: last1_
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
|
|
|
find_end_impl_ra(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
|
|
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 last1_,
|
|
|
|
RandomAccessIterator1 searched
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return searched != last1_ ? searched
|
|
|
|
: pivot == 0
|
|
|
|
? sprout::detail::find_end_impl_check_ra(
|
|
|
|
first1, last1_,
|
|
|
|
sprout::detail::search_one(first1, last1_, first2, last2, pred)
|
|
|
|
)
|
|
|
|
: sprout::detail::find_end_impl_ra(
|
|
|
|
first1, sprout::next(first1, pivot), first2, last2, pred,
|
|
|
|
pivot / 2, last1_,
|
|
|
|
sprout::detail::find_end_impl_ra(
|
|
|
|
sprout::next(first1, pivot), last1, first2, last2, pred,
|
|
|
|
(sprout::distance(first1, last1) - pivot) / 2, last1_,
|
|
|
|
last1_
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
|
|
|
find_end_impl_ra1(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
|
|
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type len1,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator2>::difference_type len2
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return len1 < len2 ? last1
|
|
|
|
: sprout::detail::find_end_impl_ra(
|
|
|
|
first1, last1 - len2 + 1, first2, last2, pred,
|
|
|
|
(len1 - len2 + 1) / 2, last1, last1
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2015-05-14 10:52:27 +00:00
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value && sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
|
2013-08-27 23:34:20 +00:00
|
|
|
RandomAccessIterator1
|
|
|
|
>::type
|
|
|
|
find_end(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
|
|
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::detail::find_end_impl_ra1(
|
|
|
|
first1, last1, first2, last2, pred,
|
|
|
|
sprout::distance(first1, last1), sprout::distance(first2, last2)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BidirectionalIterator1>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, BidirectionalIterator1>
|
|
|
|
find_end_impl_check_bi(
|
|
|
|
sprout::pair<BidirectionalIterator1, BidirectionalIterator1> const& current,
|
|
|
|
BidirectionalIterator1 first1, sprout::pair<BidirectionalIterator1, bool> searched
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<BidirectionalIterator1, BidirectionalIterator1> type;
|
|
|
|
return searched.second ? type(first1, current.second)
|
|
|
|
: searched.first == current.first ? type(sprout::prev(current.first), current.second)
|
|
|
|
: type(current.first, searched.first)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, BidirectionalIterator1>
|
|
|
|
find_end_impl_bi1(
|
|
|
|
sprout::pair<BidirectionalIterator1, BidirectionalIterator1> const& current,
|
|
|
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
|
|
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<BidirectionalIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return current.second != last1 || current.first == first1 ? current
|
|
|
|
: n == 1 ? sprout::detail::find_end_impl_check_bi(
|
|
|
|
current, first1,
|
|
|
|
sprout::detail::search_one_rev(first1, current.first, first2, last2, pred)
|
|
|
|
)
|
|
|
|
: sprout::detail::find_end_impl_bi1(
|
|
|
|
sprout::detail::find_end_impl_bi1(
|
|
|
|
current,
|
|
|
|
first1, last1, first2, last2, pred, n / 2
|
|
|
|
),
|
|
|
|
first1, last1, first2, last2, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, BidirectionalIterator1>
|
|
|
|
find_end_impl_bi(
|
|
|
|
sprout::pair<BidirectionalIterator1, BidirectionalIterator1> const& current,
|
|
|
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
|
|
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<BidirectionalIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return current.second != last1 || current.first == first1 ? current
|
|
|
|
: sprout::detail::find_end_impl_bi(
|
|
|
|
sprout::detail::find_end_impl_bi1(
|
|
|
|
current,
|
|
|
|
first1, last1, first2, last2, pred, n
|
|
|
|
),
|
|
|
|
first1, last1, first2, last2, pred, n * 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR BidirectionalIterator1
|
|
|
|
find_end(
|
|
|
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
|
|
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
|
|
|
std::bidirectional_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<BidirectionalIterator1, BidirectionalIterator1> type;
|
|
|
|
return sprout::detail::find_end_impl_bi(type(last1, last1), first1, last1, first2, last2, pred, 1).second;
|
|
|
|
}
|
2012-12-13 04:03:18 +00:00
|
|
|
|
2013-01-03 08:01:50 +00:00
|
|
|
template<typename ForwardIterator1>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, ForwardIterator1>
|
|
|
|
find_end_impl_check(sprout::pair<ForwardIterator1, ForwardIterator1> const& current, ForwardIterator1 last1, ForwardIterator1 searched) {
|
|
|
|
typedef sprout::pair<ForwardIterator1, ForwardIterator1> type;
|
|
|
|
return searched == current.first ? type(sprout::next(current.first), searched)
|
|
|
|
: searched == last1 ? type(last1, current.second)
|
|
|
|
: type(sprout::next(current.first), current.second)
|
|
|
|
;
|
|
|
|
}
|
2012-12-11 03:59:36 +00:00
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, ForwardIterator1>
|
|
|
|
find_end_impl_1(
|
|
|
|
sprout::pair<ForwardIterator1, ForwardIterator1> const& current,
|
|
|
|
ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<ForwardIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return current.first == last1 ? current
|
|
|
|
: n == 1 ? sprout::detail::find_end_impl_check(
|
|
|
|
current, last1,
|
|
|
|
sprout::detail::search_one(current.first, last1, first2, last2, pred)
|
|
|
|
)
|
|
|
|
: sprout::detail::find_end_impl_1(
|
|
|
|
sprout::detail::find_end_impl_1(
|
|
|
|
current,
|
|
|
|
last1, first2, last2, pred, n / 2
|
|
|
|
),
|
|
|
|
last1, first2, last2, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, ForwardIterator1>
|
2012-12-11 03:59:36 +00:00
|
|
|
find_end_impl(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::pair<ForwardIterator1, ForwardIterator1> const& current,
|
|
|
|
ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<ForwardIterator1>::difference_type n
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
return current.first == last1 ? current
|
2012-12-11 03:59:36 +00:00
|
|
|
: sprout::detail::find_end_impl(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::detail::find_end_impl_1(
|
|
|
|
current,
|
|
|
|
last1, first2, last2, pred, n
|
|
|
|
),
|
|
|
|
last1, first2, last2, pred, n * 2
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator1
|
|
|
|
find_end(
|
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::forward_iterator_tag*
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::pair<ForwardIterator1, ForwardIterator1> type;
|
|
|
|
return sprout::detail::find_end_impl(type(first1, last1), last1, first2, last2, pred, 1).second;
|
2012-12-11 03:59:36 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
2012-04-01 13:15:09 +00:00
|
|
|
// 25.2.6 Find end
|
2012-12-16 15:14:24 +00:00
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-10 17:55:19 +00:00
|
|
|
// O(log(N1+N2))
|
2012-12-16 15:14:24 +00:00
|
|
|
//
|
2012-04-01 13:15:09 +00:00
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator1
|
|
|
|
find_end(
|
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2,
|
2012-04-01 13:15:09 +00:00
|
|
|
BinaryPredicate pred
|
|
|
|
)
|
|
|
|
{
|
2013-08-27 23:34:20 +00:00
|
|
|
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
|
|
|
|
return first1 == last1 || first2 == last2 ? last1 : sprout::detail::find_end(first1, last1, first2, last2, pred, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator1
|
|
|
|
find_end(
|
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2012-12-13 04:03:18 +00:00
|
|
|
return sprout::find_end(first1, last1, first2, last2, sprout::equal_to<>());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_FIND_END_HPP
|