Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

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 *.

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 *.
@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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 *.


Full diff: https://github.com/llvm/llvm-project/pull/131957.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringSet.h (+5)
  • (modified) llvm/unittests/ADT/StringSetTest.cpp (+10)
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

@kazutakahirata kazutakahirata merged commit 9654df3 into llvm:main Mar 19, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_set_StringSet_insert_range branch March 19, 2025 14:13
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.

4 participants