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_ANY_ALL_NONE_OF_H
10 #define _LIBCPP___ALGORITHM_PSTL_ANY_ALL_NONE_OF_H
11
12 #include <__algorithm/pstl_find.h>
13 #include <__algorithm/pstl_frontend_dispatch.h>
14 #include <__config>
15 #include <__iterator/cpp17_iterator_concepts.h>
16 #include <__type_traits/enable_if.h>
17 #include <__type_traits/is_execution_policy.h>
18 #include <__type_traits/remove_cvref.h>
19 #include <__utility/move.h>
20 #include <optional>
21
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
24 #endif
25
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28
29 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
30
31 _LIBCPP_BEGIN_NAMESPACE_STD
32
33 template <class>
34 void __pstl_any_of(); // declaration needed for the frontend dispatch below
35
36 template <class _ExecutionPolicy,
37 class _ForwardIterator,
38 class _Predicate,
39 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
40 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
__any_of(_ExecutionPolicy && __policy,_ForwardIterator && __first,_ForwardIterator && __last,_Predicate && __pred)41 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool> __any_of(
42 _ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) noexcept {
43 return std::__pstl_frontend_dispatch(
44 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_any_of, _RawPolicy),
45 [&](_ForwardIterator __g_first, _ForwardIterator __g_last, _Predicate __g_pred) -> optional<bool> {
46 auto __res = std::__find_if(__policy, __g_first, __g_last, __g_pred);
47 if (!__res)
48 return nullopt;
49 return *__res != __g_last;
50 },
51 std::move(__first),
52 std::move(__last),
53 std::move(__pred));
54 }
55
56 template <class _ExecutionPolicy,
57 class _ForwardIterator,
58 class _Predicate,
59 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
60 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
61 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI bool
any_of(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_Predicate __pred)62 any_of(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
63 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
64 auto __res = std::__any_of(__policy, std::move(__first), std::move(__last), std::move(__pred));
65 if (!__res)
66 std::__throw_bad_alloc();
67 return *std::move(__res);
68 }
69
70 template <class>
71 void __pstl_all_of(); // declaration needed for the frontend dispatch below
72
73 template <class _ExecutionPolicy,
74 class _ForwardIterator,
75 class _Pred,
76 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
77 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
78 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
__all_of(_ExecutionPolicy && __policy,_ForwardIterator && __first,_ForwardIterator && __last,_Pred && __pred)79 __all_of(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Pred&& __pred) noexcept {
80 return std::__pstl_frontend_dispatch(
81 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_all_of, _RawPolicy),
82 [&](_ForwardIterator __g_first, _ForwardIterator __g_last, _Pred __g_pred) -> optional<bool> {
83 auto __res = std::__any_of(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __value) {
84 return !__g_pred(__value);
85 });
86 if (!__res)
87 return nullopt;
88 return !*__res;
89 },
90 std::move(__first),
91 std::move(__last),
92 std::move(__pred));
93 }
94
95 template <class _ExecutionPolicy,
96 class _ForwardIterator,
97 class _Pred,
98 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
99 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
100 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI bool
all_of(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_Pred __pred)101 all_of(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred) {
102 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
103 auto __res = std::__all_of(__policy, std::move(__first), std::move(__last), std::move(__pred));
104 if (!__res)
105 std::__throw_bad_alloc();
106 return *std::move(__res);
107 }
108
109 template <class>
110 void __pstl_none_of(); // declaration needed for the frontend dispatch below
111
112 template <class _ExecutionPolicy,
113 class _ForwardIterator,
114 class _Pred,
115 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
116 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
117 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
__none_of(_ExecutionPolicy && __policy,_ForwardIterator && __first,_ForwardIterator && __last,_Pred && __pred)118 __none_of(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Pred&& __pred) noexcept {
119 return std::__pstl_frontend_dispatch(
120 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_none_of, _RawPolicy),
121 [&](_ForwardIterator __g_first, _ForwardIterator __g_last, _Pred __g_pred) -> optional<bool> {
122 auto __res = std::__any_of(__policy, __g_first, __g_last, __g_pred);
123 if (!__res)
124 return nullopt;
125 return !*__res;
126 },
127 std::move(__first),
128 std::move(__last),
129 std::move(__pred));
130 }
131
132 template <class _ExecutionPolicy,
133 class _ForwardIterator,
134 class _Pred,
135 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
136 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
137 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI bool
none_of(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_Pred __pred)138 none_of(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred) {
139 _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
140 auto __res = std::__none_of(__policy, std::move(__first), std::move(__last), std::move(__pred));
141 if (!__res)
142 std::__throw_bad_alloc();
143 return *std::move(__res);
144 }
145
146 _LIBCPP_END_NAMESPACE_STD
147
148 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
149
150 _LIBCPP_POP_MACROS
151
152 #endif // _LIBCPP___ALGORITHM_PSTL_ANY_ALL_NONE_OF_H
153