[libcxx-commits] [libcxx] Optimize the implementation of std::find_first_of to use any_of #129319 (PR #129574)

Ali Raeisdanaei via libcxx-commits libcxx-commits at lists.llvm.org
Sat Apr 19 16:46:10 PDT 2025


https://github.com/aliraeisdanaei updated https://github.com/llvm/llvm-project/pull/129574

>From 95937e669886d7b22c9106e6c503c9fa049ef3e1 Mon Sep 17 00:00:00 2001
From: aliraeisdanaei <mrrookie2 at gmail.com>
Date: Mon, 3 Mar 2025 14:14:06 -0500
Subject: [PATCH 1/4] Change the implementation of std::find_first_of.h to use
 any_of #129319

---
 libcxx/include/__algorithm/find_first_of.h | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libcxx/include/__algorithm/find_first_of.h b/libcxx/include/__algorithm/find_first_of.h
index 45ec133154371..54c0dbd103ddb 100644
--- a/libcxx/include/__algorithm/find_first_of.h
+++ b/libcxx/include/__algorithm/find_first_of.h
@@ -26,11 +26,9 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_fir
     _ForwardIterator2 __first2,
     _ForwardIterator2 __last2,
     _BinaryPredicate&& __pred) {
-  for (; __first1 != __last1; ++__first1)
-    for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
-      if (__pred(*__first1, *__j))
-        return __first1;
-  return __last1;
+  return std::find_if(first1, last1, [&](const auto& x) {
+    return std::any_of(first2, last2, [&](const auto& y) { return x == y; });
+  });
 }
 
 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>

>From 558469e2ac291289dd5199bff31ad9630a77a997 Mon Sep 17 00:00:00 2001
From: aliraeisdanaei <mrrookie2 at gmail.com>
Date: Sat, 19 Apr 2025 19:20:02 -0400
Subject: [PATCH 2/4] use the pedicate binary instead of equality

---
 libcxx/include/__algorithm/find_first_of.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/find_first_of.h b/libcxx/include/__algorithm/find_first_of.h
index 54c0dbd103ddb..ed13a75d2102b 100644
--- a/libcxx/include/__algorithm/find_first_of.h
+++ b/libcxx/include/__algorithm/find_first_of.h
@@ -27,7 +27,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_fir
     _ForwardIterator2 __last2,
     _BinaryPredicate&& __pred) {
   return std::find_if(first1, last1, [&](const auto& x) {
-    return std::any_of(first2, last2, [&](const auto& y) { return x == y; });
+    return std::any_of(first2, last2, [&](const auto& y) { return __pred(x, y); });
   });
 }
 

>From 91305de32d41305e6c5110860cc8254ab3ea8b9a Mon Sep 17 00:00:00 2001
From: aliraeisdanaei <mrrookie2 at gmail.com>
Date: Sat, 19 Apr 2025 19:26:39 -0400
Subject: [PATCH 3/4] correct the naming of the iterators (stupid mistake)

---
 libcxx/include/__algorithm/find_first_of.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__algorithm/find_first_of.h b/libcxx/include/__algorithm/find_first_of.h
index ed13a75d2102b..0d5243002b2c8 100644
--- a/libcxx/include/__algorithm/find_first_of.h
+++ b/libcxx/include/__algorithm/find_first_of.h
@@ -26,8 +26,8 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_fir
     _ForwardIterator2 __first2,
     _ForwardIterator2 __last2,
     _BinaryPredicate&& __pred) {
-  return std::find_if(first1, last1, [&](const auto& x) {
-    return std::any_of(first2, last2, [&](const auto& y) { return __pred(x, y); });
+  return std::find_if(__first1, __last1, [&](const auto& x) {
+    return std::any_of(__first2, __last2, [&](const auto& y) { return __pred(x, y); });
   });
 }
 

>From 68e4633be8f6bf22b72ad9f0eac0c05ddf2dceef Mon Sep 17 00:00:00 2001
From: aliraeisdanaei <mrrookie2 at gmail.com>
Date: Sat, 19 Apr 2025 19:40:41 -0400
Subject: [PATCH 4/4] include find_if and any_of in the headers

---
 libcxx/include/__algorithm/find_first_of.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/include/__algorithm/find_first_of.h b/libcxx/include/__algorithm/find_first_of.h
index 0d5243002b2c8..90c25ac104cc1 100644
--- a/libcxx/include/__algorithm/find_first_of.h
+++ b/libcxx/include/__algorithm/find_first_of.h
@@ -10,7 +10,9 @@
 #ifndef _LIBCPP___ALGORITHM_FIND_FIRST_OF_H
 #define _LIBCPP___ALGORITHM_FIND_FIRST_OF_H
 
+#include <__algorithm/any_of.h>
 #include <__algorithm/comp.h>
+#include <__algorithm/find_if.h>
 #include <__config>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)



More information about the libcxx-commits mailing list