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_SEARCH_N_HPP
|
|
|
|
#define SPROUT_ALGORITHM_SEARCH_N_HPP
|
|
|
|
|
2013-01-10 17:55:19 +00:00
|
|
|
#include <iterator>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2012-12-13 09:08:59 +00:00
|
|
|
#include <sprout/functional/equal_to.hpp>
|
2013-09-06 06:54:47 +00:00
|
|
|
#include <sprout/tuple.hpp>
|
|
|
|
#include <sprout/algorithm/find.hpp>
|
|
|
|
#include <sprout/algorithm/find_if.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2013-09-06 06:54:47 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename RandomAccessIterator, typename Size, typename T, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator
|
|
|
|
search_n_impl_ra(
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& current, RandomAccessIterator const& last,
|
2013-09-06 06:54:47 +00:00
|
|
|
Size count, T const& value, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator>::difference_type len,
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& searched
|
2013-09-06 06:54:47 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::distance(searched, current) >= count || searched == last ? searched
|
|
|
|
: len == 1 ? pred(*current, value) ? searched
|
|
|
|
: sprout::distance(current, last) > count ? sprout::next(current) : last
|
|
|
|
: sprout::detail::search_n_impl_ra(
|
|
|
|
current + len / 2, last, count, value, pred, len - len / 2,
|
|
|
|
sprout::detail::search_n_impl_ra(
|
|
|
|
current, last, count, value, pred, len / 2, searched
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator, typename Size, typename T, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator>::value,
|
|
|
|
RandomAccessIterator
|
|
|
|
>::type
|
|
|
|
search_n(
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& first, RandomAccessIterator const& last, Size count, T const& value, BinaryPredicate pred,
|
2013-09-06 06:54:47 +00:00
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::distance(first, last) < count
|
|
|
|
? last
|
|
|
|
: sprout::detail::search_n_impl_ra(
|
|
|
|
first, last, count, value, pred, sprout::distance(first, last), first
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename Size>
|
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
search_n_impl_check(
|
|
|
|
sprout::tuple<ForwardIterator, ForwardIterator, Size> current,
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& last, Size count
|
2013-09-06 06:54:47 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::tuples::get<2>(current) == count ? sprout::tuples::get<1>(current) : last;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename Size, typename T, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::tuple<ForwardIterator, ForwardIterator, Size>
|
|
|
|
search_n_impl_1(
|
|
|
|
sprout::tuple<ForwardIterator, ForwardIterator, Size> current,
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& last, Size count, T const& value, BinaryPredicate pred,
|
2013-09-06 06:54:47 +00:00
|
|
|
typename std::iterator_traits<ForwardIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::tuple<ForwardIterator, ForwardIterator, Size> type;
|
|
|
|
return sprout::tuples::get<2>(current) == count || sprout::tuples::get<0>(current) == last ? current
|
|
|
|
: n == 1
|
|
|
|
? pred(*sprout::tuples::get<0>(current), value)
|
|
|
|
? type(sprout::next(sprout::tuples::get<0>(current)), sprout::tuples::get<1>(current), sprout::tuples::get<2>(current) + 1)
|
|
|
|
: type(sprout::next(sprout::tuples::get<0>(current)), sprout::next(sprout::tuples::get<0>(current)), 0)
|
|
|
|
: sprout::detail::search_n_impl_1(
|
|
|
|
sprout::detail::search_n_impl_1(
|
|
|
|
current,
|
|
|
|
last, count, value, pred, n / 2
|
|
|
|
),
|
|
|
|
last, count, value, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename Size, typename T, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::tuple<ForwardIterator, ForwardIterator, Size>
|
|
|
|
search_n_impl(
|
|
|
|
sprout::tuple<ForwardIterator, ForwardIterator, Size> current,
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& last, Size count, T const& value, BinaryPredicate pred,
|
2013-09-06 06:54:47 +00:00
|
|
|
typename std::iterator_traits<ForwardIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::tuples::get<2>(current) == count || sprout::tuples::get<0>(current) == last ? current
|
|
|
|
: sprout::detail::search_n_impl(
|
|
|
|
sprout::detail::search_n_impl_1(
|
|
|
|
current,
|
|
|
|
last, count, value, pred, n
|
|
|
|
),
|
|
|
|
last, count, value, pred, n * 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename Size, typename T, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
search_n(
|
2016-04-01 14:37:48 +00:00
|
|
|
ForwardIterator const& first, ForwardIterator const& last, Size count, T const& value, BinaryPredicate pred,
|
2013-09-06 06:54:47 +00:00
|
|
|
std::forward_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::tuple<ForwardIterator, ForwardIterator, Size> type;
|
|
|
|
return sprout::detail::search_n_impl_check(
|
|
|
|
sprout::detail::search_n_impl(type(first, first, 0), last, count, value, pred, 1),
|
|
|
|
last, count
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 13:15:09 +00:00
|
|
|
// 25.2.13 Search
|
2012-12-13 09:08:59 +00:00
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-10 17:55:19 +00:00
|
|
|
// O(log(N1+N2))
|
2012-12-13 09:08:59 +00:00
|
|
|
//
|
|
|
|
template<typename ForwardIterator, typename Size, typename T, typename BinaryPredicate>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
2012-12-13 09:08:59 +00:00
|
|
|
search_n(ForwardIterator first, ForwardIterator last, Size count, T const& value, BinaryPredicate pred) {
|
2013-09-06 06:54:47 +00:00
|
|
|
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
|
|
|
|
return count == 0 ? last : count == 1 ? sprout::find_if(first, last, sprout::bind2nd(pred, value))
|
|
|
|
: sprout::detail::search_n(first, last, count, value, pred, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 09:08:59 +00:00
|
|
|
template<typename ForwardIterator, typename Size, typename T>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
2012-12-13 09:08:59 +00:00
|
|
|
search_n(ForwardIterator first, ForwardIterator last, Size count, T const& value) {
|
2013-09-06 06:54:47 +00:00
|
|
|
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
|
|
|
|
return count == 0 ? last : count == 1 ? sprout::find(first, last, value)
|
|
|
|
: sprout::detail::search_n(first, last, count, value, sprout::equal_to<>(), category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_SEARCH_N_HPP
|