• 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_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 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_Tp>
37 __reduce(_ExecutionPolicy&& __policy,
38          _ForwardIterator&& __first,
39          _ForwardIterator&& __last,
40          _Tp&& __init,
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{});
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 _Tp,
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,
64        _Tp __init,
65        _BinaryOperation __op = {}) {
66   auto __res = std::__reduce(__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__op));
67   if (!__res)
68     std::__throw_bad_alloc();
69   return *std::move(__res);
70 }
71 
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>>
__reduce(_ExecutionPolicy && __policy,_ForwardIterator && __first,_ForwardIterator && __last)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) {
81         return std::__reduce(
82             __policy, std::move(__g_first), std::move(__g_last), __iter_value_type<_ForwardIterator>());
83       },
84       std::move(__first),
85       std::move(__last));
86 }
87 
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>
reduce(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last)93 reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
94   auto __res = std::__reduce(__policy, std::move(__first), std::move(__last));
95   if (!__res)
96     std::__throw_bad_alloc();
97   return *std::move(__res);
98 }
99 
100 _LIBCPP_END_NAMESPACE_STD
101 
102 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
103 
104 #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H
105