Skip to content

Commit 46880fe

Browse files
[ADT] Add range constructors to *Set (#132623)
DenseSet recently gained a range constructor: DenseSet<T> Dest(llvm::from_range, Src); This patch adds the same signature to SetVector, SmallPtrSet, SmallSet, and StringSet for consistency.
1 parent b73e144 commit 46880fe

File tree

8 files changed

+49
-0
lines changed

8 files changed

+49
-0
lines changed

llvm/include/llvm/ADT/SetVector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/ArrayRef.h"
2525
#include "llvm/ADT/DenseSet.h"
2626
#include "llvm/ADT/STLExtras.h"
27+
#include "llvm/ADT/STLForwardCompat.h"
2728
#include "llvm/ADT/SmallVector.h"
2829
#include "llvm/Support/Compiler.h"
2930
#include <cassert>
@@ -82,6 +83,10 @@ class SetVector {
8283
insert(Start, End);
8384
}
8485

86+
template <typename Range>
87+
SetVector(llvm::from_range_t, Range &&R)
88+
: SetVector(adl_begin(R), adl_end(R)) {}
89+
8590
ArrayRef<value_type> getArrayRef() const { return vector_; }
8691

8792
/// Clear the SetVector and return the underlying vector.

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "llvm/ADT/ADL.h"
1919
#include "llvm/ADT/EpochTracker.h"
20+
#include "llvm/ADT/STLForwardCompat.h"
2021
#include "llvm/Support/MathExtras.h"
2122
#include "llvm/Support/ReverseIteration.h"
2223
#include "llvm/Support/type_traits.h"
@@ -556,6 +557,10 @@ class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
556557
this->insert(I, E);
557558
}
558559

560+
template <typename Range>
561+
SmallPtrSet(llvm::from_range_t, Range &&R)
562+
: SmallPtrSet(adl_begin(R), adl_end(R)) {}
563+
559564
SmallPtrSet(std::initializer_list<PtrType> IL)
560565
: BaseT(SmallStorage, SmallSizePowTwo) {
561566
this->insert(IL.begin(), IL.end());

llvm/include/llvm/ADT/SmallSet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_ADT_SMALLSET_H
1616

1717
#include "llvm/ADT/ADL.h"
18+
#include "llvm/ADT/STLForwardCompat.h"
1819
#include "llvm/ADT/SmallPtrSet.h"
1920
#include "llvm/ADT/SmallVector.h"
2021
#include "llvm/ADT/iterator.h"
@@ -156,6 +157,10 @@ class SmallSet {
156157
insert(Begin, End);
157158
}
158159

160+
template <typename Range>
161+
SmallSet(llvm::from_range_t, Range &&R)
162+
: SmallSet(adl_begin(R), adl_end(R)) {}
163+
159164
template <typename RangeT>
160165
explicit SmallSet(const iterator_range<RangeT> &R) {
161166
insert(R.begin(), R.end());

llvm/include/llvm/ADT/StringSet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_ADT_STRINGSET_H
1616

1717
#include "llvm/ADT/ADL.h"
18+
#include "llvm/ADT/STLForwardCompat.h"
1819
#include "llvm/ADT/StringMap.h"
1920

2021
namespace llvm {
@@ -30,6 +31,9 @@ class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
3031
for (StringRef str : initializer)
3132
insert(str);
3233
}
34+
template <typename Range> StringSet(llvm::from_range_t, Range &&R) {
35+
insert(adl_begin(R), adl_end(R));
36+
}
3337
template <typename Container> explicit StringSet(Container &&C) {
3438
for (auto &&Str : C)
3539
insert(Str);

llvm/unittests/ADT/SetVectorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ TEST(SetVector, ConstPtrKeyTest) {
8888
EXPECT_FALSE(S.contains((const int *)&j));
8989
}
9090

91+
TEST(SetVector, CtorRange) {
92+
constexpr unsigned Args[] = {3, 1, 2};
93+
SetVector<unsigned> Set(llvm::from_range, Args);
94+
EXPECT_THAT(Set, ::testing::ElementsAre(3, 1, 2));
95+
}
96+
9197
TEST(SetVector, InsertRange) {
9298
SetVector<unsigned> Set;
9399
constexpr unsigned Args[] = {3, 1, 2};

llvm/unittests/ADT/SmallPtrSetTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ TEST(SmallPtrSetTest, RemoveIf) {
411411
EXPECT_FALSE(Removed);
412412
}
413413

414+
TEST(SmallPtrSetTest, CtorRange) {
415+
int V0 = 0;
416+
int V1 = 1;
417+
int V2 = 2;
418+
int *Args[] = {&V2, &V0, &V1};
419+
SmallPtrSet<int *, 4> Set(llvm::from_range, Args);
420+
EXPECT_THAT(Set, UnorderedElementsAre(&V0, &V1, &V2));
421+
}
422+
414423
TEST(SmallPtrSetTest, InsertRange) {
415424
int V0 = 0;
416425
int V1 = 1;

llvm/unittests/ADT/SmallSetTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ TEST(SmallSetTest, InsertPerfectFwd) {
127127
}
128128
}
129129

130+
TEST(SmallSetTest, CtorRange) {
131+
constexpr unsigned Args[] = {3, 1, 2};
132+
SmallSet<int, 4> s1(llvm::from_range, Args);
133+
EXPECT_THAT(s1, ::testing::UnorderedElementsAre(1, 2, 3));
134+
}
135+
130136
TEST(SmallSetTest, InsertRange) {
131137
SmallSet<int, 4> s1;
132138
constexpr unsigned Args[] = {3, 1, 2};

llvm/unittests/ADT/StringSetTest.cpp

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

84+
TEST_F(StringSetTest, CtorRange) {
85+
const char *Args[] = {"chair", "desk", "bed"};
86+
StringSet<> Set(llvm::from_range, Args);
87+
EXPECT_EQ(Set.size(), 3U);
88+
EXPECT_TRUE(Set.contains("bed"));
89+
EXPECT_TRUE(Set.contains("chair"));
90+
EXPECT_TRUE(Set.contains("desk"));
91+
}
92+
8493
TEST_F(StringSetTest, InsertRange) {
8594
StringSet<> Set;
8695
const char *Args[] = {"chair", "desk", "bed"};

0 commit comments

Comments
 (0)