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_FILL_H
10 #define _LIBCPP___ALGORITHM_PSTL_FILL_H
11 
12 #include <__algorithm/fill_n.h>
13 #include <__algorithm/pstl_for_each.h>
14 #include <__algorithm/pstl_frontend_dispatch.h>
15 #include <__config>
16 #include <__iterator/concepts.h>
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 <__utility/move.h>
23 #include <optional>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_PUSH_MACROS
30 #include <__undef_macros>
31 
32 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 template <class>
37 void __pstl_fill(); // declaration needed for the frontend dispatch below
38 
39 template <class _ExecutionPolicy,
40           class _ForwardIterator,
41           class _Tp,
42           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
43           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
44 _LIBCPP_HIDE_FROM_ABI optional<__empty>
45 __fill(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept {
46   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
47   return std::__pstl_frontend_dispatch(
48       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill, _RawPolicy),
49       [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) {
50         return std::__for_each(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __element) {
51           __element = __g_value;
52         });
53       },
54       std::move(__first),
55       std::move(__last),
56       __value);
57 }
58 
59 template <class _ExecutionPolicy,
60           class _ForwardIterator,
61           class _Tp,
62           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
63           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
64 _LIBCPP_HIDE_FROM_ABI void
65 fill(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
66   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
67   if (!std::__fill(__policy, std::move(__first), std::move(__last), __value))
68     std::__throw_bad_alloc();
69 }
70 
71 template <class>
72 void __pstl_fill_n(); // declaration needed for the frontend dispatch below
73 
74 template <class _ExecutionPolicy,
75           class _ForwardIterator,
76           class _SizeT,
77           class _Tp,
78           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
79           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
80 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
81 __fill_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _SizeT&& __n, const _Tp& __value) noexcept {
82   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
83   return std::__pstl_frontend_dispatch(
84       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill_n, _RawPolicy),
85       [&](_ForwardIterator __g_first, _SizeT __g_n, const _Tp& __g_value) {
86         if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value)
87           std::fill(__policy, __g_first, __g_first + __g_n, __g_value);
88         else
89           std::fill_n(__g_first, __g_n, __g_value);
90         return optional<__empty>{__empty{}};
91       },
92       std::move(__first),
93       std::move(__n),
94       __value);
95 }
96 
97 template <class _ExecutionPolicy,
98           class _ForwardIterator,
99           class _SizeT,
100           class _Tp,
101           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
102           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
103 _LIBCPP_HIDE_FROM_ABI void
104 fill_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _SizeT __n, const _Tp& __value) {
105   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
106   if (!std::__fill_n(__policy, std::move(__first), std::move(__n), __value))
107     std::__throw_bad_alloc();
108 }
109 
110 _LIBCPP_END_NAMESPACE_STD
111 
112 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
113 
114 _LIBCPP_POP_MACROS
115 
116 #endif // _LIBCPP___ALGORITHM_PSTL_FILL_H
117