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