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_FIND_H
10 #define _LIBCPP___ALGORITHM_PSTL_FIND_H
12 #include <__algorithm/comp.h>
13 #include <__algorithm/find.h>
14 #include <__algorithm/pstl_backend.h>
15 #include <__algorithm/pstl_frontend_dispatch.h>
17 #include <__iterator/cpp17_iterator_concepts.h>
18 #include <__type_traits/enable_if.h>
19 #include <__type_traits/is_execution_policy.h>
20 #include <__type_traits/remove_cvref.h>
21 #include <__utility/move.h>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
28 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30 _LIBCPP_BEGIN_NAMESPACE_STD
32 template <class _ExecutionPolicy
,
33 class _ForwardIterator
,
35 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
36 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
37 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__remove_cvref_t
<_ForwardIterator
>>
38 __find_if(_ExecutionPolicy
&&, _ForwardIterator
&& __first
, _ForwardIterator
&& __last
, _Predicate
&& __pred
) noexcept
{
39 using _Backend
= typename __select_backend
<_RawPolicy
>::type
;
40 return std::__pstl_find_if
<_RawPolicy
>(_Backend
{}, std::move(__first
), std::move(__last
), std::move(__pred
));
43 template <class _ExecutionPolicy
,
44 class _ForwardIterator
,
46 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
47 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
48 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
49 find_if(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, _Predicate __pred
) {
50 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
51 auto __res
= std::__find_if(__policy
, std::move(__first
), std::move(__last
), std::move(__pred
));
53 std::__throw_bad_alloc();
54 return *std::move(__res
);
58 void __pstl_find_if_not();
60 template <class _ExecutionPolicy
,
61 class _ForwardIterator
,
63 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
64 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
65 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__remove_cvref_t
<_ForwardIterator
>>
66 __find_if_not(_ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _ForwardIterator
&& __last
, _Predicate
&& __pred
) {
67 return std::__pstl_frontend_dispatch(
68 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find_if_not
, _RawPolicy
),
69 [&](_ForwardIterator
&& __g_first
, _ForwardIterator
&& __g_last
, _Predicate
&& __g_pred
)
70 -> optional
<__remove_cvref_t
<_ForwardIterator
>> {
71 return std::__find_if(
72 __policy
, __g_first
, __g_last
, [&](__iter_reference
<__remove_cvref_t
<_ForwardIterator
>> __value
) {
73 return !__g_pred(__value
);
81 template <class _ExecutionPolicy
,
82 class _ForwardIterator
,
84 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
85 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
86 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
87 find_if_not(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, _Predicate __pred
) {
88 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
89 auto __res
= std::__find_if_not(__policy
, std::move(__first
), std::move(__last
), std::move(__pred
));
91 std::__throw_bad_alloc();
92 return *std::move(__res
);
98 template <class _ExecutionPolicy
,
99 class _ForwardIterator
,
101 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
102 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
103 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__remove_cvref_t
<_ForwardIterator
>>
104 __find(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
) noexcept
{
105 return std::__pstl_frontend_dispatch(
106 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find
, _RawPolicy
),
107 [&](_ForwardIterator __g_first
, _ForwardIterator __g_last
, const _Tp
& __g_value
) -> optional
<_ForwardIterator
> {
109 __policy
, __g_first
, __g_last
, [&](__iter_reference
<__remove_cvref_t
<_ForwardIterator
>> __element
) {
110 return __element
== __g_value
;
118 template <class _ExecutionPolicy
,
119 class _ForwardIterator
,
121 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
122 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
123 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
124 find(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
) {
125 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator
);
126 auto __res
= std::__find(__policy
, std::move(__first
), std::move(__last
), __value
);
128 std::__throw_bad_alloc();
129 return *std::move(__res
);
132 _LIBCPP_END_NAMESPACE_STD
134 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
136 #endif // _LIBCPP___ALGORITHM_PSTL_FIND_H