[libc++] Implement any_of in terms of find_if#207274
Conversation
This way, any optimizations in find_if will be picked up by any_of. Closes llvm#129310
|
@llvm/pr-subscribers-libcxx Author: Louis Dionne (ldionne) ChangesThis way, any optimizations in find_if will be picked up by any_of. Closes #129310 Full diff: https://github.com/llvm/llvm-project/pull/207274.diff 1 Files Affected:
diff --git a/libcxx/include/__algorithm/any_of.h b/libcxx/include/__algorithm/any_of.h
index 4b6eb94517286..94a0917899d63 100644
--- a/libcxx/include/__algorithm/any_of.h
+++ b/libcxx/include/__algorithm/any_of.h
@@ -10,9 +10,10 @@
#ifndef _LIBCPP___ALGORITHM_ANY_OF_H
#define _LIBCPP___ALGORITHM_ANY_OF_H
+#include <__algorithm/find_if.h>
#include <__config>
#include <__functional/identity.h>
-#include <__type_traits/invoke.h>
+#include <__utility/forward.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -23,18 +24,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Iter, class _Sent, class _Proj, class _Pred>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
__any_of(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
- for (; __first != __last; ++__first) {
- if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
- return true;
- }
- return false;
+ auto __found = std::__find_if(std::move(__first), __last, std::forward<_Pred>(__pred), std::forward<_Proj>(__proj));
+ return __found != __last;
}
template <class _InputIterator, class _Predicate>
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
__identity __proj;
- return std::__any_of(__first, __last, __pred, __proj);
+ return std::__any_of(std::move(__first), std::move(__last), __pred, __proj);
}
_LIBCPP_END_NAMESPACE_STD
|
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for macOS 26.5 arm64:Benchmark results for Linux x86_64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for macOS 26.5 arm64:Benchmark results for Linux x86_64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for macOS 26.5 arm64:Benchmark results for Linux x86_64: |
This way, any optimizations in find_if will be picked up by any_of.
Closes #129310