pstl_generate.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. template <class>
  26. void __pstl_generate();
  27. template <class _ExecutionPolicy,
  28. class _ForwardIterator,
  29. class _Generator,
  30. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  31. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  32. _LIBCPP_HIDE_FROM_ABI void
  33. generate(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Generator __gen) {
  34. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  35. std::__pstl_frontend_dispatch(
  36. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate, _RawPolicy),
  37. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Generator __g_gen) {
  38. std::for_each(
  39. __policy, std::move(__g_first), std::move(__g_last), [&](__iter_reference<_ForwardIterator> __element) {
  40. __element = __g_gen();
  41. });
  42. },
  43. std::move(__first),
  44. std::move(__last),
  45. std::move(__gen));
  46. }
  47. template <class>
  48. void __pstl_generate_n();
  49. template <class _ExecutionPolicy,
  50. class _ForwardIterator,
  51. class _Size,
  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_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __n, _Generator __gen) {
  57. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  58. std::__pstl_frontend_dispatch(
  59. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate_n, _RawPolicy),
  60. [&__policy](_ForwardIterator __g_first, _Size __g_n, _Generator __g_gen) {
  61. std::for_each_n(__policy, std::move(__g_first), __g_n, [&](__iter_reference<_ForwardIterator> __element) {
  62. __element = __g_gen();
  63. });
  64. },
  65. std::move(__first),
  66. __n,
  67. std::move(__gen));
  68. }
  69. _LIBCPP_END_NAMESPACE_STD
  70. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  71. #endif // _LIBCPP___ALGORITHM_PSTL_GENERATE_H