[llvm] [ADT] Add SmallSet::insert_range (PR #131717)

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


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

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


>From 97e43809aa4107c1220624461938952ae935a134 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 17 Mar 2025 20:03:22 -0700
Subject: [PATCH] [ADT] Add SmallSet::insert_range

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

diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h
index ed3c6bfd3418d..163690edda1bf 100644
--- a/llvm/include/llvm/ADT/SmallSet.h
+++ b/llvm/include/llvm/ADT/SmallSet.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_ADT_SMALLSET_H
 #define LLVM_ADT_SMALLSET_H
 
+#include "llvm/ADT/ADL.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/iterator.h"
@@ -190,6 +191,10 @@ class SmallSet {
       insert(*I);
   }
 
+  template <typename Range> void insert_range(Range &&R) {
+    insert(adl_begin(R), adl_end(R));
+  }
+
   bool erase(const T &V) {
     if (!isSmall())
       return Set.erase(V);
diff --git a/llvm/unittests/ADT/SmallSetTest.cpp b/llvm/unittests/ADT/SmallSetTest.cpp
index 2feb0b1feb421..7d2431d4832a9 100644
--- a/llvm/unittests/ADT/SmallSetTest.cpp
+++ b/llvm/unittests/ADT/SmallSetTest.cpp
@@ -127,6 +127,13 @@ TEST(SmallSetTest, InsertPerfectFwd) {
   }
 }
 
+TEST(SmallSetTest, InsertRange) {
+  SmallSet<int, 4> s1;
+  constexpr unsigned Args[] = {3, 1, 2};
+  s1.insert_range(Args);
+  EXPECT_THAT(s1, ::testing::UnorderedElementsAre(1, 2, 3));
+}
+
 TEST(SmallSetTest, Grow) {
   SmallSet<int, 4> s1;
 



More information about the llvm-commits mailing list