2013-10-31 09:57:41 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-10-31 09:57:41 +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)
|
|
|
|
=============================================================================*/
|
2014-04-05 09:31:09 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_CXX14_STABLE_PARTITION_HPP
|
|
|
|
#define SPROUT_ALGORITHM_CXX14_STABLE_PARTITION_HPP
|
2013-10-31 09:57:41 +00:00
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-07 11:01:38 +00:00
|
|
|
#include <sprout/iterator/distance.hpp>
|
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/iterator/advance.hpp>
|
|
|
|
#include <sprout/algorithm/find_if_not.hpp>
|
|
|
|
#include <sprout/algorithm/cxx14/rotate.hpp>
|
2013-10-31 09:57:41 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2014-04-07 11:01:38 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename BidirectionalIterator, typename Predicate, typename Distance>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR BidirectionalIterator
|
|
|
|
inplace_stable_partition(BidirectionalIterator first, Predicate pred, Distance len) {
|
|
|
|
if (len == 1) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
BidirectionalIterator middle = first;
|
|
|
|
sprout::advance(middle, len / 2);
|
|
|
|
BidirectionalIterator left_split = sprout::detail::inplace_stable_partition(first, pred, len / 2);
|
|
|
|
Distance right_len = len - len / 2;
|
|
|
|
BidirectionalIterator right_split = sprout::find_if_not(middle, sprout::next(middle, right_len), pred);
|
|
|
|
if (right_len) {
|
|
|
|
right_split = sprout::detail::inplace_stable_partition(middle, pred, right_len);
|
|
|
|
}
|
|
|
|
sprout::rotate(left_split, middle, right_split);
|
|
|
|
sprout::advance(left_split, sprout::distance(middle, right_split));
|
|
|
|
return left_split;
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
2013-10-31 09:57:41 +00:00
|
|
|
//
|
|
|
|
// 25.3.13 Partitions
|
|
|
|
//
|
|
|
|
template<typename BidirectionalIterator, typename Predicate>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR BidirectionalIterator
|
2014-04-07 11:01:38 +00:00
|
|
|
stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred) {
|
|
|
|
first = sprout::find_if_not(first, last, pred);
|
|
|
|
return first == last ? first
|
|
|
|
: sprout::detail::inplace_stable_partition(first, pred, sprout::distance(first, last))
|
|
|
|
;
|
|
|
|
}
|
2013-10-31 09:57:41 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
2014-04-05 09:31:09 +00:00
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_CXX14_STABLE_PARTITION_HPP
|