fix recursion depth: some algorithms

This commit is contained in:
bolero-MURAKAMI 2013-01-12 02:39:51 +09:00
parent 830fc27394
commit cb584edb3f
14 changed files with 1443 additions and 71 deletions

View file

@ -1,8 +1,11 @@
#ifndef SPROUT_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP
#define SPROUT_ALGORITHM_LEXICOGRAPHICAL_COMPARE_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/iterator/type_traits/common.hpp>
#include <sprout/utility/pair.hpp>
#include <sprout/algorithm/min.hpp>
@ -12,7 +15,7 @@ namespace sprout {
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool
lexicographical_compare_impl_ra_2(
lexicographical_compare_impl_check(
RandomAccessIterator1 last1, RandomAccessIterator2 last2, Compare comp,
sprout::pair<RandomAccessIterator1, RandomAccessIterator2> const& found
)
@ -21,6 +24,7 @@ namespace sprout {
: found.first == last1 || comp(*found.first, *found.second)
;
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<RandomAccessIterator1, RandomAccessIterator2>
lexicographical_compare_impl_ra_1(
@ -50,7 +54,7 @@ namespace sprout {
)
{
typedef sprout::pair<RandomAccessIterator1, RandomAccessIterator2> found_type;
return sprout::detail::lexicographical_compare_impl_ra_2(
return sprout::detail::lexicographical_compare_impl_check(
last1, last2, comp,
sprout::detail::lexicographical_compare_impl_ra_1(
first1, sprout::next(first1, size), first2, sprout::next(first2, size), comp,
@ -59,7 +63,10 @@ namespace sprout {
);
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
bool
>::type
lexicographical_compare(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
std::random_access_iterator_tag*
@ -74,14 +81,45 @@ namespace sprout {
;
}
// Copyright (C) 2011 RiSK (sscrisk)
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool
lexicographical_compare_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
return first2 == last2 ? false
: first1 == last1 || comp(*first1, *first2) ? true
: comp(*first2, *first1) ? false
: sprout::detail::lexicographical_compare_impl(sprout::next(first1), last1, sprout::next(first2), last2, comp)
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
lexicographical_compare_impl_1(
sprout::pair<InputIterator1, InputIterator2> 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) ? type(last1, current.second)
: comp(*current.second, *current.first) ? type(current.first, last2)
: type(sprout::next(current.first), sprout::next(current.second))
: sprout::detail::lexicographical_compare_impl_1(
sprout::detail::lexicographical_compare_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>
lexicographical_compare_impl(
sprout::pair<InputIterator1, InputIterator2> 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::lexicographical_compare_impl(
sprout::detail::lexicographical_compare_impl_1(
current,
last1, last2, comp, n
),
last1, last2, comp, n * 2
)
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
@ -91,15 +129,18 @@ namespace sprout {
void*
)
{
return sprout::detail::lexicographical_compare_impl(first1, last1, first2, last2, comp);
typedef sprout::pair<InputIterator1, InputIterator2> type;
return sprout::detail::lexicographical_compare_impl_check(
last1, last2, comp,
sprout::detail::lexicographical_compare_impl(type(first1, first2), last1, last2, comp, 1)
);
}
} // namespace detail
// 25.4.8 Lexicographical comparison
//
// recursion depth:
// [first1, last1), [first2, last2) are RandomAccessIterator -> O(log N)
// otherwise -> O(N)
// O(log(N1+N2))
//
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool