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_FIND_FIRST_OF_HPP
|
|
|
|
#define SPROUT_ALGORITHM_FIND_FIRST_OF_HPP
|
|
|
|
|
2012-12-11 03:59:36 +00:00
|
|
|
#include <iterator>
|
2013-01-03 08:38:45 +00:00
|
|
|
#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-11 03:59:36 +00:00
|
|
|
#include <sprout/functional/equal_to.hpp>
|
2013-08-27 05:43:01 +00:00
|
|
|
#include <sprout/functional/bind1st.hpp>
|
2012-12-11 16:47:14 +00:00
|
|
|
#include <sprout/algorithm/find_if.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-12-11 03:59:36 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
|
|
|
find_first_of_impl_ra(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 found
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return found != first1 ? found
|
2013-08-27 05:43:01 +00:00
|
|
|
: pivot == 0 ? sprout::find_if(first2, last2, sprout::bind1st(pred, *first1)) != last2 ? first1 : last1
|
2012-12-11 03:59:36 +00:00
|
|
|
: sprout::detail::find_first_of_impl_ra(
|
|
|
|
sprout::next(first1, pivot), last1, first2, last2, pred,
|
2013-01-03 08:01:50 +00:00
|
|
|
(sprout::distance(first1, last1) - pivot) / 2,
|
2012-12-11 03:59:36 +00:00
|
|
|
sprout::detail::find_first_of_impl_ra(
|
|
|
|
first1, sprout::next(first1, pivot), first2, last2, pred,
|
|
|
|
pivot / 2,
|
|
|
|
first1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
|
|
|
|
RandomAccessIterator1
|
|
|
|
>::type
|
2012-12-11 03:59:36 +00:00
|
|
|
find_first_of(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first1 == last1 ? last1
|
|
|
|
: sprout::detail::find_first_of_impl_ra(
|
|
|
|
first1, last1, first2, last2,
|
2013-01-03 08:01:50 +00:00
|
|
|
pred, sprout::distance(first1, last1) / 2, first1
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, bool>
|
|
|
|
find_first_of_impl_1(
|
|
|
|
sprout::pair<InputIterator1, bool> current,
|
|
|
|
InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<InputIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<InputIterator1, bool> type;
|
|
|
|
return current.second || current.first == last1 ? current
|
2013-08-27 05:43:01 +00:00
|
|
|
: n == 1 ? sprout::find_if(first2, last2, sprout::bind1st(pred, *current.first)) != last2
|
2013-01-03 08:01:50 +00:00
|
|
|
? type(current.first, true)
|
|
|
|
: type(sprout::next(current.first), false)
|
|
|
|
: sprout::detail::find_first_of_impl_1(
|
|
|
|
sprout::detail::find_first_of_impl_1(
|
|
|
|
current,
|
|
|
|
last1, first2, last2, pred, n / 2
|
|
|
|
),
|
|
|
|
last1, first2, last2, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, bool>
|
2012-12-11 03:59:36 +00:00
|
|
|
find_first_of_impl(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::pair<InputIterator1, bool> current,
|
|
|
|
InputIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<InputIterator1>::difference_type n
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
return current.second || current.first == last1 ? current
|
|
|
|
: sprout::detail::find_first_of_impl(
|
|
|
|
sprout::detail::find_first_of_impl_1(
|
|
|
|
current,
|
|
|
|
last1, first2, last2, pred, n
|
|
|
|
),
|
|
|
|
last1, first2, last2, pred, n * 2
|
|
|
|
)
|
2012-12-11 03:59:36 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator1
|
|
|
|
find_first_of(
|
|
|
|
InputIterator1 first1, InputIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2,
|
|
|
|
BinaryPredicate pred,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::input_iterator_tag*
|
2012-12-11 03:59:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::pair<InputIterator1, bool> type;
|
|
|
|
return sprout::detail::find_first_of_impl(type(first1, false), last1, first2, last2, pred, 1).first;
|
2012-12-11 03:59:36 +00:00
|
|
|
}
|
2013-01-10 17:55:19 +00:00
|
|
|
} // namespace detail
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
// 25.2.7 Find first
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-10 17:55:19 +00:00
|
|
|
// O(log(N1+N2))
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
2012-12-11 03:59:36 +00:00
|
|
|
template<typename InputIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator1
|
2012-10-06 04:53:07 +00:00
|
|
|
find_first_of(
|
2012-12-11 03:59:36 +00:00
|
|
|
InputIterator1 first1, InputIterator1 last1,
|
2012-10-06 04:53:07 +00:00
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2,
|
2012-04-01 13:15:09 +00:00
|
|
|
BinaryPredicate pred
|
|
|
|
)
|
|
|
|
{
|
2012-12-11 03:59:36 +00:00
|
|
|
typedef typename std::iterator_traits<InputIterator1>::iterator_category* category;
|
|
|
|
return sprout::detail::find_first_of(first1, last1, first2, last2, pred, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 03:59:36 +00:00
|
|
|
template<typename InputIterator1, typename ForwardIterator2>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator1
|
2012-10-06 04:53:07 +00:00
|
|
|
find_first_of(
|
2012-12-11 03:59:36 +00:00
|
|
|
InputIterator1 first1, InputIterator1 last1,
|
2012-10-06 04:53:07 +00:00
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2012-12-11 03:59:36 +00:00
|
|
|
return sprout::find_first_of(first1, last1, first2, last2, sprout::equal_to<>());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_FIND_FIRST_OF_HPP
|