blob: caf9ba350efc55426d72242b5fc9a464c1d6b973 [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//
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 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 Lattnera86559e2007-02-11 08:20:35 +000041 TheTable = new ItemBucket[NumBuckets+1]();
Chris Lattner23d7b362006-10-29 23:42:03 +000042 memset(TheTable, 0, NumBuckets*sizeof(ItemBucket));
Chris Lattnera86559e2007-02-11 08:20:35 +000043
44 // Allocate one extra bucket, set it to look filled so the iterators stop at
45 // end.
46 TheTable[NumBuckets].Item = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +000047}
48
49
50/// HashString - Compute a hash code for the specified string.
51///
52static unsigned HashString(const char *Start, const char *End) {
Chris Lattner38742b92007-01-06 23:19:38 +000053 // Bernstein hash function.
Chris Lattner23d7b362006-10-29 23:42:03 +000054 unsigned int Result = 0;
Chris Lattner38742b92007-01-06 23:19:38 +000055 // TODO: investigate whether a modified bernstein hash function performs
Chris Lattner41a44292007-01-06 23:20:51 +000056 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
Chris Lattner38742b92007-01-06 23:19:38 +000057 // X*33+c -> X*33^c
Chris Lattner23d7b362006-10-29 23:42:03 +000058 while (Start != End)
59 Result = Result * 33 + *Start++;
60 Result = Result + (Result >> 5);
61 return Result;
62}
63
64/// LookupBucketFor - Look up the bucket that the specified string should end
65/// up in. If it already exists as a key in the map, the Item pointer for the
66/// specified bucket will be non-null. Otherwise, it will be null. In either
67/// case, the FullHashValue field of the bucket will be set to the hash value
68/// of the string.
Chris Lattnerbb28a812007-02-08 19:20:57 +000069unsigned StringMapImpl::LookupBucketFor(const char *NameStart,
Chris Lattner794a0142007-04-04 00:29:37 +000070 const char *NameEnd) {
Chris Lattner23d7b362006-10-29 23:42:03 +000071 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +000072 if (HTSize == 0) { // Hash table unallocated so far?
73 init(16);
74 HTSize = NumBuckets;
75 }
Chris Lattner23d7b362006-10-29 23:42:03 +000076 unsigned FullHashValue = HashString(NameStart, NameEnd);
77 unsigned BucketNo = FullHashValue & (HTSize-1);
78
79 unsigned ProbeAmt = 1;
Chris Lattner44dcd012007-02-11 20:58:00 +000080 int FirstTombstone = -1;
Chris Lattner23d7b362006-10-29 23:42:03 +000081 while (1) {
82 ItemBucket &Bucket = TheTable[BucketNo];
Chris Lattneree182422007-02-08 19:08:37 +000083 StringMapEntryBase *BucketItem = Bucket.Item;
Chris Lattner23d7b362006-10-29 23:42:03 +000084 // If we found an empty bucket, this key isn't in the table yet, return it.
85 if (BucketItem == 0) {
Chris Lattner44dcd012007-02-11 20:58:00 +000086 // If we found a tombstone, we want to reuse the tombstone instead of an
87 // empty bucket. This reduces probing.
88 if (FirstTombstone != -1) {
89 TheTable[FirstTombstone].FullHashValue = FullHashValue;
90 return FirstTombstone;
91 }
92
Chris Lattner23d7b362006-10-29 23:42:03 +000093 Bucket.FullHashValue = FullHashValue;
94 return BucketNo;
95 }
96
Chris Lattner44dcd012007-02-11 20:58:00 +000097 if (BucketItem == getTombstoneVal()) {
98 // Skip over tombstones. However, remember the first one we see.
99 if (FirstTombstone == -1) FirstTombstone = BucketNo;
100 } else if (Bucket.FullHashValue == FullHashValue) {
101 // If the full hash value matches, check deeply for a match. The common
102 // case here is that we are only looking at the buckets (for item info
103 // being non-null and for the full hash value) not at the items. This
104 // is important for cache locality.
105
Chris Lattner23d7b362006-10-29 23:42:03 +0000106 // Do the comparison like this because NameStart isn't necessarily
107 // null-terminated!
108 char *ItemStr = (char*)BucketItem+ItemSize;
Chris Lattneree182422007-02-08 19:08:37 +0000109 unsigned ItemStrLen = BucketItem->getKeyLength();
110 if (unsigned(NameEnd-NameStart) == ItemStrLen &&
111 memcmp(ItemStr, NameStart, ItemStrLen) == 0) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000112 // We found a match!
113 return BucketNo;
114 }
115 }
116
117 // Okay, we didn't find the item. Probe to the next bucket.
118 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
119
120 // Use quadratic probing, it has fewer clumping artifacts than linear
121 // probing and has good cache behavior in the common case.
122 ++ProbeAmt;
123 }
124}
125
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000126
127/// FindKey - Look up the bucket that contains the specified key. If it exists
128/// in the map, return the bucket number of the key. Otherwise return -1.
129/// This does not modify the map.
130int StringMapImpl::FindKey(const char *KeyStart, const char *KeyEnd) const {
131 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +0000132 if (HTSize == 0) return -1; // Really empty table?
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000133 unsigned FullHashValue = HashString(KeyStart, KeyEnd);
134 unsigned BucketNo = FullHashValue & (HTSize-1);
135
136 unsigned ProbeAmt = 1;
137 while (1) {
138 ItemBucket &Bucket = TheTable[BucketNo];
139 StringMapEntryBase *BucketItem = Bucket.Item;
140 // If we found an empty bucket, this key isn't in the table yet, return.
141 if (BucketItem == 0)
142 return -1;
143
Chris Lattner44dcd012007-02-11 20:58:00 +0000144 if (BucketItem == getTombstoneVal()) {
145 // Ignore tombstones.
146 } else if (Bucket.FullHashValue == FullHashValue) {
147 // If the full hash value matches, check deeply for a match. The common
148 // case here is that we are only looking at the buckets (for item info
149 // being non-null and for the full hash value) not at the items. This
150 // is important for cache locality.
151
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000152 // Do the comparison like this because NameStart isn't necessarily
153 // null-terminated!
154 char *ItemStr = (char*)BucketItem+ItemSize;
155 unsigned ItemStrLen = BucketItem->getKeyLength();
156 if (unsigned(KeyEnd-KeyStart) == ItemStrLen &&
157 memcmp(ItemStr, KeyStart, ItemStrLen) == 0) {
158 // We found a match!
159 return BucketNo;
160 }
161 }
162
163 // Okay, we didn't find the item. Probe to the next bucket.
164 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
165
166 // Use quadratic probing, it has fewer clumping artifacts than linear
167 // probing and has good cache behavior in the common case.
168 ++ProbeAmt;
169 }
170}
171
Chris Lattner44dcd012007-02-11 20:58:00 +0000172/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
173/// delete it. This aborts if the value isn't in the table.
174void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
175 const char *VStr = (char*)V + ItemSize;
176 StringMapEntryBase *V2 = RemoveKey(VStr, VStr+V->getKeyLength());
177 V2 = V2;
178 assert(V == V2 && "Didn't find key?");
179}
180
181/// RemoveKey - Remove the StringMapEntry for the specified key from the
182/// table, returning it. If the key is not in the table, this returns null.
183StringMapEntryBase *StringMapImpl::RemoveKey(const char *KeyStart,
184 const char *KeyEnd) {
185 int Bucket = FindKey(KeyStart, KeyEnd);
186 if (Bucket == -1) return 0;
187
188 StringMapEntryBase *Result = TheTable[Bucket].Item;
189 TheTable[Bucket].Item = getTombstoneVal();
190 --NumItems;
191 ++NumTombstones;
192 return Result;
193}
194
195
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000196
Chris Lattner23d7b362006-10-29 23:42:03 +0000197/// RehashTable - Grow the table, redistributing values into the buckets with
198/// the appropriate mod-of-hashtable-size.
Chris Lattnerbb28a812007-02-08 19:20:57 +0000199void StringMapImpl::RehashTable() {
Chris Lattner23d7b362006-10-29 23:42:03 +0000200 unsigned NewSize = NumBuckets*2;
Chris Lattnera86559e2007-02-11 08:20:35 +0000201 // Allocate one extra bucket which will always be non-empty. This allows the
202 // iterators to stop at end.
203 ItemBucket *NewTableArray = new ItemBucket[NewSize+1]();
Chris Lattner23d7b362006-10-29 23:42:03 +0000204 memset(NewTableArray, 0, NewSize*sizeof(ItemBucket));
Chris Lattnera86559e2007-02-11 08:20:35 +0000205 NewTableArray[NewSize].Item = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +0000206
207 // Rehash all the items into their new buckets. Luckily :) we already have
208 // the hash values available, so we don't have to rehash any strings.
209 for (ItemBucket *IB = TheTable, *E = TheTable+NumBuckets; IB != E; ++IB) {
Chris Lattner44dcd012007-02-11 20:58:00 +0000210 if (IB->Item && IB->Item != getTombstoneVal()) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000211 // Fast case, bucket available.
212 unsigned FullHash = IB->FullHashValue;
213 unsigned NewBucket = FullHash & (NewSize-1);
214 if (NewTableArray[NewBucket].Item == 0) {
215 NewTableArray[FullHash & (NewSize-1)].Item = IB->Item;
216 NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash;
217 continue;
218 }
219
Chris Lattner44dcd012007-02-11 20:58:00 +0000220 // Otherwise probe for a spot.
Chris Lattner23d7b362006-10-29 23:42:03 +0000221 unsigned ProbeSize = 1;
222 do {
223 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
224 } while (NewTableArray[NewBucket].Item);
225
226 // Finally found a slot. Fill it in.
227 NewTableArray[NewBucket].Item = IB->Item;
228 NewTableArray[NewBucket].FullHashValue = FullHash;
229 }
230 }
231
232 delete[] TheTable;
233
234 TheTable = NewTableArray;
235 NumBuckets = NewSize;
236}