1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_PSTL_SORT_H
10 #define _LIBCPP___ALGORITHM_PSTL_SORT_H
12 #include <__algorithm/pstl_backend.h>
13 #include <__algorithm/pstl_frontend_dispatch.h>
14 #include <__algorithm/pstl_stable_sort.h>
16 #include <__functional/operations.h>
17 #include <__type_traits/is_execution_policy.h>
18 #include <__type_traits/remove_cvref.h>
19 #include <__utility/empty.h>
20 #include <__utility/forward.h>
21 #include <__utility/move.h>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
28 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30 _LIBCPP_BEGIN_NAMESPACE_STD
35 template <class _ExecutionPolicy
,
36 class _RandomAccessIterator
,
38 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
39 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
40 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__empty
> __sort(
41 _ExecutionPolicy
&& __policy
, _RandomAccessIterator __first
, _RandomAccessIterator __last
, _Comp __comp
) noexcept
{
42 return std::__pstl_frontend_dispatch(
43 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_sort
, _RawPolicy
),
44 [&__policy
](_RandomAccessIterator __g_first
, _RandomAccessIterator __g_last
, _Comp __g_comp
) {
45 std::stable_sort(__policy
, std::move(__g_first
), std::move(__g_last
), std::move(__g_comp
));
46 return optional
<__empty
>{__empty
{}};
53 template <class _ExecutionPolicy
,
54 class _RandomAccessIterator
,
56 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
57 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
58 _LIBCPP_HIDE_FROM_ABI
void
59 sort(_ExecutionPolicy
&& __policy
, _RandomAccessIterator __first
, _RandomAccessIterator __last
, _Comp __comp
) {
60 if (!std::__sort(__policy
, std::move(__first
), std::move(__last
), std::move(__comp
)))
61 std::__throw_bad_alloc();
64 template <class _ExecutionPolicy
,
65 class _RandomAccessIterator
,
66 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
67 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
68 _LIBCPP_HIDE_FROM_ABI
void
69 sort(_ExecutionPolicy
&& __policy
, _RandomAccessIterator __first
, _RandomAccessIterator __last
) {
70 std::sort(std::forward
<_ExecutionPolicy
>(__policy
), std::move(__first
), std::move(__last
), less
{});
73 _LIBCPP_END_NAMESPACE_STD
75 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
77 #endif // _LIBCPP___ALGORITHM_PSTL_SORT_H