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_MERGE_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H
11 
12 #include <__algorithm/merge.h>
13 #include <__algorithm/pstl_backends/cpu_backends/backend.h>
14 #include <__config>
15 #include <__iterator/iterator_traits.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__utility/move.h>
18 #include <__utility/terminate_on_exception.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template <class _ExecutionPolicy,
29           class _ForwardIterator1,
30           class _ForwardIterator2,
31           class _ForwardOutIterator,
32           class _Comp>
33 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator __pstl_merge(
34     __cpu_backend_tag,
35     _ForwardIterator1 __first1,
36     _ForwardIterator1 __last1,
37     _ForwardIterator2 __first2,
38     _ForwardIterator2 __last2,
39     _ForwardOutIterator __result,
40     _Comp __comp) {
41   if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
42                 __has_random_access_iterator_category<_ForwardIterator1>::value &&
43                 __has_random_access_iterator_category<_ForwardIterator2>::value &&
44                 __has_random_access_iterator_category<_ForwardOutIterator>::value) {
45     return std::__terminate_on_exception([&] {
46       __par_backend::__parallel_merge(
47           __first1,
48           __last1,
49           __first2,
50           __last2,
51           __result,
52           __comp,
53           [](_ForwardIterator1 __g_first1,
54              _ForwardIterator1 __g_last1,
55              _ForwardIterator2 __g_first2,
56              _ForwardIterator2 __g_last2,
57              _ForwardOutIterator __g_result,
58              _Comp __g_comp) {
59             return std::__pstl_merge<__remove_parallel_policy_t<_ExecutionPolicy>>(
60                 __cpu_backend_tag{},
61                 std::move(__g_first1),
62                 std::move(__g_last1),
63                 std::move(__g_first2),
64                 std::move(__g_last2),
65                 std::move(__g_result),
66                 std::move(__g_comp));
67           });
68       return __result + (__last1 - __first1) + (__last2 - __first2);
69     });
70   } else {
71     return std::merge(__first1, __last1, __first2, __last2, __result, __comp);
72   }
73 }
74 
75 _LIBCPP_END_NAMESPACE_STD
76 
77 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
78 
79 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H
80