1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_PSTL_COPY_H
10 #define _LIBCPP___ALGORITHM_PSTL_COPY_H
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>
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>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
31 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
33 _LIBCPP_BEGIN_NAMESPACE_STD
35 // TODO: Use the std::copy/move shenanigans to forward to std::memmove
40 template <class _ExecutionPolicy
,
41 class _ForwardIterator
,
42 class _ForwardOutIterator
,
43 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
44 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
45 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<_ForwardOutIterator
>
46 __copy(_ExecutionPolicy
&& __policy
,
47 _ForwardIterator
&& __first
,
48 _ForwardIterator
&& __last
,
49 _ForwardOutIterator
&& __result
) noexcept
{
50 return std::__pstl_frontend_dispatch(
51 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy
, _RawPolicy
),
52 [&__policy
](_ForwardIterator __g_first
, _ForwardIterator __g_last
, _ForwardOutIterator __g_result
) {
53 return std::__transform(__policy
, __g_first
, __g_last
, __g_result
, __identity());
60 template <class _ExecutionPolicy
,
61 class _ForwardIterator
,
62 class _ForwardOutIterator
,
63 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
64 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
65 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
66 copy(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, _ForwardOutIterator __result
) {
67 auto __res
= std::__copy(__policy
, std::move(__first
), std::move(__last
), std::move(__result
));
69 std::__throw_bad_alloc();
70 return *std::move(__res
);
76 template <class _ExecutionPolicy
,
77 class _ForwardIterator
,
78 class _ForwardOutIterator
,
80 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
81 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
82 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<_ForwardOutIterator
> __copy_n(
83 _ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _Size
&& __n
, _ForwardOutIterator
&& __result
) noexcept
{
84 return std::__pstl_frontend_dispatch(
85 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy_n
, _RawPolicy
),
87 _ForwardIterator __g_first
, _Size __g_n
, _ForwardOutIterator __g_result
) -> optional
<_ForwardIterator
> {
88 if constexpr (__has_random_access_iterator_category_or_concept
<_ForwardIterator
>::value
)
89 return std::__copy(__policy
, std::move(__g_first
), std::move(__g_first
+ __g_n
), std::move(__g_result
));
91 return std::copy_n(__g_first
, __g_n
, __g_result
);
98 template <class _ExecutionPolicy
,
99 class _ForwardIterator
,
100 class _ForwardOutIterator
,
102 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
103 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
104 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
105 copy_n(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _Size __n
, _ForwardOutIterator __result
) {
106 auto __res
= std::__copy_n(__policy
, std::move(__first
), std::move(__n
), std::move(__result
));
108 std::__throw_bad_alloc();
109 return *std::move(__res
);
112 _LIBCPP_END_NAMESPACE_STD
114 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
116 #endif // _LIBCPP___ALGORITHM_PSTL_COPY_H