2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 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_ADJACENT_FIND_HPP
|
|
|
|
#define SPROUT_ALGORITHM_ADJACENT_FIND_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>
|
2013-02-26 07:14:04 +00:00
|
|
|
#include <sprout/iterator/type_traits/category.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
|
|
|
|
|
|
|
namespace sprout {
|
2012-12-11 03:59:36 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename RandomAccessIterator>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator
|
2016-04-01 14:37:48 +00:00
|
|
|
adjacent_find_impl_check_ra(RandomAccessIterator const& found, RandomAccessIterator const& last) {
|
2013-01-03 08:01:50 +00:00
|
|
|
return sprout::distance(found, last) == 1 ? last
|
2012-12-11 03:59:36 +00:00
|
|
|
: found
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator
|
|
|
|
adjacent_find_impl_ra(
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& first, RandomAccessIterator const& last, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot, RandomAccessIterator const& found
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return found != first ? found
|
|
|
|
: pivot == 0 ? (pred(*first, *last) ? first : last)
|
|
|
|
: sprout::detail::adjacent_find_impl_ra(
|
|
|
|
sprout::next(first, pivot), last, pred,
|
2013-01-03 08:01:50 +00:00
|
|
|
(sprout::distance(first, last) - pivot) / 2,
|
2012-12-11 03:59:36 +00:00
|
|
|
sprout::detail::adjacent_find_impl_ra(
|
|
|
|
first, sprout::next(first, pivot), pred,
|
|
|
|
pivot / 2,
|
|
|
|
first
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator>::value,
|
|
|
|
RandomAccessIterator
|
|
|
|
>::type
|
2012-12-11 03:59:36 +00:00
|
|
|
adjacent_find(
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& first, RandomAccessIterator const& last, BinaryPredicate pred,
|
2012-12-11 03:59:36 +00:00
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
return first == last || sprout::distance(first, last) == 1 ? last
|
|
|
|
: adjacent_find_impl_check_ra(
|
2012-12-11 03:59:36 +00:00
|
|
|
sprout::detail::adjacent_find_impl_ra(
|
2013-01-03 08:01:50 +00:00
|
|
|
first, sprout::next(first, sprout::distance(first, last) - 1), pred,
|
|
|
|
(sprout::distance(first, last) - 1) / 2, first
|
2012-12-11 03:59:36 +00:00
|
|
|
),
|
|
|
|
last
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2013-01-03 08:01:50 +00:00
|
|
|
template<typename ForwardIterator>
|
2012-12-11 03:59:36 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
2016-04-01 14:37:48 +00:00
|
|
|
adjacent_find_impl_check(ForwardIterator const& found, ForwardIterator const& last) {
|
2013-01-10 17:55:19 +00:00
|
|
|
return sprout::next(found) == last ? last
|
|
|
|
: found
|
2013-01-03 08:01:50 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename BinaryPredicate>
|
2013-01-31 14:25:18 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
2013-01-03 08:01:50 +00:00
|
|
|
adjacent_find_impl_1(
|
2013-01-10 17:55:19 +00:00
|
|
|
sprout::pair<ForwardIterator, ForwardIterator> const& current,
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& last, BinaryPredicate pred, typename std::iterator_traits<ForwardIterator>::difference_type n
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-10 17:55:19 +00:00
|
|
|
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
|
|
|
|
return current.second == last ? current
|
|
|
|
: n == 1 ? pred(*current.first, *current.second)
|
|
|
|
? type(current.first, last)
|
|
|
|
: type(current.second, sprout::next(current.second))
|
2013-01-03 08:01:50 +00:00
|
|
|
: sprout::detail::adjacent_find_impl_1(
|
|
|
|
sprout::detail::adjacent_find_impl_1(
|
|
|
|
current,
|
|
|
|
last, pred, n / 2
|
|
|
|
),
|
|
|
|
last, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename BinaryPredicate>
|
2013-01-31 14:25:18 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
2013-01-03 08:01:50 +00:00
|
|
|
adjacent_find_impl(
|
2013-01-10 17:55:19 +00:00
|
|
|
sprout::pair<ForwardIterator, ForwardIterator> const& current,
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& last, BinaryPredicate pred, typename std::iterator_traits<ForwardIterator>::difference_type n
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-10 17:55:19 +00:00
|
|
|
return current.second == last ? current
|
2013-01-03 08:01:50 +00:00
|
|
|
: sprout::detail::adjacent_find_impl(
|
|
|
|
sprout::detail::adjacent_find_impl_1(
|
|
|
|
current,
|
|
|
|
last, pred, n
|
|
|
|
),
|
|
|
|
last, pred, n * 2
|
|
|
|
)
|
2012-12-11 03:59:36 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
adjacent_find(
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& first, ForwardIterator const& last, BinaryPredicate pred,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::forward_iterator_tag*
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-10 17:55:19 +00:00
|
|
|
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
|
2012-12-11 03:59:36 +00:00
|
|
|
return first == last ? last
|
2013-01-03 08:01:50 +00:00
|
|
|
: sprout::detail::adjacent_find_impl_check(
|
2013-01-10 17:55:19 +00:00
|
|
|
sprout::detail::adjacent_find_impl(type(first, sprout::next(first)), last, pred, 1).first,
|
|
|
|
last
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
2012-12-11 03:59:36 +00:00
|
|
|
;
|
|
|
|
}
|
2013-01-10 17:55:19 +00:00
|
|
|
} // namespace detail
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
// 25.2.8 Adjacent find
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-03 08:01:50 +00:00
|
|
|
// O(log N)
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
2012-04-01 13:15:09 +00:00
|
|
|
template<typename ForwardIterator, typename BinaryPredicate>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred) {
|
2012-12-11 03:59:36 +00:00
|
|
|
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
|
|
|
|
return sprout::detail::adjacent_find(first, last, pred, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardIterator>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
adjacent_find(ForwardIterator first, ForwardIterator last) {
|
2012-04-01 13:15:09 +00:00
|
|
|
return sprout::adjacent_find(
|
2012-10-06 04:53:07 +00:00
|
|
|
first, last,
|
2012-04-01 13:15:09 +00:00
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::equal_to<typename std::iterator_traits<ForwardIterator>::value_type>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_ADJACENT_FIND_HPP
|