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___PSTL_CPU_ALGOS_MERGE_H |
10 | #define _LIBCPP___PSTL_CPU_ALGOS_MERGE_H |
11 | |
12 | #include <__algorithm/merge.h> |
13 | #include <__assert> |
14 | #include <__config> |
15 | #include <__iterator/concepts.h> |
16 | #include <__pstl/backend_fwd.h> |
17 | #include <__pstl/cpu_algos/cpu_traits.h> |
18 | #include <__type_traits/is_execution_policy.h> |
19 | #include <__utility/move.h> |
20 | #include <optional> |
21 | |
22 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
23 | # pragma GCC system_header |
24 | #endif |
25 | |
26 | _LIBCPP_PUSH_MACROS |
27 | #include <__undef_macros> |
28 | |
29 | _LIBCPP_BEGIN_NAMESPACE_STD |
30 | namespace __pstl { |
31 | |
32 | template <class _Backend, class _RawExecutionPolicy> |
33 | struct __cpu_parallel_merge { |
34 | template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardOutIterator, class _Comp> |
35 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()( |
36 | _Policy&& __policy, |
37 | _ForwardIterator1 __first1, |
38 | _ForwardIterator1 __last1, |
39 | _ForwardIterator2 __first2, |
40 | _ForwardIterator2 __last2, |
41 | _ForwardOutIterator __result, |
42 | _Comp __comp) const noexcept { |
43 | if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> && |
44 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
45 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value && |
46 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
47 | auto __res = __cpu_traits<_Backend>::__merge( |
48 | __first1, |
49 | __last1, |
50 | __first2, |
51 | __last2, |
52 | __result, |
53 | __comp, |
54 | [&__policy](_ForwardIterator1 __g_first1, |
55 | _ForwardIterator1 __g_last1, |
56 | _ForwardIterator2 __g_first2, |
57 | _ForwardIterator2 __g_last2, |
58 | _ForwardOutIterator __g_result, |
59 | _Comp __g_comp) { |
60 | using _MergeUnseq = __pstl::__merge<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>; |
61 | [[maybe_unused]] auto __g_res = _MergeUnseq()( |
62 | std::__remove_parallel_policy(__policy), |
63 | std::move(__g_first1), |
64 | std::move(__g_last1), |
65 | std::move(__g_first2), |
66 | std::move(__g_last2), |
67 | std::move(__g_result), |
68 | std::move(__g_comp)); |
69 | _LIBCPP_ASSERT_INTERNAL(__g_res, "unsed/sed should never try to allocate!" ); |
70 | }); |
71 | if (!__res) |
72 | return nullopt; |
73 | return __result + (__last1 - __first1) + (__last2 - __first2); |
74 | } else { |
75 | return std::merge(__first1, __last1, __first2, __last2, __result, __comp); |
76 | } |
77 | } |
78 | }; |
79 | |
80 | } // namespace __pstl |
81 | _LIBCPP_END_NAMESPACE_STD |
82 | |
83 | _LIBCPP_POP_MACROS |
84 | |
85 | #endif // _LIBCPP___PSTL_CPU_ALGOS_MERGE_H |
86 | |