add C++14 constexpr stable_partition, stable_sort, inplace_merge

This commit is contained in:
bolero-MURAKAMI 2014-04-07 20:01:38 +09:00
parent 262a5aa48b
commit 856fb90150
4 changed files with 178 additions and 14 deletions

View file

@ -0,0 +1,53 @@
/*=============================================================================
Copyright (c) 2011-2014 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_CXX14_DETAIL_INSERTION_SORT_HPP
#define SPROUT_ALGORITHM_CXX14_DETAIL_INSERTION_SORT_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/algorithm/cxx14/move_backward.hpp>
#include <sprout/utility/move.hpp>
namespace sprout {
namespace detail {
template<typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
unguarded_linear_insert(RandomAccessIterator last, Compare comp) {
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
value_type val = sprout::move(*last);
RandomAccessIterator next = last;
--next;
while (comp(val, *next)) {
*last = sprout::move(*next);
last = next;
--next;
}
*last = sprout::move(val);
}
template<typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
insertion_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
if (first == last) {
return;
}
for (RandomAccessIterator i = first + 1; i != last; ++i) {
if (comp(*i, *first)) {
value_type val = sprout::move(*i);
sprout::move_backward(first, i, i + 1);
*first = sprout::move(val);
} else {
sprout::detail::unguarded_linear_insert(i, comp);
}
}
}
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_DETAIL_INSERTION_SORT_HPP

View file

@ -8,20 +8,79 @@
#ifndef SPROUT_ALGORITHM_CXX14_INPLACE_MERGE_HPP
#define SPROUT_ALGORITHM_CXX14_INPLACE_MERGE_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/distance.hpp>
#include <sprout/iterator/advance.hpp>
#include <sprout/algorithm/lower_bound.hpp>
#include <sprout/algorithm/upper_bound.hpp>
#include <sprout/algorithm/cxx14/iter_swap.hpp>
#include <sprout/algorithm/cxx14/rotate.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename BidirectionalIterator, typename Distance, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
inplace_merge(
BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last,
Distance len1, Distance len2, Compare comp
)
{
if (len1 == 0 || len2 == 0) {
return;
}
if (len1 + len2 == 2) {
if (comp(*middle, *first)) {
sprout::iter_swap(first, middle);
}
return;
}
BidirectionalIterator first_cut = first;
BidirectionalIterator second_cut = middle;
Distance len11 = 0;
Distance len22 = 0;
if (len1 > len2) {
len11 = len1 / 2;
sprout::advance(first_cut, len11);
second_cut = sprout::lower_bound(middle, last, *first_cut, comp);
len22 = sprout::distance(middle, second_cut);
} else {
len22 = len2 / 2;
sprout::advance(second_cut, len22);
first_cut = sprout::upper_bound(first, middle, *second_cut, comp);
len11 = sprout::distance(first, first_cut);
}
sprout::rotate(first_cut, middle, second_cut);
BidirectionalIterator new_middle = first_cut;
sprout::advance(new_middle, sprout::distance(middle, second_cut));
sprout::detail::inplace_merge(first, first_cut, new_middle, len11, len22, comp);
sprout::detail::inplace_merge(new_middle, second_cut, last, len1 - len11, len2 - len22, comp);
}
} // namespace detail
//
// 25.4.4 Merge
//
// !!! TODO: implementation
template<typename BidirectionalIterator>
inline SPROUT_CXX14_CONSTEXPR void
inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
template<typename BidirectionalIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp) {
typedef typename std::iterator_traits<BidirectionalIterator>::difference_type difference_type;
if (first == middle || middle == last) {
return;
}
difference_type const len1 = sprout::distance(first, middle);
difference_type const len2 = sprout::distance(middle, last);
sprout::detail::inplace_merge(first, middle, last, len1, len2, comp);
}
template<typename BidirectionalIterator>
inline SPROUT_CXX14_CONSTEXPR void
inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last) {
sprout::inplace_merge(
first, middle, last,
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<BidirectionalIterator>::value_type>()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_INPLACE_MERGE_HPP

View file

@ -9,15 +9,44 @@
#define SPROUT_ALGORITHM_CXX14_STABLE_PARTITION_HPP
#include <sprout/config.hpp>
#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>
namespace sprout {
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
//
// 25.3.13 Partitions
//
// !!! TODO: implementation
template<typename BidirectionalIterator, typename Predicate>
inline SPROUT_CXX14_CONSTEXPR BidirectionalIterator
stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred);
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))
;
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_STABLE_PARTITION_HPP

View file

@ -9,19 +9,42 @@
#define SPROUT_ALGORITHM_CXX14_STABLE_SORT_HPP
#include <sprout/config.hpp>
#include <sprout/algorithm/cxx14/detail/insertion_sort.hpp>
#include <sprout/algorithm/cxx14/inplace_merge.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
inplace_stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
if (last - first < 15) {
sprout::detail::insertion_sort(first, last, comp);
return;
}
RandomAccessIterator middle = first + (last - first) / 2;
sprout::detail::inplace_stable_sort(first, middle, comp);
sprout::detail::inplace_stable_sort(middle, last, comp);
sprout::detail::inplace_merge(first, middle, last, middle - first, last - middle, comp);
}
} // namespace detail
//
// 25.4.1.2 stable_sort
//
// !!! TODO: implementation
template<typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR void
stable_sort(RandomAccessIterator first, RandomAccessIterator last);
template<typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
sprout::detail::inplace_stable_sort(first, last, comp);
}
template<typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR void
stable_sort(RandomAccessIterator first, RandomAccessIterator last) {
sprout::stable_sort(
first, last,
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<RandomAccessIterator>::value_type>()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_STABLE_SORT_HPP