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_LIBCPP_PUSH_MACROS 24#include <__undef_macros> 25 26#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 27 28_LIBCPP_BEGIN_NAMESPACE_STD 29 30template <class> 31void __pstl_reduce(); 32 33template <class _ExecutionPolicy, 34 class _ForwardIterator, 35 class _Tp, 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, 43 _Tp&& __init, 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{}); 50 }, 51 std::move(__first), 52 std::move(__last), 53 std::move(__init), 54 std::move(__op)); 55} 56 57template <class _ExecutionPolicy, 58 class _ForwardIterator, 59 class _Tp, 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 64reduce(_ExecutionPolicy&& __policy, 65 _ForwardIterator __first, 66 _ForwardIterator __last, 67 _Tp __init, 68 _BinaryOperation __op = {}) { 69 auto __res = std::__reduce(__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__op)); 70 if (!__res) 71 std::__throw_bad_alloc(); 72 return *std::move(__res); 73} 74 75template <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) { 84 return std::__reduce( 85 __policy, std::move(__g_first), std::move(__g_last), __iter_value_type<_ForwardIterator>()); 86 }, 87 std::move(__first), 88 std::move(__last)); 89} 90 91template <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> 96reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) { 97 auto __res = std::__reduce(__policy, std::move(__first), std::move(__last)); 98 if (!__res) 99 std::__throw_bad_alloc(); 100 return *std::move(__res); 101} 102 103_LIBCPP_END_NAMESPACE_STD 104 105#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 106 107_LIBCPP_POP_MACROS 108 109#endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H 110