blob: 0c61732a61b307996080b910da932eadb9f4867a [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.
Chris Lattnerbb28a812007-02-08 19:20:57 +000068unsigned StringMapImpl::LookupBucketFor(const char *NameStart,
Chris Lattner794a0142007-04-04 00:29:37 +000069 const char *NameEnd) {
Chris Lattner23d7b362006-10-29 23:42:03 +000070 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +000071 if (HTSize == 0) { // Hash table unallocated so far?
72 init(16);
73 HTSize = NumBuckets;
74 }
Chris Lattner23d7b362006-10-29 23:42:03 +000075 unsigned FullHashValue = HashString(NameStart, NameEnd);
76 unsigned BucketNo = FullHashValue & (HTSize-1);
77
78 unsigned ProbeAmt = 1;
Chris Lattner44dcd012007-02-11 20:58:00 +000079 int FirstTombstone = -1;
Chris Lattner23d7b362006-10-29 23:42:03 +000080 while (1) {
81 ItemBucket &Bucket = TheTable[BucketNo];
Chris Lattneree182422007-02-08 19:08:37 +000082 StringMapEntryBase *BucketItem = Bucket.Item;
Chris Lattner23d7b362006-10-29 23:42:03 +000083 // If we found an empty bucket, this key isn't in the table yet, return it.
84 if (BucketItem == 0) {
Chris Lattner44dcd012007-02-11 20:58:00 +000085 // If we found a tombstone, we want to reuse the tombstone instead of an
86 // empty bucket. This reduces probing.
87 if (FirstTombstone != -1) {
88 TheTable[FirstTombstone].FullHashValue = FullHashValue;
89 return FirstTombstone;
90 }
91
Chris Lattner23d7b362006-10-29 23:42:03 +000092 Bucket.FullHashValue = FullHashValue;
93 return BucketNo;
94 }
95
Chris Lattner44dcd012007-02-11 20:58:00 +000096 if (BucketItem == getTombstoneVal()) {
97 // Skip over tombstones. However, remember the first one we see.
98 if (FirstTombstone == -1) FirstTombstone = BucketNo;
99 } else if (Bucket.FullHashValue == FullHashValue) {
100 // If the full hash value matches, check deeply for a match. The common
101 // case here is that we are only looking at the buckets (for item info
102 // being non-null and for the full hash value) not at the items. This
103 // is important for cache locality.
104
Chris Lattner23d7b362006-10-29 23:42:03 +0000105 // Do the comparison like this because NameStart isn't necessarily
106 // null-terminated!
107 char *ItemStr = (char*)BucketItem+ItemSize;
Chris Lattneree182422007-02-08 19:08:37 +0000108 unsigned ItemStrLen = BucketItem->getKeyLength();
109 if (unsigned(NameEnd-NameStart) == ItemStrLen &&
110 memcmp(ItemStr, NameStart, ItemStrLen) == 0) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000111 // We found a match!
112 return BucketNo;
113 }
114 }
115
116 // Okay, we didn't find the item. Probe to the next bucket.
117 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
118
119 // Use quadratic probing, it has fewer clumping artifacts than linear
120 // probing and has good cache behavior in the common case.
121 ++ProbeAmt;
122 }
123}
124
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000125
126/// FindKey - Look up the bucket that contains the specified key. If it exists
127/// in the map, return the bucket number of the key. Otherwise return -1.
128/// This does not modify the map.
129int StringMapImpl::FindKey(const char *KeyStart, const char *KeyEnd) const {
130 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +0000131 if (HTSize == 0) return -1; // Really empty table?
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000132 unsigned FullHashValue = HashString(KeyStart, KeyEnd);
133 unsigned BucketNo = FullHashValue & (HTSize-1);
134
135 unsigned ProbeAmt = 1;
136 while (1) {
137 ItemBucket &Bucket = TheTable[BucketNo];
138 StringMapEntryBase *BucketItem = Bucket.Item;
139 // If we found an empty bucket, this key isn't in the table yet, return.
140 if (BucketItem == 0)
141 return -1;
142
Chris Lattner44dcd012007-02-11 20:58:00 +0000143 if (BucketItem == getTombstoneVal()) {
144 // Ignore tombstones.
145 } else if (Bucket.FullHashValue == FullHashValue) {
146 // If the full hash value matches, check deeply for a match. The common
147 // case here is that we are only looking at the buckets (for item info
148 // being non-null and for the full hash value) not at the items. This
149 // is important for cache locality.
150
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000151 // Do the comparison like this because NameStart isn't necessarily
152 // null-terminated!
153 char *ItemStr = (char*)BucketItem+ItemSize;
154 unsigned ItemStrLen = BucketItem->getKeyLength();
155 if (unsigned(KeyEnd-KeyStart) == ItemStrLen &&
156 memcmp(ItemStr, KeyStart, ItemStrLen) == 0) {
157 // We found a match!
158 return BucketNo;
159 }
160 }
161
162 // Okay, we didn't find the item. Probe to the next bucket.
163 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
164
165 // Use quadratic probing, it has fewer clumping artifacts than linear
166 // probing and has good cache behavior in the common case.
167 ++ProbeAmt;
168 }
169}
170
Chris Lattner44dcd012007-02-11 20:58:00 +0000171/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
172/// delete it. This aborts if the value isn't in the table.
173void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
174 const char *VStr = (char*)V + ItemSize;
175 StringMapEntryBase *V2 = RemoveKey(VStr, VStr+V->getKeyLength());
176 V2 = V2;
177 assert(V == V2 && "Didn't find key?");
178}
179
180/// RemoveKey - Remove the StringMapEntry for the specified key from the
181/// table, returning it. If the key is not in the table, this returns null.
182StringMapEntryBase *StringMapImpl::RemoveKey(const char *KeyStart,
183 const char *KeyEnd) {
184 int Bucket = FindKey(KeyStart, KeyEnd);
185 if (Bucket == -1) return 0;
186
187 StringMapEntryBase *Result = TheTable[Bucket].Item;
188 TheTable[Bucket].Item = getTombstoneVal();
189 --NumItems;
190 ++NumTombstones;
191 return Result;
192}
193
194
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000195
Chris Lattner23d7b362006-10-29 23:42:03 +0000196/// RehashTable - Grow the table, redistributing values into the buckets with
197/// the appropriate mod-of-hashtable-size.
Chris Lattnerbb28a812007-02-08 19:20:57 +0000198void StringMapImpl::RehashTable() {
Chris Lattner23d7b362006-10-29 23:42:03 +0000199 unsigned NewSize = NumBuckets*2;
Chris Lattnera86559e2007-02-11 08:20:35 +0000200 // Allocate one extra bucket which will always be non-empty. This allows the
201 // iterators to stop at end.
Chris Lattnerd2f197d2007-04-04 00:44:31 +0000202 ItemBucket *NewTableArray =(ItemBucket*)calloc(NewSize+1, sizeof(ItemBucket));
Chris Lattnera86559e2007-02-11 08:20:35 +0000203 NewTableArray[NewSize].Item = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +0000204
205 // Rehash all the items into their new buckets. Luckily :) we already have
206 // the hash values available, so we don't have to rehash any strings.
207 for (ItemBucket *IB = TheTable, *E = TheTable+NumBuckets; IB != E; ++IB) {
Chris Lattner44dcd012007-02-11 20:58:00 +0000208 if (IB->Item && IB->Item != getTombstoneVal()) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000209 // Fast case, bucket available.
210 unsigned FullHash = IB->FullHashValue;
211 unsigned NewBucket = FullHash & (NewSize-1);
212 if (NewTableArray[NewBucket].Item == 0) {
213 NewTableArray[FullHash & (NewSize-1)].Item = IB->Item;
214 NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash;
215 continue;
216 }
217
Chris Lattner44dcd012007-02-11 20:58:00 +0000218 // Otherwise probe for a spot.
Chris Lattner23d7b362006-10-29 23:42:03 +0000219 unsigned ProbeSize = 1;
220 do {
221 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
222 } while (NewTableArray[NewBucket].Item);
223
224 // Finally found a slot. Fill it in.
225 NewTableArray[NewBucket].Item = IB->Item;
226 NewTableArray[NewBucket].FullHashValue = FullHash;
227 }
228 }
229
Chris Lattner12ba8062007-04-04 17:24:28 +0000230 free(TheTable);
Chris Lattner23d7b362006-10-29 23:42:03 +0000231
232 TheTable = NewTableArray;
233 NumBuckets = NewSize;
234}