Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __algorithm / pstl_find.h
blobadc05ea1a9e55a06cdaa8a91a6eeaaeabe0e1668
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 //===----------------------------------------------------------------------===//
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>
16 #include <__config>
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>
22 #include <optional>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
26 #endif
28 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30 _LIBCPP_BEGIN_NAMESPACE_STD
32 template <class _ExecutionPolicy,
33 class _ForwardIterator,
34 class _Predicate,
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,
45 class _Predicate,
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));
52 if (!__res)
53 std::__throw_bad_alloc();
54 return *std::move(__res);
57 template <class>
58 void __pstl_find_if_not();
60 template <class _ExecutionPolicy,
61 class _ForwardIterator,
62 class _Predicate,
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);
74 });
76 std::move(__first),
77 std::move(__last),
78 std::move(__pred));
81 template <class _ExecutionPolicy,
82 class _ForwardIterator,
83 class _Predicate,
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));
90 if (!__res)
91 std::__throw_bad_alloc();
92 return *std::move(__res);
95 template <class>
96 void __pstl_find();
98 template <class _ExecutionPolicy,
99 class _ForwardIterator,
100 class _Tp,
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> {
108 return std::find_if(
109 __policy, __g_first, __g_last, [&](__iter_reference<__remove_cvref_t<_ForwardIterator>> __element) {
110 return __element == __g_value;
113 std::move(__first),
114 std::move(__last),
115 __value);
118 template <class _ExecutionPolicy,
119 class _ForwardIterator,
120 class _Tp,
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);
127 if (!__res)
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