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_TRANSFORM_H
10 #define _LIBCPP___ALGORITHM_PSTL_TRANSFORM_H
11 
12 #include <__algorithm/pstl_backend.h>
13 #include <__config>
14 #include <__iterator/cpp17_iterator_concepts.h>
15 #include <__type_traits/enable_if.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__type_traits/remove_cvref.h>
18 #include <__utility/move.h>
19 #include <optional>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template <class _ExecutionPolicy,
30           class _ForwardIterator,
31           class _ForwardOutIterator,
32           class _UnaryOperation,
33           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
34           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
35 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardOutIterator>>
36 __transform(_ExecutionPolicy&&,
37             _ForwardIterator&& __first,
38             _ForwardIterator&& __last,
39             _ForwardOutIterator&& __result,
40             _UnaryOperation&& __op) noexcept {
41   using _Backend = typename __select_backend<_RawPolicy>::type;
42   return std::__pstl_transform<_RawPolicy>(
43       _Backend{}, std::move(__first), std::move(__last), std::move(__result), std::move(__op));
44 }
45 
46 template <class _ExecutionPolicy,
47           class _ForwardIterator,
48           class _ForwardOutIterator,
49           class _UnaryOperation,
50           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
51           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
52 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator transform(
53     _ExecutionPolicy&& __policy,
54     _ForwardIterator __first,
55     _ForwardIterator __last,
56     _ForwardOutIterator __result,
57     _UnaryOperation __op) {
58   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
59   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardOutIterator);
60   _LIBCPP_REQUIRE_CPP17_OUTPUT_ITERATOR(_ForwardOutIterator, decltype(__op(*__first)));
61   auto __res = std::__transform(__policy, std::move(__first), std::move(__last), std::move(__result), std::move(__op));
62   if (!__res)
63     std::__throw_bad_alloc();
64   return *std::move(__res);
65 }
66 
67 template <class _ExecutionPolicy,
68           class _ForwardIterator1,
69           class _ForwardIterator2,
70           class _ForwardOutIterator,
71           class _BinaryOperation,
72           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
73           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
74 _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardOutIterator>>
75 __transform(_ExecutionPolicy&&,
76             _ForwardIterator1&& __first1,
77             _ForwardIterator1&& __last1,
78             _ForwardIterator2&& __first2,
79             _ForwardOutIterator&& __result,
80             _BinaryOperation&& __op) noexcept {
81   using _Backend = typename __select_backend<_RawPolicy>::type;
82   return std::__pstl_transform<_RawPolicy>(
83       _Backend{}, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__result), std::move(__op));
84 }
85 
86 template <class _ExecutionPolicy,
87           class _ForwardIterator1,
88           class _ForwardIterator2,
89           class _ForwardOutIterator,
90           class _BinaryOperation,
91           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
92           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
93 _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator transform(
94     _ExecutionPolicy&& __policy,
95     _ForwardIterator1 __first1,
96     _ForwardIterator1 __last1,
97     _ForwardIterator2 __first2,
98     _ForwardOutIterator __result,
99     _BinaryOperation __op) {
100   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator1);
101   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator2);
102   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardOutIterator);
103   _LIBCPP_REQUIRE_CPP17_OUTPUT_ITERATOR(_ForwardOutIterator, decltype(__op(*__first1, *__first2)));
104   auto __res = std::__transform(
105       __policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__result), std::move(__op));
106   if (!__res)
107     std::__throw_bad_alloc();
108   return *std::move(__res);
109 }
110 
111 _LIBCPP_END_NAMESPACE_STD
112 
113 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
114 
115 #endif // _LIBCPP___ALGORITHM_PSTL_TRANSFORM_H
116