1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
11 
12 #include <__algorithm/pstl_backends/cpu_backends/backend.h>
13 #include <__algorithm/stable_sort.h>
14 #include <__config>
15 #include <__type_traits/is_execution_policy.h>
16 #include <__utility/terminate_on_exception.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _ExecutionPolicy, class _RandomAccessIterator, class _Comp>
27 _LIBCPP_HIDE_FROM_ABI void
28 __pstl_stable_sort(__cpu_backend_tag, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) {
29   if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy>) {
30     std::__terminate_on_exception([&] {
31       __par_backend::__parallel_stable_sort(
32           __first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
33             std::stable_sort(__g_first, __g_last, __g_comp);
34           });
35     });
36   } else {
37     std::stable_sort(__first, __last, __comp);
38   }
39 }
40 
41 _LIBCPP_END_NAMESPACE_STD
42 
43 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
44 
45 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
46