Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __algorithm / pstl_for_each.h
blob819a43d685abed5d99339484046aced0030c15bd
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_FOR_EACH_H
10 #define _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H
12 #include <__algorithm/for_each.h>
13 #include <__algorithm/for_each_n.h>
14 #include <__algorithm/pstl_backend.h>
15 #include <__algorithm/pstl_frontend_dispatch.h>
16 #include <__config>
17 #include <__iterator/concepts.h>
18 #include <__iterator/cpp17_iterator_concepts.h>
19 #include <__type_traits/enable_if.h>
20 #include <__type_traits/is_execution_policy.h>
21 #include <__type_traits/remove_cvref.h>
22 #include <__type_traits/void_t.h>
23 #include <__utility/empty.h>
24 #include <__utility/move.h>
25 #include <optional>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
29 #endif
31 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
33 _LIBCPP_BEGIN_NAMESPACE_STD
35 template <class _ExecutionPolicy,
36 class _ForwardIterator,
37 class _Function,
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 __for_each(_ExecutionPolicy&&, _ForwardIterator&& __first, _ForwardIterator&& __last, _Function&& __func) noexcept {
42 using _Backend = typename __select_backend<_RawPolicy>::type;
43 return std::__pstl_for_each<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__func));
46 template <class _ExecutionPolicy,
47 class _ForwardIterator,
48 class _Function,
49 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
50 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
51 _LIBCPP_HIDE_FROM_ABI void
52 for_each(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) {
53 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
54 if (!std::__for_each(__policy, std::move(__first), std::move(__last), std::move(__func)))
55 std::__throw_bad_alloc();
58 template <class>
59 void __pstl_for_each_n(); // declaration needed for the frontend dispatch below
61 template <class _ExecutionPolicy,
62 class _ForwardIterator,
63 class _Size,
64 class _Function,
65 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
66 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
67 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
68 __for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __size, _Function&& __func) noexcept {
69 return std::__pstl_frontend_dispatch(
70 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_for_each_n, _RawPolicy),
71 [&](_ForwardIterator __g_first, _Size __g_size, _Function __g_func) -> optional<__empty> {
72 if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
73 std::for_each(__policy, std::move(__g_first), __g_first + __g_size, std::move(__g_func));
74 return __empty{};
75 } else {
76 std::for_each_n(std::move(__g_first), __g_size, std::move(__g_func));
77 return __empty{};
80 std::move(__first),
81 std::move(__size),
82 std::move(__func));
85 template <class _ExecutionPolicy,
86 class _ForwardIterator,
87 class _Size,
88 class _Function,
89 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
90 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
91 _LIBCPP_HIDE_FROM_ABI void
92 for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __size, _Function __func) {
93 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
94 auto __res = std::__for_each_n(__policy, std::move(__first), std::move(__size), std::move(__func));
95 if (!__res)
96 std::__throw_bad_alloc();
99 _LIBCPP_END_NAMESPACE_STD
101 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
103 #endif // _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H