2007-01-27 07:10:46 +00:00
|
|
|
//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-01-27 07:18:32 +00:00
|
|
|
// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
|
|
|
|
// overview of the algorithm.
|
2007-01-27 07:10:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-07-09 16:54:03 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2007-07-17 02:16:12 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2007-01-27 07:10:46 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
bool SmallPtrSetImpl::insert(void *Ptr) {
|
|
|
|
if (isSmall()) {
|
|
|
|
// Check to see if it is already in the set.
|
|
|
|
for (void **APtr = SmallArray, **E = SmallArray+NumElements;
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If more than 3/4 of the array is full, grow.
|
2007-02-07 01:11:25 +00:00
|
|
|
if (NumElements*4 >= CurArraySize*3 ||
|
|
|
|
CurArraySize-(NumElements+NumTombstones) < CurArraySize/8)
|
2007-01-27 07:10:46 +00:00
|
|
|
Grow();
|
|
|
|
|
|
|
|
// Okay, we know we have space. Find a hash bucket.
|
|
|
|
void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
|
|
|
|
if (*Bucket == Ptr) return false; // Already inserted, good.
|
|
|
|
|
|
|
|
// Otherwise, insert it!
|
2007-02-07 01:11:25 +00:00
|
|
|
if (*Bucket == getTombstoneMarker())
|
|
|
|
--NumTombstones;
|
2007-01-27 07:10:46 +00:00
|
|
|
*Bucket = Ptr;
|
|
|
|
++NumElements; // Track density.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-01-27 07:59:10 +00:00
|
|
|
bool SmallPtrSetImpl::erase(void *Ptr) {
|
|
|
|
if (isSmall()) {
|
|
|
|
// Check to see if it is in the set.
|
|
|
|
for (void **APtr = SmallArray, **E = SmallArray+NumElements;
|
|
|
|
APtr != E; ++APtr)
|
|
|
|
if (*APtr == Ptr) {
|
2007-06-21 23:23:32 +00:00
|
|
|
// If it is in the set, replace this element.
|
|
|
|
*APtr = E[-1];
|
2007-01-27 07:59:10 +00:00
|
|
|
E[-1] = getEmptyMarker();
|
|
|
|
--NumElements;
|
2007-02-05 23:10:31 +00:00
|
|
|
return true;
|
2007-01-27 07:59:10 +00: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 01:11:25 +00:00
|
|
|
++NumTombstones;
|
2007-01-27 07:59:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-01-27 07:10:46 +00:00
|
|
|
void * const *SmallPtrSetImpl::FindBucketFor(void *Ptr) const {
|
|
|
|
unsigned Bucket = Hash(Ptr);
|
|
|
|
unsigned ArraySize = CurArraySize;
|
|
|
|
unsigned ProbeAmt = 1;
|
|
|
|
void *const *Array = CurArray;
|
|
|
|
void *const *Tombstone = 0;
|
|
|
|
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.
|
|
|
|
///
|
|
|
|
void SmallPtrSetImpl::Grow() {
|
|
|
|
// Allocate at twice as many buckets, but at least 128.
|
|
|
|
unsigned OldSize = CurArraySize;
|
|
|
|
unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
|
|
|
|
|
|
|
|
void **OldBuckets = CurArray;
|
|
|
|
bool WasSmall = isSmall();
|
|
|
|
|
|
|
|
// Install the new array. Clear all the buckets to empty.
|
2007-07-16 21:27:44 +00:00
|
|
|
CurArray = (void**)malloc(sizeof(void*) * (NewSize+1));
|
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
2007-01-27 07:10:46 +00: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.
|
|
|
|
for (void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
|
|
|
|
BucketPtr != E; ++BucketPtr) {
|
|
|
|
void *Elt = *BucketPtr;
|
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = Elt;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Copy over all valid entries.
|
|
|
|
for (void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
|
|
|
|
BucketPtr != E; ++BucketPtr) {
|
|
|
|
// Copy over the element if it is valid.
|
|
|
|
void *Elt = *BucketPtr;
|
|
|
|
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = Elt;
|
|
|
|
}
|
|
|
|
|
2007-07-16 21:27:44 +00:00
|
|
|
free(OldBuckets);
|
2007-04-14 21:50:21 +00:00
|
|
|
NumTombstones = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) {
|
|
|
|
NumElements = that.NumElements;
|
|
|
|
NumTombstones = 0;
|
|
|
|
if (that.isSmall()) {
|
|
|
|
CurArraySize = that.CurArraySize;
|
|
|
|
CurArray = &SmallArray[0];
|
2007-06-21 23:23:32 +00:00
|
|
|
// Copy the entire contents of the array, including the -1's and the null
|
|
|
|
// terminator.
|
|
|
|
memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
|
2007-04-14 21:50:21 +00:00
|
|
|
} else {
|
2007-06-22 00:11:18 +00:00
|
|
|
CurArraySize = that.NumElements < 64 ? 128 : that.CurArraySize*2;
|
2007-07-16 21:27:44 +00:00
|
|
|
CurArray = (void**)malloc(sizeof(void*) * (CurArraySize+1));
|
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
2007-04-14 21:50:21 +00:00
|
|
|
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
|
|
|
|
|
|
|
// The end pointer, always valid, is set to a valid element to help the
|
|
|
|
// iterator.
|
|
|
|
CurArray[CurArraySize] = 0;
|
|
|
|
|
|
|
|
// Copy over all valid entries.
|
2007-07-09 20:59:01 +00:00
|
|
|
for (void **BucketPtr = that.CurArray, **E = that.CurArray+that.CurArraySize;
|
2007-04-14 21:50:21 +00:00
|
|
|
BucketPtr != E; ++BucketPtr) {
|
|
|
|
// Copy over the element if it is valid.
|
|
|
|
void *Elt = *BucketPtr;
|
|
|
|
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = Elt;
|
|
|
|
}
|
2007-01-27 07:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-09 16:54:03 +00: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-09 22:27:20 +00:00
|
|
|
if (isSmall() && RHS.isSmall())
|
|
|
|
assert(CurArraySize == RHS.CurArraySize &&
|
|
|
|
"Cannot assign sets with different small sizes");
|
2007-07-18 19:54:15 +00:00
|
|
|
|
2007-07-09 22:27:20 +00:00
|
|
|
// If we're becoming small, prepare to insert into our stack space
|
2007-07-19 06:45:33 +00:00
|
|
|
if (RHS.isSmall()) {
|
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
2007-07-09 22:27:20 +00:00
|
|
|
CurArray = &SmallArray[0];
|
|
|
|
// Otherwise, allocate new heap space (unless we were the same size)
|
2007-07-19 06:45:33 +00:00
|
|
|
} else if (CurArraySize != RHS.CurArraySize) {
|
2007-07-18 19:54:15 +00:00
|
|
|
if (isSmall())
|
|
|
|
CurArray = (void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
|
|
|
|
else
|
|
|
|
CurArray = (void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
|
2007-07-16 21:27:44 +00:00
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
|
|
|
}
|
2007-07-09 18:51:15 +00:00
|
|
|
|
2007-07-09 22:27:20 +00: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-18 19:54:15 +00:00
|
|
|
|
|
|
|
NumElements = RHS.NumElements;
|
|
|
|
NumTombstones = RHS.NumTombstones;
|
2007-07-09 16:54:03 +00:00
|
|
|
}
|
2007-07-17 02:16:12 +00:00
|
|
|
|
|
|
|
SmallPtrSetImpl::~SmallPtrSetImpl() {
|
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
|
|
|
}
|