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_FOR_EACH_H
10 #define _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H
11
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>
26
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
29 #endif
30
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33
34 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
35
36 _LIBCPP_BEGIN_NAMESPACE_STD
37
38 template <class _ExecutionPolicy,
39 class _ForwardIterator,
40 class _Function,
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>
__for_each(_ExecutionPolicy &&,_ForwardIterator && __first,_ForwardIterator && __last,_Function && __func)44 __for_each(_ExecutionPolicy&&, _ForwardIterator&& __first, _ForwardIterator&& __last, _Function&& __func) noexcept {
45 using _Backend = typename __select_backend<_RawPolicy>::type;
46 return std::__pstl_for_each<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__func));
47 }
48
49 template <class _ExecutionPolicy,
50 class _ForwardIterator,
51 class _Function,
52 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
53 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
54 _LIBCPP_HIDE_FROM_ABI void
for_each(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_Function __func)55 for_each(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) {
56 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
57 if (!std::__for_each(__policy, std::move(__first), std::move(__last), std::move(__func)))
58 std::__throw_bad_alloc();
59 }
60
61 template <class>
62 void __pstl_for_each_n(); // declaration needed for the frontend dispatch below
63
64 template <class _ExecutionPolicy,
65 class _ForwardIterator,
66 class _Size,
67 class _Function,
68 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
69 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
70 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
__for_each_n(_ExecutionPolicy && __policy,_ForwardIterator && __first,_Size && __size,_Function && __func)71 __for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __size, _Function&& __func) noexcept {
72 return std::__pstl_frontend_dispatch(
73 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_for_each_n, _RawPolicy),
74 [&](_ForwardIterator __g_first, _Size __g_size, _Function __g_func) -> optional<__empty> {
75 if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
76 std::for_each(__policy, std::move(__g_first), __g_first + __g_size, std::move(__g_func));
77 return __empty{};
78 } else {
79 std::for_each_n(std::move(__g_first), __g_size, std::move(__g_func));
80 return __empty{};
81 }
82 },
83 std::move(__first),
84 std::move(__size),
85 std::move(__func));
86 }
87
88 template <class _ExecutionPolicy,
89 class _ForwardIterator,
90 class _Size,
91 class _Function,
92 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
93 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
94 _LIBCPP_HIDE_FROM_ABI void
for_each_n(_ExecutionPolicy && __policy,_ForwardIterator __first,_Size __size,_Function __func)95 for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __size, _Function __func) {
96 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
97 auto __res = std::__for_each_n(__policy, std::move(__first), std::move(__size), std::move(__func));
98 if (!__res)
99 std::__throw_bad_alloc();
100 }
101
102 _LIBCPP_END_NAMESPACE_STD
103
104 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
105
106 _LIBCPP_POP_MACROS
107
108 #endif // _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H
109