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 SmallSet::insert_range #131717

Merged

Conversation

kazutakahirata
Copy link
Contributor

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

This patch adds SmallSet::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 patch adds SmallSet::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/131717.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallSet.h (+5)
  • (modified) llvm/unittests/ADT/SmallSetTest.cpp (+7)
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;
 

@kazutakahirata kazutakahirata merged commit 2df0254 into llvm:main Mar 18, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_set_SmallSet_insert_range branch March 18, 2025 07:20
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