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_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>
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>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
31 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
33 _LIBCPP_BEGIN_NAMESPACE_STD
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 __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
,
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();
59 void __pstl_for_each_n(); // declaration needed for the frontend dispatch below
61 template <class _ExecutionPolicy
,
62 class _ForwardIterator
,
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
));
76 std::for_each_n(std::move(__g_first
), __g_size
, std::move(__g_func
));
85 template <class _ExecutionPolicy
,
86 class _ForwardIterator
,
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
));
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