mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-04 14:14:09 +00:00
fix recursion depth: min_element, max_element, minmax_element
This commit is contained in:
parent
86f68671a1
commit
1c085cb707
10 changed files with 539 additions and 17 deletions
|
@ -4,30 +4,89 @@
|
|||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
|
||||
namespace detail {
|
||||
template<typename InputIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR InputIterator
|
||||
iter_max(InputIterator a, InputIterator b, Compare comp) {
|
||||
return comp(*a, *b) ? b : a;
|
||||
}
|
||||
|
||||
template<typename RandomAccessIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR RandomAccessIterator
|
||||
max_element_impl_ra(
|
||||
RandomAccessIterator first, RandomAccessIterator last, Compare comp,
|
||||
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot
|
||||
)
|
||||
{
|
||||
return pivot == 0 ? first
|
||||
: sprout::detail::iter_max(
|
||||
sprout::detail::max_element_impl_ra(
|
||||
first, sprout::next(first, pivot), comp,
|
||||
pivot / 2
|
||||
),
|
||||
sprout::detail::max_element_impl_ra(
|
||||
sprout::next(first, pivot), last, comp,
|
||||
(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) - pivot) / 2
|
||||
),
|
||||
comp
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename RandomAccessIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR RandomAccessIterator
|
||||
max_element(
|
||||
RandomAccessIterator first, RandomAccessIterator last, Compare comp,
|
||||
std::random_access_iterator_tag*
|
||||
)
|
||||
{
|
||||
return first == last ? last
|
||||
: sprout::detail::max_element_impl_ra(
|
||||
first, last, comp,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
template<typename ForwardIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
max_element_impl(
|
||||
ForwardIterator first, ForwardIterator last, Compare comp,
|
||||
ForwardIterator max
|
||||
ForwardIterator found
|
||||
)
|
||||
{
|
||||
return first == last ? max
|
||||
: sprout::detail::max_element_impl(sprout::next(first), last, comp, comp(*max, *first) ? first : max)
|
||||
return first == last ? found
|
||||
: sprout::detail::max_element_impl(sprout::next(first), last, comp, sprout::detail::iter_max(found, first, comp))
|
||||
;
|
||||
}
|
||||
template<typename ForwardIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
max_element(
|
||||
ForwardIterator first, ForwardIterator last, Compare comp,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return first == last ? last
|
||||
: sprout::detail::max_element_impl(sprout::next(first), last, comp, first)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 25.4.7 Minimum and maximum
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
max_element(ForwardIterator first, ForwardIterator last, Compare comp) {
|
||||
return sprout::detail::max_element_impl(first, last, comp, first);
|
||||
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
|
||||
return sprout::detail::max_element(first, last, comp, category());
|
||||
}
|
||||
|
||||
template<typename ForwardIterator>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue