1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/algorithm/search.hpp

114 lines
3.8 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_SEARCH_HPP
#define SPROUT_ALGORITHM_SEARCH_HPP
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
2012-12-13 03:18:19 +00:00
#include <sprout/functional/equal_to.hpp>
#include <sprout/detail/algorithm/search_one.hpp>
2012-04-01 13:15:09 +00:00
namespace sprout {
2012-12-13 03:18:19 +00:00
namespace detail {
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
search_impl_ra(
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 last1_, RandomAccessIterator1 searched
)
{
return searched < first1 || searched == last1_ ? searched
: pivot == 0 ? sprout::detail::search_one(first1, last1_, first2, last2, pred)
: sprout::detail::search_impl_ra(
sprout::next(first1, pivot), last1, first2, last2, pred,
(sprout::distance(first1, last1) - pivot) / 2, last1_,
2012-12-13 03:18:19 +00:00
sprout::detail::search_impl_ra(
first1, sprout::next(first1, pivot), first2, last2, pred,
pivot / 2, last1_,
first1
)
)
;
}
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
search(
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
std::random_access_iterator_tag*
)
{
return first1 == last1 ? last1
: sprout::detail::search_impl_ra(
first1, last1, first2, last2, pred,
sprout::distance(first1, last1) / 2, last1, first1
2012-12-13 03:18:19 +00:00
)
;
}
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
)
{
return searched == first1 || searched == last1 ? searched
: sprout::detail::search_impl(
sprout::next(first1), last1, first2, last2, pred,
sprout::detail::search_one(sprout::next(first1), last1, first2, last2, pred)
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
void*
)
{
return sprout::detail::search_impl(
first1, last1, first2, last2, pred,
sprout::detail::search_one(first1, last1, first2, last2, pred)
);
}
} //namespace detail
2012-04-01 13:15:09 +00:00
// 25.2.13 Search
2012-12-13 03:18:19 +00:00
//
// recursion depth:
// [first1, last1) is RandomAccessIterator -> O(log N1)
// otherwise -> O(N1)
// [first2, last2) is RandomAccessIterator -> O(log N2)
// otherwise -> O(N2)
//
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR ForwardIterator1
search(
ForwardIterator1 first1, ForwardIterator1 last1,
2012-12-13 03:18:19 +00:00
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred
2012-04-01 13:15:09 +00:00
)
{
2012-12-13 03:18:19 +00:00
typedef typename std::iterator_traits<ForwardIterator1>::iterator_category* category;
return sprout::detail::search(first1, last1, first2, last2, pred, category());
2012-04-01 13:15:09 +00:00
}
2012-12-13 03:18:19 +00:00
template<typename ForwardIterator1, typename ForwardIterator2>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR ForwardIterator1
search(
ForwardIterator1 first1, ForwardIterator1 last1,
2012-12-13 03:18:19 +00:00
ForwardIterator2 first2, ForwardIterator2 last2
2012-04-01 13:15:09 +00:00
)
{
2012-12-13 03:18:19 +00:00
return sprout::search(first1, last1, first2, last2, sprout::equal_to<>());
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_SEARCH_HPP