Skip to content

Commit

Permalink
Use SparseT[] and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
optimisan committed Nov 19, 2024
1 parent 8a5db30 commit 4d29a87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions llvm/include/llvm/ADT/SparseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class SparseSet {
struct Deleter {
void operator()(SparseT *S) { free(S); }
};
std::unique_ptr<SparseT, Deleter> Sparse;
std::unique_ptr<SparseT[], Deleter> Sparse;

unsigned Universe = 0;
KeyFunctorT KeyIndexOf;
Expand Down Expand Up @@ -209,7 +209,7 @@ class SparseSet {
assert(Idx < Universe && "Key out of range");
assert(Sparse != nullptr && "Invalid sparse type");
const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
for (unsigned i = Sparse.get()[Idx], e = size(); i < e; i += Stride) {
for (unsigned i = Sparse[Idx], e = size(); i < e; i += Stride) {
const unsigned FoundIdx = ValIndexOf(Dense[i]);
assert(FoundIdx < Universe && "Invalid key in set. Did object mutate?");
if (Idx == FoundIdx)
Expand Down Expand Up @@ -259,7 +259,7 @@ class SparseSet {
iterator I = findIndex(Idx);
if (I != end())
return std::make_pair(I, false);
Sparse.get()[Idx] = size();
Sparse[Idx] = size();
Dense.push_back(Val);
return std::make_pair(end() - 1, true);
}
Expand Down Expand Up @@ -296,7 +296,7 @@ class SparseSet {
*I = Dense.back();
unsigned BackIdx = ValIndexOf(Dense.back());
assert(BackIdx < Universe && "Invalid key in set. Did object mutate?");
Sparse.get()[BackIdx] = I - begin();
Sparse[BackIdx] = I - begin();
}
// This depends on SmallVector::pop_back() not invalidating iterators.
// std::vector::pop_back() doesn't give that guarantee.
Expand Down
12 changes: 12 additions & 0 deletions llvm/unittests/ADT/SparseSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ TEST(SparseSetTest, PopBack) {
for (unsigned i = 0; i < UpperBound; ++i)
ASSERT_TRUE(Set.insert(i).second);
}

TEST(SparseSetTest, MoveConstructor) {
USet Set;
Set.setUniverse(2);
Set.insert(1);
EXPECT_FALSE(Set.empty());
// Move and check original is empty.
USet OtherSet(std::move(Set));
EXPECT_TRUE(Set.empty());
EXPECT_TRUE(OtherSet.contains(1));
}

} // namespace

0 comments on commit 4d29a87

Please sign in to comment.