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_BACKENDS_CPU_BACKENDS_MERGE_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H
12 #include <__algorithm/merge.h>
13 #include <__algorithm/pstl_backends/cpu_backends/backend.h>
15 #include <__iterator/concepts.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__utility/move.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
24 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
27 # include <__undef_macros>
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 template <class _ExecutionPolicy
,
32 class _ForwardIterator1
,
33 class _ForwardIterator2
,
34 class _ForwardOutIterator
,
36 _LIBCPP_HIDE_FROM_ABI optional
<_ForwardOutIterator
> __pstl_merge(
38 _ForwardIterator1 __first1
,
39 _ForwardIterator1 __last1
,
40 _ForwardIterator2 __first2
,
41 _ForwardIterator2 __last2
,
42 _ForwardOutIterator __result
,
44 if constexpr (__is_parallel_execution_policy_v
<_ExecutionPolicy
> &&
45 __has_random_access_iterator_category_or_concept
<_ForwardIterator1
>::value
&&
46 __has_random_access_iterator_category_or_concept
<_ForwardIterator2
>::value
&&
47 __has_random_access_iterator_category_or_concept
<_ForwardOutIterator
>::value
) {
48 auto __res
= __par_backend::__parallel_merge(
55 [](_ForwardIterator1 __g_first1
,
56 _ForwardIterator1 __g_last1
,
57 _ForwardIterator2 __g_first2
,
58 _ForwardIterator2 __g_last2
,
59 _ForwardOutIterator __g_result
,
61 [[maybe_unused
]] auto __g_res
= std::__pstl_merge
<__remove_parallel_policy_t
<_ExecutionPolicy
>>(
63 std::move(__g_first1
),
65 std::move(__g_first2
),
67 std::move(__g_result
),
69 _LIBCPP_ASSERT_INTERNAL(__g_res
, "unsed/sed should never try to allocate!");
73 return __result
+ (__last1
- __first1
) + (__last2
- __first2
);
75 return std::merge(__first1
, __last1
, __first2
, __last2
, __result
, __comp
);
79 _LIBCPP_END_NAMESPACE_STD
83 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
85 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H