• 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_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 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
32 
33 _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 // TODO: Use the std::copy/move shenanigans to forward to std::memmove
36 
37 template <class>
38 void __pstl_copy();
39 
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>
__copy(_ExecutionPolicy && __policy,_ForwardIterator && __first,_ForwardIterator && __last,_ForwardOutIterator && __result)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());
54       },
55       std::move(__first),
56       std::move(__last),
57       std::move(__result));
58 }
59 
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
copy(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_ForwardOutIterator __result)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));
68   if (!__res)
69     std::__throw_bad_alloc();
70   return *std::move(__res);
71 }
72 
73 template <class>
74 void __pstl_copy_n();
75 
76 template <class _ExecutionPolicy,
77           class _ForwardIterator,
78           class _ForwardOutIterator,
79           class _Size,
80           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
81           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
__copy_n(_ExecutionPolicy && __policy,_ForwardIterator && __first,_Size && __n,_ForwardOutIterator && __result)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),
86       [&__policy](
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));
90         else
91           return std::copy_n(__g_first, __g_n, __g_result);
92       },
93       std::move(__first),
94       std::move(__n),
95       std::move(__result));
96 }
97 
98 template <class _ExecutionPolicy,
99           class _ForwardIterator,
100           class _ForwardOutIterator,
101           class _Size,
102           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
103           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
104 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
copy_n(_ExecutionPolicy && __policy,_ForwardIterator __first,_Size __n,_ForwardOutIterator __result)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));
107   if (!__res)
108     std::__throw_bad_alloc();
109   return *std::move(__res);
110 }
111 
112 _LIBCPP_END_NAMESPACE_STD
113 
114 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
115 
116 #endif // _LIBCPP___ALGORITHM_PSTL_COPY_H
117