• 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___NUMERIC_PSTL_TRANSFORM_REDUCE_H
10 #define _LIBCPP___NUMERIC_PSTL_TRANSFORM_REDUCE_H
11 
12 #include <__algorithm/pstl_backend.h>
13 #include <__algorithm/pstl_frontend_dispatch.h>
14 #include <__config>
15 #include <__functional/operations.h>
16 #include <__numeric/transform_reduce.h>
17 #include <__type_traits/is_execution_policy.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 _ForwardIterator1,
31           class _ForwardIterator2,
32           class _Tp,
33           class _BinaryOperation1,
34           class _BinaryOperation2,
35           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
36           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
__transform_reduce(_ExecutionPolicy &&,_ForwardIterator1 && __first1,_ForwardIterator1 && __last1,_ForwardIterator2 && __first2,_Tp && __init,_BinaryOperation1 && __reduce,_BinaryOperation2 && __transform)37 _LIBCPP_HIDE_FROM_ABI optional<_Tp> __transform_reduce(
38     _ExecutionPolicy&&,
39     _ForwardIterator1&& __first1,
40     _ForwardIterator1&& __last1,
41     _ForwardIterator2&& __first2,
42     _Tp&& __init,
43     _BinaryOperation1&& __reduce,
44     _BinaryOperation2&& __transform) noexcept {
45   using _Backend = typename __select_backend<_RawPolicy>::type;
46   return std::__pstl_transform_reduce<_RawPolicy>(
47       _Backend{},
48       std::move(__first1),
49       std::move(__last1),
50       std::move(__first2),
51       std::move(__init),
52       std::move(__reduce),
53       std::move(__transform));
54 }
55 
56 template <class _ExecutionPolicy,
57           class _ForwardIterator1,
58           class _ForwardIterator2,
59           class _Tp,
60           class _BinaryOperation1,
61           class _BinaryOperation2,
62           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
63           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
transform_reduce(_ExecutionPolicy && __policy,_ForwardIterator1 __first1,_ForwardIterator1 __last1,_ForwardIterator2 __first2,_Tp __init,_BinaryOperation1 __reduce,_BinaryOperation2 __transform)64 _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
65     _ExecutionPolicy&& __policy,
66     _ForwardIterator1 __first1,
67     _ForwardIterator1 __last1,
68     _ForwardIterator2 __first2,
69     _Tp __init,
70     _BinaryOperation1 __reduce,
71     _BinaryOperation2 __transform) {
72   auto __res = std::__transform_reduce(
73       __policy,
74       std::move(__first1),
75       std::move(__last1),
76       std::move(__first2),
77       std::move(__init),
78       std::move(__reduce),
79       std::move(__transform));
80 
81   if (!__res)
82     std::__throw_bad_alloc();
83   return *std::move(__res);
84 }
85 
86 // This overload doesn't get a customization point because it's trivial to detect (through e.g.
87 // __desugars_to) when specializing the more general variant, which should always be preferred
88 template <class _ExecutionPolicy,
89           class _ForwardIterator1,
90           class _ForwardIterator2,
91           class _Tp,
92           enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
transform_reduce(_ExecutionPolicy && __policy,_ForwardIterator1 __first1,_ForwardIterator1 __last1,_ForwardIterator2 __first2,_Tp __init)93 _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
94     _ExecutionPolicy&& __policy,
95     _ForwardIterator1 __first1,
96     _ForwardIterator1 __last1,
97     _ForwardIterator2 __first2,
98     _Tp __init) {
99   return std::transform_reduce(__policy, __first1, __last1, __first2, __init, plus{}, multiplies{});
100 }
101 
102 template <class _ExecutionPolicy,
103           class _ForwardIterator,
104           class _Tp,
105           class _BinaryOperation,
106           class _UnaryOperation,
107           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
108           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
__transform_reduce(_ExecutionPolicy &&,_ForwardIterator && __first,_ForwardIterator && __last,_Tp && __init,_BinaryOperation && __reduce,_UnaryOperation && __transform)109 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_Tp>> __transform_reduce(
110     _ExecutionPolicy&&,
111     _ForwardIterator&& __first,
112     _ForwardIterator&& __last,
113     _Tp&& __init,
114     _BinaryOperation&& __reduce,
115     _UnaryOperation&& __transform) noexcept {
116   using _Backend = typename __select_backend<_RawPolicy>::type;
117   return std::__pstl_transform_reduce<_RawPolicy>(
118       _Backend{},
119       std::move(__first),
120       std::move(__last),
121       std::move(__init),
122       std::move(__reduce),
123       std::move(__transform));
124 }
125 
126 template <class _ExecutionPolicy,
127           class _ForwardIterator,
128           class _Tp,
129           class _BinaryOperation,
130           class _UnaryOperation,
131           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
132           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
transform_reduce(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_Tp __init,_BinaryOperation __reduce,_UnaryOperation __transform)133 _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
134     _ExecutionPolicy&& __policy,
135     _ForwardIterator __first,
136     _ForwardIterator __last,
137     _Tp __init,
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));
142   if (!__res)
143     std::__throw_bad_alloc();
144   return *std::move(__res);
145 }
146 
147 _LIBCPP_END_NAMESPACE_STD
148 
149 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
150 
151 #endif // _LIBCPP___NUMERIC_PSTL_TRANSFORM_REDUCE_H
152