mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-04 14:14:09 +00:00
fix recursion depth: some algorithms
This commit is contained in:
parent
bb59363682
commit
e2b207d3be
34 changed files with 796 additions and 87 deletions
|
@ -1,9 +1,13 @@
|
|||
#ifndef SPROUT_ALGORITHM_SEARCH_HPP
|
||||
#define SPROUT_ALGORITHM_SEARCH_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/iterator/type_traits/is_iterator.hpp>
|
||||
#include <sprout/functional/equal_to.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
#include <sprout/detail/algorithm/search_one.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
@ -31,7 +35,10 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
|
||||
RandomAccessIterator1
|
||||
>::type
|
||||
search(
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
||||
ForwardIterator2 first2, ForwardIterator2 last2,
|
||||
|
@ -47,19 +54,53 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
|
||||
template<typename ForwardIterator1>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
|
||||
search_impl_check(sprout::pair<ForwardIterator1, bool> const& current, ForwardIterator1 last1, ForwardIterator1 searched) {
|
||||
typedef sprout::pair<ForwardIterator1, bool> type;
|
||||
return searched == current.first || searched == last1 ? type(searched, true)
|
||||
: type(sprout::next(current.first), false)
|
||||
;
|
||||
}
|
||||
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator1
|
||||
search_impl(
|
||||
ForwardIterator1 first1, ForwardIterator1 last1,
|
||||
ForwardIterator2 first2, ForwardIterator2 last2,
|
||||
BinaryPredicate pred,
|
||||
ForwardIterator1 searched
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
|
||||
search_impl_1(
|
||||
sprout::pair<ForwardIterator1, bool> const& current,
|
||||
ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
||||
typename std::iterator_traits<ForwardIterator1>::difference_type n
|
||||
)
|
||||
{
|
||||
return searched == first1 || searched == last1 ? searched
|
||||
typedef sprout::pair<ForwardIterator1, bool> type;
|
||||
return current.second || current.first == last1 ? current
|
||||
: n == 1 ? sprout::detail::search_impl_check(
|
||||
current, last1,
|
||||
sprout::detail::search_one(current.first, last1, first2, last2, pred)
|
||||
)
|
||||
: sprout::detail::search_impl_1(
|
||||
sprout::detail::search_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, bool>
|
||||
search_impl(
|
||||
sprout::pair<ForwardIterator1, bool> const& current,
|
||||
ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
||||
typename std::iterator_traits<ForwardIterator1>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<ForwardIterator1, bool> type;
|
||||
return current.second || current.first == last1 ? current
|
||||
: sprout::detail::search_impl(
|
||||
sprout::next(first1), last1, first2, last2, pred,
|
||||
sprout::detail::search_one(sprout::next(first1), last1, first2, last2, pred)
|
||||
sprout::detail::search_impl_1(
|
||||
current,
|
||||
last1, first2, last2, pred, n
|
||||
),
|
||||
last1, first2, last2, pred, n * 2
|
||||
)
|
||||
;
|
||||
}
|
||||
|
@ -72,20 +113,15 @@ namespace sprout {
|
|||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::search_impl(
|
||||
first1, last1, first2, last2, pred,
|
||||
sprout::detail::search_one(first1, last1, first2, last2, pred)
|
||||
);
|
||||
typedef sprout::pair<ForwardIterator1, bool> type;
|
||||
return sprout::detail::search_impl(type(first1, false), last1, first2, last2, pred, 1).first;
|
||||
}
|
||||
} //namespace detail
|
||||
} // namespace detail
|
||||
|
||||
// 25.2.13 Search
|
||||
//
|
||||
// recursion depth:
|
||||
// [first1, last1) is RandomAccessIterator -> O(log N1)
|
||||
// otherwise -> O(N1)
|
||||
// [first2, last2) is RandomAccessIterator -> O(log N2)
|
||||
// otherwise -> O(N2)
|
||||
// O(log(N1+N2))
|
||||
//
|
||||
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue