[llvm] [ADT] Add set_intersects to check if there is any intersection (PR #127907)

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 20 07:50:56 PST 2025


https://github.com/teresajohnson updated https://github.com/llvm/llvm-project/pull/127907

>From 82efc9664f16b1dc3cbf095c267fba0cd68cda42 Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Wed, 19 Feb 2025 12:53:34 -0800
Subject: [PATCH 1/2] [ADT] Add set_intersects to check if there is any
 intersection

Add a facility to check if there is any intersection between 2 sets.
This will be used in some follow on changes to MemProf.
---
 llvm/include/llvm/ADT/SetOperations.h    | 17 +++++++++++++++++
 llvm/unittests/ADT/SetOperationsTest.cpp | 11 +++++++++++
 2 files changed, 28 insertions(+)

diff --git a/llvm/include/llvm/ADT/SetOperations.h b/llvm/include/llvm/ADT/SetOperations.h
index 86a27b683ebc1..4eef3727a97bb 100644
--- a/llvm/include/llvm/ADT/SetOperations.h
+++ b/llvm/include/llvm/ADT/SetOperations.h
@@ -157,6 +157,23 @@ bool set_is_subset(const S1Ty &S1, const S2Ty &S2) {
   return true;
 }
 
+template <class S1Ty, class S2Ty>
+bool set_intersects_impl(const S1Ty &S1, const S2Ty &S2) {
+  for (const auto &E : S1)
+    if (S2.count(E))
+      return true;
+  return false;
+}
+
+/// set_intersects(A, B) - Return true iff A ^ B is non empty
+template <class S1Ty, class S2Ty>
+bool set_intersects(const S1Ty &S1, const S2Ty &S2) {
+  if (S1.size() < S2.size())
+    return set_intersects_impl(S1, S2);
+  else
+    return set_intersects_impl(S2, S1);
+}
+
 } // namespace llvm
 
 #endif
diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index f99f5c9b2af10..84691b08dd4f4 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -273,4 +273,15 @@ TEST(SetOperationsTest, SetIsSubset) {
   EXPECT_FALSE(set_is_subset(Set1, Set2));
 }
 
+TEST(SetOperationsTest, SetIntersects) {
+  std::set<int> Set1 = {1, 2, 3, 4};
+  std::set<int> Set2 = {3, 4, 5};
+  EXPECT_TRUE(set_intersects(Set1, Set2));
+  EXPECT_TRUE(set_intersects(Set2, Set1));
+
+  Set2 = {5, 6, 7};
+  EXPECT_FALSE(set_intersects(Set1, Set2));
+  EXPECT_FALSE(set_intersects(Set2, Set1));
+}
+
 } // namespace

>From dc33f4f5caf6068f9a25677d0e83711497e2af4c Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Thu, 20 Feb 2025 07:49:46 -0800
Subject: [PATCH 2/2] Address comments

---
 llvm/include/llvm/ADT/SetOperations.h    | 9 ++++++---
 llvm/unittests/ADT/SetOperationsTest.cpp | 6 ++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/SetOperations.h b/llvm/include/llvm/ADT/SetOperations.h
index 4eef3727a97bb..4d4ff4045f813 100644
--- a/llvm/include/llvm/ADT/SetOperations.h
+++ b/llvm/include/llvm/ADT/SetOperations.h
@@ -157,6 +157,8 @@ bool set_is_subset(const S1Ty &S1, const S2Ty &S2) {
   return true;
 }
 
+namespace detail {
+
 template <class S1Ty, class S2Ty>
 bool set_intersects_impl(const S1Ty &S1, const S2Ty &S2) {
   for (const auto &E : S1)
@@ -165,13 +167,14 @@ bool set_intersects_impl(const S1Ty &S1, const S2Ty &S2) {
   return false;
 }
 
+} // namespace detail
+
 /// set_intersects(A, B) - Return true iff A ^ B is non empty
 template <class S1Ty, class S2Ty>
 bool set_intersects(const S1Ty &S1, const S2Ty &S2) {
   if (S1.size() < S2.size())
-    return set_intersects_impl(S1, S2);
-  else
-    return set_intersects_impl(S2, S1);
+    return detail::set_intersects_impl(S1, S2);
+  return detail::set_intersects_impl(S2, S1);
 }
 
 } // namespace llvm
diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index 84691b08dd4f4..6d191160f774a 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -282,6 +282,12 @@ TEST(SetOperationsTest, SetIntersects) {
   Set2 = {5, 6, 7};
   EXPECT_FALSE(set_intersects(Set1, Set2));
   EXPECT_FALSE(set_intersects(Set2, Set1));
+
+  // Check that intersecting with a null set returns false.
+  Set1.clear();
+  EXPECT_FALSE(set_intersects(Set1, Set2));
+  EXPECT_FALSE(set_intersects(Set2, Set1));
+  EXPECT_FALSE(set_intersects(Set1, Set1));
 }
 
 } // namespace



More information about the llvm-commits mailing list