pstl_generate.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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___ALGORITHM_PSTL_GENERATE_H
  9. #define _LIBCPP___ALGORITHM_PSTL_GENERATE_H
  10. #include <__algorithm/pstl_backend.h>
  11. #include <__algorithm/pstl_for_each.h>
  12. #include <__algorithm/pstl_frontend_dispatch.h>
  13. #include <__config>
  14. #include <__iterator/cpp17_iterator_concepts.h>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_execution_policy.h>
  18. #include <__type_traits/remove_cvref.h>
  19. #include <__utility/move.h>
  20. #include <optional>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_PUSH_MACROS
  25. #include <__undef_macros>
  26. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. template <class>
  29. void __pstl_generate();
  30. template <class _ExecutionPolicy,
  31. class _ForwardIterator,
  32. class _Generator,
  33. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  34. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  35. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
  36. __generate(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Generator&& __gen) {
  37. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  38. return std::__pstl_frontend_dispatch(
  39. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate, _RawPolicy),
  40. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Generator __g_gen) {
  41. return std::__for_each(
  42. __policy, std::move(__g_first), std::move(__g_last), [&](__iter_reference<_ForwardIterator> __element) {
  43. __element = __g_gen();
  44. });
  45. },
  46. std::move(__first),
  47. std::move(__last),
  48. std::move(__gen));
  49. }
  50. template <class _ExecutionPolicy,
  51. class _ForwardIterator,
  52. class _Generator,
  53. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  54. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  55. _LIBCPP_HIDE_FROM_ABI void
  56. generate(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Generator __gen) {
  57. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  58. if (!std::__generate(__policy, std::move(__first), std::move(__last), std::move(__gen)))
  59. std::__throw_bad_alloc();
  60. }
  61. template <class>
  62. void __pstl_generate_n();
  63. template <class _ExecutionPolicy,
  64. class _ForwardIterator,
  65. class _Size,
  66. class _Generator,
  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<__empty>
  70. __generate_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __n, _Generator&& __gen) {
  71. return std::__pstl_frontend_dispatch(
  72. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate_n, _RawPolicy),
  73. [&__policy](_ForwardIterator __g_first, _Size __g_n, _Generator __g_gen) {
  74. return std::__for_each_n(
  75. __policy, std::move(__g_first), std::move(__g_n), [&](__iter_reference<_ForwardIterator> __element) {
  76. __element = __g_gen();
  77. });
  78. },
  79. std::move(__first),
  80. __n,
  81. std::move(__gen));
  82. }
  83. template <class _ExecutionPolicy,
  84. class _ForwardIterator,
  85. class _Size,
  86. class _Generator,
  87. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  88. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  89. _LIBCPP_HIDE_FROM_ABI void
  90. generate_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __n, _Generator __gen) {
  91. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  92. if (!std::__generate_n(__policy, std::move(__first), std::move(__n), std::move(__gen)))
  93. std::__throw_bad_alloc();
  94. }
  95. _LIBCPP_END_NAMESPACE_STD
  96. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  97. _LIBCPP_POP_MACROS
  98. #endif // _LIBCPP___ALGORITHM_PSTL_GENERATE_H