Sprout/sprout/algorithm/minmax_element.hpp

209 lines
7.6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2019 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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_MINMAX_ELEMENT_HPP
#define SPROUT_ALGORITHM_MINMAX_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>
#include <sprout/utility/pair/access.hpp>
2012-04-01 13:15:09 +00:00
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
2013-08-26 03:08:35 +00:00
template<typename ForwardIteratorPair, typename Compare>
inline SPROUT_CONSTEXPR ForwardIteratorPair
iter_minmax_pair(ForwardIteratorPair const& a, ForwardIteratorPair const& b, Compare comp) {
return ForwardIteratorPair(
comp(*sprout::first(b), *sprout::first(a)) ? sprout::first(b) : sprout::first(a),
2013-08-26 03:08:35 +00:00
comp(*sprout::second(b), *sprout::second(a)) ? sprout::second(a) : sprout::second(b)
);
}
2013-08-26 03:08:35 +00:00
template<typename ForwardIteratorPair, typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR ForwardIteratorPair
2016-04-01 14:37:48 +00:00
iter_minmax(ForwardIteratorPair const& a, ForwardIterator const& b, Compare comp) {
2013-08-26 03:08:35 +00:00
return ForwardIteratorPair(
2013-01-11 17:39:51 +00:00
comp(*b, *sprout::first(a)) ? b : sprout::first(a),
2013-08-26 08:38:11 +00:00
comp(*b, *sprout::second(a)) ? sprout::second(a) : b
2013-01-11 17:39:51 +00:00
);
}
2015-02-15 06:14:26 +00:00
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
2016-04-01 14:37:48 +00:00
iter_minmax(ForwardIterator const& a, ForwardIterator const& b, Compare comp) {
2015-02-15 06:14:26 +00:00
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return comp(*b, *a)
? type(b, a)
: type(a, b)
;
}
template<typename RandomAccessIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<RandomAccessIterator, RandomAccessIterator>
minmax_element_impl_ra(
2016-04-01 14:37:48 +00:00
RandomAccessIterator const& first, Compare comp,
2015-02-15 06:14:26 +00:00
typename std::iterator_traits<RandomAccessIterator>::difference_type half
)
{
2015-02-15 06:14:26 +00:00
return half == 1
? iter_minmax(first, sprout::next(first), comp)
2013-01-11 17:39:51 +00:00
: sprout::detail::iter_minmax_pair(
sprout::detail::minmax_element_impl_ra(
2015-02-15 06:14:26 +00:00
first, comp, half / 2
),
sprout::detail::minmax_element_impl_ra(
2015-02-15 06:14:26 +00:00
sprout::next(first, half - (half & 1)), comp, (half + 1) / 2
),
comp
)
;
}
2015-02-15 06:14:26 +00:00
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,
sprout::pair<RandomAccessIterator, RandomAccessIterator>
>::type
minmax_element(
2016-04-01 14:37:48 +00:00
RandomAccessIterator const& first, RandomAccessIterator const& last, Compare comp,
std::random_access_iterator_tag*
)
{
2015-02-15 06:14:26 +00:00
return first == last || sprout::next(first) == last
? sprout::pair<RandomAccessIterator, RandomAccessIterator>(first, first)
: sprout::distance(first, last) % 2 == 0
? sprout::detail::minmax_element_impl_ra(
first, comp,
sprout::distance(first, last) / 2
)
: sprout::detail::iter_minmax(
sprout::detail::minmax_element_impl_ra(
first, comp,
sprout::distance(first, last) / 2
),
sprout::prev(last),
comp
)
;
}
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, sprout::pair<ForwardIterator, ForwardIterator> >
minmax_element_impl_3(
sprout::pair<ForwardIterator, ForwardIterator> minmax,
2016-04-01 14:37:48 +00:00
ForwardIterator const& first, ForwardIterator const& next,
ForwardIterator const& last, Compare comp) {
2015-02-15 06:14:26 +00:00
typedef sprout::pair<ForwardIterator, sprout::pair<ForwardIterator, ForwardIterator> > type;
return next == last
? type(
next,
sprout::detail::iter_minmax(minmax, first, comp)
)
: type(
sprout::next(next),
sprout::detail::iter_minmax_pair(
minmax,
sprout::detail::iter_minmax(first, next, comp),
comp
)
)
;
}
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, sprout::pair<ForwardIterator, ForwardIterator> >
minmax_element_impl_1(
sprout::pair<ForwardIterator, sprout::pair<ForwardIterator, ForwardIterator> > const& current,
2016-04-01 14:37:48 +00:00
ForwardIterator const& last, Compare comp, typename std::iterator_traits<ForwardIterator>::difference_type n
2013-01-11 17:39:51 +00:00
)
{
return current.first == last ? current
2015-02-15 06:14:26 +00:00
: n == 1 ? sprout::detail::minmax_element_impl_3(
current.second, current.first, sprout::next(current.first),
last, comp)
2013-01-11 17:39:51 +00:00
: sprout::detail::minmax_element_impl_1(
sprout::detail::minmax_element_impl_1(
current,
last, comp, n / 2
),
last, comp, n - n / 2
)
;
}
2015-02-15 06:14:26 +00:00
2013-01-11 17:39:51 +00:00
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, sprout::pair<ForwardIterator, ForwardIterator> >
2012-10-06 04:53:07 +00:00
minmax_element_impl(
2013-01-11 17:39:51 +00:00
sprout::pair<ForwardIterator, sprout::pair<ForwardIterator, ForwardIterator> > const& current,
2016-04-01 14:37:48 +00:00
ForwardIterator const& last, Compare comp, typename std::iterator_traits<ForwardIterator>::difference_type n
2012-04-01 13:15:09 +00:00
)
{
2013-01-11 17:39:51 +00:00
return current.first == last ? current
: sprout::detail::minmax_element_impl(
sprout::detail::minmax_element_impl_1(
current,
last, comp, n
),
last, comp, n * 2
)
2012-04-01 13:15:09 +00:00
;
}
2015-02-15 06:14:26 +00:00
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, sprout::pair<ForwardIterator, ForwardIterator> >
minmax_element_impl_2(
2016-04-01 14:37:48 +00:00
ForwardIterator const& first, ForwardIterator const& next, ForwardIterator const& last, Compare comp
2015-02-15 06:14:26 +00:00
)
{
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return next == last ? sprout::pair<ForwardIterator, type>(next, type(first, first))
: sprout::pair<ForwardIterator, type>(sprout::next(next), iter_minmax(first, next, comp))
;
}
template<typename ForwardIterator, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
minmax_element(
2016-04-01 14:37:48 +00:00
ForwardIterator const& first, ForwardIterator const& last, Compare comp,
2013-02-23 06:21:27 +00:00
std::forward_iterator_tag*
)
{
2013-01-11 17:39:51 +00:00
return first == last ? sprout::pair<ForwardIterator, ForwardIterator>(first, first)
2015-02-15 06:14:26 +00:00
: sprout::detail::minmax_element_impl(
sprout::detail::minmax_element_impl_2(first, sprout::next(first), last, comp),
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>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
2012-10-06 04:53:07 +00:00
minmax_element(ForwardIterator first, ForwardIterator last, Compare comp) {
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
return sprout::detail::minmax_element(first, last, comp, category());
2012-04-01 13:15:09 +00:00
}
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
2012-10-06 04:53:07 +00:00
minmax_element(ForwardIterator first, ForwardIterator last) {
return sprout::minmax_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_MINMAX_ELEMENT_HPP