forked from OSchip/llvm-project
Rename the non-templated base class of SmallPtrSet to
'SmallPtrSetImplBase'. This more closely matches the organization of SmallVector and should allow introducing a SmallPtrSetImpl which serves the same purpose as SmallVectorImpl: isolating the element type from the particular small size chosen. This in turn allows a lot of simplification of APIs by not coding them against a specific small size which is rarely needed. llvm-svn: 200687
This commit is contained in:
parent
bd2b3177da
commit
173bd7ed2e
|
@ -8,7 +8,7 @@
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
//
|
//
|
||||||
// This file defines the SmallPtrSet class. See the doxygen comment for
|
// This file defines the SmallPtrSet class. See the doxygen comment for
|
||||||
// SmallPtrSetImpl for more details on the algorithm used.
|
// SmallPtrSetImplBase for more details on the algorithm used.
|
||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ namespace llvm {
|
||||||
|
|
||||||
class SmallPtrSetIteratorImpl;
|
class SmallPtrSetIteratorImpl;
|
||||||
|
|
||||||
/// SmallPtrSetImpl - This is the common code shared among all the
|
/// SmallPtrSetImplBase - This is the common code shared among all the
|
||||||
/// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one
|
/// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one
|
||||||
/// for small and one for large sets.
|
/// for small and one for large sets.
|
||||||
///
|
///
|
||||||
|
@ -45,7 +45,7 @@ class SmallPtrSetIteratorImpl;
|
||||||
/// (-2), to allow deletion. The hash table is resized when the table is 3/4 or
|
/// (-2), to allow deletion. The hash table is resized when the table is 3/4 or
|
||||||
/// more. When this happens, the table is doubled in size.
|
/// more. When this happens, the table is doubled in size.
|
||||||
///
|
///
|
||||||
class SmallPtrSetImpl {
|
class SmallPtrSetImplBase {
|
||||||
friend class SmallPtrSetIteratorImpl;
|
friend class SmallPtrSetIteratorImpl;
|
||||||
protected:
|
protected:
|
||||||
/// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
|
/// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
|
||||||
|
@ -61,18 +61,18 @@ protected:
|
||||||
unsigned NumTombstones;
|
unsigned NumTombstones;
|
||||||
|
|
||||||
// Helpers to copy and move construct a SmallPtrSet.
|
// Helpers to copy and move construct a SmallPtrSet.
|
||||||
SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl &that);
|
SmallPtrSetImplBase(const void **SmallStorage, const SmallPtrSetImplBase &that);
|
||||||
#if LLVM_HAS_RVALUE_REFERENCES
|
#if LLVM_HAS_RVALUE_REFERENCES
|
||||||
SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
|
SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize,
|
||||||
SmallPtrSetImpl &&that);
|
SmallPtrSetImplBase &&that);
|
||||||
#endif
|
#endif
|
||||||
explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize) :
|
explicit SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize) :
|
||||||
SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) {
|
SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) {
|
||||||
assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
|
assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
|
||||||
"Initial size must be a power of two!");
|
"Initial size must be a power of two!");
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
~SmallPtrSetImpl();
|
~SmallPtrSetImplBase();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return size() == 0; }
|
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return size() == 0; }
|
||||||
|
@ -132,15 +132,15 @@ private:
|
||||||
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
||||||
void Grow(unsigned NewSize);
|
void Grow(unsigned NewSize);
|
||||||
|
|
||||||
void operator=(const SmallPtrSetImpl &RHS) LLVM_DELETED_FUNCTION;
|
void operator=(const SmallPtrSetImplBase &RHS) LLVM_DELETED_FUNCTION;
|
||||||
protected:
|
protected:
|
||||||
/// swap - Swaps the elements of two sets.
|
/// swap - Swaps the elements of two sets.
|
||||||
/// Note: This method assumes that both sets have the same small size.
|
/// Note: This method assumes that both sets have the same small size.
|
||||||
void swap(SmallPtrSetImpl &RHS);
|
void swap(SmallPtrSetImplBase &RHS);
|
||||||
|
|
||||||
void CopyFrom(const SmallPtrSetImpl &RHS);
|
void CopyFrom(const SmallPtrSetImplBase &RHS);
|
||||||
#if LLVM_HAS_RVALUE_REFERENCES
|
#if LLVM_HAS_RVALUE_REFERENCES
|
||||||
void MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS);
|
void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -170,8 +170,8 @@ protected:
|
||||||
void AdvanceIfNotValid() {
|
void AdvanceIfNotValid() {
|
||||||
assert(Bucket <= End);
|
assert(Bucket <= End);
|
||||||
while (Bucket != End &&
|
while (Bucket != End &&
|
||||||
(*Bucket == SmallPtrSetImpl::getEmptyMarker() ||
|
(*Bucket == SmallPtrSetImplBase::getEmptyMarker() ||
|
||||||
*Bucket == SmallPtrSetImpl::getTombstoneMarker()))
|
*Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
|
||||||
++Bucket;
|
++Bucket;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -238,24 +238,24 @@ struct RoundUpToPowerOfTwo {
|
||||||
/// SmallPtrSet - This class implements a set which is optimized for holding
|
/// SmallPtrSet - This class implements a set which is optimized for holding
|
||||||
/// SmallSize or less elements. This internally rounds up SmallSize to the next
|
/// SmallSize or less elements. This internally rounds up SmallSize to the next
|
||||||
/// power of two if it is not already a power of two. See the comments above
|
/// power of two if it is not already a power of two. See the comments above
|
||||||
/// SmallPtrSetImpl for details of the algorithm.
|
/// SmallPtrSetImplBase for details of the algorithm.
|
||||||
template<class PtrType, unsigned SmallSize>
|
template<class PtrType, unsigned SmallSize>
|
||||||
class SmallPtrSet : public SmallPtrSetImpl {
|
class SmallPtrSet : public SmallPtrSetImplBase {
|
||||||
// Make sure that SmallSize is a power of two, round up if not.
|
// Make sure that SmallSize is a power of two, round up if not.
|
||||||
enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
|
enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
|
||||||
/// SmallStorage - Fixed size storage used in 'small mode'.
|
/// SmallStorage - Fixed size storage used in 'small mode'.
|
||||||
const void *SmallStorage[SmallSizePowTwo];
|
const void *SmallStorage[SmallSizePowTwo];
|
||||||
typedef PointerLikeTypeTraits<PtrType> PtrTraits;
|
typedef PointerLikeTypeTraits<PtrType> PtrTraits;
|
||||||
public:
|
public:
|
||||||
SmallPtrSet() : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {}
|
SmallPtrSet() : SmallPtrSetImplBase(SmallStorage, SmallSizePowTwo) {}
|
||||||
SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImpl(SmallStorage, that) {}
|
SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImplBase(SmallStorage, that) {}
|
||||||
#if LLVM_HAS_RVALUE_REFERENCES
|
#if LLVM_HAS_RVALUE_REFERENCES
|
||||||
SmallPtrSet(SmallPtrSet &&that)
|
SmallPtrSet(SmallPtrSet &&that)
|
||||||
: SmallPtrSetImpl(SmallStorage, SmallSizePowTwo, std::move(that)) {}
|
: SmallPtrSetImplBase(SmallStorage, SmallSizePowTwo, std::move(that)) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
SmallPtrSet(It I, It E) : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {
|
SmallPtrSet(It I, It E) : SmallPtrSetImplBase(SmallStorage, SmallSizePowTwo) {
|
||||||
insert(I, E);
|
insert(I, E);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,7 +309,7 @@ public:
|
||||||
|
|
||||||
/// swap - Swaps the elements of two sets.
|
/// swap - Swaps the elements of two sets.
|
||||||
void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
|
void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
|
||||||
SmallPtrSetImpl::swap(RHS);
|
SmallPtrSetImplBase::swap(RHS);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
void SmallPtrSetImpl::shrink_and_clear() {
|
void SmallPtrSetImplBase::shrink_and_clear() {
|
||||||
assert(!isSmall() && "Can't shrink a small set!");
|
assert(!isSmall() && "Can't shrink a small set!");
|
||||||
free(CurArray);
|
free(CurArray);
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ void SmallPtrSetImpl::shrink_and_clear() {
|
||||||
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
|
bool SmallPtrSetImplBase::insert_imp(const void * Ptr) {
|
||||||
if (isSmall()) {
|
if (isSmall()) {
|
||||||
// Check to see if it is already in the set.
|
// Check to see if it is already in the set.
|
||||||
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
||||||
|
@ -71,7 +71,7 @@ bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
|
bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
|
||||||
if (isSmall()) {
|
if (isSmall()) {
|
||||||
// Check to see if it is in the set.
|
// Check to see if it is in the set.
|
||||||
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
||||||
|
@ -98,7 +98,7 @@ bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
|
const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
|
||||||
unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
|
unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
|
||||||
unsigned ArraySize = CurArraySize;
|
unsigned ArraySize = CurArraySize;
|
||||||
unsigned ProbeAmt = 1;
|
unsigned ProbeAmt = 1;
|
||||||
|
@ -127,7 +127,7 @@ const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
|
||||||
|
|
||||||
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
||||||
///
|
///
|
||||||
void SmallPtrSetImpl::Grow(unsigned NewSize) {
|
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
|
||||||
// Allocate at twice as many buckets, but at least 128.
|
// Allocate at twice as many buckets, but at least 128.
|
||||||
unsigned OldSize = CurArraySize;
|
unsigned OldSize = CurArraySize;
|
||||||
|
|
||||||
|
@ -163,8 +163,8 @@ void SmallPtrSetImpl::Grow(unsigned NewSize) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
|
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
||||||
const SmallPtrSetImpl& that) {
|
const SmallPtrSetImplBase& that) {
|
||||||
SmallArray = SmallStorage;
|
SmallArray = SmallStorage;
|
||||||
|
|
||||||
// If we're becoming small, prepare to insert into our stack space
|
// If we're becoming small, prepare to insert into our stack space
|
||||||
|
@ -187,8 +187,9 @@ SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#if LLVM_HAS_RVALUE_REFERENCES
|
#if LLVM_HAS_RVALUE_REFERENCES
|
||||||
SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
|
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
||||||
SmallPtrSetImpl &&that) {
|
unsigned SmallSize,
|
||||||
|
SmallPtrSetImplBase &&that) {
|
||||||
SmallArray = SmallStorage;
|
SmallArray = SmallStorage;
|
||||||
|
|
||||||
// Copy over the basic members.
|
// Copy over the basic members.
|
||||||
|
@ -217,7 +218,7 @@ SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
|
||||||
|
|
||||||
/// CopyFrom - implement operator= from a smallptrset that has the same pointer
|
/// CopyFrom - implement operator= from a smallptrset that has the same pointer
|
||||||
/// type, but may have a different small size.
|
/// type, but may have a different small size.
|
||||||
void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
|
||||||
assert(&RHS != this && "Self-copy should be handled by the caller.");
|
assert(&RHS != this && "Self-copy should be handled by the caller.");
|
||||||
|
|
||||||
if (isSmall() && RHS.isSmall())
|
if (isSmall() && RHS.isSmall())
|
||||||
|
@ -254,7 +255,8 @@ void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if LLVM_HAS_RVALUE_REFERENCES
|
#if LLVM_HAS_RVALUE_REFERENCES
|
||||||
void SmallPtrSetImpl::MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS) {
|
void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
|
||||||
|
SmallPtrSetImplBase &&RHS) {
|
||||||
assert(&RHS != this && "Self-move should be handled by the caller.");
|
assert(&RHS != this && "Self-move should be handled by the caller.");
|
||||||
|
|
||||||
if (!isSmall())
|
if (!isSmall())
|
||||||
|
@ -282,7 +284,7 @@ void SmallPtrSetImpl::MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
|
void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
|
||||||
if (this == &RHS) return;
|
if (this == &RHS) return;
|
||||||
|
|
||||||
// We can only avoid copying elements if neither set is small.
|
// We can only avoid copying elements if neither set is small.
|
||||||
|
@ -332,7 +334,7 @@ void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
|
||||||
std::swap(this->NumElements, RHS.NumElements);
|
std::swap(this->NumElements, RHS.NumElements);
|
||||||
}
|
}
|
||||||
|
|
||||||
SmallPtrSetImpl::~SmallPtrSetImpl() {
|
SmallPtrSetImplBase::~SmallPtrSetImplBase() {
|
||||||
if (!isSmall())
|
if (!isSmall())
|
||||||
free(CurArray);
|
free(CurArray);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue