Skip to content

[ADT] Use range-based for loops (NFC) #99605

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 1 commit into from
Jul 19, 2024

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@kazutakahirata kazutakahirata requested review from nikic and kuhar July 19, 2024 04:53
@llvmbot
Copy link
Member

llvmbot commented Jul 19, 2024

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/SetOperations.h (+14-19)
diff --git a/llvm/include/llvm/ADT/SetOperations.h b/llvm/include/llvm/ADT/SetOperations.h
index 2b1a103565f7d..40e4b05fd7425 100644
--- a/llvm/include/llvm/ADT/SetOperations.h
+++ b/llvm/include/llvm/ADT/SetOperations.h
@@ -43,9 +43,8 @@ static constexpr bool HasMemberEraseIter =
 template <class S1Ty, class S2Ty> bool set_union(S1Ty &S1, const S2Ty &S2) {
   bool Changed = false;
 
-  for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); SI != SE;
-       ++SI)
-    if (S1.insert(*SI).second)
+  for (const auto &E : S2)
+    if (S1.insert(E).second)
       Changed = true;
 
   return Changed;
@@ -73,10 +72,9 @@ template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
 template <class S1Ty, class S2Ty>
 S1Ty set_intersection_impl(const S1Ty &S1, const S2Ty &S2) {
   S1Ty Result;
-  for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end(); SI != SE;
-       ++SI)
-    if (S2.count(*SI))
-      Result.insert(*SI);
+  for (const auto &E : S1)
+    if (S2.count(E))
+      Result.insert(E);
   return Result;
 }
 
@@ -94,10 +92,9 @@ S1Ty set_intersection(const S1Ty &S1, const S2Ty &S2) {
 template <class S1Ty, class S2Ty>
 S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
   S1Ty Result;
-  for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end(); SI != SE;
-       ++SI)
-    if (!S2.count(*SI)) // if the element is not in set2
-      Result.insert(*SI);
+  for (const auto &E : S1)
+    if (!S2.count(E)) // if the element is not in set2
+      Result.insert(E);
   return Result;
 }
 
@@ -132,9 +129,8 @@ template <class S1Ty, class S2Ty> void set_subtract(S1Ty &S1, const S2Ty &S2) {
     }
   }
 
-  for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); SI != SE;
-       ++SI)
-    S1.erase(*SI);
+  for (const auto &E : S2)
+    S1.erase(E);
 }
 
 /// set_subtract(A, B, C, D) - Compute A := A - B, set C to the elements of B
@@ -142,12 +138,11 @@ template <class S1Ty, class S2Ty> void set_subtract(S1Ty &S1, const S2Ty &S2) {
 /// from A (B - A).
 template <class S1Ty, class S2Ty>
 void set_subtract(S1Ty &S1, const S2Ty &S2, S1Ty &Removed, S1Ty &Remaining) {
-  for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); SI != SE;
-       ++SI)
-    if (S1.erase(*SI))
-      Removed.insert(*SI);
+  for (const auto &E : S2)
+    if (S1.erase(E))
+      Removed.insert(E);
     else
-      Remaining.insert(*SI);
+      Remaining.insert(E);
 }
 
 /// set_is_subset(A, B) - Return true iff A in B

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@kazutakahirata kazutakahirata merged commit 8d0cbef into llvm:main Jul 19, 2024
7 of 9 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_SetOperations_loop branch July 19, 2024 07:13
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary: 

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251478
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