1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/algorithm/partition_point.hpp

44 lines
1.3 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_PARTITION_POINT_HPP
#define SPROUT_ALGORITHM_PARTITION_POINT_HPP
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk)
namespace detail {
template<typename ForwardIterator, typename Predicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR ForwardIterator
partition_point_impl(ForwardIterator first, ForwardIterator last, Predicate pred, ForwardIterator 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,
sprout::next(mid, 1 + sprout::distance(sprout::next(mid), last) / 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:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(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
);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_PARTITION_POINT_HPP