Skip to content

Commit 9654df3

Browse files
[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 *.
1 parent 0aba833 commit 9654df3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

llvm/include/llvm/ADT/StringSet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_ADT_STRINGSET_H
1515
#define LLVM_ADT_STRINGSET_H
1616

17+
#include "llvm/ADT/ADL.h"
1718
#include "llvm/ADT/StringMap.h"
1819

1920
namespace llvm {
@@ -45,6 +46,10 @@ class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
4546
insert(*it);
4647
}
4748

49+
template <typename Range> void insert_range(Range &&R) {
50+
insert(adl_begin(R), adl_end(R));
51+
}
52+
4853
template <typename ValueTy>
4954
std::pair<typename Base::iterator, bool>
5055
insert(const StringMapEntry<ValueTy> &mapEntry) {

llvm/unittests/ADT/StringSetTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ TEST_F(StringSetTest, Equal) {
8181
ASSERT_TRUE(A == A);
8282
}
8383

84+
TEST_F(StringSetTest, InsertRange) {
85+
StringSet<> Set;
86+
const char *Args[] = {"chair", "desk", "bed"};
87+
Set.insert_range(Args);
88+
EXPECT_EQ(Set.size(), 3U);
89+
EXPECT_TRUE(Set.contains("bed"));
90+
EXPECT_TRUE(Set.contains("chair"));
91+
EXPECT_TRUE(Set.contains("desk"));
92+
}
93+
8494
} // end anonymous namespace

0 commit comments

Comments
 (0)