Sprout/sprout/algorithm/cxx14/inplace_merge.hpp

87 lines
3.3 KiB
C++
Raw Normal View History

2014-04-06 04:16:08 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2014-04-06 04:16:08 +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)
=============================================================================*/
#ifndef SPROUT_ALGORITHM_CXX14_INPLACE_MERGE_HPP
#define SPROUT_ALGORITHM_CXX14_INPLACE_MERGE_HPP
#include <iterator>
2014-04-06 04:16:08 +00:00
#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
2014-04-06 04:16:08 +00:00
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
2014-04-06 04:16:08 +00:00
//
// 25.4.4 Merge
//
template<typename BidirectionalIterator, typename Compare>
2014-04-06 04:16:08 +00:00
inline SPROUT_CXX14_CONSTEXPR void
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);
}
2014-04-06 04:16:08 +00:00
template<typename BidirectionalIterator>
2014-04-06 04:16:08 +00:00
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>()
);
}
2014-04-06 04:16:08 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_INPLACE_MERGE_HPP