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_FIND_H
10 #define _LIBCPP___ALGORITHM_PSTL_FIND_H
11 
12 #include <__algorithm/comp.h>
13 #include <__algorithm/find.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 <__utility/move.h>
23 #include <__utility/terminate_on_exception.h>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 template <class _ExecutionPolicy,
34           class _ForwardIterator,
35           class _Predicate,
36           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
37           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
38 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
39 find_if(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
40   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
41   using _Backend = typename __select_backend<_RawPolicy>::type;
42   return std::__pstl_find_if<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__pred));
43 }
44 
45 template <class>
46 void __pstl_find_if_not();
47 
48 template <class _ExecutionPolicy,
49           class _ForwardIterator,
50           class _Predicate,
51           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
52           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
53 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
54 find_if_not(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
55   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
56   return std::__pstl_frontend_dispatch(
57       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find_if_not),
58       [&](_ForwardIterator __g_first, _ForwardIterator __g_last, _Predicate __g_pred) {
59         return std::find_if(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __value) {
60           return !__g_pred(__value);
61         });
62       },
63       std::move(__first),
64       std::move(__last),
65       std::move(__pred));
66 }
67 
68 template <class>
69 void __pstl_find();
70 
71 template <class _ExecutionPolicy,
72           class _ForwardIterator,
73           class _Tp,
74           class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
75           enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
76 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
77 find(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
78   _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
79   return std::__pstl_frontend_dispatch(
80       _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find),
81       [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) {
82         return std::find_if(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __element) {
83           return __element == __g_value;
84         });
85       },
86       std::move(__first),
87       std::move(__last),
88       __value);
89 }
90 
91 _LIBCPP_END_NAMESPACE_STD
92 
93 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
94 
95 #endif // _LIBCPP___ALGORITHM_PSTL_FIND_H
96