File indexing completed on 2026-05-03 08:13:59
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___PSTL_CPU_ALGOS_MERGE_H
0010 #define _LIBCPP___PSTL_CPU_ALGOS_MERGE_H
0011
0012 #include <__algorithm/merge.h>
0013 #include <__assert>
0014 #include <__config>
0015 #include <__iterator/concepts.h>
0016 #include <__pstl/backend_fwd.h>
0017 #include <__pstl/cpu_algos/cpu_traits.h>
0018 #include <__type_traits/is_execution_policy.h>
0019 #include <__utility/move.h>
0020 #include <optional>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_PUSH_MACROS
0027 #include <__undef_macros>
0028
0029 #if _LIBCPP_STD_VER >= 17
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032 namespace __pstl {
0033
0034 template <class _Backend, class _RawExecutionPolicy>
0035 struct __cpu_parallel_merge {
0036 template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardOutIterator, class _Comp>
0037 _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()(
0038 _Policy&& __policy,
0039 _ForwardIterator1 __first1,
0040 _ForwardIterator1 __last1,
0041 _ForwardIterator2 __first2,
0042 _ForwardIterator2 __last2,
0043 _ForwardOutIterator __result,
0044 _Comp __comp) const noexcept {
0045 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0046 __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
0047 __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
0048 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0049 auto __res = __cpu_traits<_Backend>::__merge(
0050 __first1,
0051 __last1,
0052 __first2,
0053 __last2,
0054 __result,
0055 __comp,
0056 [&__policy](_ForwardIterator1 __g_first1,
0057 _ForwardIterator1 __g_last1,
0058 _ForwardIterator2 __g_first2,
0059 _ForwardIterator2 __g_last2,
0060 _ForwardOutIterator __g_result,
0061 _Comp __g_comp) {
0062 using _MergeUnseq = __pstl::__merge<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0063 [[maybe_unused]] auto __g_res = _MergeUnseq()(
0064 std::__remove_parallel_policy(__policy),
0065 std::move(__g_first1),
0066 std::move(__g_last1),
0067 std::move(__g_first2),
0068 std::move(__g_last2),
0069 std::move(__g_result),
0070 std::move(__g_comp));
0071 _LIBCPP_ASSERT_INTERNAL(__g_res, "unsed/sed should never try to allocate!");
0072 });
0073 if (!__res)
0074 return nullopt;
0075 return __result + (__last1 - __first1) + (__last2 - __first2);
0076 } else {
0077 return std::merge(__first1, __last1, __first2, __last2, __result, __comp);
0078 }
0079 }
0080 };
0081
0082 }
0083 _LIBCPP_END_NAMESPACE_STD
0084
0085 #endif
0086
0087 _LIBCPP_POP_MACROS
0088
0089 #endif