Skip to content

Commit b5dfb37

Browse files
committed
[libc++] Forward std::all_of and std::none_of to std::all_of
1 parent b07bfdb commit b5dfb37

File tree

3 files changed

+146
-9
lines changed

3 files changed

+146
-9
lines changed

libcxx/include/__algorithm/all_of.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#ifndef _LIBCPP___ALGORITHM_ALL_OF_H
1111
#define _LIBCPP___ALGORITHM_ALL_OF_H
1212

13+
#include <__algorithm/any_of.h>
1314
#include <__config>
1415
#include <__functional/identity.h>
1516
#include <__type_traits/invoke.h>
17+
#include <__utility/move.h>
1618

1719
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1820
# pragma GCC system_header
@@ -23,11 +25,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2325
template <class _Iter, class _Sent, class _Proj, class _Pred>
2426
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
2527
__all_of(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
26-
for (; __first != __last; ++__first) {
27-
if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
28-
return false;
29-
}
30-
return true;
28+
using _Ref = decltype(std::__invoke(__proj, *__first));
29+
auto __negated_pred = [&__pred](_Ref __arg) { return !std::__invoke(__pred, std::forward<_Ref>(__arg)); };
30+
return !std::__any_of(std::move(__first), std::move(__last), __negated_pred, __proj);
3131
}
3232

3333
template <class _InputIterator, class _Predicate>

libcxx/include/__algorithm/none_of.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef _LIBCPP___ALGORITHM_NONE_OF_H
1111
#define _LIBCPP___ALGORITHM_NONE_OF_H
1212

13+
#include <__algorithm/any_of.h>
1314
#include <__config>
1415

1516
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -21,10 +22,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2122
template <class _InputIterator, class _Predicate>
2223
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
2324
none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
24-
for (; __first != __last; ++__first)
25-
if (__pred(*__first))
26-
return false;
27-
return true;
25+
return !std::any_of(__first, __last, __pred);
2826
}
2927

3028
_LIBCPP_END_NAMESPACE_STD
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// <algorithm>
12+
//
13+
// Range algorithms that take predicates should support predicates that return a non-boolean value as long as the
14+
// returned type is implicitly convertible to bool.
15+
16+
#include <algorithm>
17+
18+
#include <initializer_list>
19+
#include <ranges>
20+
21+
#include "boolean_testable.h"
22+
#include "test_macros.h"
23+
24+
using Value = StrictComparable<int>;
25+
using Iterator = StrictBooleanIterator<Value*>;
26+
using Range = std::ranges::subrange<Iterator>;
27+
auto pred1 = StrictUnaryPredicate;
28+
auto pred2 = StrictBinaryPredicate;
29+
30+
void f(Iterator it, Range in, Iterator out, std::size_t n, Value const& val, std::initializer_list<Value> ilist) {
31+
(void)std::any_of(it, it, pred1);
32+
(void)std::all_of(it, it, pred1);
33+
(void)std::none_of(it, it, pred1);
34+
(void)std::find_if(it, it, pred1);
35+
(void)std::find_if_not(it, it, pred1);
36+
(void)std::find_first_of(it, it, it, it);
37+
(void)std::find_first_of(it, it, it, it, pred2);
38+
(void)std::adjacent_find(it, it);
39+
(void)std::adjacent_find(it, it, pred2);
40+
(void)std::mismatch(it, it, it, it);
41+
(void)std::mismatch(it, it, it, it, pred2);
42+
(void)std::mismatch(it, it, it);
43+
(void)std::mismatch(it, it, it);
44+
(void)std::mismatch(it, it, it, pred2);
45+
(void)std::equal(it, it, it, it);
46+
(void)std::equal(it, it, it, it, pred2);
47+
(void)std::equal(it, it, it);
48+
(void)std::equal(it, it, it, pred2);
49+
(void)std::lexicographical_compare(it, it, it, it);
50+
(void)std::lexicographical_compare(it, it, it, it, pred2);
51+
(void)std::partition_point(it, it, pred1);
52+
(void)std::lower_bound(it, it, val);
53+
(void)std::lower_bound(it, it, val, pred2);
54+
(void)std::upper_bound(it, it, val);
55+
(void)std::upper_bound(it, it, val, pred2);
56+
(void)std::equal_range(it, it, val);
57+
(void)std::equal_range(it, it, val, pred2);
58+
(void)std::binary_search(it, it, val);
59+
(void)std::binary_search(it, it, val, pred2);
60+
(void)std::min(val, val);
61+
(void)std::min(val, val, pred2);
62+
(void)std::min(ilist);
63+
(void)std::min(ilist, pred2);
64+
(void)std::max(val, val);
65+
(void)std::max(val, val, pred2);
66+
(void)std::max(ilist);
67+
(void)std::max(ilist, pred2);
68+
(void)std::minmax(val, val);
69+
(void)std::minmax(val, val, pred2);
70+
(void)std::minmax(ilist);
71+
(void)std::minmax(ilist, pred2);
72+
(void)std::min_element(it, it);
73+
(void)std::min_element(it, it, pred2);
74+
(void)std::max_element(it, it);
75+
(void)std::max_element(it, it, pred2);
76+
(void)std::minmax_element(it, it);
77+
(void)std::minmax_element(it, it, pred2);
78+
(void)std::count_if(it, it, pred1);
79+
(void)std::search(it, it, it ,it);
80+
(void)std::search(it, it, it ,it, pred2);
81+
(void)std::search_n(it, it, n, val);
82+
(void)std::search_n(it, it, n, val, pred2);
83+
(void)std::is_partitioned(it ,it, pred1);
84+
(void)std::is_sorted(it ,it);
85+
(void)std::is_sorted(it ,it, pred2);
86+
(void)std::is_sorted_until(it ,it);
87+
(void)std::is_sorted_until(it ,it, pred2);
88+
(void)std::is_heap(it, it);
89+
(void)std::is_heap(it, it, pred2);
90+
(void)std::is_heap_until(it, it);
91+
(void)std::is_heap_until(it, it, pred2);
92+
(void)std::clamp(val, val, val);
93+
(void)std::clamp(val, val, val, pred2);
94+
(void)std::is_permutation(it, it, it, it);
95+
(void)std::is_permutation(it, it, it, it, pred2);
96+
(void)std::copy_if(it, it, out, pred1);
97+
(void)std::remove_copy_if(it, it, out, pred1);
98+
(void)std::remove_copy(it, it, out, val);
99+
(void)std::replace(it, it, val, val);
100+
(void)std::replace_if(it, it, pred1, val);
101+
(void)std::replace_copy_if(it, it, out, pred1, val);
102+
(void)std::replace_copy(it, it, out, val, val);
103+
(void)std::unique_copy(it, it, out, pred2);
104+
(void)std::partition_copy(it, it, out, out, pred1);
105+
(void)std::partial_sort_copy(it, it, it, it, pred2);
106+
(void)std::merge(it, it, it, it, out);
107+
(void)std::merge(it, it, it, it, out, pred2);
108+
(void)std::set_difference(it, it, it, it, out, pred2);
109+
(void)std::set_intersection(it, it, it, it, out, pred2);
110+
(void)std::set_symmetric_difference(it, it, it, it, out, pred2);
111+
(void)std::set_union(it, it, it, it, out, pred2);
112+
(void)std::remove_if(it, it, pred1);
113+
(void)std::remove(it, it, val);
114+
(void)std::unique(it, it, pred2);
115+
(void)std::partition(it, it, pred1);
116+
(void)std::stable_partition(it, it, pred1);
117+
(void)std::sort(it, it);
118+
(void)std::sort(it, it, pred2);
119+
(void)std::stable_sort(it, it);
120+
(void)std::stable_sort(it, it, pred2);
121+
(void)std::partial_sort(it, it, it);
122+
(void)std::partial_sort(it, it, it, pred2);
123+
(void)std::nth_element(it, it, it);
124+
(void)std::nth_element(it, it, it, pred2);
125+
(void)std::inplace_merge(it, it, it);
126+
(void)std::inplace_merge(it, it, it, pred2);
127+
(void)std::make_heap(it, it);
128+
(void)std::make_heap(it, it, pred2);
129+
(void)std::push_heap(it, it);
130+
(void)std::push_heap(it, it, pred2);
131+
(void)std::pop_heap(it, it);
132+
(void)std::pop_heap(it, it, pred2);
133+
(void)std::sort_heap(it, it);
134+
(void)std::sort_heap(it, it, pred2);
135+
(void)std::prev_permutation(it, it);
136+
(void)std::prev_permutation(it, it, pred2);
137+
(void)std::next_permutation(it, it);
138+
(void)std::next_permutation(it, it, pred2);
139+
}

0 commit comments

Comments
 (0)