Skip to content

Commit 6ef46d1

Browse files
committed
[NFC] Use unique_ptr in SparseSet
This allows implementing the move constructor.
1 parent 74150e7 commit 6ef46d1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

llvm/include/llvm/ADT/SparseSet.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ class SparseSet {
129129
using DenseT = SmallVector<ValueT, 8>;
130130
using size_type = unsigned;
131131
DenseT Dense;
132-
SparseT *Sparse = nullptr;
132+
133+
struct Deleter {
134+
void operator()(SparseT *S) { free(S); }
135+
};
136+
std::unique_ptr<SparseT, Deleter> Sparse;
137+
133138
unsigned Universe = 0;
134139
KeyFunctorT KeyIndexOf;
135140
SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf;
@@ -144,7 +149,7 @@ class SparseSet {
144149
SparseSet() = default;
145150
SparseSet(const SparseSet &) = delete;
146151
SparseSet &operator=(const SparseSet &) = delete;
147-
~SparseSet() { free(Sparse); }
152+
SparseSet(SparseSet &&) = default;
148153

149154
/// setUniverse - Set the universe size which determines the largest key the
150155
/// set can hold. The universe must be sized before any elements can be
@@ -159,11 +164,10 @@ class SparseSet {
159164
// Hysteresis prevents needless reallocations.
160165
if (U >= Universe/4 && U <= Universe)
161166
return;
162-
free(Sparse);
163167
// The Sparse array doesn't actually need to be initialized, so malloc
164168
// would be enough here, but that will cause tools like valgrind to
165169
// complain about branching on uninitialized data.
166-
Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
170+
Sparse.reset(static_cast<SparseT *>(safe_calloc(U, sizeof(SparseT))));
167171
Universe = U;
168172
}
169173

@@ -205,7 +209,7 @@ class SparseSet {
205209
assert(Idx < Universe && "Key out of range");
206210
assert(Sparse != nullptr && "Invalid sparse type");
207211
const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
208-
for (unsigned i = Sparse[Idx], e = size(); i < e; i += Stride) {
212+
for (unsigned i = Sparse.get()[Idx], e = size(); i < e; i += Stride) {
209213
const unsigned FoundIdx = ValIndexOf(Dense[i]);
210214
assert(FoundIdx < Universe && "Invalid key in set. Did object mutate?");
211215
if (Idx == FoundIdx)
@@ -255,7 +259,7 @@ class SparseSet {
255259
iterator I = findIndex(Idx);
256260
if (I != end())
257261
return std::make_pair(I, false);
258-
Sparse[Idx] = size();
262+
Sparse.get()[Idx] = size();
259263
Dense.push_back(Val);
260264
return std::make_pair(end() - 1, true);
261265
}
@@ -292,7 +296,7 @@ class SparseSet {
292296
*I = Dense.back();
293297
unsigned BackIdx = ValIndexOf(Dense.back());
294298
assert(BackIdx < Universe && "Invalid key in set. Did object mutate?");
295-
Sparse[BackIdx] = I - begin();
299+
Sparse.get()[BackIdx] = I - begin();
296300
}
297301
// This depends on SmallVector::pop_back() not invalidating iterators.
298302
// std::vector::pop_back() doesn't give that guarantee.

0 commit comments

Comments
 (0)