mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
Merge pull request #27 from kariya-mitsuru/find_end
[algorithm.find_end] Change find_end
This commit is contained in:
commit
11b172e02d
2 changed files with 250 additions and 67 deletions
|
@ -16,74 +16,148 @@
|
||||||
#include <sprout/functional/equal_to.hpp>
|
#include <sprout/functional/equal_to.hpp>
|
||||||
#include <sprout/utility/pair/pair.hpp>
|
#include <sprout/utility/pair/pair.hpp>
|
||||||
#include <sprout/detail/algorithm/search_one.hpp>
|
#include <sprout/detail/algorithm/search_one.hpp>
|
||||||
|
#include <sprout/detail/algorithm/search_one_rev.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
// !!!
|
template<typename RandomAccessIterator1>
|
||||||
// template<typename RandomAccessIterator1>
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
||||||
// inline SPROUT_CONSTEXPR RandomAccessIterator1
|
find_end_impl_check_ra(RandomAccessIterator1 first1, RandomAccessIterator1 last1_, RandomAccessIterator1 searched) {
|
||||||
// find_end_impl_check_ra(RandomAccessIterator1 first1, RandomAccessIterator1 result, RandomAccessIterator1 searched) {
|
return searched == first1 ? searched
|
||||||
// return searched == first1 ? searched
|
: last1_
|
||||||
// : result
|
;
|
||||||
// ;
|
}
|
||||||
// }
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
||||||
// template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
||||||
// inline SPROUT_CONSTEXPR RandomAccessIterator1
|
find_end_impl_ra(
|
||||||
// find_end_impl_ra(
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
||||||
// RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
||||||
// ForwardIterator2 first2, ForwardIterator2 last2,
|
BinaryPredicate pred,
|
||||||
// BinaryPredicate pred,
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 last1_,
|
||||||
// typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 last1_, RandomAccessIterator1 result,
|
RandomAccessIterator1 searched
|
||||||
// RandomAccessIterator1 searched
|
)
|
||||||
// )
|
{
|
||||||
// {
|
return searched != last1_ ? searched
|
||||||
// return searched == last1_ ? result
|
: pivot == 0
|
||||||
// : searched < first1 ? pivot == 0
|
? sprout::detail::find_end_impl_check_ra(
|
||||||
// ? sprout::detail::find_end_impl_check_ra(
|
first1, last1_,
|
||||||
// first1, searched,
|
sprout::detail::search_one(first1, last1_, first2, last2, pred)
|
||||||
// sprout::detail::search_one(first1, last1_, first2, last2, pred)
|
)
|
||||||
// )
|
: sprout::detail::find_end_impl_ra(
|
||||||
// : sprout::detail::find_end_impl_ra(
|
first1, sprout::next(first1, pivot), first2, last2, pred,
|
||||||
// sprout::next(first1, pivot), last1, first2, last2, pred,
|
pivot / 2, last1_,
|
||||||
// (sprout::distance(first1, last1) - pivot) / 2, last1_, searched,
|
sprout::detail::find_end_impl_ra(
|
||||||
// sprout::detail::find_end_impl_ra(
|
sprout::next(first1, pivot), last1, first2, last2, pred,
|
||||||
// first1, sprout::next(first1, pivot), first2, last2, pred,
|
(sprout::distance(first1, last1) - pivot) / 2, last1_,
|
||||||
// pivot / 2, last1_, searched,
|
last1_
|
||||||
// first1
|
)
|
||||||
// )
|
)
|
||||||
// )
|
;
|
||||||
// : pivot == 0 ? sprout::detail::search_one(first1, last1_, first2, last2, pred)
|
}
|
||||||
// : sprout::detail::find_end_impl_ra(
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
||||||
// sprout::next(first1, pivot), last1, first2, last2, pred,
|
inline SPROUT_CONSTEXPR RandomAccessIterator1
|
||||||
// (sprout::distance(first1, last1) - pivot) / 2, last1_, result,
|
find_end_impl_ra1(
|
||||||
// sprout::detail::find_end_impl_ra(
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
||||||
// first1, sprout::next(first1, pivot), first2, last2, pred,
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
||||||
// pivot / 2, last1_, result,
|
BinaryPredicate pred,
|
||||||
// first1
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type len1,
|
||||||
// )
|
typename std::iterator_traits<RandomAccessIterator2>::difference_type len2
|
||||||
// )
|
)
|
||||||
// ;
|
{
|
||||||
// }
|
return len1 < len2 ? last1
|
||||||
// template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
: sprout::detail::find_end_impl_ra(
|
||||||
// inline SPROUT_CONSTEXPR typename std::enable_if<
|
first1, last1 - len2 + 1, first2, last2, pred,
|
||||||
// sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
|
(len1 - len2 + 1) / 2, last1, last1
|
||||||
// RandomAccessIterator1
|
)
|
||||||
// >::type
|
;
|
||||||
// find_end(
|
}
|
||||||
// RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
||||||
// ForwardIterator2 first2, ForwardIterator2 last2,
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||||
// BinaryPredicate pred,
|
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value
|
||||||
// std::random_access_iterator_tag*
|
&& sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
|
||||||
// )
|
RandomAccessIterator1
|
||||||
// {
|
>::type
|
||||||
// return first1 == last1 ? last1
|
find_end(
|
||||||
// : sprout::detail::find_end_impl_ra(
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
||||||
// first1, last1, first2, last2, pred,
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
||||||
// sprout::distance(first1, last1) / 2, last1, last1,
|
BinaryPredicate pred,
|
||||||
// first1
|
std::random_access_iterator_tag*
|
||||||
// )
|
)
|
||||||
// ;
|
{
|
||||||
// }
|
return sprout::detail::find_end_impl_ra1(
|
||||||
|
first1, last1, first2, last2, pred,
|
||||||
|
sprout::distance(first1, last1), sprout::distance(first2, last2)
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename BidirectionalIterator1>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, BidirectionalIterator1>
|
||||||
|
find_end_impl_check_bi(
|
||||||
|
sprout::pair<BidirectionalIterator1, BidirectionalIterator1> const& current,
|
||||||
|
BidirectionalIterator1 first1, sprout::pair<BidirectionalIterator1, bool> searched
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef sprout::pair<BidirectionalIterator1, BidirectionalIterator1> type;
|
||||||
|
return searched.second ? type(first1, current.second)
|
||||||
|
: searched.first == current.first ? type(sprout::prev(current.first), current.second)
|
||||||
|
: type(current.first, searched.first)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, BidirectionalIterator1>
|
||||||
|
find_end_impl_bi1(
|
||||||
|
sprout::pair<BidirectionalIterator1, BidirectionalIterator1> const& current,
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
||||||
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2, BinaryPredicate pred,
|
||||||
|
typename std::iterator_traits<BidirectionalIterator1>::difference_type n
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return current.second != last1 || current.first == first1 ? current
|
||||||
|
: n == 1 ? sprout::detail::find_end_impl_check_bi(
|
||||||
|
current, first1,
|
||||||
|
sprout::detail::search_one_rev(first1, current.first, first2, last2, pred)
|
||||||
|
)
|
||||||
|
: sprout::detail::find_end_impl_bi1(
|
||||||
|
sprout::detail::find_end_impl_bi1(
|
||||||
|
current,
|
||||||
|
first1, last1, first2, last2, pred, n / 2
|
||||||
|
),
|
||||||
|
first1, last1, first2, last2, pred, n - n / 2
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, BidirectionalIterator1>
|
||||||
|
find_end_impl_bi(
|
||||||
|
sprout::pair<BidirectionalIterator1, BidirectionalIterator1> const& current,
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
||||||
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2, BinaryPredicate pred,
|
||||||
|
typename std::iterator_traits<BidirectionalIterator1>::difference_type n
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return current.second != last1 || current.first == first1 ? current
|
||||||
|
: sprout::detail::find_end_impl_bi(
|
||||||
|
sprout::detail::find_end_impl_bi1(
|
||||||
|
current,
|
||||||
|
first1, last1, first2, last2, pred, n
|
||||||
|
),
|
||||||
|
first1, last1, first2, last2, pred, n * 2
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
||||||
|
inline SPROUT_CONSTEXPR BidirectionalIterator1
|
||||||
|
find_end(
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
||||||
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2,
|
||||||
|
BinaryPredicate pred,
|
||||||
|
std::bidirectional_iterator_tag*
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef sprout::pair<BidirectionalIterator1, BidirectionalIterator1> type;
|
||||||
|
return sprout::detail::find_end_impl_bi(type(last1, last1), first1, last1, first2, last2, pred, 1).second;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename ForwardIterator1>
|
template<typename ForwardIterator1>
|
||||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, ForwardIterator1>
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, ForwardIterator1>
|
||||||
|
@ -161,8 +235,8 @@ namespace sprout {
|
||||||
BinaryPredicate pred
|
BinaryPredicate pred
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
typedef typename std::iterator_traits<ForwardIterator1>::iterator_category* category;
|
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
|
||||||
return sprout::detail::find_end(first1, last1, first2, last2, pred, category());
|
return first1 == last1 || first2 == last2 ? last1 : sprout::detail::find_end(first1, last1, first2, last2, pred, category());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ForwardIterator1, typename ForwardIterator2>
|
template<typename ForwardIterator1, typename ForwardIterator2>
|
||||||
|
|
109
sprout/detail/algorithm/search_one_rev.hpp
Normal file
109
sprout/detail/algorithm/search_one_rev.hpp
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
/*=============================================================================
|
||||||
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
||||||
|
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)
|
||||||
|
=============================================================================*/
|
||||||
|
#ifndef SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_REV_HPP
|
||||||
|
#define SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_REV_HPP
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/iterator/operation.hpp>
|
||||||
|
#include <sprout/functional/equal_to.hpp>
|
||||||
|
#include <sprout/tuple/tuple/tuple.hpp>
|
||||||
|
#include <sprout/tuple/tuple/get.hpp>
|
||||||
|
#include <sprout/utility/pair/pair.hpp>
|
||||||
|
|
||||||
|
namespace sprout {
|
||||||
|
namespace detail {
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool>
|
||||||
|
search_one_rev_impl_1(
|
||||||
|
sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool> const& current,
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator2 first2, BinaryPredicate pred,
|
||||||
|
typename std::iterator_traits<BidirectionalIterator1>::difference_type n
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool> type;
|
||||||
|
return sprout::tuples::get<2>(current) ? current
|
||||||
|
: sprout::tuples::get<1>(current) == first2 ? type(sprout::tuples::get<0>(current), first2, true)
|
||||||
|
: sprout::tuples::get<0>(current) == first1 ? type(first1, sprout::tuples::get<1>(current), true)
|
||||||
|
: n == 1 ? pred(*sprout::prev(sprout::tuples::get<0>(current)), *sprout::prev(sprout::tuples::get<1>(current)))
|
||||||
|
? type(sprout::prev(sprout::tuples::get<0>(current)), sprout::prev(sprout::tuples::get<1>(current)), false)
|
||||||
|
: type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), true)
|
||||||
|
: sprout::detail::search_one_rev_impl_1(
|
||||||
|
sprout::detail::search_one_rev_impl_1(
|
||||||
|
current,
|
||||||
|
first1, first2, pred, n / 2
|
||||||
|
),
|
||||||
|
first1, first2, pred, n - n / 2
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool>
|
||||||
|
search_one_rev_impl(
|
||||||
|
sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool> const& current,
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator2 first2, BinaryPredicate pred,
|
||||||
|
typename std::iterator_traits<BidirectionalIterator1>::difference_type n
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool> type;
|
||||||
|
return sprout::tuples::get<2>(current) ? current
|
||||||
|
: sprout::tuples::get<1>(current) == first2 ? type(sprout::tuples::get<0>(current), first2, true)
|
||||||
|
: sprout::tuples::get<0>(current) == first1 ? type(first1, sprout::tuples::get<1>(current), true)
|
||||||
|
: sprout::detail::search_one_rev_impl(
|
||||||
|
sprout::detail::search_one_rev_impl_1(
|
||||||
|
current,
|
||||||
|
first1, first2, pred, n
|
||||||
|
),
|
||||||
|
first1, first2, pred, n * 2
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, bool>
|
||||||
|
search_one_rev_check(
|
||||||
|
sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool> const& result,
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1, BidirectionalIterator2 first2
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef sprout::pair<BidirectionalIterator1, bool> type;
|
||||||
|
return sprout::tuples::get<1>(result) == first2 ? type(sprout::tuples::get<0>(result), false)
|
||||||
|
: type(last1, sprout::tuples::get<0>(result) == first1)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// search_one_rev
|
||||||
|
//
|
||||||
|
// recursion depth:
|
||||||
|
// O(log(N1+N2))
|
||||||
|
//
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2, typename BinaryPredicate>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, bool>
|
||||||
|
search_one_rev(
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
||||||
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2, BinaryPredicate pred
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef sprout::tuples::tuple<BidirectionalIterator1, BidirectionalIterator2, bool> type;
|
||||||
|
return sprout::detail::search_one_rev_check(
|
||||||
|
sprout::detail::search_one_rev_impl(type(last1, last2, false), first1, first2, pred, 1),
|
||||||
|
first1, last1, first2);
|
||||||
|
}
|
||||||
|
template<typename BidirectionalIterator1, typename BidirectionalIterator2>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<BidirectionalIterator1, bool>
|
||||||
|
search_one_rev(
|
||||||
|
BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
||||||
|
BidirectionalIterator2 first2, BidirectionalIterator2 last2
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return sprout::detail::search_one(first1, last1, first2, last2, sprout::equal_to<>());
|
||||||
|
}
|
||||||
|
} // namespace detail
|
||||||
|
} // namespace sprout
|
||||||
|
|
||||||
|
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_REV_HPP
|
Loading…
Reference in a new issue