• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H
11 
12 #include <__algorithm/merge.h>
13 #include <__algorithm/pstl_backends/cpu_backends/backend.h>
14 #include <__config>
15 #include <__iterator/concepts.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__utility/move.h>
18 #include <optional>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
25 
26 _LIBCPP_PUSH_MACROS
27 #  include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _ExecutionPolicy,
32           class _ForwardIterator1,
33           class _ForwardIterator2,
34           class _ForwardOutIterator,
35           class _Comp>
__pstl_merge(__cpu_backend_tag,_ForwardIterator1 __first1,_ForwardIterator1 __last1,_ForwardIterator2 __first2,_ForwardIterator2 __last2,_ForwardOutIterator __result,_Comp __comp)36 _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> __pstl_merge(
37     __cpu_backend_tag,
38     _ForwardIterator1 __first1,
39     _ForwardIterator1 __last1,
40     _ForwardIterator2 __first2,
41     _ForwardIterator2 __last2,
42     _ForwardOutIterator __result,
43     _Comp __comp) {
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(
49         __first1,
50         __last1,
51         __first2,
52         __last2,
53         __result,
54         __comp,
55         [](_ForwardIterator1 __g_first1,
56            _ForwardIterator1 __g_last1,
57            _ForwardIterator2 __g_first2,
58            _ForwardIterator2 __g_last2,
59            _ForwardOutIterator __g_result,
60            _Comp __g_comp) {
61           [[maybe_unused]] auto __g_res = std::__pstl_merge<__remove_parallel_policy_t<_ExecutionPolicy>>(
62               __cpu_backend_tag{},
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 _LIBCPP_END_NAMESPACE_STD
80 
81 _LIBCPP_POP_MACROS
82 
83 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
84 
85 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_MERGE_H
86