xref: /freebsd/contrib/llvm-project/libcxx/include/__pstl/cpu_algos/stable_sort.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 _LIBCPP_BEGIN_NAMESPACE_STD
25 namespace __pstl {
26 
27 template <class _Backend, class _RawExecutionPolicy>
28 struct __cpu_parallel_stable_sort {
29   template <class _Policy, class _RandomAccessIterator, class _Comp>
30   _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator__cpu_parallel_stable_sort31   operator()(_Policy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) const noexcept {
32     if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy>) {
33       return __cpu_traits<_Backend>::__stable_sort(
34           __first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
35             std::stable_sort(__g_first, __g_last, __g_comp);
36           });
37     } else {
38       std::stable_sort(__first, __last, __comp);
39       return __empty{};
40     }
41   }
42 };
43 
44 } // namespace __pstl
45 _LIBCPP_END_NAMESPACE_STD
46 
47 #endif // _LIBCPP___PSTL_CPU_ALGOS_STABLE_SORT_H
48