/*============================================================================= Copyright (c) 2011-2016 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) =============================================================================*/ #ifndef SPROUT_ALGORITHM_MINMAX_ELEMENT_HPP #define SPROUT_ALGORITHM_MINMAX_ELEMENT_HPP #include #include #include #include #include #include #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT namespace sprout { namespace detail { template 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), comp(*sprout::second(b), *sprout::second(a)) ? sprout::second(a) : sprout::second(b) ); } template inline SPROUT_CONSTEXPR ForwardIteratorPair iter_minmax(ForwardIteratorPair const& a, ForwardIterator b, Compare comp) { return ForwardIteratorPair( comp(*b, *sprout::first(a)) ? b : sprout::first(a), comp(*b, *sprout::second(a)) ? sprout::second(a) : b ); } template inline SPROUT_CONSTEXPR sprout::pair iter_minmax(ForwardIterator a, ForwardIterator b, Compare comp) { typedef sprout::pair type; return comp(*b, *a) ? type(b, a) : type(a, b) ; } template inline SPROUT_CONSTEXPR sprout::pair minmax_element_impl_ra( RandomAccessIterator first, Compare comp, typename std::iterator_traits::difference_type half ) { return half == 1 ? iter_minmax(first, sprout::next(first), comp) : sprout::detail::iter_minmax_pair( sprout::detail::minmax_element_impl_ra( first, comp, half / 2 ), sprout::detail::minmax_element_impl_ra( sprout::next(first, half - (half & 1)), comp, (half + 1) / 2 ), comp ) ; } template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_constant_distance_iterator::value, sprout::pair >::type minmax_element( RandomAccessIterator first, RandomAccessIterator last, Compare comp, std::random_access_iterator_tag* ) { return first == last || sprout::next(first) == last ? sprout::pair(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 inline SPROUT_CONSTEXPR sprout::pair > minmax_element_impl_3( sprout::pair minmax, ForwardIterator first, ForwardIterator next, ForwardIterator last, Compare comp) { typedef sprout::pair > 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 ) ) ; } template inline SPROUT_CONSTEXPR sprout::pair > minmax_element_impl_1( sprout::pair > const& current, ForwardIterator last, Compare comp, typename std::iterator_traits::difference_type n ) { return current.first == last ? current : n == 1 ? sprout::detail::minmax_element_impl_3( current.second, current.first, sprout::next(current.first), last, comp) : sprout::detail::minmax_element_impl_1( sprout::detail::minmax_element_impl_1( current, last, comp, n / 2 ), last, comp, n - n / 2 ) ; } template inline SPROUT_CONSTEXPR sprout::pair > minmax_element_impl( sprout::pair > const& current, ForwardIterator last, Compare comp, typename std::iterator_traits::difference_type n ) { return current.first == last ? current : sprout::detail::minmax_element_impl( sprout::detail::minmax_element_impl_1( current, last, comp, n ), last, comp, n * 2 ) ; } template inline SPROUT_CONSTEXPR sprout::pair > minmax_element_impl_2( ForwardIterator first, ForwardIterator next, ForwardIterator last, Compare comp ) { typedef sprout::pair type; return next == last ? sprout::pair(next, type(first, first)) : sprout::pair(sprout::next(next), iter_minmax(first, next, comp)) ; } template inline SPROUT_CONSTEXPR sprout::pair minmax_element( ForwardIterator first, ForwardIterator last, Compare comp, std::forward_iterator_tag* ) { return first == last ? sprout::pair(first, first) : sprout::detail::minmax_element_impl( sprout::detail::minmax_element_impl_2(first, sprout::next(first), last, comp), last, comp, 1).second ; } } // namespace detail // 25.4.7 Minimum and maximum // // recursion depth: // O(log N) // template inline SPROUT_CONSTEXPR sprout::pair minmax_element(ForwardIterator first, ForwardIterator last, Compare comp) { typedef typename std::iterator_traits::iterator_category* category; return sprout::detail::minmax_element(first, last, comp, category()); } template inline SPROUT_CONSTEXPR sprout::pair minmax_element(ForwardIterator first, ForwardIterator last) { return sprout::minmax_element( first, last, NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() ); } } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_MINMAX_ELEMENT_HPP