Sprout/sprout/algorithm/partition_point.hpp

77 lines
2.5 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011 RiSK (sscrisk)
https://github.com/sscrisk/CEL---ConstExpr-Library
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_PARTITION_POINT_HPP
#define SPROUT_ALGORITHM_PARTITION_POINT_HPP
2015-04-27 16:55:35 +00:00
#include <iterator>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
2015-04-27 16:55:35 +00:00
#include <sprout/detail/predef.hpp>
2012-04-01 13:15:09 +00:00
#include <sprout/iterator/operation.hpp>
namespace sprout {
2017-08-06 01:09:03 +00:00
#if defined(SPROUT_CONFIG_DISABLE_CXX14_CONSTEXPR) || SPROUT_GCC_IN_RANGE((5, 1, 0), (6, 0, 0))
2012-04-01 13:15:09 +00:00
namespace detail {
template<typename ForwardIterator, typename Predicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR ForwardIterator
2016-04-01 14:37:48 +00:00
partition_point_impl(ForwardIterator const& first, ForwardIterator const& last, Predicate pred, ForwardIterator const& mid) {
2012-04-01 13:15:09 +00:00
return mid == last ? mid
: pred(*mid) ? sprout::detail::partition_point_impl(
2012-10-06 04:53:07 +00:00
sprout::next(mid), last, pred,
2013-01-10 17:55:19 +00:00
sprout::next(mid, 1 + (sprout::distance(mid, last) - 1) / 2)
)
: sprout::detail::partition_point_impl(
2012-10-06 04:53:07 +00:00
first, mid, pred,
sprout::next(first, sprout::distance(first, mid) / 2)
)
2012-04-01 13:15:09 +00:00
;
}
} // namespace detail
// 25.3.13 Partitions
//
// recursion depth:
2013-01-10 17:55:19 +00:00
// O(log N)
//
2012-04-01 13:15:09 +00:00
template<typename ForwardIterator, typename Predicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR ForwardIterator
partition_point(ForwardIterator first, ForwardIterator last, Predicate pred) {
return sprout::detail::partition_point_impl(
first, last, pred,
sprout::next(first, sprout::distance(first, last) / 2)
2012-10-06 04:53:07 +00:00
);
}
2015-02-11 14:15:08 +00:00
#else
// 25.3.13 Partitions
//
// recursion depth:
// 0
//
template<typename ForwardIterator, typename Predicate>
2015-03-11 11:40:19 +00:00
inline SPROUT_CXX14_CONSTEXPR ForwardIterator
2015-02-11 14:15:08 +00:00
partition_point(ForwardIterator first, ForwardIterator last, Predicate pred) {
typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_type;
for (difference_type len = sprout::distance(first, last); len != 0; ) {
2015-03-11 11:40:19 +00:00
difference_type const half = len / 2;
ForwardIterator const mid = sprout::next(first, half);
2015-02-11 14:15:08 +00:00
if (pred(*mid)) {
first = sprout::next(mid);
len -= half + 1;
} else {
len = half;
}
}
return first;
2012-04-01 13:15:09 +00:00
}
2015-02-11 14:15:08 +00:00
#endif
2012-04-01 13:15:09 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_PARTITION_POINT_HPP