1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-02-04 21:33:56 +00:00

add C++14 constexpr partial_sort, partial_sort_copy

This commit is contained in:
bolero-MURAKAMI 2014-04-07 12:54:40 +09:00
parent 13e0c9b5f5
commit 262a5aa48b
20 changed files with 96 additions and 12 deletions

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/bogo_sort.hpp>
#include <sprout/algorithm/fit/bogo_sort.hpp>
#include <sprout/algorithm/cxx14/bogo_sort.hpp>
#endif // #ifndef SPROUT_ALGORITHM_BOGO_SORT_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/bozo_sort.hpp>
#include <sprout/algorithm/fit/bozo_sort.hpp>
#include <sprout/algorithm/cxx14/bozo_sort.hpp>
#endif // #ifndef SPROUT_ALGORITHM_BOZO_SORT_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/clamp_range.hpp>
#include <sprout/algorithm/fit/clamp_range.hpp>
#include <sprout/algorithm/cxx14/clamp_range.hpp>
#endif // #ifndef SPROUT_ALGORITHM_CLAMP_RANGE_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/copy_until.hpp>
#include <sprout/algorithm/fit/copy_until.hpp>
#include <sprout/algorithm/cxx14/copy_until.hpp>
#endif // #ifndef SPROUT_ALGORITHM_COPY_UNTIL_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/copy_while.hpp>
#include <sprout/algorithm/fit/copy_while.hpp>
#include <sprout/algorithm/cxx14/copy_while.hpp>
#endif // #ifndef SPROUT_ALGORITHM_COPY_WHILE_HPP

View file

@ -8,20 +8,50 @@
#ifndef SPROUT_ALGORITHM_CXX14_PARTIAL_SORT_HPP
#define SPROUT_ALGORITHM_CXX14_PARTIAL_SORT_HPP
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/algorithm/cxx14/detail/heap_tool.hpp>
#include <sprout/algorithm/cxx14/make_heap.hpp>
#include <sprout/algorithm/cxx14/sort_heap.hpp>
#include <sprout/utility/swap.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename Compare, typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR void
partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp) {
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type difference_type;
sprout::detail::make_heap<Compare>(first, middle, comp);
difference_type len = middle - first;
for (RandomAccessIterator i = middle; i != last; ++i) {
if (comp(*i, *first)) {
sprout::swap(*i, *first);
sprout::detail::push_heap_front<Compare>(first, middle, comp, len);
}
}
sprout::detail::sort_heap<Compare>(first, middle, comp);
}
} // namespace detail
//
// 25.4.1.3 partial_sort
//
// !!! TODO: implementation
template<typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR void
partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
template<typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp) {
typedef typename std::add_lvalue_reference<Compare>::type compare_ref;
sprout::detail::partial_sort<compare_ref>(first, middle, last, comp);
}
template<typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR void
partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last) {
sprout::partial_sort(
first, middle, last,
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<RandomAccessIterator>::value_type>()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_PARTIAL_SORT_HPP

View file

@ -8,20 +8,56 @@
#ifndef SPROUT_ALGORITHM_CXX14_PARTIAL_SORT_COPY_HPP
#define SPROUT_ALGORITHM_CXX14_PARTIAL_SORT_COPY_HPP
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/algorithm/cxx14/detail/heap_tool.hpp>
#include <sprout/algorithm/cxx14/make_heap.hpp>
#include <sprout/algorithm/cxx14/sort_heap.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename InputIterator, typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp) {
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type difference_type;
RandomAccessIterator r = result_first;
if (r != result_last) {
difference_type len = 0;
for (; first != last && r != result_last; ++first, ++r, ++len) {
*r = *first;
}
sprout::detail::make_heap<Compare>(result_first, r, comp);
for (; first != last; ++first) {
if (comp(*first, *result_first)) {
*result_first = *first;
sprout::detail::push_heap_front<Compare>(result_first, r, comp, len);
}
}
sprout::detail::sort_heap<Compare>(result_first, r, comp);
}
return r;
}
} // namespace detail
//
// 25.4.1.4 partial_sort_copy
//
// !!! TODO: implementation
template<typename InputIterator, typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last);
template<typename InputIterator, typename RandomAccessIterator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp) {
typedef typename std::add_lvalue_reference<Compare>::type compare_ref;
return sprout::detail::partial_sort_copy<compare_ref>(first, last, result_first, result_last, comp);
}
template<typename InputIterator, typename RandomAccessIterator>
inline SPROUT_CXX14_CONSTEXPR RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last) {
return sprout::partial_sort_copy(
first, last, result_first, result_last,
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<RandomAccessIterator>::value_type>()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_PARTIAL_SORT_COPY_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/inplace_merge.hpp>
#include <sprout/algorithm/fit/inplace_merge.hpp>
#include <sprout/algorithm/cxx14/inplace_merge.hpp>
#endif // #ifndef SPROUT_ALGORITHM_INPLACE_MERGE_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/make_heap.hpp>
#include <sprout/algorithm/fit/make_heap.hpp>
#include <sprout/algorithm/cxx14/make_heap.hpp>
#endif // #ifndef SPROUT_ALGORITHM_MAKE_HEAP_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/merge.hpp>
#include <sprout/algorithm/fit/merge.hpp>
#include <sprout/algorithm/cxx14/merge.hpp>
#endif // #ifndef SPROUT_ALGORITHM_MERGE_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/next_permutation.hpp>
#include <sprout/algorithm/fit/next_permutation.hpp>
#include <sprout/algorithm/cxx14/next_permutation.hpp>
#endif // #ifndef SPROUT_ALGORITHM_NEXT_PERMUTATION_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/pop_heap.hpp>
#include <sprout/algorithm/fit/pop_heap.hpp>
#include <sprout/algorithm/cxx14/pop_heap.hpp>
#endif // #ifndef SPROUT_ALGORITHM_POP_HEAP_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/prev_permutation.hpp>
#include <sprout/algorithm/fit/prev_permutation.hpp>
#include <sprout/algorithm/cxx14/prev_permutation.hpp>
#endif // #ifndef SPROUT_ALGORITHM_PREV_PERMUTATION_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/push_heap.hpp>
#include <sprout/algorithm/fit/push_heap.hpp>
#include <sprout/algorithm/cxx14/push_heap.hpp>
#endif // #ifndef SPROUT_ALGORITHM_PUSH_HEAP_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/random_swap.hpp>
#include <sprout/algorithm/fit/random_swap.hpp>
#include <sprout/algorithm/cxx14/random_swap.hpp>
#endif // #ifndef SPROUT_ALGORITHM_RANDOM_SWAP_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/set_difference.hpp>
#include <sprout/algorithm/fit/set_difference.hpp>
#include <sprout/algorithm/cxx14/set_difference.hpp>
#endif // #ifndef SPROUT_ALGORITHM_SET_DIFFERENCE_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/set_intersection.hpp>
#include <sprout/algorithm/fit/set_intersection.hpp>
#include <sprout/algorithm/cxx14/set_intersection.hpp>
#endif // #ifndef SPROUT_ALGORITHM_SET_INTERSECTION_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/set_symmetric_difference.hpp>
#include <sprout/algorithm/fit/set_symmetric_difference.hpp>
#include <sprout/algorithm/cxx14/set_symmetric_difference.hpp>
#endif // #ifndef SPROUT_ALGORITHM_SET_SYMMETRIC_DIFFERENCE_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/set_union.hpp>
#include <sprout/algorithm/fit/set_union.hpp>
#include <sprout/algorithm/cxx14/set_union.hpp>
#endif // #ifndef SPROUT_ALGORITHM_SET_UNION_HPP

View file

@ -11,5 +11,6 @@
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/sort_heap.hpp>
#include <sprout/algorithm/fit/sort_heap.hpp>
#include <sprout/algorithm/cxx14/sort_heap.hpp>
#endif // #ifndef SPROUT_ALGORITHM_SORT_HEAP_HPP