diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -80,6 +80,7 @@ __algorithm/pstl_backends/cpu_backends/for_each.h __algorithm/pstl_backends/cpu_backends/merge.h __algorithm/pstl_backends/cpu_backends/serial.h + __algorithm/pstl_backends/cpu_backends/stable_sort.h __algorithm/pstl_backends/cpu_backends/thread.h __algorithm/pstl_backends/cpu_backends/transform.h __algorithm/pstl_backends/cpu_backends/transform_reduce.h @@ -89,6 +90,7 @@ __algorithm/pstl_for_each.h __algorithm/pstl_frontend_dispatch.h __algorithm/pstl_merge.h + __algorithm/pstl_stable_sort.h __algorithm/pstl_transform.h __algorithm/push_heap.h __algorithm/ranges_adjacent_find.h diff --git a/libcxx/include/__algorithm/pstl_backend.h b/libcxx/include/__algorithm/pstl_backend.h --- a/libcxx/include/__algorithm/pstl_backend.h +++ b/libcxx/include/__algorithm/pstl_backend.h @@ -32,6 +32,9 @@ template _Iterator __pstl_find_if(_Backend, _Iterator __first, _Iterator __last, _Predicate __pred); + template + void __pstl_stable_sort(_Backend, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp); + template _OutIterator __pstl_transform(_InIterator __first, _InIterator __last, _OutIterator __result, _UnaryOperation __op); diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backend.h b/libcxx/include/__algorithm/pstl_backends/cpu_backend.h --- a/libcxx/include/__algorithm/pstl_backends/cpu_backend.h +++ b/libcxx/include/__algorithm/pstl_backends/cpu_backend.h @@ -37,6 +37,12 @@ _Compare __comp, _LeafMerge __leaf_merge); + template + void __parallel_stable_sort(_RandomAccessIterator __first, + _RandomAccessIterator __last, + _Comp __comp, + _LeafSort __leaf_sort); + TODO: Document the parallel backend */ @@ -46,6 +52,7 @@ #include <__algorithm/pstl_backends/cpu_backends/find_if.h> #include <__algorithm/pstl_backends/cpu_backends/for_each.h> #include <__algorithm/pstl_backends/cpu_backends/merge.h> +#include <__algorithm/pstl_backends/cpu_backends/stable_sort.h> #include <__algorithm/pstl_backends/cpu_backends/transform.h> #include <__algorithm/pstl_backends/cpu_backends/transform_reduce.h> diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h b/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h --- a/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h +++ b/libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h @@ -12,6 +12,7 @@ #include <__config> #include <__utility/move.h> +#include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -35,6 +36,12 @@ return __reduce(std::move(__first), std::move(__last), std::move(__init)); } +template +_LIBCPP_HIDE_FROM_ABI void __parallel_stable_sort( + _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) { + __leaf_sort(__first, __last, __comp); +} + _LIBCPP_HIDE_FROM_ABI inline void __cancel_execution() {} template +#include <__algorithm/stable_sort.h> +#include <__config> +#include <__type_traits/is_execution_policy.h> +#include <__utility/terminate_on_exception.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +template +_LIBCPP_HIDE_FROM_ABI void +__pstl_stable_sort(__cpu_backend_tag, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) { + if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy>) { + std::__terminate_on_exception([&] { + __par_backend::__parallel_stable_sort( + __first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) { + std::stable_sort(__g_first, __g_last, __g_comp); + }); + }); + } else { + std::stable_sort(__first, __last, __comp); + } +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h b/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h --- a/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h +++ b/libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h @@ -12,6 +12,7 @@ #include <__assert> #include <__config> #include <__utility/move.h> +#include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -38,6 +39,12 @@ return __reduce(std::move(__first), std::move(__last), std::move(__init)); } +template +_LIBCPP_HIDE_FROM_ABI void __parallel_stable_sort( + _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) { + __leaf_sort(__first, __last, __comp); +} + _LIBCPP_HIDE_FROM_ABI inline void __cancel_execution() {} template +#include <__config> +#include <__functional/operations.h> +#include <__type_traits/is_execution_policy.h> +#include <__type_traits/remove_cvref.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +template , + class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>, + enable_if_t, int> = 0> +_LIBCPP_HIDE_FROM_ABI void +stable_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp = {}) { + using _Backend = typename __select_backend<_RawPolicy>::type; + std::__pstl_stable_sort<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__comp)); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -1806,6 +1806,7 @@ #include <__algorithm/pstl_find.h> #include <__algorithm/pstl_for_each.h> #include <__algorithm/pstl_merge.h> +#include <__algorithm/pstl_stable_sort.h> #include <__algorithm/pstl_transform.h> #include <__algorithm/push_heap.h> #include <__algorithm/ranges_adjacent_find.h> diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -345,6 +345,9 @@ module pstl_backends_cpu_backends_serial { private header "__algorithm/pstl_backends/cpu_backends/serial.h" } + module pstl_backends_cpu_backends_stable_sort { + private header "__algorithm/pstl_backends/cpu_backends/stable_sort.h" + } module pstl_backends_cpu_backends_thread { private header "__algorithm/pstl_backends/cpu_backends/thread.h" } diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -123,6 +123,7 @@ #include <__algorithm/pstl_backends/cpu_backends/for_each.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/for_each.h'}} #include <__algorithm/pstl_backends/cpu_backends/merge.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/merge.h'}} #include <__algorithm/pstl_backends/cpu_backends/serial.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/serial.h'}} +#include <__algorithm/pstl_backends/cpu_backends/stable_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/stable_sort.h'}} #include <__algorithm/pstl_backends/cpu_backends/thread.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/thread.h'}} #include <__algorithm/pstl_backends/cpu_backends/transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/transform.h'}} #include <__algorithm/pstl_backends/cpu_backends/transform_reduce.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/transform_reduce.h'}} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/pstl.stable_sort.pass.cpp @@ -0,0 +1,143 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// UNSUPPORTED: libcpp-has-no-incomplete-pstl + +// + +// template +// void stable_sort(ExecutionPolicy&& exec, +// RandomAccessIterator first, RandomAccessIterator last); +// +// template +// void stable_sort(ExecutionPolicy&& exec, +// RandomAccessIterator first, RandomAccessIterator last, +// Compare comp); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_execution_policies.h" +#include "test_iterators.h" + +EXECUTION_POLICY_SFINAE_TEST(stable_sort); + +static_assert(sfinae_test_stable_sort); +static_assert(!sfinae_test_stable_sort); + +struct OrderedValue { + int value; + double original_order; + bool operator==(const OrderedValue& other) const { return other.value == value; } + + auto operator<(const OrderedValue& rhs) const { return value < rhs.value; } + auto operator>(const OrderedValue& rhs) const { return value > rhs.value; } +}; + +template +void test_one(std::array input, std::array expected) { + std::stable_sort(Iter(input.data()), Iter(input.data() + input.size())); + assert(input == expected); +} + +template +struct Test { + template + void operator()(Policy&& policy) { + + // Empty sequence. + test_one({}, {}); + // 1-element sequence. + test_one({1}, {1}); + // 2-element sequence. + test_one({2, 1}, {1, 2}); + // 3-element sequence. + test_one({2, 1, 3}, {1, 2, 3}); + // Longer sequence. + test_one({2, 1, 3, 6, 8, 4, 11, 5}, {1, 2, 3, 4, 5, 6, 8, 11}); + // Longer sequence with duplicates. + test_one({2, 1, 3, 6, 2, 8, 6}, {1, 2, 2, 3, 6, 6, 8}); + // All elements are the same. + test_one({1, 1, 1}, {1, 1, 1}); + // Already sorted. + test_one({1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}); + // Reverse-sorted. + test_one({5, 4, 3, 2, 1}, {1, 2, 3, 4, 5}); + // Repeating pattern. + test_one({1, 2, 1, 2, 1, 2}, {1, 1, 1, 2, 2, 2}); + + { // The sort is stable (equivalent elements remain in the same order). + using V = OrderedValue; + using Array = std::array; + Array in = {V{10, 10.1}, {12, 12.1}, {3, 3.1}, {5, 5.1}, {3, 3.2}, {3, 3.3}, {11, 11.1}, + {12, 12.2}, {4, 4.1}, {4, 4.2}, {4, 4.3}, {1, 1.1}, {6, 6.1}, {3, 3.4}, + {10, 10.2}, {8, 8.1}, {12, 12.3}, {1, 1.2}, {1, 1.3}, {5, 5.2}}; + Array expected = {V{1, 1.1}, {1, 1.2}, {1, 1.3}, {3, 3.1}, {3, 3.2}, {3, 3.3}, {3, 3.4}, + {4, 4.1}, {4, 4.2}, {4, 4.3}, {5, 5.1}, {5, 5.2}, {6, 6.1}, {8, 8.1}, + {10, 10.1}, {10, 10.2}, {11, 11.1}, {12, 12.1}, {12, 12.2}, {12, 12.3}}; + + std::stable_sort(policy, in.begin(), in.end()); + assert(in == expected); + } + + { // A custom comparator works and is stable. + using V = OrderedValue; + using Array = std::array; + + Array in = { + V{1, 1.1}, + {2, 2.1}, + {2, 2.2}, + {3, 3.1}, + {2, 2.3}, + {3, 3.2}, + {4, 4.1}, + {5, 5.1}, + {2, 2.4}, + {5, 5.2}, + {1, 1.2}}; + Array expected = { + V{5, 5.1}, + {5, 5.2}, + {4, 4.1}, + {3, 3.1}, + {3, 3.2}, + {2, 2.1}, + {2, 2.2}, + {2, 2.3}, + {2, 2.4}, + {1, 1.1}, + {1, 1.2}}; + + std::stable_sort(policy, in.begin(), in.end(), std::greater{}); + assert(in == expected); + } + } +}; + +int main(int, char**) { + types::for_each(types::random_access_iterator_list{}, TestIteratorWithPolicies{}); + +#ifndef TEST_HAS_NO_EXCEPTIONS + std::set_terminate(terminate_successful); + int a[] = {1, 2}; + try { + std::stable_sort(std::execution::par, std::begin(a), std::end(a), [](int, int) -> bool { throw int{}; }); + } catch (int) { + assert(false); + } +#endif + + return 0; +}