1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_PSTL_GENERATE_H
10 #define _LIBCPP___ALGORITHM_PSTL_GENERATE_H
12 #include <__algorithm/pstl_backend.h>
13 #include <__algorithm/pstl_for_each.h>
14 #include <__algorithm/pstl_frontend_dispatch.h>
16 #include <__iterator/cpp17_iterator_concepts.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__type_traits/enable_if.h>
19 #include <__type_traits/is_execution_policy.h>
20 #include <__type_traits/remove_cvref.h>
21 #include <__utility/move.h>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
28 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30 _LIBCPP_BEGIN_NAMESPACE_STD
33 void __pstl_generate();
35 template <class _ExecutionPolicy
,
36 class _ForwardIterator
,
38 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
39 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
40 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__empty
>
41 __generate(_ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _ForwardIterator
&& __last
, _Generator
&& __gen
) {
42 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
43 return std::__pstl_frontend_dispatch(
44 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate
, _RawPolicy
),
45 [&__policy
](_ForwardIterator __g_first
, _ForwardIterator __g_last
, _Generator __g_gen
) {
46 return std::__for_each(
47 __policy
, std::move(__g_first
), std::move(__g_last
), [&](__iter_reference
<_ForwardIterator
> __element
) {
48 __element
= __g_gen();
56 template <class _ExecutionPolicy
,
57 class _ForwardIterator
,
59 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
60 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
61 _LIBCPP_HIDE_FROM_ABI
void
62 generate(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, _Generator __gen
) {
63 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
64 if (!std::__generate(__policy
, std::move(__first
), std::move(__last
), std::move(__gen
)))
65 std::__throw_bad_alloc();
69 void __pstl_generate_n();
71 template <class _ExecutionPolicy
,
72 class _ForwardIterator
,
75 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
76 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
77 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__empty
>
78 __generate_n(_ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _Size
&& __n
, _Generator
&& __gen
) {
79 return std::__pstl_frontend_dispatch(
80 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate_n
, _RawPolicy
),
81 [&__policy
](_ForwardIterator __g_first
, _Size __g_n
, _Generator __g_gen
) {
82 return std::__for_each_n(
83 __policy
, std::move(__g_first
), std::move(__g_n
), [&](__iter_reference
<_ForwardIterator
> __element
) {
84 __element
= __g_gen();
92 template <class _ExecutionPolicy
,
93 class _ForwardIterator
,
96 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
97 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
98 _LIBCPP_HIDE_FROM_ABI
void
99 generate_n(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _Size __n
, _Generator __gen
) {
100 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
101 if (!std::__generate_n(__policy
, std::move(__first
), std::move(__n
), std::move(__gen
)))
102 std::__throw_bad_alloc();
105 _LIBCPP_END_NAMESPACE_STD
107 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
109 #endif // _LIBCPP___ALGORITHM_PSTL_GENERATE_H