| 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 | #if _LIBCPP_STD_VER >= 17 |
| 30 | |
| 31 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 32 | namespace __pstl { |
| 33 | |
| 34 | template <class _Backend, class _RawExecutionPolicy> |
| 35 | struct __cpu_parallel_merge { |
| 36 | template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardOutIterator, class _Comp> |
| 37 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()( |
| 38 | _Policy&& __policy, |
| 39 | _ForwardIterator1 __first1, |
| 40 | _ForwardIterator1 __last1, |
| 41 | _ForwardIterator2 __first2, |
| 42 | _ForwardIterator2 __last2, |
| 43 | _ForwardOutIterator __result, |
| 44 | _Comp __comp) const noexcept { |
| 45 | if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> && |
| 46 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
| 47 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value && |
| 48 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
| 49 | auto __res = __cpu_traits<_Backend>::__merge( |
| 50 | __first1, |
| 51 | __last1, |
| 52 | __first2, |
| 53 | __last2, |
| 54 | __result, |
| 55 | __comp, |
| 56 | [&__policy](_ForwardIterator1 __g_first1, |
| 57 | _ForwardIterator1 __g_last1, |
| 58 | _ForwardIterator2 __g_first2, |
| 59 | _ForwardIterator2 __g_last2, |
| 60 | _ForwardOutIterator __g_result, |
| 61 | _Comp __g_comp) { |
| 62 | using _MergeUnseq = __pstl::__merge<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>; |
| 63 | [[maybe_unused]] auto __g_res = _MergeUnseq()( |
| 64 | std::__remove_parallel_policy(__policy), |
| 65 | std::move(__g_first1), |
| 66 | std::move(__g_last1), |
| 67 | std::move(__g_first2), |
| 68 | std::move(__g_last2), |
| 69 | std::move(__g_result), |
| 70 | std::move(__g_comp)); |
| 71 | _LIBCPP_ASSERT_INTERNAL(__g_res, "unsed/sed should never try to allocate!" ); |
| 72 | }); |
| 73 | if (!__res) |
| 74 | return nullopt; |
| 75 | return __result + (__last1 - __first1) + (__last2 - __first2); |
| 76 | } else { |
| 77 | return std::merge(__first1, __last1, __first2, __last2, __result, __comp); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | } // namespace __pstl |
| 83 | _LIBCPP_END_NAMESPACE_STD |
| 84 | |
| 85 | #endif // _LIBCPP_STD_VER >= 17 |
| 86 | |
| 87 | _LIBCPP_POP_MACROS |
| 88 | |
| 89 | #endif // _LIBCPP___PSTL_CPU_ALGOS_MERGE_H |
| 90 | |