Skip to content

[ADT] Add set_intersects to check if there is any intersection #127907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

teresajohnson
Copy link
Contributor

Add a facility to check if there is any intersection between 2 sets.
This will be used in some follow on changes to MemProf.

Add a facility to check if there is any intersection between 2 sets.
This will be used in some follow on changes to MemProf.
@llvmbot
Copy link
Member

llvmbot commented Feb 19, 2025

@llvm/pr-subscribers-llvm-adt

Author: Teresa Johnson (teresajohnson)

Changes

Add a facility to check if there is any intersection between 2 sets.
This will be used in some follow on changes to MemProf.


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/SetOperations.h (+17)
  • (modified) llvm/unittests/ADT/SetOperationsTest.cpp (+11)
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

Copy link
Contributor

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

bool set_intersects(const S1Ty &S1, const S2Ty &S2) {
if (S1.size() < S2.size())
return set_intersects_impl(S1, S2);
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no else after return https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return (unless with if constexpr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Set2 = {5, 6, 7};
EXPECT_FALSE(set_intersects(Set1, Set2));
EXPECT_FALSE(set_intersects(Set2, Set1));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a testcase with empty sets (the usual source of edge cases)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider putting this in a detail namespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor Author

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review

Set2 = {5, 6, 7};
EXPECT_FALSE(set_intersects(Set1, Set2));
EXPECT_FALSE(set_intersects(Set2, Set1));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

bool set_intersects(const S1Ty &S1, const S2Ty &S2) {
if (S1.size() < S2.size())
return set_intersects_impl(S1, S2);
else
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@teresajohnson teresajohnson merged commit 60c6202 into llvm:main Feb 20, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants