[llvm] [ADT] Add SetVector::insert_range (PR #131715)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 17 20:49:31 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

This patch adds SetVector::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/131715.diff


2 Files Affected:

- (modified) llvm/include/llvm/ADT/SetVector.h (+5) 
- (modified) llvm/unittests/ADT/SetVectorTest.cpp (+8) 


``````````diff
diff --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h
index 69931e02953d8..395b22685240f 100644
--- a/llvm/include/llvm/ADT/SetVector.h
+++ b/llvm/include/llvm/ADT/SetVector.h
@@ -20,6 +20,7 @@
 #ifndef LLVM_ADT_SETVECTOR_H
 #define LLVM_ADT_SETVECTOR_H
 
+#include "llvm/ADT/ADL.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
@@ -184,6 +185,10 @@ class SetVector {
       insert(*Start);
   }
 
+  template <typename Range> void insert_range(Range &&R) {
+    insert(adl_begin(R), adl_end(R));
+  }
+
   /// Remove an item from the set vector.
   bool remove(const value_type& X) {
     if constexpr (canBeSmall())
diff --git a/llvm/unittests/ADT/SetVectorTest.cpp b/llvm/unittests/ADT/SetVectorTest.cpp
index a0114c0e3509c..ee565a01fd922 100644
--- a/llvm/unittests/ADT/SetVectorTest.cpp
+++ b/llvm/unittests/ADT/SetVectorTest.cpp
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
@@ -86,3 +87,10 @@ TEST(SetVector, ConstPtrKeyTest) {
   EXPECT_FALSE(S.contains(&j));
   EXPECT_FALSE(S.contains((const int *)&j));
 }
+
+TEST(SetVector, InsertRange) {
+  SetVector<unsigned> Set;
+  constexpr unsigned Args[] = {3, 1, 2};
+  Set.insert_range(Args);
+  EXPECT_THAT(Set, ::testing::ElementsAre(3, 1, 2));
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/131715


More information about the llvm-commits mailing list