[libcxx-commits] [libcxx] [libc++] Forward find* algorithms to find_if (PR #179938)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 19 03:53:09 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

This propagates any optimizations to the whole family of `find` functions.

---
Full diff: https://github.com/llvm/llvm-project/pull/179938.diff


6 Files Affected:

- (modified) libcxx/include/__algorithm/find.h (+6-4) 
- (modified) libcxx/include/__algorithm/find_if.h (+12-4) 
- (modified) libcxx/include/__algorithm/find_if_not.h (+20-4) 
- (modified) libcxx/include/__algorithm/ranges_find_if.h (+3-12) 
- (modified) libcxx/include/__algorithm/ranges_find_if_not.h (+3-8) 
- (modified) libcxx/include/__algorithm/ranges_remove_if.h (+2-3) 


``````````diff
diff --git a/libcxx/include/__algorithm/find.h b/libcxx/include/__algorithm/find.h
index 852bc2da3eb7b..9cc3c3acb23aa 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,11 @@ _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(
+      std::move(__first),
+      std::move(__last),
+      [&]<class _ValT>(_ValT&& __val) -> bool { 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..2a96e13ce3546 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_CXX17 _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/find_if_not.h b/libcxx/include/__algorithm/find_if_not.h
index b4441b297c4bf..d37da4c645468 100644
--- a/libcxx/include/__algorithm/find_if_not.h
+++ b/libcxx/include/__algorithm/find_if_not.h
@@ -10,23 +10,39 @@
 #ifndef _LIBCPP___ALGORITHM_FIND_IF_NOT_H
 #define _LIBCPP___ALGORITHM_FIND_IF_NOT_H
 
+#include <__algorithm/find_if.h>
 #include <__config>
+#include <__functional/identity.h>
+#include <__type_traits/invoke.h>
+#include <__utility/move.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+template <class _Iter, class _Sent, class _Pred, class _Proj>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter
+__find_if_not(_Iter __first, _Sent __last, _Pred&& __pred, _Proj&& __proj) {
+  return std::__find_if(
+      std::move(__first),
+      std::move(__last),
+      [&]<class _ValT>(_ValT&& __val) -> bool { return !std::__invoke(__pred, __val); },
+      __proj);
+}
+
 template <class _InputIterator, class _Predicate>
 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
-  for (; __first != __last; ++__first)
-    if (!__pred(*__first))
-      break;
-  return __first;
+  return std::__find_if_not(__first, __last, __pred, __identity());
 }
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_FIND_IF_NOT_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);
   }
 };
 
diff --git a/libcxx/include/__algorithm/ranges_find_if_not.h b/libcxx/include/__algorithm/ranges_find_if_not.h
index 9a359b2afdab1..ae0d64fdc77df 100644
--- a/libcxx/include/__algorithm/ranges_find_if_not.h
+++ b/libcxx/include/__algorithm/ranges_find_if_not.h
@@ -9,17 +9,14 @@
 #ifndef _LIBCPP___ALGORITHM_RANGES_FIND_IF_NOT_H
 #define _LIBCPP___ALGORITHM_RANGES_FIND_IF_NOT_H
 
-#include <__algorithm/ranges_find_if.h>
+#include <__algorithm/find_if_not.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>
 #include <__ranges/concepts.h>
 #include <__ranges/dangling.h>
-#include <__utility/forward.h>
 #include <__utility/move.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -41,15 +38,13 @@ struct __find_if_not {
             indirect_unary_predicate<projected<_Ip, _Proj>> _Pred>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip
   operator()(_Ip __first, _Sp __last, _Pred __pred, _Proj __proj = {}) const {
-    auto __pred2 = [&](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
-    return ranges::__find_if_impl(std::move(__first), std::move(__last), __pred2, __proj);
+    return std::__find_if_not(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 {
-    auto __pred2 = [&](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
-    return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred2, __proj);
+    return std::__find_if_not(ranges::begin(__r), ranges::end(__r), __pred, __proj);
   }
 };
 
diff --git a/libcxx/include/__algorithm/ranges_remove_if.h b/libcxx/include/__algorithm/ranges_remove_if.h
index 384b3d41d0801..397959b888b0d 100644
--- a/libcxx/include/__algorithm/ranges_remove_if.h
+++ b/libcxx/include/__algorithm/ranges_remove_if.h
@@ -10,10 +10,9 @@
 #define _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H
 #include <__config>
 
-#include <__algorithm/ranges_find_if.h>
+#include <__algorithm/find_if.h>
 #include <__functional/identity.h>
 #include <__functional/invoke.h>
-#include <__functional/ranges_operations.h>
 #include <__iterator/concepts.h>
 #include <__iterator/iter_move.h>
 #include <__iterator/permutable.h>
@@ -39,7 +38,7 @@ namespace ranges {
 template <class _Iter, class _Sent, class _Proj, class _Pred>
 _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
 __remove_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
-  auto __new_end = ranges::__find_if_impl(__first, __last, __pred, __proj);
+  auto __new_end = std::__find_if(__first, __last, __pred, __proj);
   if (__new_end == __last)
     return {__new_end, __new_end};
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/179938


More information about the libcxx-commits mailing list