@@ -129,7 +129,12 @@ class SparseSet {
129
129
using DenseT = SmallVector<ValueT, 8 >;
130
130
using size_type = unsigned ;
131
131
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
+
133
138
unsigned Universe = 0 ;
134
139
KeyFunctorT KeyIndexOf;
135
140
SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf;
@@ -144,7 +149,7 @@ class SparseSet {
144
149
SparseSet () = default ;
145
150
SparseSet (const SparseSet &) = delete ;
146
151
SparseSet &operator =(const SparseSet &) = delete ;
147
- ~ SparseSet () { free (Sparse); }
152
+ SparseSet (SparseSet &&) = default ;
148
153
149
154
// / setUniverse - Set the universe size which determines the largest key the
150
155
// / set can hold. The universe must be sized before any elements can be
@@ -159,11 +164,10 @@ class SparseSet {
159
164
// Hysteresis prevents needless reallocations.
160
165
if (U >= Universe/4 && U <= Universe)
161
166
return ;
162
- free (Sparse);
163
167
// The Sparse array doesn't actually need to be initialized, so malloc
164
168
// would be enough here, but that will cause tools like valgrind to
165
169
// 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) )));
167
171
Universe = U;
168
172
}
169
173
@@ -205,7 +209,7 @@ class SparseSet {
205
209
assert (Idx < Universe && " Key out of range" );
206
210
assert (Sparse != nullptr && " Invalid sparse type" );
207
211
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) {
209
213
const unsigned FoundIdx = ValIndexOf (Dense[i]);
210
214
assert (FoundIdx < Universe && " Invalid key in set. Did object mutate?" );
211
215
if (Idx == FoundIdx)
@@ -255,7 +259,7 @@ class SparseSet {
255
259
iterator I = findIndex (Idx);
256
260
if (I != end ())
257
261
return std::make_pair (I, false );
258
- Sparse[Idx] = size ();
262
+ Sparse. get () [Idx] = size ();
259
263
Dense.push_back (Val);
260
264
return std::make_pair (end () - 1 , true );
261
265
}
@@ -292,7 +296,7 @@ class SparseSet {
292
296
*I = Dense.back ();
293
297
unsigned BackIdx = ValIndexOf (Dense.back ());
294
298
assert (BackIdx < Universe && " Invalid key in set. Did object mutate?" );
295
- Sparse[BackIdx] = I - begin ();
299
+ Sparse. get () [BackIdx] = I - begin ();
296
300
}
297
301
// This depends on SmallVector::pop_back() not invalidating iterators.
298
302
// std::vector::pop_back() doesn't give that guarantee.
0 commit comments