pstl_reduce.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template <class>
  24. void __pstl_reduce();
  25. template <class _ExecutionPolicy,
  26. class _ForwardIterator,
  27. class _Tp,
  28. class _BinaryOperation = plus<>,
  29. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  30. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  31. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_Tp>
  32. __reduce(_ExecutionPolicy&& __policy,
  33. _ForwardIterator&& __first,
  34. _ForwardIterator&& __last,
  35. _Tp&& __init,
  36. _BinaryOperation&& __op = {}) noexcept {
  37. return std::__pstl_frontend_dispatch(
  38. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
  39. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Tp __g_init, _BinaryOperation __g_op) {
  40. return std::__transform_reduce(
  41. __policy, std::move(__g_first), std::move(__g_last), std::move(__g_init), std::move(__g_op), __identity{});
  42. },
  43. std::move(__first),
  44. std::move(__last),
  45. std::move(__init),
  46. std::move(__op));
  47. }
  48. template <class _ExecutionPolicy,
  49. class _ForwardIterator,
  50. class _Tp,
  51. class _BinaryOperation = plus<>,
  52. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  53. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  54. _LIBCPP_HIDE_FROM_ABI _Tp
  55. reduce(_ExecutionPolicy&& __policy,
  56. _ForwardIterator __first,
  57. _ForwardIterator __last,
  58. _Tp __init,
  59. _BinaryOperation __op = {}) {
  60. auto __res = std::__reduce(__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__op));
  61. if (!__res)
  62. std::__throw_bad_alloc();
  63. return *std::move(__res);
  64. }
  65. template <class _ExecutionPolicy,
  66. class _ForwardIterator,
  67. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  68. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  69. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_value_type<_ForwardIterator>>
  70. __reduce(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last) noexcept {
  71. return std::__pstl_frontend_dispatch(
  72. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
  73. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last) {
  74. return std::__reduce(
  75. __policy, std::move(__g_first), std::move(__g_last), __iter_value_type<_ForwardIterator>());
  76. },
  77. std::move(__first),
  78. std::move(__last));
  79. }
  80. template <class _ExecutionPolicy,
  81. class _ForwardIterator,
  82. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  83. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  84. _LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
  85. reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
  86. auto __res = std::__reduce(__policy, std::move(__first), std::move(__last));
  87. if (!__res)
  88. std::__throw_bad_alloc();
  89. return *std::move(__res);
  90. }
  91. _LIBCPP_END_NAMESPACE_STD
  92. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  93. _LIBCPP_POP_MACROS
  94. #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H