Sprout/sprout/algorithm/min_element.hpp

139 lines
4.6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
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)
=============================================================================*/
2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_MIN_ELEMENT_HPP
#define SPROUT_ALGORITHM_MIN_ELEMENT_HPP
#include <iterator>
2013-01-11 17:39:51 +00:00
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
2013-02-26 07:14:04 +00:00
#include <sprout/iterator/type_traits/category.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/pair/pair.hpp>
2012-04-01 13:15:09 +00:00
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
2013-01-11 17:39:51 +00:00
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR ForwardIterator
iter_min(ForwardIterator a, ForwardIterator 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,
(sprout::distance(first, last) - pivot) / 2
),
comp
)
;
}
template<typename RandomAccessIterator, typename Compare>
2013-01-11 17:39:51 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator>::value,
RandomAccessIterator
>::type
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,
sprout::distance(first, last) / 2
)
;
}
2012-04-01 13:15:09 +00:00
template<typename ForwardIterator, typename Compare>
2013-01-11 17:39:51 +00:00
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
min_element_impl_1(
sprout::pair<ForwardIterator, ForwardIterator> const& current,
ForwardIterator last, Compare comp, typename std::iterator_traits<ForwardIterator>::difference_type n
)
{
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return current.first == last ? current
: n == 1 ? type(sprout::next(current.first), sprout::detail::iter_min(current.second, current.first, comp))
: sprout::detail::min_element_impl_1(
sprout::detail::min_element_impl_1(
current,
last, comp, n / 2
),
last, comp, n - n / 2
)
;
}
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
2012-10-06 04:53:07 +00:00
min_element_impl(
2013-01-11 17:39:51 +00:00
sprout::pair<ForwardIterator, ForwardIterator> const& current,
ForwardIterator last, Compare comp, typename std::iterator_traits<ForwardIterator>::difference_type n
)
{
2013-01-11 17:39:51 +00:00
return current.first == last ? current
: sprout::detail::min_element_impl(
sprout::detail::min_element_impl_1(
current,
last, comp, n
),
last, comp, n * 2
)
;
}
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR ForwardIterator
min_element(
ForwardIterator first, ForwardIterator last, Compare comp,
2013-02-23 06:21:27 +00:00
std::forward_iterator_tag*
2012-04-01 13:15:09 +00:00
)
{
2013-01-11 17:39:51 +00:00
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return first == last ? last
2013-01-11 17:39:51 +00:00
: sprout::detail::min_element_impl(type(sprout::next(first), first), last, comp, 1).second
2012-04-01 13:15:09 +00:00
;
}
} // namespace detail
// 25.4.7 Minimum and maximum
//
// recursion depth:
2013-01-11 17:39:51 +00:00
// O(log 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) {
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