2012-04-01 13:15:09 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_MIN_ELEMENT_HPP
|
|
|
|
#define SPROUT_ALGORITHM_MIN_ELEMENT_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2012-12-16 12:46:16 +00:00
|
|
|
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
2012-04-01 13:15:09 +00:00
|
|
|
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2012-12-16 12:46:16 +00:00
|
|
|
template<typename InputIterator, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
iter_min(InputIterator a, InputIterator b, Compare comp) {
|
|
|
|
return comp(*b, *a) ? b : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RandomAccessIterator, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR RandomAccessIterator
|
|
|
|
min_element_impl_ra(
|
|
|
|
RandomAccessIterator first, RandomAccessIterator last, Compare comp,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return pivot == 0 ? first
|
|
|
|
: sprout::detail::iter_min(
|
|
|
|
sprout::detail::min_element_impl_ra(
|
|
|
|
first, sprout::next(first, pivot), comp,
|
|
|
|
pivot / 2
|
|
|
|
),
|
|
|
|
sprout::detail::min_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
|
|
|
|
min_element(
|
|
|
|
RandomAccessIterator first, RandomAccessIterator last, Compare comp,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first == last ? last
|
|
|
|
: sprout::detail::min_element_impl_ra(
|
|
|
|
first, last, comp,
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copyright (C) 2011 RiSK (sscrisk)
|
2012-04-01 13:15:09 +00:00
|
|
|
template<typename ForwardIterator, typename Compare>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
min_element_impl(
|
|
|
|
ForwardIterator first, ForwardIterator last, Compare comp,
|
2012-12-16 12:46:16 +00:00
|
|
|
ForwardIterator found
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first == last ? found
|
|
|
|
: sprout::detail::min_element_impl(sprout::next(first), last, comp, sprout::detail::iter_min(found, first, comp))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
min_element(
|
|
|
|
ForwardIterator first, ForwardIterator last, Compare comp,
|
|
|
|
void*
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2012-12-16 12:46:16 +00:00
|
|
|
return first == last ? last
|
|
|
|
: sprout::detail::min_element_impl(sprout::next(first), last, comp, first)
|
2012-04-01 13:15:09 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// 25.4.7 Minimum and maximum
|
2012-12-16 12:46:16 +00:00
|
|
|
//
|
|
|
|
// recursion depth:
|
|
|
|
// [first, last) is RandomAccessIterator -> O(log N)
|
|
|
|
// otherwise -> O(N)
|
|
|
|
//
|
2012-04-01 13:15:09 +00:00
|
|
|
template<typename ForwardIterator, typename Compare>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
min_element(ForwardIterator first, ForwardIterator last, Compare comp) {
|
2012-12-16 12:46:16 +00:00
|
|
|
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
|
|
|
|
return sprout::detail::min_element(first, last, comp, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardIterator>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
|
|
|
min_element(ForwardIterator first, ForwardIterator last) {
|
|
|
|
return sprout::min_element(
|
|
|
|
first, last,
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
|
|
|
|
);
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_MIN_ELEMENT_HPP
|