Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __algorithm / pstl_generate.h
blob56538392d5b5dddc9950541ca38f39392521618c
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 //===----------------------------------------------------------------------===//
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>
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>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
26 #endif
28 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30 _LIBCPP_BEGIN_NAMESPACE_STD
32 template <class>
33 void __pstl_generate();
35 template <class _ExecutionPolicy,
36 class _ForwardIterator,
37 class _Generator,
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();
49 });
51 std::move(__first),
52 std::move(__last),
53 std::move(__gen));
56 template <class _ExecutionPolicy,
57 class _ForwardIterator,
58 class _Generator,
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();
68 template <class>
69 void __pstl_generate_n();
71 template <class _ExecutionPolicy,
72 class _ForwardIterator,
73 class _Size,
74 class _Generator,
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();
85 });
87 std::move(__first),
88 __n,
89 std::move(__gen));
92 template <class _ExecutionPolicy,
93 class _ForwardIterator,
94 class _Size,
95 class _Generator,
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