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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 17 20:48:57 PDT 2025


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

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


>From 1daaa05ee5753483b1ea919f6988ec4cac3c6c8c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 17 Mar 2025 14:03:03 -0700
Subject: [PATCH] [ADT] Add SetVector::insert_range

This patch adds SetVector::insert_range for consistency with
DenseSet::insert_range and std::set::insert_range from C++23.
---
 llvm/include/llvm/ADT/SetVector.h    | 5 +++++
 llvm/unittests/ADT/SetVectorTest.cpp | 8 ++++++++
 2 files changed, 13 insertions(+)

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));
+}



More information about the llvm-commits mailing list