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___NUMERIC_PSTL_TRANSFORM_REDUCE_H
10 #define _LIBCPP___NUMERIC_PSTL_TRANSFORM_REDUCE_H
12 #include <__algorithm/pstl_backend.h>
13 #include <__algorithm/pstl_frontend_dispatch.h>
15 #include <__functional/operations.h>
16 #include <__numeric/transform_reduce.h>
17 #include <__type_traits/is_execution_policy.h>
18 #include <__utility/move.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
25 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 template <class _ExecutionPolicy
,
30 class _ForwardIterator1
,
31 class _ForwardIterator2
,
33 class _BinaryOperation1
,
34 class _BinaryOperation2
,
35 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
36 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
37 _LIBCPP_HIDE_FROM_ABI optional
<_Tp
> __transform_reduce(
39 _ForwardIterator1
&& __first1
,
40 _ForwardIterator1
&& __last1
,
41 _ForwardIterator2
&& __first2
,
43 _BinaryOperation1
&& __reduce
,
44 _BinaryOperation2
&& __transform
) noexcept
{
45 using _Backend
= typename __select_backend
<_RawPolicy
>::type
;
46 return std::__pstl_transform_reduce
<_RawPolicy
>(
53 std::move(__transform
));
56 template <class _ExecutionPolicy
,
57 class _ForwardIterator1
,
58 class _ForwardIterator2
,
60 class _BinaryOperation1
,
61 class _BinaryOperation2
,
62 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
63 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
64 _LIBCPP_HIDE_FROM_ABI _Tp
transform_reduce(
65 _ExecutionPolicy
&& __policy
,
66 _ForwardIterator1 __first1
,
67 _ForwardIterator1 __last1
,
68 _ForwardIterator2 __first2
,
70 _BinaryOperation1 __reduce
,
71 _BinaryOperation2 __transform
) {
72 auto __res
= std::__transform_reduce(
79 std::move(__transform
));
82 std::__throw_bad_alloc();
83 return *std::move(__res
);
86 // This overload doesn't get a customization point because it's trivial to detect (through e.g.
87 // __is_trivial_plus_operation) when specializing the more general variant, which should always be preferred
88 template <class _ExecutionPolicy
,
89 class _ForwardIterator1
,
90 class _ForwardIterator2
,
92 enable_if_t
<is_execution_policy_v
<__remove_cvref_t
<_ExecutionPolicy
>>, int> = 0>
93 _LIBCPP_HIDE_FROM_ABI _Tp
transform_reduce(
94 _ExecutionPolicy
&& __policy
,
95 _ForwardIterator1 __first1
,
96 _ForwardIterator1 __last1
,
97 _ForwardIterator2 __first2
,
99 return std::transform_reduce(__policy
, __first1
, __last1
, __first2
, __init
, plus
{}, multiplies
{});
102 template <class _ExecutionPolicy
,
103 class _ForwardIterator
,
105 class _BinaryOperation
,
106 class _UnaryOperation
,
107 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
108 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
109 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__remove_cvref_t
<_Tp
>> __transform_reduce(
111 _ForwardIterator
&& __first
,
112 _ForwardIterator
&& __last
,
114 _BinaryOperation
&& __reduce
,
115 _UnaryOperation
&& __transform
) noexcept
{
116 using _Backend
= typename __select_backend
<_RawPolicy
>::type
;
117 return std::__pstl_transform_reduce
<_RawPolicy
>(
123 std::move(__transform
));
126 template <class _ExecutionPolicy
,
127 class _ForwardIterator
,
129 class _BinaryOperation
,
130 class _UnaryOperation
,
131 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
132 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
133 _LIBCPP_HIDE_FROM_ABI _Tp
transform_reduce(
134 _ExecutionPolicy
&& __policy
,
135 _ForwardIterator __first
,
136 _ForwardIterator __last
,
138 _BinaryOperation __reduce
,
139 _UnaryOperation __transform
) {
140 auto __res
= std::__transform_reduce(
141 __policy
, std::move(__first
), std::move(__last
), std::move(__init
), std::move(__reduce
), std::move(__transform
));
143 std::__throw_bad_alloc();
144 return *std::move(__res
);
147 _LIBCPP_END_NAMESPACE_STD
149 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
151 #endif // _LIBCPP___NUMERIC_PSTL_TRANSFORM_REDUCE_H