2007-01-27 07:10:46 +00:00
|
|
|
//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-27 07:10:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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;
|
|
|
|
|
2007-08-05 07:32:14 +00: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-06 22:12:43 +00:00
|
|
|
bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
|
2007-01-27 07:10:46 +00:00
|
|
|
if (isSmall()) {
|
|
|
|
// Check to see if it is already in the set.
|
2007-07-27 18:07:02 +00:00
|
|
|
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
2007-01-27 07:10:46 +00: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.
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2008-08-05 14:45:15 +00:00
|
|
|
const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
|
2007-01-27 07:10:46 +00:00
|
|
|
if (*Bucket == Ptr) return false; // Already inserted, good.
|
|
|
|
|
|
|
|
// Otherwise, insert it!
|
2007-02-07 01:11:25 +00:00
|
|
|
if (*Bucket == getTombstoneMarker())
|
|
|
|
--NumTombstones;
|
2008-08-05 14:45:15 +00:00
|
|
|
*Bucket = Ptr;
|
2007-01-27 07:10:46 +00:00
|
|
|
++NumElements; // Track density.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-06 22:12:43 +00:00
|
|
|
bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
|
2007-01-27 07:59:10 +00:00
|
|
|
if (isSmall()) {
|
|
|
|
// Check to see if it is in the set.
|
2007-07-27 18:07:02 +00:00
|
|
|
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
|
2007-01-27 07:59:10 +00:00
|
|
|
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-07-27 18:07:02 +00:00
|
|
|
const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
|
2007-01-27 07:10:46 +00:00
|
|
|
unsigned Bucket = Hash(Ptr);
|
|
|
|
unsigned ArraySize = CurArraySize;
|
|
|
|
unsigned ProbeAmt = 1;
|
2007-07-27 18:07:02 +00:00
|
|
|
const void *const *Array = CurArray;
|
|
|
|
const void *const *Tombstone = 0;
|
2007-01-27 07:10:46 +00: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.
|
|
|
|
///
|
|
|
|
void SmallPtrSetImpl::Grow() {
|
|
|
|
// Allocate at twice as many buckets, but at least 128.
|
|
|
|
unsigned OldSize = CurArraySize;
|
|
|
|
unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
|
|
|
|
|
2007-07-27 18:07:02 +00:00
|
|
|
const void **OldBuckets = CurArray;
|
2007-01-27 07:10:46 +00:00
|
|
|
bool WasSmall = isSmall();
|
|
|
|
|
|
|
|
// Install the new array. Clear all the buckets to empty.
|
2007-07-27 18:07:02 +00:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
|
2007-07-16 21:27:44 +00:00
|
|
|
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.
|
2007-07-27 18:07:02 +00:00
|
|
|
for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
|
2007-01-27 07:10:46 +00:00
|
|
|
BucketPtr != E; ++BucketPtr) {
|
2007-07-27 18:07:02 +00:00
|
|
|
const void *Elt = *BucketPtr;
|
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
2007-01-27 07:10:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Copy over all valid entries.
|
2007-07-27 18:07:02 +00:00
|
|
|
for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
|
2007-01-27 07:10:46 +00:00
|
|
|
BucketPtr != E; ++BucketPtr) {
|
|
|
|
// Copy over the element if it is valid.
|
2007-07-27 18:07:02 +00:00
|
|
|
const void *Elt = *BucketPtr;
|
2007-01-27 07:10:46 +00:00
|
|
|
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
2007-07-27 18:07:02 +00:00
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
2007-01-27 07:10:46 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2007-07-24 21:31:23 +00:00
|
|
|
// If we're becoming small, prepare to insert into our stack space
|
2007-04-14 21:50:21 +00:00
|
|
|
if (that.isSmall()) {
|
|
|
|
CurArray = &SmallArray[0];
|
2007-07-24 21:31:23 +00:00
|
|
|
// Otherwise, allocate new heap space (unless we were the same size)
|
2007-04-14 21:50:21 +00:00
|
|
|
} else {
|
2007-07-27 18:07:02 +00:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
|
2007-07-16 21:27:44 +00:00
|
|
|
assert(CurArray && "Failed to allocate memory?");
|
2007-01-27 07:10:46 +00:00
|
|
|
}
|
2007-07-24 21:31:23 +00: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 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())
|
2007-07-27 18:07:02 +00:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
|
2007-07-18 19:54:15 +00:00
|
|
|
else
|
2007-07-27 18:07:02 +00:00
|
|
|
CurArray = (const 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);
|
|
|
|
}
|