[llvm] 9654df3 - [ADT] Add StringSet::insert_range (#131957)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 19 07:13:05 PDT 2025


Author: Kazu Hirata
Date: 2025-03-19T07:13:01-07:00
New Revision: 9654df3f5e8f898341717ac25097dba6f667c356

URL: https://github.com/llvm/llvm-project/commit/9654df3f5e8f898341717ac25097dba6f667c356
DIFF: https://github.com/llvm/llvm-project/commit/9654df3f5e8f898341717ac25097dba6f667c356.diff

LOG: [ADT] Add StringSet::insert_range (#131957)

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

In the unit test, I'm using contains instead of
testing::UnorderedElementsAre because the latter doesn't seem to work
with char *.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringSet.h
    llvm/unittests/ADT/StringSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringSet.h b/llvm/include/llvm/ADT/StringSet.h
index bf2f04f424d13..454af55172d66 100644
--- a/llvm/include/llvm/ADT/StringSet.h
+++ b/llvm/include/llvm/ADT/StringSet.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_ADT_STRINGSET_H
 #define LLVM_ADT_STRINGSET_H
 
+#include "llvm/ADT/ADL.h"
 #include "llvm/ADT/StringMap.h"
 
 namespace llvm {
@@ -45,6 +46,10 @@ class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
       insert(*it);
   }
 
+  template <typename Range> void insert_range(Range &&R) {
+    insert(adl_begin(R), adl_end(R));
+  }
+
   template <typename ValueTy>
   std::pair<typename Base::iterator, bool>
   insert(const StringMapEntry<ValueTy> &mapEntry) {

diff  --git a/llvm/unittests/ADT/StringSetTest.cpp b/llvm/unittests/ADT/StringSetTest.cpp
index a804c1f17d1ce..8de05a2fe79d4 100644
--- a/llvm/unittests/ADT/StringSetTest.cpp
+++ b/llvm/unittests/ADT/StringSetTest.cpp
@@ -81,4 +81,14 @@ TEST_F(StringSetTest, Equal) {
   ASSERT_TRUE(A == A);
 }
 
+TEST_F(StringSetTest, InsertRange) {
+  StringSet<> Set;
+  const char *Args[] = {"chair", "desk", "bed"};
+  Set.insert_range(Args);
+  EXPECT_EQ(Set.size(), 3U);
+  EXPECT_TRUE(Set.contains("bed"));
+  EXPECT_TRUE(Set.contains("chair"));
+  EXPECT_TRUE(Set.contains("desk"));
+}
+
 } // end anonymous namespace


        


More information about the llvm-commits mailing list