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