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/cpp17_iterator_concepts.h>
18 #include <__iterator/iterator_traits.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/move.h>
24 #include <__utility/terminate_on_exception.h>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 template <class _ExecutionPolicy,
35           class _ForwardIterator,
36           class _Function,
37           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
38           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
39 _LIBCPP_HIDE_FROM_ABI void
40 for_each(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Function __func) {
41   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
42   using _Backend = typename __select_backend<_RawPolicy>::type;
43   std::__pstl_for_each<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__func));
44 }
45 
46 template <class>
47 void __pstl_for_each_n(); // declaration needed for the frontend dispatch below
48 
49 template <class _ExecutionPolicy,
50           class _ForwardIterator,
51           class _Size,
52           class _Function,
53           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
54           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
55 _LIBCPP_HIDE_FROM_ABI void
56 for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __size, _Function __func) {
57   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
58   return std::__pstl_frontend_dispatch(
59       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_for_each_n),
60       [&](_ForwardIterator __g_first, _Size __g_size, _Function __g_func) {
61         if constexpr (__has_random_access_iterator_category<_ForwardIterator>::value) {
62           std::for_each(__policy, std::move(__g_first), __g_first + __g_size, std::move(__g_func));
63         } else {
64           std::for_each_n(std::move(__g_first), __g_size, std::move(__g_func));
65         }
66       },
67       __first,
68       __size,
69       std::move(__func));
70 }
71 
72 _LIBCPP_END_NAMESPACE_STD
73 
74 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
75 
76 #endif // _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H
77