add new ShouldRehash method to factor out common code. Fix the dtor to not

delete tombstones.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34189 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-02-11 21:46:36 +00:00
parent 8adde28039
commit a96b4ee7ff

View File

@ -58,6 +58,16 @@ protected:
StringMapImpl(unsigned InitSize, unsigned ItemSize); StringMapImpl(unsigned InitSize, unsigned ItemSize);
void RehashTable(); void RehashTable();
/// ShouldRehash - Return true if the table should be rehashed after a new
/// element was recently inserted.
bool ShouldRehash() const {
// If the hash table is now more than 3/4 full, or if fewer than 1/8 of
// the buckets are empty (meaning that many are filled with tombstones),
// grow the table.
return NumItems*4 > NumBuckets*3 ||
NumBuckets-(NumItems+NumTombstones) < NumBuckets/8;
}
/// LookupBucketFor - Look up the bucket that the specified string should end /// LookupBucketFor - Look up the bucket that the specified string should end
/// up in. If it already exists as a key in the map, the Item pointer for the /// up in. If it already exists as a key in the map, the Item pointer for the
/// specified bucket will be non-null. Otherwise, it will be null. In either /// specified bucket will be non-null. Otherwise, it will be null. In either
@ -218,11 +228,7 @@ public:
Bucket.Item = KeyValue; Bucket.Item = KeyValue;
++NumItems; ++NumItems;
// If the hash table is now more than 3/4 full, or if fewer than 1/8 of if (ShouldRehash())
// the buckets are empty (meaning that many are filled with tombstones),
// grow the table.
if (NumItems*4 > NumBuckets*3 ||
NumBuckets-(NumItems+NumTombstones) < NumBuckets/8)
RehashTable(); RehashTable();
return true; return true;
} }
@ -247,11 +253,7 @@ public:
// filled in by LookupBucketFor. // filled in by LookupBucketFor.
Bucket.Item = NewItem; Bucket.Item = NewItem;
// If the hash table is now more than 3/4 full, or if fewer than 1/8 of if (ShouldRehash())
// the buckets are empty (meaning that many are filled with tombstones),
// grow the table.
if (NumItems*4 > NumBuckets*3 ||
NumBuckets-(NumItems+NumTombstones) < NumBuckets/8)
RehashTable(); RehashTable();
return *NewItem; return *NewItem;
} }
@ -264,8 +266,8 @@ public:
~StringMap() { ~StringMap() {
for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) { for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item)) if (I->Item && I->Item != getTombstoneVal())
Id->Destroy(Allocator); static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
} }
delete [] TheTable; delete [] TheTable;
} }