Sprout/sprout/detail/algorithm/search_one.hpp

127 lines
5.7 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-12-13 03:18:19 +00:00
#ifndef SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_HPP
#define SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
2012-12-21 14:12:54 +00:00
#include <sprout/iterator/type_traits/common.hpp>
2012-12-13 03:18:19 +00:00
#include <sprout/functional/equal_to.hpp>
#include <sprout/algorithm/mismatch.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.hpp>
2012-12-13 03:18:19 +00:00
namespace sprout {
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
search_one_impl_ra(
2013-07-22 13:00:09 +00:00
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2, BinaryPredicate pred
2012-12-13 03:18:19 +00:00
)
{
return sprout::mismatch(first1, last1, first2, pred).first == last1 ? first1
2012-12-13 03:18:19 +00:00
: last1
;
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
2015-05-14 10:52:27 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value && sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
RandomAccessIterator1
>::type
2012-12-13 03:18:19 +00:00
search_one(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
std::random_access_iterator_tag*
)
{
return sprout::distance(first1, last1) < sprout::distance(first2, last2) ? last1
2012-12-13 03:18:19 +00:00
: first1 == last1 ? first1
: sprout::detail::search_one_impl_ra(
first1, sprout::next(first1, sprout::distance(first2, last2)), first2, last2, pred
2012-12-13 03:18:19 +00:00
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool>
search_one_impl_1(
sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> const& current,
ForwardIterator1 last1, ForwardIterator2 last2, BinaryPredicate pred,
ForwardIterator1 first1_, typename std::iterator_traits<ForwardIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> type;
return sprout::tuples::get<2>(current) ? current
: sprout::tuples::get<1>(current) == last2 ? type(first1_, last2, true)
: sprout::tuples::get<0>(current) == last1 ? type(last1, last2, true)
: n == 1 ? !pred(*sprout::tuples::get<0>(current), *sprout::tuples::get<1>(current))
? type(sprout::next(sprout::tuples::get<0>(current)), last2, true)
: type(sprout::next(sprout::tuples::get<0>(current)), sprout::next(sprout::tuples::get<1>(current)), false)
: sprout::detail::search_one_impl_1(
sprout::detail::search_one_impl_1(
current,
last1, last2, pred, first1_, n / 2
),
last1, last2, pred, first1_, n - n / 2
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool>
2012-12-13 03:18:19 +00:00
search_one_impl(
sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> const& current,
ForwardIterator1 last1, ForwardIterator2 last2, BinaryPredicate pred,
ForwardIterator1 first1_, typename std::iterator_traits<ForwardIterator1>::difference_type n
2012-12-13 03:18:19 +00:00
)
{
typedef sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> type;
return sprout::tuples::get<2>(current) ? current
: sprout::tuples::get<1>(current) == last2 ? type(first1_, last2, true)
: sprout::tuples::get<0>(current) == last1 ? type(last1, last2, true)
: sprout::detail::search_one_impl(
sprout::detail::search_one_impl_1(
current,
last1, last2, pred, first1_, n
),
last1, last2, pred, first1_, n * 2
)
2012-12-13 03:18:19 +00:00
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one(
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
2013-02-23 06:21:27 +00:00
std::forward_iterator_tag*
2012-12-13 03:18:19 +00:00
)
{
typedef sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> type;
2013-01-12 16:16:48 +00:00
return sprout::tuples::get<0>(sprout::detail::search_one_impl(type(first1, first2, false), last1, last2, pred, first1, 1));
2012-12-13 03:18:19 +00:00
}
//
// search_one
//
// 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>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) {
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
return sprout::detail::search_one(first1, last1, first2, last2, pred, category());
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) {
return sprout::detail::search_one(first1, last1, first2, last2, sprout::equal_to<>());
}
2013-01-10 17:55:19 +00:00
} // namespace detail
2012-12-13 03:18:19 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_HPP