[libcxx-commits] [libcxx] [libc++] Use find_if in the generic find implementation (PR #179938)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Feb 5 05:26:45 PST 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/179938
As a drive-by, this also forwards `ranges::find_if` to the new `__find_if`.
>From dcd59cd77ccc510092f04ca936f3d92c973cf94f Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 5 Feb 2026 14:26:02 +0100
Subject: [PATCH] [libc++] Use find_if in the generic find implementation
---
libcxx/include/__algorithm/find.h | 6 ++----
libcxx/include/__algorithm/find_if.h | 16 ++++++++++++----
libcxx/include/__algorithm/ranges_find_if.h | 15 +++------------
3 files changed, 17 insertions(+), 20 deletions(-)
diff --git a/libcxx/include/__algorithm/find.h b/libcxx/include/__algorithm/find.h
index 852bc2da3eb7b..8fb711cdc1d77 100644
--- a/libcxx/include/__algorithm/find.h
+++ b/libcxx/include/__algorithm/find.h
@@ -10,6 +10,7 @@
#ifndef _LIBCPP___ALGORITHM_FIND_H
#define _LIBCPP___ALGORITHM_FIND_H
+#include <__algorithm/find_if.h>
#include <__algorithm/find_segment_if.h>
#include <__algorithm/min.h>
#include <__algorithm/simd_utils.h>
@@ -48,10 +49,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Iter, class _Sent, class _Tp, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
__find_loop(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
- for (; __first != __last; ++__first)
- if (std::__invoke(__proj, *__first) == __value)
- break;
- return __first;
+ return std::__find_if(__first, __last, [&](auto&& __val) { return __val == __value; }, __proj);
}
template <class _Iter, class _Sent, class _Tp, class _Proj>
diff --git a/libcxx/include/__algorithm/find_if.h b/libcxx/include/__algorithm/find_if.h
index 9f7fa480a5571..7b4dcaceb9ff2 100644
--- a/libcxx/include/__algorithm/find_if.h
+++ b/libcxx/include/__algorithm/find_if.h
@@ -11,7 +11,9 @@
#define _LIBCPP___ALGORITHM_FIND_IF_H
#include <__config>
+#include <__functional/identity.h>
#include <__memory/valid_range.h>
+#include <__type_traits/invoke.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -19,17 +21,23 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-template <class _InputIterator, class _Predicate>
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
-find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
+template <class _Iter, class _Sent, class _Pred, class _Proj>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter
+__find_if(_Iter __first, _Sent __last, _Pred&& __pred, _Proj&& __proj) {
std::__assume_valid_range(__first, __last);
for (; __first != __last; ++__first)
- if (__pred(*__first))
+ if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
break;
return __first;
}
+template <class _InputIterator, class _Predicate>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
+find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
+ return std::__find_if(__first, __last, __pred, __identity());
+}
+
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___ALGORITHM_FIND_IF_H
diff --git a/libcxx/include/__algorithm/ranges_find_if.h b/libcxx/include/__algorithm/ranges_find_if.h
index ed6406e6186a3..c72ade02516b3 100644
--- a/libcxx/include/__algorithm/ranges_find_if.h
+++ b/libcxx/include/__algorithm/ranges_find_if.h
@@ -9,10 +9,10 @@
#ifndef _LIBCPP___ALGORITHM_RANGES_FIND_IF_H
#define _LIBCPP___ALGORITHM_RANGES_FIND_IF_H
+#include <__algorithm/find_if.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/invoke.h>
-#include <__functional/ranges_operations.h>
#include <__iterator/concepts.h>
#include <__iterator/projected.h>
#include <__ranges/access.h>
@@ -33,15 +33,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
namespace ranges {
-template <class _Ip, class _Sp, class _Pred, class _Proj>
-_LIBCPP_HIDE_FROM_ABI constexpr _Ip __find_if_impl(_Ip __first, _Sp __last, _Pred& __pred, _Proj& __proj) {
- for (; __first != __last; ++__first) {
- if (std::invoke(__pred, std::invoke(__proj, *__first)))
- break;
- }
- return __first;
-}
-
struct __find_if {
template <input_iterator _Ip,
sentinel_for<_Ip> _Sp,
@@ -49,13 +40,13 @@ struct __find_if {
indirect_unary_predicate<projected<_Ip, _Proj>> _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip
operator()(_Ip __first, _Sp __last, _Pred __pred, _Proj __proj = {}) const {
- return ranges::__find_if_impl(std::move(__first), std::move(__last), __pred, __proj);
+ return std::__find_if(std::move(__first), std::move(__last), __pred, __proj);
}
template <input_range _Rp, class _Proj = identity, indirect_unary_predicate<projected<iterator_t<_Rp>, _Proj>> _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Rp>
operator()(_Rp&& __r, _Pred __pred, _Proj __proj = {}) const {
- return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred, __proj);
+ return std::__find_if(ranges::begin(__r), ranges::end(__r), __pred, __proj);
}
};
More information about the libcxx-commits
mailing list