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

162 lines
6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2016-02-25 09:48:28 +00:00
Copyright (c) 2011-2016 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_HPP
#define SPROUT_ALGORITHM_SEARCH_HPP
2013-01-10 17:55:19 +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-13 03:18:19 +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 03:18:19 +00:00
#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(
2016-04-01 14:37:48 +00:00
RandomAccessIterator1 const& first1, RandomAccessIterator1 const& last1,
ForwardIterator2 const& first2, ForwardIterator2 const& last2,
2012-12-13 03:18:19 +00:00
BinaryPredicate pred,
2016-04-01 14:37:48 +00:00
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 const& last1_, RandomAccessIterator1 const& searched
2012-12-13 03:18:19 +00:00
)
{
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>
2013-01-10 17:55:19 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
RandomAccessIterator1
>::type
2012-12-13 03:18:19 +00:00
search(
2016-04-01 14:37:48 +00:00
RandomAccessIterator1 const& first1, RandomAccessIterator1 const& last1,
ForwardIterator2 const& first2, ForwardIterator2 const& last2,
2012-12-13 03:18:19 +00:00
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
)
;
}
2013-01-10 17:55:19 +00:00
template<typename ForwardIterator1>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
2016-04-01 14:37:48 +00:00
search_impl_fork(sprout::pair<ForwardIterator1, bool> const& current, ForwardIterator1 const& last1, ForwardIterator1 const& searched) {
2013-01-10 17:55:19 +00:00
typedef sprout::pair<ForwardIterator1, bool> type;
return searched == current.first || searched == last1 ? type(searched, true)
: type(sprout::next(current.first), false)
;
}
2012-12-13 03:18:19 +00:00
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
2013-01-10 17:55:19 +00:00
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
search_impl_1(
sprout::pair<ForwardIterator1, bool> const& current,
2016-04-01 14:37:48 +00:00
ForwardIterator1 const& last1, ForwardIterator2 const& first2, ForwardIterator2 const& last2, BinaryPredicate pred,
2013-01-10 17:55:19 +00:00
typename std::iterator_traits<ForwardIterator1>::difference_type n
)
{
return current.second || current.first == last1 ? current
2013-01-12 16:16:48 +00:00
: n == 1 ? sprout::detail::search_impl_fork(
2013-01-10 17:55:19 +00:00
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>
2012-12-13 03:18:19 +00:00
search_impl(
2013-01-10 17:55:19 +00:00
sprout::pair<ForwardIterator1, bool> const& current,
2016-04-01 14:37:48 +00:00
ForwardIterator1 const& last1, ForwardIterator2 const& first2, ForwardIterator2 const& last2, BinaryPredicate pred,
2013-01-10 17:55:19 +00:00
typename std::iterator_traits<ForwardIterator1>::difference_type n
2012-12-13 03:18:19 +00:00
)
{
2013-01-10 17:55:19 +00:00
return current.second || current.first == last1 ? current
2012-12-13 03:18:19 +00:00
: sprout::detail::search_impl(
2013-01-10 17:55:19 +00:00
sprout::detail::search_impl_1(
current,
last1, first2, last2, pred, n
),
last1, first2, last2, pred, n * 2
2012-12-13 03:18:19 +00:00
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search(
2016-04-01 14:37:48 +00:00
ForwardIterator1 const& first1, ForwardIterator1 const& last1,
ForwardIterator2 const& first2, ForwardIterator2 const& last2,
2012-12-13 03:18:19 +00:00
BinaryPredicate pred,
2013-02-23 06:21:27 +00:00
std::forward_iterator_tag*
2012-12-13 03:18:19 +00:00
)
{
2013-01-10 17:55:19 +00:00
typedef sprout::pair<ForwardIterator1, bool> type;
return sprout::detail::search_impl(type(first1, false), last1, first2, last2, pred, 1).first;
2012-12-13 03:18:19 +00:00
}
2013-01-10 17:55:19 +00:00
} // namespace detail
2012-04-01 13:15:09 +00:00
// 25.2.13 Search
2012-12-13 03:18:19 +00:00
//
// recursion depth:
2013-01-10 17:55:19 +00:00
// O(log(N1+N2))
2012-12-13 03:18:19 +00:00
//
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
}
2014-12-14 03:59:51 +00:00
//
template<typename ForwardIterator, typename Searcher>
inline SPROUT_CONSTEXPR ForwardIterator
search(ForwardIterator first, ForwardIterator last, Searcher const& searcher) {
return searcher(first, last);
}
2012-04-01 13:15:09 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_SEARCH_HPP