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"
|
2007-07-10 00:54:03 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2007-07-17 10:16:12 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2007-08-05 15:32:14 +08:00
|
|
|
void SmallPtrSetImpl::shrink_and_clear() {
|
|
|
|
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.
|
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
|
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
|
|
|
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
|
|
|
|
|
|
|
// The end pointer, always valid, is set to a valid element to help the
|
|
|
|
// iterator.
|
|
|
|
CurArray[CurArraySize] = 0;
|
|
|
|
}
|
|
|
|
|
2007-11-07 06:12:43 +08:00
|
|
|
bool SmallPtrSetImpl::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)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Nope, there isn't. If we stay small, just 'pushback' now.
|
|
|
|
if (NumElements < CurArraySize-1) {
|
|
|
|
SmallArray[NumElements++] = Ptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// 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));
|
2007-01-27 15:10:46 +08:00
|
|
|
if (*Bucket == Ptr) return false; // Already inserted, good.
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-07 06:12:43 +08:00
|
|
|
bool SmallPtrSetImpl::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;
|
|
|
|
}
|
|
|
|
|
2007-07-28 02:07:02 +08:00
|
|
|
const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
|
2007-01-27 15:10:46 +08:00
|
|
|
unsigned Bucket = Hash(Ptr);
|
|
|
|
unsigned ArraySize = CurArraySize;
|
|
|
|
unsigned ProbeAmt = 1;
|
2007-07-28 02:07:02 +08:00
|
|
|
const void *const *Array = CurArray;
|
|
|
|
const void *const *Tombstone = 0;
|
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.
|
|
|
|
///
|
2011-03-31 02:32:48 +08:00
|
|
|
void SmallPtrSetImpl::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.
|
2007-07-28 02:07:02 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
|
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*));
|
|
|
|
|
|
|
|
// The end pointer, always valid, is set to a valid element to help the
|
|
|
|
// iterator.
|
|
|
|
CurArray[NewSize] = 0;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:02:37 +08:00
|
|
|
SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
|
|
|
|
const SmallPtrSetImpl& that) {
|
|
|
|
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 {
|
2007-07-28 02:07:02 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
|
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
|
|
|
|
memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
|
|
|
|
|
|
|
|
NumElements = that.NumElements;
|
|
|
|
NumTombstones = that.NumTombstones;
|
2007-01-27 15:10:46 +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.
|
|
|
|
void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
2007-07-10 06:27:20 +08:00
|
|
|
if (isSmall() && RHS.isSmall())
|
|
|
|
assert(CurArraySize == RHS.CurArraySize &&
|
|
|
|
"Cannot assign sets with different small sizes");
|
2007-07-19 03:54:15 +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())
|
2007-07-28 02:07:02 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
|
2007-07-19 03:54:15 +08:00
|
|
|
else
|
2007-07-28 02:07:02 +08:00
|
|
|
CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
|
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
|
|
|
|
memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
|
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
|
|
|
|
|
|
|
SmallPtrSetImpl::~SmallPtrSetImpl() {
|
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
|
|
|
}
|