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