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/empty.h>
17#include <optional>
18
19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20#  pragma GCC system_header
21#endif
22
23#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27template <class _ExecutionPolicy, class _RandomAccessIterator, class _Comp>
28_LIBCPP_HIDE_FROM_ABI optional<__empty>
29__pstl_stable_sort(__cpu_backend_tag, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) {
30  if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy>) {
31    return __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  } else {
36    std::stable_sort(__first, __last, __comp);
37    return __empty{};
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