mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-02 14:04:20 +00:00
fix recursion depth: some algorithms
This commit is contained in:
parent
830fc27394
commit
cb584edb3f
14 changed files with 1443 additions and 71 deletions
|
@ -2,16 +2,19 @@
|
|||
#define SPROUT_ALGORITHM_INCLUDES_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/iterator/type_traits/is_iterator.hpp>
|
||||
#include <sprout/functional/less.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR RandomAccessIterator2
|
||||
template<typename RandomAccessIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR InputIterator2
|
||||
includes_impl_ra(
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
|
||||
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot
|
||||
)
|
||||
{
|
||||
|
@ -30,10 +33,13 @@ namespace sprout {
|
|||
)
|
||||
;
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
template<typename RandomAccessIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
|
||||
bool
|
||||
>::type
|
||||
includes(
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
|
||||
std::random_access_iterator_tag*
|
||||
)
|
||||
{
|
||||
|
@ -46,33 +52,67 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
|
||||
return first2 == last2 ? true
|
||||
: first1 == last1 ? false
|
||||
: !comp(*first1, *first2) && !comp(*first2, *first1)
|
||||
? sprout::detail::includes_impl(sprout::next(first1), last1, sprout::next(first2), last2)
|
||||
: sprout::detail::includes_impl(sprout::next(first1), last1, first2, last2)
|
||||
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
|
||||
includes_impl_1(
|
||||
sprout::pair<InputIterator1, InputIterator2> const& current,
|
||||
InputIterator1 last1, InputIterator2 last2, Compare comp,
|
||||
typename std::iterator_traits<InputIterator1>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<InputIterator1, InputIterator2> type;
|
||||
return current.second == last2 || current.first == last1 ? current
|
||||
: n == 1 ? !comp(*current.first, *current.second) && !comp(*current.second, *current.first)
|
||||
? type(sprout::next(current.first), sprout::next(current.second))
|
||||
: type(sprout::next(current.first), current.second)
|
||||
: sprout::detail::includes_impl_1(
|
||||
sprout::detail::includes_impl_1(
|
||||
current,
|
||||
last1, last2, comp, n / 2
|
||||
),
|
||||
last1, last2, comp, n - n / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
|
||||
includes_impl(
|
||||
sprout::pair<InputIterator1, InputIterator2> const& current,
|
||||
InputIterator1 last1, InputIterator2 last2, Compare comp,
|
||||
typename std::iterator_traits<InputIterator1>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<InputIterator1, InputIterator2> type;
|
||||
return current.second == last2 || current.first == last1 ? current
|
||||
: sprout::detail::includes_impl(
|
||||
sprout::detail::includes_impl_1(
|
||||
current,
|
||||
last1, last2, comp, n
|
||||
),
|
||||
last1, last2, comp, n * 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes(
|
||||
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
|
||||
InputIterator1 first1, InputIterator1 last1,
|
||||
InputIterator2 first2, InputIterator2 last2,
|
||||
Compare comp,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::includes_impl(first1, last1, first2, comp);
|
||||
typedef sprout::pair<InputIterator1, InputIterator2> type;
|
||||
return first2 == last2 ? true
|
||||
: sprout::detail::includes_impl(type(first1, first2), last1, last2, comp, 1).second == last2
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 25.4.5.1 includes
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
// O(log(N1+N2))
|
||||
//
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue