Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 1 | //===--- StringMap.cpp - String Hash table map implementation -------------===// |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 10 | // This file implements the StringMap class. |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 14 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 15 | #include <cassert> |
| 16 | using namespace llvm; |
| 17 | |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 18 | StringMapVisitor::~StringMapVisitor() { |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 21 | StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) { |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 22 | assert((InitSize & (InitSize-1)) == 0 && |
| 23 | "Init Size must be a power of 2 or zero!"); |
| 24 | NumBuckets = InitSize ? InitSize : 512; |
| 25 | ItemSize = itemSize; |
| 26 | NumItems = 0; |
| 27 | |
| 28 | TheTable = new ItemBucket[NumBuckets](); |
| 29 | memset(TheTable, 0, NumBuckets*sizeof(ItemBucket)); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /// HashString - Compute a hash code for the specified string. |
| 34 | /// |
| 35 | static unsigned HashString(const char *Start, const char *End) { |
Chris Lattner | 38742b9 | 2007-01-06 23:19:38 +0000 | [diff] [blame] | 36 | // Bernstein hash function. |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 37 | unsigned int Result = 0; |
Chris Lattner | 38742b9 | 2007-01-06 23:19:38 +0000 | [diff] [blame] | 38 | // TODO: investigate whether a modified bernstein hash function performs |
Chris Lattner | 41a4429 | 2007-01-06 23:20:51 +0000 | [diff] [blame] | 39 | // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx |
Chris Lattner | 38742b9 | 2007-01-06 23:19:38 +0000 | [diff] [blame] | 40 | // X*33+c -> X*33^c |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 41 | while (Start != End) |
| 42 | Result = Result * 33 + *Start++; |
| 43 | Result = Result + (Result >> 5); |
| 44 | return Result; |
| 45 | } |
| 46 | |
| 47 | /// LookupBucketFor - Look up the bucket that the specified string should end |
| 48 | /// up in. If it already exists as a key in the map, the Item pointer for the |
| 49 | /// specified bucket will be non-null. Otherwise, it will be null. In either |
| 50 | /// case, the FullHashValue field of the bucket will be set to the hash value |
| 51 | /// of the string. |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 52 | unsigned StringMapImpl::LookupBucketFor(const char *NameStart, |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 53 | const char *NameEnd) { |
| 54 | unsigned HTSize = NumBuckets; |
| 55 | unsigned FullHashValue = HashString(NameStart, NameEnd); |
| 56 | unsigned BucketNo = FullHashValue & (HTSize-1); |
| 57 | |
| 58 | unsigned ProbeAmt = 1; |
| 59 | while (1) { |
| 60 | ItemBucket &Bucket = TheTable[BucketNo]; |
Chris Lattner | ee18242 | 2007-02-08 19:08:37 +0000 | [diff] [blame] | 61 | StringMapEntryBase *BucketItem = Bucket.Item; |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 62 | // If we found an empty bucket, this key isn't in the table yet, return it. |
| 63 | if (BucketItem == 0) { |
| 64 | Bucket.FullHashValue = FullHashValue; |
| 65 | return BucketNo; |
| 66 | } |
| 67 | |
| 68 | // If the full hash value matches, check deeply for a match. The common |
| 69 | // case here is that we are only looking at the buckets (for item info |
| 70 | // being non-null and for the full hash value) not at the items. This |
| 71 | // is important for cache locality. |
| 72 | if (Bucket.FullHashValue == FullHashValue) { |
| 73 | // Do the comparison like this because NameStart isn't necessarily |
| 74 | // null-terminated! |
| 75 | char *ItemStr = (char*)BucketItem+ItemSize; |
Chris Lattner | ee18242 | 2007-02-08 19:08:37 +0000 | [diff] [blame] | 76 | unsigned ItemStrLen = BucketItem->getKeyLength(); |
| 77 | if (unsigned(NameEnd-NameStart) == ItemStrLen && |
| 78 | memcmp(ItemStr, NameStart, ItemStrLen) == 0) { |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 79 | // We found a match! |
| 80 | return BucketNo; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Okay, we didn't find the item. Probe to the next bucket. |
| 85 | BucketNo = (BucketNo+ProbeAmt) & (HTSize-1); |
| 86 | |
| 87 | // Use quadratic probing, it has fewer clumping artifacts than linear |
| 88 | // probing and has good cache behavior in the common case. |
| 89 | ++ProbeAmt; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// RehashTable - Grow the table, redistributing values into the buckets with |
| 94 | /// the appropriate mod-of-hashtable-size. |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 95 | void StringMapImpl::RehashTable() { |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 96 | unsigned NewSize = NumBuckets*2; |
| 97 | ItemBucket *NewTableArray = new ItemBucket[NewSize](); |
| 98 | memset(NewTableArray, 0, NewSize*sizeof(ItemBucket)); |
| 99 | |
| 100 | // Rehash all the items into their new buckets. Luckily :) we already have |
| 101 | // the hash values available, so we don't have to rehash any strings. |
| 102 | for (ItemBucket *IB = TheTable, *E = TheTable+NumBuckets; IB != E; ++IB) { |
| 103 | if (IB->Item) { |
| 104 | // Fast case, bucket available. |
| 105 | unsigned FullHash = IB->FullHashValue; |
| 106 | unsigned NewBucket = FullHash & (NewSize-1); |
| 107 | if (NewTableArray[NewBucket].Item == 0) { |
| 108 | NewTableArray[FullHash & (NewSize-1)].Item = IB->Item; |
| 109 | NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash; |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | unsigned ProbeSize = 1; |
| 114 | do { |
| 115 | NewBucket = (NewBucket + ProbeSize++) & (NewSize-1); |
| 116 | } while (NewTableArray[NewBucket].Item); |
| 117 | |
| 118 | // Finally found a slot. Fill it in. |
| 119 | NewTableArray[NewBucket].Item = IB->Item; |
| 120 | NewTableArray[NewBucket].FullHashValue = FullHash; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | delete[] TheTable; |
| 125 | |
| 126 | TheTable = NewTableArray; |
| 127 | NumBuckets = NewSize; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /// VisitEntries - This method walks through all of the items, |
| 132 | /// invoking Visitor.Visit for each of them. |
Chris Lattner | bb28a81 | 2007-02-08 19:20:57 +0000 | [diff] [blame^] | 133 | void StringMapImpl::VisitEntries(const StringMapVisitor &Visitor) const { |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 134 | for (ItemBucket *IB = TheTable, *E = TheTable+NumBuckets; IB != E; ++IB) { |
Chris Lattner | ee18242 | 2007-02-08 19:08:37 +0000 | [diff] [blame] | 135 | if (StringMapEntryBase *Id = IB->Item) |
Chris Lattner | 23d7b36 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 136 | Visitor.Visit((char*)Id + ItemSize, Id); |
| 137 | } |
| 138 | } |