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_COPY_H
10 #define _LIBCPP___ALGORITHM_PSTL_COPY_H
11 
12 #include <__algorithm/copy_n.h>
13 #include <__algorithm/pstl_backend.h>
14 #include <__algorithm/pstl_frontend_dispatch.h>
15 #include <__algorithm/pstl_transform.h>
16 #include <__config>
17 #include <__functional/identity.h>
18 #include <__iterator/concepts.h>
19 #include <__type_traits/enable_if.h>
20 #include <__type_traits/is_constant_evaluated.h>
21 #include <__type_traits/is_execution_policy.h>
22 #include <__type_traits/is_trivially_copyable.h>
23 #include <__type_traits/remove_cvref.h>
24 #include <__utility/move.h>
25 #include <optional>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 // TODO: Use the std::copy/move shenanigans to forward to std::memmove
39 
40 template <class>
41 void __pstl_copy();
42 
43 template <class _ExecutionPolicy,
44           class _ForwardIterator,
45           class _ForwardOutIterator,
46           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
47           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
48 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
49 __copy(_ExecutionPolicy&& __policy,
50        _ForwardIterator&& __first,
51        _ForwardIterator&& __last,
52        _ForwardOutIterator&& __result) noexcept {
53   return std::__pstl_frontend_dispatch(
54       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy, _RawPolicy),
55       [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _ForwardOutIterator __g_result) {
56         return std::__transform(__policy, __g_first, __g_last, __g_result, __identity());
57       },
58       std::move(__first),
59       std::move(__last),
60       std::move(__result));
61 }
62 
63 template <class _ExecutionPolicy,
64           class _ForwardIterator,
65           class _ForwardOutIterator,
66           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
67           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
68 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
69 copy(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _ForwardOutIterator __result) {
70   auto __res = std::__copy(__policy, std::move(__first), std::move(__last), std::move(__result));
71   if (!__res)
72     std::__throw_bad_alloc();
73   return *std::move(__res);
74 }
75 
76 template <class>
77 void __pstl_copy_n();
78 
79 template <class _ExecutionPolicy,
80           class _ForwardIterator,
81           class _ForwardOutIterator,
82           class _Size,
83           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
84           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
85 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> __copy_n(
86     _ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __n, _ForwardOutIterator&& __result) noexcept {
87   return std::__pstl_frontend_dispatch(
88       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy_n, _RawPolicy),
89       [&__policy](
90           _ForwardIterator __g_first, _Size __g_n, _ForwardOutIterator __g_result) -> optional<_ForwardIterator> {
91         if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value)
92           return std::__copy(__policy, std::move(__g_first), std::move(__g_first + __g_n), std::move(__g_result));
93         else
94           return std::copy_n(__g_first, __g_n, __g_result);
95       },
96       std::move(__first),
97       std::move(__n),
98       std::move(__result));
99 }
100 
101 template <class _ExecutionPolicy,
102           class _ForwardIterator,
103           class _ForwardOutIterator,
104           class _Size,
105           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
106           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
107 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
108 copy_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __n, _ForwardOutIterator __result) {
109   auto __res = std::__copy_n(__policy, std::move(__first), std::move(__n), std::move(__result));
110   if (!__res)
111     std::__throw_bad_alloc();
112   return *std::move(__res);
113 }
114 
115 _LIBCPP_END_NAMESPACE_STD
116 
117 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
118 
119 _LIBCPP_POP_MACROS
120 
121 #endif // _LIBCPP___ALGORITHM_PSTL_COPY_H
122