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_SORT_H
10#define _LIBCPP___ALGORITHM_PSTL_SORT_H
11
12#include <__algorithm/pstl_backend.h>
13#include <__algorithm/pstl_frontend_dispatch.h>
14#include <__algorithm/pstl_stable_sort.h>
15#include <__config>
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>
22#include <optional>
23
24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25#  pragma GCC system_header
26#endif
27
28_LIBCPP_PUSH_MACROS
29#include <__undef_macros>
30
31#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
32
33_LIBCPP_BEGIN_NAMESPACE_STD
34
35template <class>
36void __pstl_sort();
37
38template <class _ExecutionPolicy,
39          class _RandomAccessIterator,
40          class _Comp,
41          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
42          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
43[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty> __sort(
44    _ExecutionPolicy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) noexcept {
45  return std::__pstl_frontend_dispatch(
46      _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_sort, _RawPolicy),
47      [&__policy](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
48        std::stable_sort(__policy, std::move(__g_first), std::move(__g_last), std::move(__g_comp));
49        return optional<__empty>{__empty{}};
50      },
51      std::move(__first),
52      std::move(__last),
53      std::move(__comp));
54}
55
56template <class _ExecutionPolicy,
57          class _RandomAccessIterator,
58          class _Comp,
59          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
60          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
61_LIBCPP_HIDE_FROM_ABI void
62sort(_ExecutionPolicy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) {
63  if (!std::__sort(__policy, std::move(__first), std::move(__last), std::move(__comp)))
64    std::__throw_bad_alloc();
65}
66
67template <class _ExecutionPolicy,
68          class _RandomAccessIterator,
69          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
70          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
71_LIBCPP_HIDE_FROM_ABI void
72sort(_ExecutionPolicy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last) {
73  std::sort(std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), less{});
74}
75
76_LIBCPP_END_NAMESPACE_STD
77
78#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
79
80_LIBCPP_POP_MACROS
81
82#endif // _LIBCPP___ALGORITHM_PSTL_SORT_H
83