Skip to content

Commit 4d29a87

Browse files
committed
Use SparseT[] and add test
1 parent 8a5db30 commit 4d29a87

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

llvm/include/llvm/ADT/SparseSet.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class SparseSet {
133133
struct Deleter {
134134
void operator()(SparseT *S) { free(S); }
135135
};
136-
std::unique_ptr<SparseT, Deleter> Sparse;
136+
std::unique_ptr<SparseT[], Deleter> Sparse;
137137

138138
unsigned Universe = 0;
139139
KeyFunctorT KeyIndexOf;
@@ -209,7 +209,7 @@ class SparseSet {
209209
assert(Idx < Universe && "Key out of range");
210210
assert(Sparse != nullptr && "Invalid sparse type");
211211
const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
212-
for (unsigned i = Sparse.get()[Idx], e = size(); i < e; i += Stride) {
212+
for (unsigned i = Sparse[Idx], e = size(); i < e; i += Stride) {
213213
const unsigned FoundIdx = ValIndexOf(Dense[i]);
214214
assert(FoundIdx < Universe && "Invalid key in set. Did object mutate?");
215215
if (Idx == FoundIdx)
@@ -259,7 +259,7 @@ class SparseSet {
259259
iterator I = findIndex(Idx);
260260
if (I != end())
261261
return std::make_pair(I, false);
262-
Sparse.get()[Idx] = size();
262+
Sparse[Idx] = size();
263263
Dense.push_back(Val);
264264
return std::make_pair(end() - 1, true);
265265
}
@@ -296,7 +296,7 @@ class SparseSet {
296296
*I = Dense.back();
297297
unsigned BackIdx = ValIndexOf(Dense.back());
298298
assert(BackIdx < Universe && "Invalid key in set. Did object mutate?");
299-
Sparse.get()[BackIdx] = I - begin();
299+
Sparse[BackIdx] = I - begin();
300300
}
301301
// This depends on SmallVector::pop_back() not invalidating iterators.
302302
// std::vector::pop_back() doesn't give that guarantee.

llvm/unittests/ADT/SparseSetTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,16 @@ TEST(SparseSetTest, PopBack) {
204204
for (unsigned i = 0; i < UpperBound; ++i)
205205
ASSERT_TRUE(Set.insert(i).second);
206206
}
207+
208+
TEST(SparseSetTest, MoveConstructor) {
209+
USet Set;
210+
Set.setUniverse(2);
211+
Set.insert(1);
212+
EXPECT_FALSE(Set.empty());
213+
// Move and check original is empty.
214+
USet OtherSet(std::move(Set));
215+
EXPECT_TRUE(Set.empty());
216+
EXPECT_TRUE(OtherSet.contains(1));
217+
}
218+
207219
} // namespace

0 commit comments

Comments
 (0)