pstl_reduce.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef _LIBCPP___NUMERIC_PSTL_REDUCE_H
  9. #define _LIBCPP___NUMERIC_PSTL_REDUCE_H
  10. #include <__algorithm/pstl_frontend_dispatch.h>
  11. #include <__config>
  12. #include <__functional/identity.h>
  13. #include <__iterator/iterator_traits.h>
  14. #include <__numeric/pstl_transform_reduce.h>
  15. #include <__type_traits/is_execution_policy.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class>
  22. void __pstl_reduce();
  23. template <class _ExecutionPolicy,
  24. class _ForwardIterator,
  25. class _Tp,
  26. class _BinaryOperation = plus<>,
  27. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  28. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  29. _LIBCPP_HIDE_FROM_ABI _Tp
  30. reduce(_ExecutionPolicy&& __policy,
  31. _ForwardIterator __first,
  32. _ForwardIterator __last,
  33. _Tp __init,
  34. _BinaryOperation __op = {}) {
  35. return std::__pstl_frontend_dispatch(
  36. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
  37. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Tp __g_init, _BinaryOperation __g_op) {
  38. return std::transform_reduce(
  39. __policy, std::move(__g_first), std::move(__g_last), std::move(__g_init), std::move(__g_op), __identity{});
  40. },
  41. std::move(__first),
  42. std::move(__last),
  43. std::move(__init),
  44. std::move(__op));
  45. }
  46. template <class _ExecutionPolicy,
  47. class _ForwardIterator,
  48. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  49. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  50. _LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
  51. reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
  52. return std::__pstl_frontend_dispatch(
  53. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
  54. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last) {
  55. return std::reduce(__policy, __g_first, __g_last, __iter_value_type<_ForwardIterator>());
  56. },
  57. std::move(__first),
  58. std::move(__last));
  59. }
  60. _LIBCPP_END_NAMESPACE_STD
  61. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  62. #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H