xref: /llvm-project/libcxx/include/__pstl/cpu_algos/stable_sort.h (revision bbff52bfd49336bc0fdc83d8dfc616266bc07cbf)
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___PSTL_CPU_ALGOS_STABLE_SORT_H
10 #define _LIBCPP___PSTL_CPU_ALGOS_STABLE_SORT_H
11 
12 #include <__algorithm/stable_sort.h>
13 #include <__config>
14 #include <__pstl/backend_fwd.h>
15 #include <__pstl/cpu_algos/cpu_traits.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__utility/empty.h>
18 #include <optional>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 #if _LIBCPP_STD_VER >= 17
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 namespace __pstl {
28 
29 template <class _Backend, class _RawExecutionPolicy>
30 struct __cpu_parallel_stable_sort {
31   template <class _Policy, class _RandomAccessIterator, class _Comp>
32   _LIBCPP_HIDE_FROM_ABI optional<__empty>
33   operator()(_Policy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) const noexcept {
34     if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy>) {
35       return __cpu_traits<_Backend>::__stable_sort(
36           __first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
37             std::stable_sort(__g_first, __g_last, __g_comp);
38           });
39     } else {
40       std::stable_sort(__first, __last, __comp);
41       return __empty{};
42     }
43   }
44 };
45 
46 } // namespace __pstl
47 _LIBCPP_END_NAMESPACE_STD
48 
49 #endif // _LIBCPP_STD_VER >= 17
50 
51 #endif // _LIBCPP___PSTL_CPU_ALGOS_STABLE_SORT_H
52