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_MERGE_H
10#define _LIBCPP___ALGORITHM_PSTL_MERGE_H
11
12#include <__algorithm/pstl_backend.h>
13#include <__config>
14#include <__functional/operations.h>
15#include <__type_traits/enable_if.h>
16#include <__type_traits/is_execution_policy.h>
17#include <__type_traits/remove_cvref.h>
18#include <__utility/move.h>
19#include <optional>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22#  pragma GCC system_header
23#endif
24
25_LIBCPP_PUSH_MACROS
26#include <__undef_macros>
27
28#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
29
30_LIBCPP_BEGIN_NAMESPACE_STD
31
32template <class _ExecutionPolicy,
33          class _ForwardIterator1,
34          class _ForwardIterator2,
35          class _ForwardOutIterator,
36          class _Comp                                         = std::less<>,
37          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
38          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
39[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
40__merge(_ExecutionPolicy&&,
41        _ForwardIterator1 __first1,
42        _ForwardIterator1 __last1,
43        _ForwardIterator2 __first2,
44        _ForwardIterator2 __last2,
45        _ForwardOutIterator __result,
46        _Comp __comp = {}) noexcept {
47  using _Backend = typename __select_backend<_RawPolicy>::type;
48  return std::__pstl_merge<_RawPolicy>(
49      _Backend{},
50      std::move(__first1),
51      std::move(__last1),
52      std::move(__first2),
53      std::move(__last2),
54      std::move(__result),
55      std::move(__comp));
56}
57
58template <class _ExecutionPolicy,
59          class _ForwardIterator1,
60          class _ForwardIterator2,
61          class _ForwardOutIterator,
62          class _Comp                                         = std::less<>,
63          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
64          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
65_LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
66merge(_ExecutionPolicy&& __policy,
67      _ForwardIterator1 __first1,
68      _ForwardIterator1 __last1,
69      _ForwardIterator2 __first2,
70      _ForwardIterator2 __last2,
71      _ForwardOutIterator __result,
72      _Comp __comp = {}) {
73  auto __res = std::__merge(
74      __policy,
75      std::move(__first1),
76      std::move(__last1),
77      std::move(__first2),
78      std::move(__last2),
79      std::move(__result),
80      std::move(__comp));
81  if (!__res)
82    std::__throw_bad_alloc();
83  return *std::move(__res);
84}
85
86_LIBCPP_END_NAMESPACE_STD
87
88#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
89
90_LIBCPP_POP_MACROS
91
92#endif // _LIBCPP___ALGORITHM_PSTL_MERGE_H
93