pstl_reduce.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_Tp>
  30. __reduce(_ExecutionPolicy&& __policy,
  31. _ForwardIterator&& __first,
  32. _ForwardIterator&& __last,
  33. _Tp&& __init,
  34. _BinaryOperation&& __op = {}) noexcept {
  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 _Tp,
  49. class _BinaryOperation = plus<>,
  50. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  51. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  52. _LIBCPP_HIDE_FROM_ABI _Tp
  53. reduce(_ExecutionPolicy&& __policy,
  54. _ForwardIterator __first,
  55. _ForwardIterator __last,
  56. _Tp __init,
  57. _BinaryOperation __op = {}) {
  58. auto __res = std::__reduce(__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__op));
  59. if (!__res)
  60. std::__throw_bad_alloc();
  61. return *std::move(__res);
  62. }
  63. template <class _ExecutionPolicy,
  64. class _ForwardIterator,
  65. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  66. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  67. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_value_type<_ForwardIterator>>
  68. __reduce(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last) noexcept {
  69. return std::__pstl_frontend_dispatch(
  70. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
  71. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last) {
  72. return std::__reduce(
  73. __policy, std::move(__g_first), std::move(__g_last), __iter_value_type<_ForwardIterator>());
  74. },
  75. std::move(__first),
  76. std::move(__last));
  77. }
  78. template <class _ExecutionPolicy,
  79. class _ForwardIterator,
  80. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  81. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  82. _LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
  83. reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
  84. auto __res = std::__reduce(__policy, std::move(__first), std::move(__last));
  85. if (!__res)
  86. std::__throw_bad_alloc();
  87. return *std::move(__res);
  88. }
  89. _LIBCPP_END_NAMESPACE_STD
  90. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  91. #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H