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_FILL_H
10 #define _LIBCPP___ALGORITHM_PSTL_FILL_H
12 #include <__algorithm/fill_n.h>
13 #include <__algorithm/pstl_for_each.h>
14 #include <__algorithm/pstl_frontend_dispatch.h>
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>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
29 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
31 _LIBCPP_BEGIN_NAMESPACE_STD
34 void __pstl_fill(); // declaration needed for the frontend dispatch below
36 template <class _ExecutionPolicy
,
37 class _ForwardIterator
,
39 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
40 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
41 _LIBCPP_HIDE_FROM_ABI optional
<__empty
>
42 __fill(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
) noexcept
{
43 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
44 return std::__pstl_frontend_dispatch(
45 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill
, _RawPolicy
),
46 [&](_ForwardIterator __g_first
, _ForwardIterator __g_last
, const _Tp
& __g_value
) {
47 return std::__for_each(__policy
, __g_first
, __g_last
, [&](__iter_reference
<_ForwardIterator
> __element
) {
48 __element
= __g_value
;
56 template <class _ExecutionPolicy
,
57 class _ForwardIterator
,
59 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
60 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
61 _LIBCPP_HIDE_FROM_ABI
void
62 fill(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
) {
63 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
64 if (!std::__fill(__policy
, std::move(__first
), std::move(__last
), __value
))
65 std::__throw_bad_alloc();
69 void __pstl_fill_n(); // declaration needed for the frontend dispatch below
71 template <class _ExecutionPolicy
,
72 class _ForwardIterator
,
75 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
76 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
77 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__empty
>
78 __fill_n(_ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _SizeT
&& __n
, const _Tp
& __value
) noexcept
{
79 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
80 return std::__pstl_frontend_dispatch(
81 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill_n
, _RawPolicy
),
82 [&](_ForwardIterator __g_first
, _SizeT __g_n
, const _Tp
& __g_value
) {
83 if constexpr (__has_random_access_iterator_category_or_concept
<_ForwardIterator
>::value
)
84 std::fill(__policy
, __g_first
, __g_first
+ __g_n
, __g_value
);
86 std::fill_n(__g_first
, __g_n
, __g_value
);
87 return optional
<__empty
>{__empty
{}};
94 template <class _ExecutionPolicy
,
95 class _ForwardIterator
,
98 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
99 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
100 _LIBCPP_HIDE_FROM_ABI
void
101 fill_n(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _SizeT __n
, const _Tp
& __value
) {
102 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
103 if (!std::__fill_n(__policy
, std::move(__first
), std::move(__n
), __value
))
104 std::__throw_bad_alloc();
107 _LIBCPP_END_NAMESPACE_STD
109 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
111 #endif // _LIBCPP___ALGORITHM_PSTL_FILL_H