Skip to content
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

[ADT] Add SmallPtrSet::insert_range #131716

Conversation

kazutakahirata
Copy link
Contributor

This pach adds SmallPtrSet::insert_range for consistency with
DenseSet::insert_range and std::set::insert_range from C++23.

This pach adds SmallPtrSet::insert_range for consistency with
DenseSet::insert_range and std::set::insert_range from C++23.
@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This pach adds SmallPtrSet::insert_range for consistency with
DenseSet::insert_range and std::set::insert_range from C++23.


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallPtrSet.h (+5)
  • (modified) llvm/unittests/ADT/SmallPtrSetTest.cpp (+8)
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index cf6abc9129b40..af26661699444 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_ADT_SMALLPTRSET_H
 #define LLVM_ADT_SMALLPTRSET_H
 
+#include "llvm/ADT/ADL.h"
 #include "llvm/ADT/EpochTracker.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/ReverseIteration.h"
@@ -469,6 +470,10 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
     insert(IL.begin(), IL.end());
   }
 
+  template <typename Range> void insert_range(Range &&R) {
+    insert(adl_begin(R), adl_end(R));
+  }
+
   iterator begin() const {
     if (shouldReverseIterate())
       return makeIterator(EndPointer() - 1);
diff --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp
index 1d9a0d1725a92..2a634a68d6e66 100644
--- a/llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -411,6 +411,14 @@ TEST(SmallPtrSetTest, RemoveIf) {
   EXPECT_FALSE(Removed);
 }
 
+TEST(SmallPtrSetTest, InsertRange) {
+  int Vals[3] = {0, 1, 2};
+  SmallPtrSet<int *, 4> Set;
+  int *Args[] = {&Vals[2], &Vals[0], &Vals[1]};
+  Set.insert_range(Args);
+  EXPECT_THAT(Set, UnorderedElementsAre(&Vals[0], &Vals[1], &Vals[2]));
+}
+
 TEST(SmallPtrSetTest, Reserve) {
   // Check that we don't do anything silly when using reserve().
   SmallPtrSet<int *, 4> Set;

@kazutakahirata kazutakahirata merged commit f6ad65a into llvm:main Mar 18, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_set_SmallPtrSet_insert_range branch March 18, 2025 07:21
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.

3 participants