| 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_BACKENDS_STATPART_MERGE_H |
| 10 | #define _LIBCPP___PSTL_BACKENDS_STATPART_MERGE_H |
| 11 | |
| 12 | #include <__algorithm/merge.h> |
| 13 | #include <__config> |
| 14 | #include <__execution/is_execution_policy.h> |
| 15 | #include <__iterator/concepts.h> |
| 16 | #include <__iterator/iterator_traits.h> |
| 17 | #include <__optional/nullopt_t.h> |
| 18 | #include <__optional/optional.h> |
| 19 | #include <__pstl/backend_fwd.h> |
| 20 | #include <__pstl/backends/statpart/basis.h> |
| 21 | #include <__utility/move.h> |
| 22 | |
| 23 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 24 | # pragma GCC system_header |
| 25 | #endif |
| 26 | |
| 27 | _LIBCPP_PUSH_MACROS |
| 28 | #include <__undef_macros> |
| 29 | |
| 30 | #if _LIBCPP_STD_VER >= 17 |
| 31 | |
| 32 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 33 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 34 | namespace __pstl { |
| 35 | |
| 36 | template <class _ExecutionPolicy> |
| 37 | struct __merge<__statpart_backend_tag, _ExecutionPolicy> { |
| 38 | template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardOutIterator, class _Comp> |
| 39 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()( |
| 40 | _Policy&&, |
| 41 | _ForwardIterator1 __first1, |
| 42 | _ForwardIterator1 __last1, |
| 43 | _ForwardIterator2 __first2, |
| 44 | _ForwardIterator2 __last2, |
| 45 | _ForwardOutIterator __result, |
| 46 | _Comp __comp) const noexcept { |
| 47 | if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && |
| 48 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
| 49 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value && |
| 50 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
| 51 | auto __res = __statpart::__merge( |
| 52 | __first1, |
| 53 | __last1, |
| 54 | __first2, |
| 55 | __last2, |
| 56 | __result, |
| 57 | __comp, |
| 58 | [](_ForwardIterator1 __brick_first1, |
| 59 | _ForwardIterator1 __brick_last1, |
| 60 | _ForwardIterator2 __brick_first2, |
| 61 | _ForwardIterator2 __brick_last2, |
| 62 | _ForwardOutIterator __brick_result, |
| 63 | _Comp __g_comp) { |
| 64 | std::merge(std::move(__brick_first1), |
| 65 | std::move(__brick_last1), |
| 66 | std::move(__brick_first2), |
| 67 | std::move(__brick_last2), |
| 68 | std::move(__brick_result), |
| 69 | std::move(__g_comp)); |
| 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_EXPLICIT_ABI_ANNOTATIONS |
| 82 | _LIBCPP_END_NAMESPACE_STD |
| 83 | |
| 84 | #endif // _LIBCPP_STD_VER >= 17 |
| 85 | |
| 86 | _LIBCPP_POP_MACROS |
| 87 | |
| 88 | #endif // _LIBCPP___PSTL_BACKENDS_STATPART_MERGE_H |
| 89 | |