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_REDUCE_H
10 #define _LIBCPP___NUMERIC_PSTL_REDUCE_H
12 #include <__algorithm/pstl_frontend_dispatch.h>
14 #include <__functional/identity.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__numeric/pstl_transform_reduce.h>
17 #include <__type_traits/is_execution_policy.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
25 _LIBCPP_BEGIN_NAMESPACE_STD
30 template <class _ExecutionPolicy
,
31 class _ForwardIterator
,
33 class _BinaryOperation
= plus
<>,
34 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
35 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
36 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<_Tp
>
37 __reduce(_ExecutionPolicy
&& __policy
,
38 _ForwardIterator
&& __first
,
39 _ForwardIterator
&& __last
,
41 _BinaryOperation
&& __op
= {}) noexcept
{
42 return std::__pstl_frontend_dispatch(
43 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce
, _RawPolicy
),
44 [&__policy
](_ForwardIterator __g_first
, _ForwardIterator __g_last
, _Tp __g_init
, _BinaryOperation __g_op
) {
45 return std::__transform_reduce(
46 __policy
, std::move(__g_first
), std::move(__g_last
), std::move(__g_init
), std::move(__g_op
), __identity
{});
54 template <class _ExecutionPolicy
,
55 class _ForwardIterator
,
57 class _BinaryOperation
= plus
<>,
58 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
59 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
60 _LIBCPP_HIDE_FROM_ABI _Tp
61 reduce(_ExecutionPolicy
&& __policy
,
62 _ForwardIterator __first
,
63 _ForwardIterator __last
,
65 _BinaryOperation __op
= {}) {
66 auto __res
= std::__reduce(__policy
, std::move(__first
), std::move(__last
), std::move(__init
), std::move(__op
));
68 std::__throw_bad_alloc();
69 return *std::move(__res
);
72 template <class _ExecutionPolicy
,
73 class _ForwardIterator
,
74 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
75 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
76 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__iter_value_type
<_ForwardIterator
>>
77 __reduce(_ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _ForwardIterator
&& __last
) noexcept
{
78 return std::__pstl_frontend_dispatch(
79 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce
, _RawPolicy
),
80 [&__policy
](_ForwardIterator __g_first
, _ForwardIterator __g_last
) {
82 __policy
, std::move(__g_first
), std::move(__g_last
), __iter_value_type
<_ForwardIterator
>());
88 template <class _ExecutionPolicy
,
89 class _ForwardIterator
,
90 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
91 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
92 _LIBCPP_HIDE_FROM_ABI __iter_value_type
<_ForwardIterator
>
93 reduce(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
) {
94 auto __res
= std::__reduce(__policy
, std::move(__first
), std::move(__last
));
96 std::__throw_bad_alloc();
97 return *std::move(__res
);
100 _LIBCPP_END_NAMESPACE_STD
102 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
104 #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H