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_REDUCE_H
10 #define _LIBCPP___NUMERIC_PSTL_REDUCE_H
11 
12 #include <__algorithm/pstl_frontend_dispatch.h>
13 #include <__config>
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>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class>
28 void __pstl_reduce();
29 
30 template <class _ExecutionPolicy,
31           class _ForwardIterator,
32           class _Tp,
33           class _BinaryOperation                              = plus<>,
34           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
35           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
36 _LIBCPP_HIDE_FROM_ABI _Tp
37 reduce(_ExecutionPolicy&& __policy,
38        _ForwardIterator __first,
39        _ForwardIterator __last,
40        _Tp __init,
41        _BinaryOperation __op = {}) {
42   return std::__pstl_frontend_dispatch(
43       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce),
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{});
47       },
48       std::move(__first),
49       std::move(__last),
50       std::move(__init),
51       std::move(__op));
52 }
53 
54 template <class _ExecutionPolicy,
55           class _ForwardIterator,
56           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
57           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
58 _LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
59 reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
60   return std::__pstl_frontend_dispatch(
61       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce),
62       [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last) {
63         return std::reduce(__policy, __g_first, __g_last, __iter_value_type<_ForwardIterator>());
64       },
65       std::move(__first),
66       std::move(__last));
67 }
68 
69 _LIBCPP_END_NAMESPACE_STD
70 
71 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
72 
73 #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H
74