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
24 #include <__undef_macros>
26 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
28 _LIBCPP_BEGIN_NAMESPACE_STD
33 template <class _ExecutionPolicy
,
34 class _ForwardIterator
,
36 class _BinaryOperation
= plus
<>,
37 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
38 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
39 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<_Tp
>
40 __reduce(_ExecutionPolicy
&& __policy
,
41 _ForwardIterator
&& __first
,
42 _ForwardIterator
&& __last
,
44 _BinaryOperation
&& __op
= {}) noexcept
{
45 return std::__pstl_frontend_dispatch(
46 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce
, _RawPolicy
),
47 [&__policy
](_ForwardIterator __g_first
, _ForwardIterator __g_last
, _Tp __g_init
, _BinaryOperation __g_op
) {
48 return std::__transform_reduce(
49 __policy
, std::move(__g_first
), std::move(__g_last
), std::move(__g_init
), std::move(__g_op
), __identity
{});
57 template <class _ExecutionPolicy
,
58 class _ForwardIterator
,
60 class _BinaryOperation
= plus
<>,
61 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
62 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
63 _LIBCPP_HIDE_FROM_ABI _Tp
64 reduce(_ExecutionPolicy
&& __policy
,
65 _ForwardIterator __first
,
66 _ForwardIterator __last
,
68 _BinaryOperation __op
= {}) {
69 auto __res
= std::__reduce(__policy
, std::move(__first
), std::move(__last
), std::move(__init
), std::move(__op
));
71 std::__throw_bad_alloc();
72 return *std::move(__res
);
75 template <class _ExecutionPolicy
,
76 class _ForwardIterator
,
77 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
78 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
79 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__iter_value_type
<_ForwardIterator
>>
80 __reduce(_ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _ForwardIterator
&& __last
) noexcept
{
81 return std::__pstl_frontend_dispatch(
82 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce
, _RawPolicy
),
83 [&__policy
](_ForwardIterator __g_first
, _ForwardIterator __g_last
) {
85 __policy
, std::move(__g_first
), std::move(__g_last
), __iter_value_type
<_ForwardIterator
>());
91 template <class _ExecutionPolicy
,
92 class _ForwardIterator
,
93 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
94 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
95 _LIBCPP_HIDE_FROM_ABI __iter_value_type
<_ForwardIterator
>
96 reduce(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
) {
97 auto __res
= std::__reduce(__policy
, std::move(__first
), std::move(__last
));
99 std::__throw_bad_alloc();
100 return *std::move(__res
);
103 _LIBCPP_END_NAMESPACE_STD
105 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
109 #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H