Skip to content

Commit 784de75

Browse files
committedFeb 3, 2014
Introduce SmallPtrSetImpl<T *> which allows insert, erase, count, and
iteration. This alows the majority of operations to be performed without encoding a specific small size. It follows the model of SmallVectorImpl<T>. llvm-svn: 200688
1 parent 173bd7e commit 784de75

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed
 

‎llvm/include/llvm/ADT/SmallPtrSet.h

+45-22
Original file line numberDiff line numberDiff line change
@@ -235,30 +235,27 @@ struct RoundUpToPowerOfTwo {
235235
};
236236

237237

238-
/// SmallPtrSet - This class implements a set which is optimized for holding
239-
/// SmallSize or less elements. This internally rounds up SmallSize to the next
240-
/// power of two if it is not already a power of two. See the comments above
241-
/// SmallPtrSetImplBase for details of the algorithm.
242-
template<class PtrType, unsigned SmallSize>
243-
class SmallPtrSet : public SmallPtrSetImplBase {
244-
// Make sure that SmallSize is a power of two, round up if not.
245-
enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
246-
/// SmallStorage - Fixed size storage used in 'small mode'.
247-
const void *SmallStorage[SmallSizePowTwo];
238+
/// \brief A templated base class for \c SmallPtrSet which provides the
239+
/// typesafe interface that is common across all small sizes.
240+
///
241+
/// This is particularly useful for passing around between interface boundaries
242+
/// to avoid encoding a particular small size in the interface boundary.
243+
template <typename PtrType>
244+
class SmallPtrSetImpl : public SmallPtrSetImplBase {
248245
typedef PointerLikeTypeTraits<PtrType> PtrTraits;
249-
public:
250-
SmallPtrSet() : SmallPtrSetImplBase(SmallStorage, SmallSizePowTwo) {}
251-
SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImplBase(SmallStorage, that) {}
246+
protected:
247+
// Constructors that forward to the base.
248+
SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl &that)
249+
: SmallPtrSetImplBase(SmallStorage, that) {}
252250
#if LLVM_HAS_RVALUE_REFERENCES
253-
SmallPtrSet(SmallPtrSet &&that)
254-
: SmallPtrSetImplBase(SmallStorage, SmallSizePowTwo, std::move(that)) {}
251+
SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
252+
SmallPtrSetImpl &&that)
253+
: SmallPtrSetImplBase(SmallStorage, SmallSize, std::move(that)) {}
255254
#endif
255+
explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize)
256+
: SmallPtrSetImplBase(SmallStorage, SmallSize) {}
256257

257-
template<typename It>
258-
SmallPtrSet(It I, It E) : SmallPtrSetImplBase(SmallStorage, SmallSizePowTwo) {
259-
insert(I, E);
260-
}
261-
258+
public:
262259
/// insert - This returns true if the pointer was new to the set, false if it
263260
/// was already in the set.
264261
bool insert(PtrType Ptr) {
@@ -290,19 +287,45 @@ class SmallPtrSet : public SmallPtrSetImplBase {
290287
inline iterator end() const {
291288
return iterator(CurArray+CurArraySize, CurArray+CurArraySize);
292289
}
290+
};
291+
292+
/// SmallPtrSet - This class implements a set which is optimized for holding
293+
/// SmallSize or less elements. This internally rounds up SmallSize to the next
294+
/// power of two if it is not already a power of two. See the comments above
295+
/// SmallPtrSetImplBase for details of the algorithm.
296+
template<class PtrType, unsigned SmallSize>
297+
class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
298+
typedef SmallPtrSetImpl<PtrType> BaseT;
299+
300+
// Make sure that SmallSize is a power of two, round up if not.
301+
enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
302+
/// SmallStorage - Fixed size storage used in 'small mode'.
303+
const void *SmallStorage[SmallSizePowTwo];
304+
public:
305+
SmallPtrSet() : BaseT(SmallStorage, SmallSizePowTwo) {}
306+
SmallPtrSet(const SmallPtrSet &that) : BaseT(SmallStorage, that) {}
307+
#if LLVM_HAS_RVALUE_REFERENCES
308+
SmallPtrSet(SmallPtrSet &&that)
309+
: BaseT(SmallStorage, SmallSizePowTwo, std::move(that)) {}
310+
#endif
311+
312+
template<typename It>
313+
SmallPtrSet(It I, It E) : BaseT(SmallStorage, SmallSizePowTwo) {
314+
this->insert(I, E);
315+
}
293316

294317
SmallPtrSet<PtrType, SmallSize> &
295318
operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
296319
if (&RHS != this)
297-
CopyFrom(RHS);
320+
this->CopyFrom(RHS);
298321
return *this;
299322
}
300323

301324
#if LLVM_HAS_RVALUE_REFERENCES
302325
SmallPtrSet<PtrType, SmallSize>&
303326
operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) {
304327
if (&RHS != this)
305-
MoveFrom(SmallSizePowTwo, std::move(RHS));
328+
this->MoveFrom(SmallSizePowTwo, std::move(RHS));
306329
return *this;
307330
}
308331
#endif

0 commit comments

Comments
 (0)