2007-01-27 15:10:46 +08:00
|
|
|
//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-27 15:10:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-01-27 15:18:32 +08:00
|
|
|
// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
|
|
|
|
// overview of the algorithm.
|
2007-01-27 15:10:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-04-18 18:37:32 +08:00
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2007-07-10 00:54:03 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2012-03-07 04:40:02 +08:00
|
|
|
#include <algorithm>
|
2007-07-17 10:16:12 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::shrink_and_clear() {
|
2007-08-05 15:32:14 +08:00
|
|
|
assert(!isSmall() && "Can't shrink a small set!");
|
|
|
|
free(CurArray);
|
|
|
|
|
|
|
|
// Reduce the number of buckets.
|
|
|
|
CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
|
|
|
|
NumElements = NumTombstones = 0;
|
|
|
|
|
|
|
|
// Install the new array. Clear all the buckets to empty.
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
|
2007-08-05 15:32:14 +08:00
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
|
|
|
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
|
|
|
}
|
|
|
|
|
2014-11-19 15:49:26 +08:00
|
|
|
std::pair<const void *const *, bool>
|
|
|
|
SmallPtrSetImplBase::insert_imp(const void *Ptr) {
|
2007-01-27 15:10:46 +08:00
|
|
|
if (isSmall()) {
|
|
|
|
// Check to see if it is already in the set.
|
2007-07-28 02:07:02 +08:00
|
|
|
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
2007-01-27 15:10:46 +08:00
|
|
|
APtr != E; ++APtr)
|
|
|
|
if (*APtr == Ptr)
|
2014-11-19 15:49:26 +08:00
|
|
|
return std::make_pair(APtr, false);
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// Nope, there isn't. If we stay small, just 'pushback' now.
|
2014-08-20 12:41:36 +08:00
|
|
|
if (NumElements < CurArraySize) {
|
2007-01-27 15:10:46 +08:00
|
|
|
SmallArray[NumElements++] = Ptr;
|
2014-11-19 15:49:26 +08:00
|
|
|
return std::make_pair(SmallArray + (NumElements - 1), true);
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
|
|
|
// Otherwise, hit the big set case, which will call grow.
|
|
|
|
}
|
|
|
|
|
2011-03-31 02:32:48 +08:00
|
|
|
if (NumElements*4 >= CurArraySize*3) {
|
|
|
|
// If more than 3/4 of the array is full, grow.
|
|
|
|
Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
|
|
|
|
} else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
|
|
|
|
// If fewer of 1/8 of the array is empty (meaning that many are filled with
|
|
|
|
// tombstones), rehash.
|
|
|
|
Grow(CurArraySize);
|
|
|
|
}
|
2007-01-27 15:10:46 +08:00
|
|
|
|
|
|
|
// Okay, we know we have space. Find a hash bucket.
|
2008-08-05 22:45:15 +08:00
|
|
|
const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
|
2014-11-19 15:49:26 +08:00
|
|
|
if (*Bucket == Ptr)
|
|
|
|
return std::make_pair(Bucket, false); // Already inserted, good.
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// Otherwise, insert it!
|
2007-02-07 09:11:25 +08:00
|
|
|
if (*Bucket == getTombstoneMarker())
|
|
|
|
--NumTombstones;
|
2008-08-05 22:45:15 +08:00
|
|
|
*Bucket = Ptr;
|
2007-01-27 15:10:46 +08:00
|
|
|
++NumElements; // Track density.
|
2014-11-19 15:49:26 +08:00
|
|
|
return std::make_pair(Bucket, true);
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
|
2007-01-27 15:59:10 +08:00
|
|
|
if (isSmall()) {
|
|
|
|
// Check to see if it is in the set.
|
2007-07-28 02:07:02 +08:00
|
|
|
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
2007-01-27 15:59:10 +08:00
|
|
|
APtr != E; ++APtr)
|
|
|
|
if (*APtr == Ptr) {
|
2007-06-22 07:23:32 +08:00
|
|
|
// If it is in the set, replace this element.
|
|
|
|
*APtr = E[-1];
|
2007-01-27 15:59:10 +08:00
|
|
|
E[-1] = getEmptyMarker();
|
|
|
|
--NumElements;
|
2007-02-06 07:10:31 +08:00
|
|
|
return true;
|
2007-01-27 15:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we know we have space. Find a hash bucket.
|
|
|
|
void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
|
|
|
|
if (*Bucket != Ptr) return false; // Not in the set?
|
|
|
|
|
|
|
|
// Set this as a tombstone.
|
|
|
|
*Bucket = getTombstoneMarker();
|
|
|
|
--NumElements;
|
2007-02-07 09:11:25 +08:00
|
|
|
++NumTombstones;
|
2007-01-27 15:59:10 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
|
2012-04-18 18:37:32 +08:00
|
|
|
unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
|
2007-01-27 15:10:46 +08:00
|
|
|
unsigned ArraySize = CurArraySize;
|
|
|
|
unsigned ProbeAmt = 1;
|
2007-07-28 02:07:02 +08:00
|
|
|
const void *const *Array = CurArray;
|
2014-04-07 12:17:22 +08:00
|
|
|
const void *const *Tombstone = nullptr;
|
2007-01-27 15:10:46 +08:00
|
|
|
while (1) {
|
|
|
|
// Found Ptr's bucket?
|
|
|
|
if (Array[Bucket] == Ptr)
|
|
|
|
return Array+Bucket;
|
|
|
|
|
|
|
|
// If we found an empty bucket, the pointer doesn't exist in the set.
|
|
|
|
// Return a tombstone if we've seen one so far, or the empty bucket if
|
|
|
|
// not.
|
|
|
|
if (Array[Bucket] == getEmptyMarker())
|
|
|
|
return Tombstone ? Tombstone : Array+Bucket;
|
|
|
|
|
|
|
|
// If this is a tombstone, remember it. If Ptr ends up not in the set, we
|
|
|
|
// prefer to return it than something that would require more probing.
|
|
|
|
if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
|
|
|
|
Tombstone = Array+Bucket; // Remember the first tombstone found.
|
|
|
|
|
|
|
|
// It's a hash collision or a tombstone. Reprobe.
|
|
|
|
Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
|
|
|
///
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
|
2007-01-27 15:10:46 +08:00
|
|
|
// Allocate at twice as many buckets, but at least 128.
|
|
|
|
unsigned OldSize = CurArraySize;
|
|
|
|
|
2007-07-28 02:07:02 +08:00
|
|
|
const void **OldBuckets = CurArray;
|
2007-01-27 15:10:46 +08:00
|
|
|
bool WasSmall = isSmall();
|
|
|
|
|
|
|
|
// Install the new array. Clear all the buckets to empty.
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * NewSize);
|
2007-07-17 05:27:44 +08:00
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
2007-01-27 15:10:46 +08:00
|
|
|
CurArraySize = NewSize;
|
|
|
|
memset(CurArray, -1, NewSize*sizeof(void*));
|
|
|
|
|
|
|
|
// Copy over all the elements.
|
|
|
|
if (WasSmall) {
|
|
|
|
// Small sets store their elements in order.
|
2007-07-28 02:07:02 +08:00
|
|
|
for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
|
2007-01-27 15:10:46 +08:00
|
|
|
BucketPtr != E; ++BucketPtr) {
|
2007-07-28 02:07:02 +08:00
|
|
|
const void *Elt = *BucketPtr;
|
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Copy over all valid entries.
|
2007-07-28 02:07:02 +08:00
|
|
|
for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
|
2007-01-27 15:10:46 +08:00
|
|
|
BucketPtr != E; ++BucketPtr) {
|
|
|
|
// Copy over the element if it is valid.
|
2007-07-28 02:07:02 +08:00
|
|
|
const void *Elt = *BucketPtr;
|
2007-01-27 15:10:46 +08:00
|
|
|
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
2007-07-28 02:07:02 +08:00
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:27:44 +08:00
|
|
|
free(OldBuckets);
|
2007-04-15 05:50:21 +08:00
|
|
|
NumTombstones = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
|
|
|
const SmallPtrSetImplBase& that) {
|
2010-06-30 23:02:37 +08:00
|
|
|
SmallArray = SmallStorage;
|
|
|
|
|
2007-07-25 05:31:23 +08:00
|
|
|
// If we're becoming small, prepare to insert into our stack space
|
2007-04-15 05:50:21 +08:00
|
|
|
if (that.isSmall()) {
|
2010-06-30 23:02:37 +08:00
|
|
|
CurArray = SmallArray;
|
2007-07-25 05:31:23 +08:00
|
|
|
// Otherwise, allocate new heap space (unless we were the same size)
|
2007-04-15 05:50:21 +08:00
|
|
|
} else {
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
|
2007-07-17 05:27:44 +08:00
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
2007-07-25 05:31:23 +08:00
|
|
|
|
|
|
|
// Copy over the new array size
|
|
|
|
CurArraySize = that.CurArraySize;
|
|
|
|
|
|
|
|
// Copy over the contents from the other set
|
2013-03-30 06:07:12 +08:00
|
|
|
memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
|
2007-07-25 05:31:23 +08:00
|
|
|
|
|
|
|
NumElements = that.NumElements;
|
|
|
|
NumTombstones = that.NumTombstones;
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
2007-07-10 00:54:03 +08:00
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
|
|
|
unsigned SmallSize,
|
|
|
|
SmallPtrSetImplBase &&that) {
|
2013-11-20 19:14:33 +08:00
|
|
|
SmallArray = SmallStorage;
|
|
|
|
|
|
|
|
// Copy over the basic members.
|
|
|
|
CurArraySize = that.CurArraySize;
|
|
|
|
NumElements = that.NumElements;
|
|
|
|
NumTombstones = that.NumTombstones;
|
|
|
|
|
|
|
|
// When small, just copy into our small buffer.
|
|
|
|
if (that.isSmall()) {
|
|
|
|
CurArray = SmallArray;
|
|
|
|
memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize);
|
2014-08-20 12:41:36 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, we steal the large memory allocation and no copy is needed.
|
|
|
|
CurArray = that.CurArray;
|
|
|
|
that.CurArray = that.SmallArray;
|
2013-11-20 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:29:56 +08:00
|
|
|
// Make the "that" object small and empty.
|
|
|
|
that.CurArraySize = SmallSize;
|
|
|
|
assert(that.CurArray == that.SmallArray);
|
|
|
|
that.NumElements = 0;
|
|
|
|
that.NumTombstones = 0;
|
2013-11-20 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2007-07-10 00:54:03 +08:00
|
|
|
/// CopyFrom - implement operator= from a smallptrset that has the same pointer
|
|
|
|
/// type, but may have a different small size.
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
|
2013-11-26 08:54:44 +08:00
|
|
|
assert(&RHS != this && "Self-copy should be handled by the caller.");
|
2013-11-26 08:44:36 +08:00
|
|
|
|
2007-07-10 06:27:20 +08:00
|
|
|
if (isSmall() && RHS.isSmall())
|
|
|
|
assert(CurArraySize == RHS.CurArraySize &&
|
|
|
|
"Cannot assign sets with different small sizes");
|
2013-03-30 06:07:12 +08:00
|
|
|
|
2007-07-10 06:27:20 +08:00
|
|
|
// If we're becoming small, prepare to insert into our stack space
|
2007-07-19 14:45:33 +08:00
|
|
|
if (RHS.isSmall()) {
|
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
2010-06-30 23:02:37 +08:00
|
|
|
CurArray = SmallArray;
|
2007-07-10 06:27:20 +08:00
|
|
|
// Otherwise, allocate new heap space (unless we were the same size)
|
2007-07-19 14:45:33 +08:00
|
|
|
} else if (CurArraySize != RHS.CurArraySize) {
|
2007-07-19 03:54:15 +08:00
|
|
|
if (isSmall())
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
|
2013-11-19 01:33:32 +08:00
|
|
|
else {
|
|
|
|
const void **T = (const void**)realloc(CurArray,
|
|
|
|
sizeof(void*) * RHS.CurArraySize);
|
|
|
|
if (!T)
|
|
|
|
free(CurArray);
|
|
|
|
CurArray = T;
|
|
|
|
}
|
2007-07-17 05:27:44 +08:00
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
|
|
|
}
|
2007-07-10 02:51:15 +08:00
|
|
|
|
2007-07-10 06:27:20 +08:00
|
|
|
// Copy over the new array size
|
|
|
|
CurArraySize = RHS.CurArraySize;
|
|
|
|
|
|
|
|
// Copy over the contents from the other set
|
2013-03-30 06:07:12 +08:00
|
|
|
memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
|
2007-07-19 03:54:15 +08:00
|
|
|
|
|
|
|
NumElements = RHS.NumElements;
|
|
|
|
NumTombstones = RHS.NumTombstones;
|
2007-07-10 00:54:03 +08:00
|
|
|
}
|
2007-07-17 10:16:12 +08:00
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
|
|
|
|
SmallPtrSetImplBase &&RHS) {
|
2013-11-26 08:54:44 +08:00
|
|
|
assert(&RHS != this && "Self-move should be handled by the caller.");
|
|
|
|
|
2013-11-20 19:14:33 +08:00
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
|
|
|
|
|
|
|
if (RHS.isSmall()) {
|
|
|
|
// Copy a small RHS rather than moving.
|
|
|
|
CurArray = SmallArray;
|
|
|
|
memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
|
|
|
|
} else {
|
|
|
|
CurArray = RHS.CurArray;
|
|
|
|
RHS.CurArray = RHS.SmallArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the rest of the trivial members.
|
|
|
|
CurArraySize = RHS.CurArraySize;
|
|
|
|
NumElements = RHS.NumElements;
|
|
|
|
NumTombstones = RHS.NumTombstones;
|
2013-11-21 02:29:56 +08:00
|
|
|
|
|
|
|
// Make the RHS small and empty.
|
|
|
|
RHS.CurArraySize = SmallSize;
|
|
|
|
assert(RHS.CurArray == RHS.SmallArray);
|
|
|
|
RHS.NumElements = 0;
|
|
|
|
RHS.NumTombstones = 0;
|
2013-11-20 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
|
2012-03-07 04:40:02 +08:00
|
|
|
if (this == &RHS) return;
|
|
|
|
|
|
|
|
// We can only avoid copying elements if neither set is small.
|
|
|
|
if (!this->isSmall() && !RHS.isSmall()) {
|
|
|
|
std::swap(this->CurArray, RHS.CurArray);
|
|
|
|
std::swap(this->CurArraySize, RHS.CurArraySize);
|
|
|
|
std::swap(this->NumElements, RHS.NumElements);
|
|
|
|
std::swap(this->NumTombstones, RHS.NumTombstones);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: From here on we assume that both sets have the same small size.
|
|
|
|
|
|
|
|
// If only RHS is small, copy the small elements into LHS and move the pointer
|
|
|
|
// from LHS to RHS.
|
|
|
|
if (!this->isSmall() && RHS.isSmall()) {
|
2012-03-08 06:48:42 +08:00
|
|
|
std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
|
|
|
|
this->SmallArray);
|
2012-03-07 04:40:02 +08:00
|
|
|
std::swap(this->NumElements, RHS.NumElements);
|
|
|
|
std::swap(this->CurArraySize, RHS.CurArraySize);
|
|
|
|
RHS.CurArray = this->CurArray;
|
|
|
|
RHS.NumTombstones = this->NumTombstones;
|
|
|
|
this->CurArray = this->SmallArray;
|
|
|
|
this->NumTombstones = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only LHS is small, copy the small elements into RHS and move the pointer
|
|
|
|
// from RHS to LHS.
|
|
|
|
if (this->isSmall() && !RHS.isSmall()) {
|
2012-03-08 06:48:42 +08:00
|
|
|
std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
|
|
|
|
RHS.SmallArray);
|
2012-03-07 04:40:02 +08:00
|
|
|
std::swap(RHS.NumElements, this->NumElements);
|
|
|
|
std::swap(RHS.CurArraySize, this->CurArraySize);
|
|
|
|
this->CurArray = RHS.CurArray;
|
|
|
|
this->NumTombstones = RHS.NumTombstones;
|
|
|
|
RHS.CurArray = RHS.SmallArray;
|
|
|
|
RHS.NumTombstones = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both a small, just swap the small elements.
|
|
|
|
assert(this->isSmall() && RHS.isSmall());
|
|
|
|
assert(this->CurArraySize == RHS.CurArraySize);
|
2012-03-08 06:48:42 +08:00
|
|
|
std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
|
2012-03-08 06:33:21 +08:00
|
|
|
RHS.SmallArray);
|
2012-03-07 04:40:02 +08:00
|
|
|
std::swap(this->NumElements, RHS.NumElements);
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
SmallPtrSetImplBase::~SmallPtrSetImplBase() {
|
2007-07-17 10:16:12 +08:00
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
|
|
|
}
|