blob: 040308bbfd4891840603c3b56d5ad6bd8bdf1797 [file] [log] [blame]
Chris Lattnerbb28a812007-02-08 19:20:57 +00001//===--- StringMap.cpp - String Hash table map implementation -------------===//
Chris Lattner23d7b362006-10-29 23:42:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner23d7b362006-10-29 23:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerbb28a812007-02-08 19:20:57 +000010// This file implements the StringMap class.
Chris Lattner23d7b362006-10-29 23:42:03 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbb28a812007-02-08 19:20:57 +000014#include "llvm/ADT/StringMap.h"
Chris Lattner23d7b362006-10-29 23:42:03 +000015#include <cassert>
16using namespace llvm;
17
Chris Lattnerbb28a812007-02-08 19:20:57 +000018StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
Chris Lattner794a0142007-04-04 00:29:37 +000019 ItemSize = itemSize;
20
21 // If a size is specified, initialize the table with that many buckets.
22 if (InitSize) {
23 init(InitSize);
24 return;
25 }
26
27 // Otherwise, initialize it with zero buckets to avoid the allocation.
28 TheTable = 0;
29 NumBuckets = 0;
30 NumItems = 0;
31 NumTombstones = 0;
32}
33
34void StringMapImpl::init(unsigned InitSize) {
Chris Lattner23d7b362006-10-29 23:42:03 +000035 assert((InitSize & (InitSize-1)) == 0 &&
36 "Init Size must be a power of 2 or zero!");
Chris Lattneref4c9162007-04-03 22:15:38 +000037 NumBuckets = InitSize ? InitSize : 16;
Chris Lattner23d7b362006-10-29 23:42:03 +000038 NumItems = 0;
Chris Lattner44dcd012007-02-11 20:58:00 +000039 NumTombstones = 0;
Chris Lattner23d7b362006-10-29 23:42:03 +000040
Chris Lattnerd2f197d2007-04-04 00:44:31 +000041 TheTable = (ItemBucket*)calloc(NumBuckets+1, sizeof(ItemBucket));
Chris Lattnera86559e2007-02-11 08:20:35 +000042
43 // Allocate one extra bucket, set it to look filled so the iterators stop at
44 // end.
45 TheTable[NumBuckets].Item = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +000046}
47
48
49/// HashString - Compute a hash code for the specified string.
50///
51static unsigned HashString(const char *Start, const char *End) {
Chris Lattner38742b92007-01-06 23:19:38 +000052 // Bernstein hash function.
Chris Lattner23d7b362006-10-29 23:42:03 +000053 unsigned int Result = 0;
Chris Lattner38742b92007-01-06 23:19:38 +000054 // TODO: investigate whether a modified bernstein hash function performs
Chris Lattner41a44292007-01-06 23:20:51 +000055 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
Chris Lattner38742b92007-01-06 23:19:38 +000056 // X*33+c -> X*33^c
Chris Lattner23d7b362006-10-29 23:42:03 +000057 while (Start != End)
58 Result = Result * 33 + *Start++;
59 Result = Result + (Result >> 5);
60 return Result;
61}
62
63/// LookupBucketFor - Look up the bucket that the specified string should end
64/// up in. If it already exists as a key in the map, the Item pointer for the
65/// specified bucket will be non-null. Otherwise, it will be null. In either
66/// case, the FullHashValue field of the bucket will be set to the hash value
67/// of the string.
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000068unsigned StringMapImpl::LookupBucketFor(const StringRef &Name) {
Chris Lattner23d7b362006-10-29 23:42:03 +000069 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +000070 if (HTSize == 0) { // Hash table unallocated so far?
71 init(16);
72 HTSize = NumBuckets;
73 }
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000074 unsigned FullHashValue = HashString(Name.begin(), Name.end());
Chris Lattner23d7b362006-10-29 23:42:03 +000075 unsigned BucketNo = FullHashValue & (HTSize-1);
76
77 unsigned ProbeAmt = 1;
Chris Lattner44dcd012007-02-11 20:58:00 +000078 int FirstTombstone = -1;
Chris Lattner23d7b362006-10-29 23:42:03 +000079 while (1) {
80 ItemBucket &Bucket = TheTable[BucketNo];
Chris Lattneree182422007-02-08 19:08:37 +000081 StringMapEntryBase *BucketItem = Bucket.Item;
Chris Lattner23d7b362006-10-29 23:42:03 +000082 // If we found an empty bucket, this key isn't in the table yet, return it.
83 if (BucketItem == 0) {
Chris Lattner44dcd012007-02-11 20:58:00 +000084 // If we found a tombstone, we want to reuse the tombstone instead of an
85 // empty bucket. This reduces probing.
86 if (FirstTombstone != -1) {
87 TheTable[FirstTombstone].FullHashValue = FullHashValue;
88 return FirstTombstone;
89 }
90
Chris Lattner23d7b362006-10-29 23:42:03 +000091 Bucket.FullHashValue = FullHashValue;
92 return BucketNo;
93 }
94
Chris Lattner44dcd012007-02-11 20:58:00 +000095 if (BucketItem == getTombstoneVal()) {
96 // Skip over tombstones. However, remember the first one we see.
97 if (FirstTombstone == -1) FirstTombstone = BucketNo;
98 } else if (Bucket.FullHashValue == FullHashValue) {
99 // If the full hash value matches, check deeply for a match. The common
100 // case here is that we are only looking at the buckets (for item info
101 // being non-null and for the full hash value) not at the items. This
102 // is important for cache locality.
103
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000104 // Do the comparison like this because Name isn't necessarily
Chris Lattner23d7b362006-10-29 23:42:03 +0000105 // null-terminated!
106 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000107 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000108 // We found a match!
109 return BucketNo;
110 }
111 }
112
113 // Okay, we didn't find the item. Probe to the next bucket.
114 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
115
116 // Use quadratic probing, it has fewer clumping artifacts than linear
117 // probing and has good cache behavior in the common case.
118 ++ProbeAmt;
119 }
120}
121
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000122
123/// FindKey - Look up the bucket that contains the specified key. If it exists
124/// in the map, return the bucket number of the key. Otherwise return -1.
125/// This does not modify the map.
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000126int StringMapImpl::FindKey(const StringRef &Key) const {
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000127 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +0000128 if (HTSize == 0) return -1; // Really empty table?
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000129 unsigned FullHashValue = HashString(Key.begin(), Key.end());
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000130 unsigned BucketNo = FullHashValue & (HTSize-1);
131
132 unsigned ProbeAmt = 1;
133 while (1) {
134 ItemBucket &Bucket = TheTable[BucketNo];
135 StringMapEntryBase *BucketItem = Bucket.Item;
136 // If we found an empty bucket, this key isn't in the table yet, return.
137 if (BucketItem == 0)
138 return -1;
139
Chris Lattner44dcd012007-02-11 20:58:00 +0000140 if (BucketItem == getTombstoneVal()) {
141 // Ignore tombstones.
142 } else if (Bucket.FullHashValue == FullHashValue) {
143 // If the full hash value matches, check deeply for a match. The common
144 // case here is that we are only looking at the buckets (for item info
145 // being non-null and for the full hash value) not at the items. This
146 // is important for cache locality.
147
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000148 // Do the comparison like this because NameStart isn't necessarily
149 // null-terminated!
150 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000151 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000152 // We found a match!
153 return BucketNo;
154 }
155 }
156
157 // Okay, we didn't find the item. Probe to the next bucket.
158 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
159
160 // Use quadratic probing, it has fewer clumping artifacts than linear
161 // probing and has good cache behavior in the common case.
162 ++ProbeAmt;
163 }
164}
165
Chris Lattner44dcd012007-02-11 20:58:00 +0000166/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
167/// delete it. This aborts if the value isn't in the table.
168void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
169 const char *VStr = (char*)V + ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000170 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
Chris Lattner44dcd012007-02-11 20:58:00 +0000171 V2 = V2;
172 assert(V == V2 && "Didn't find key?");
173}
174
175/// RemoveKey - Remove the StringMapEntry for the specified key from the
176/// table, returning it. If the key is not in the table, this returns null.
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000177StringMapEntryBase *StringMapImpl::RemoveKey(const StringRef &Key) {
178 int Bucket = FindKey(Key);
Chris Lattner44dcd012007-02-11 20:58:00 +0000179 if (Bucket == -1) return 0;
180
181 StringMapEntryBase *Result = TheTable[Bucket].Item;
182 TheTable[Bucket].Item = getTombstoneVal();
183 --NumItems;
184 ++NumTombstones;
185 return Result;
186}
187
188
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000189
Chris Lattner23d7b362006-10-29 23:42:03 +0000190/// RehashTable - Grow the table, redistributing values into the buckets with
191/// the appropriate mod-of-hashtable-size.
Chris Lattnerbb28a812007-02-08 19:20:57 +0000192void StringMapImpl::RehashTable() {
Chris Lattner23d7b362006-10-29 23:42:03 +0000193 unsigned NewSize = NumBuckets*2;
Chris Lattnera86559e2007-02-11 08:20:35 +0000194 // Allocate one extra bucket which will always be non-empty. This allows the
195 // iterators to stop at end.
Chris Lattnerd2f197d2007-04-04 00:44:31 +0000196 ItemBucket *NewTableArray =(ItemBucket*)calloc(NewSize+1, sizeof(ItemBucket));
Chris Lattnera86559e2007-02-11 08:20:35 +0000197 NewTableArray[NewSize].Item = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +0000198
199 // Rehash all the items into their new buckets. Luckily :) we already have
200 // the hash values available, so we don't have to rehash any strings.
201 for (ItemBucket *IB = TheTable, *E = TheTable+NumBuckets; IB != E; ++IB) {
Chris Lattner44dcd012007-02-11 20:58:00 +0000202 if (IB->Item && IB->Item != getTombstoneVal()) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000203 // Fast case, bucket available.
204 unsigned FullHash = IB->FullHashValue;
205 unsigned NewBucket = FullHash & (NewSize-1);
206 if (NewTableArray[NewBucket].Item == 0) {
207 NewTableArray[FullHash & (NewSize-1)].Item = IB->Item;
208 NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash;
209 continue;
210 }
211
Chris Lattner44dcd012007-02-11 20:58:00 +0000212 // Otherwise probe for a spot.
Chris Lattner23d7b362006-10-29 23:42:03 +0000213 unsigned ProbeSize = 1;
214 do {
215 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
216 } while (NewTableArray[NewBucket].Item);
217
218 // Finally found a slot. Fill it in.
219 NewTableArray[NewBucket].Item = IB->Item;
220 NewTableArray[NewBucket].FullHashValue = FullHash;
221 }
222 }
223
Chris Lattner12ba8062007-04-04 17:24:28 +0000224 free(TheTable);
Chris Lattner23d7b362006-10-29 23:42:03 +0000225
226 TheTable = NewTableArray;
227 NumBuckets = NewSize;
228}