[llvm] [ADT] Add SmallPtrSet::insert_range (PR #131716)

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


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

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


>From b26b8594e6b78a511c81809aedf072668725e5e9 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 17 Mar 2025 10:49:16 -0700
Subject: [PATCH] [ADT] Add SmallPtrSet::insert_range

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

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;



More information about the llvm-commits mailing list