| Chris Lattner | 751a420 | 2007-02-08 19:20:57 +0000 | [diff] [blame] | 1 | //===--- StringMap.cpp - String Hash table map implementation -------------===// | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
| Chris Lattner | 751a420 | 2007-02-08 19:20:57 +0000 | [diff] [blame] | 10 | // This file implements the StringMap class. | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Chris Lattner | 751a420 | 2007-02-08 19:20:57 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" | 
| Daniel Dunbar | 7766800 | 2009-10-17 18:21:06 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" | 
| Benjamin Kramer | ffa24e0 | 2012-08-29 22:57:04 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" | 
| Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 18 | #include <cassert> | 
| Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 19 |  | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 20 | using namespace llvm; | 
|  | 21 |  | 
| Mehdi Amini | be8a57f | 2016-03-25 05:57:57 +0000 | [diff] [blame] | 22 | /// Returns the number of buckets to allocate to ensure that the DenseMap can | 
|  | 23 | /// accommodate \p NumEntries without need to grow(). | 
|  | 24 | static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) { | 
|  | 25 | // Ensure that "NumEntries * 4 < NumBuckets * 3" | 
|  | 26 | if (NumEntries == 0) | 
|  | 27 | return 0; | 
|  | 28 | // +1 is required because of the strict equality. | 
|  | 29 | // For example if NumEntries is 48, we need to return 401. | 
|  | 30 | return NextPowerOf2(NumEntries * 4 / 3 + 1); | 
|  | 31 | } | 
|  | 32 |  | 
| Chris Lattner | 751a420 | 2007-02-08 19:20:57 +0000 | [diff] [blame] | 33 | StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) { | 
| Chris Lattner | 2376346 | 2007-04-04 00:29:37 +0000 | [diff] [blame] | 34 | ItemSize = itemSize; | 
|  | 35 |  | 
|  | 36 | // If a size is specified, initialize the table with that many buckets. | 
|  | 37 | if (InitSize) { | 
| Mehdi Amini | be8a57f | 2016-03-25 05:57:57 +0000 | [diff] [blame] | 38 | // The table will grow when the number of entries reach 3/4 of the number of | 
|  | 39 | // buckets. To guarantee that "InitSize" number of entries can be inserted | 
|  | 40 | // in the table without growing, we allocate just what is needed here. | 
|  | 41 | init(getMinBucketToReserveForEntries(InitSize)); | 
| Chris Lattner | 2376346 | 2007-04-04 00:29:37 +0000 | [diff] [blame] | 42 | return; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | // Otherwise, initialize it with zero buckets to avoid the allocation. | 
| Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 46 | TheTable = nullptr; | 
| Chris Lattner | 2376346 | 2007-04-04 00:29:37 +0000 | [diff] [blame] | 47 | NumBuckets = 0; | 
|  | 48 | NumItems = 0; | 
|  | 49 | NumTombstones = 0; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | void StringMapImpl::init(unsigned InitSize) { | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 53 | assert((InitSize & (InitSize-1)) == 0 && | 
|  | 54 | "Init Size must be a power of 2 or zero!"); | 
| Matthias Braun | c20b338 | 2017-07-20 01:30:39 +0000 | [diff] [blame] | 55 |  | 
|  | 56 | unsigned NewNumBuckets = InitSize ? InitSize : 16; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 57 | NumItems = 0; | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 58 | NumTombstones = 0; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 59 |  | 
| Serge Pavlov | 4500001 | 2018-02-15 09:45:59 +0000 | [diff] [blame] | 60 | TheTable = (StringMapEntryBase **)calloc(NewNumBuckets+1, | 
|  | 61 | sizeof(StringMapEntryBase **) + | 
|  | 62 | sizeof(unsigned)); | 
|  | 63 |  | 
| Matthias Braun | c20b338 | 2017-07-20 01:30:39 +0000 | [diff] [blame] | 64 | if (TheTable == nullptr) | 
|  | 65 | report_bad_alloc_error("Allocation of StringMap table failed."); | 
|  | 66 |  | 
|  | 67 | // Set the member only if TheTable was successfully allocated | 
|  | 68 | NumBuckets = NewNumBuckets; | 
|  | 69 |  | 
| Chris Lattner | e15605c | 2007-02-11 08:20:35 +0000 | [diff] [blame] | 70 | // Allocate one extra bucket, set it to look filled so the iterators stop at | 
|  | 71 | // end. | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 72 | TheTable[NumBuckets] = (StringMapEntryBase*)2; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 75 | /// LookupBucketFor - Look up the bucket that the specified string should end | 
|  | 76 | /// up in.  If it already exists as a key in the map, the Item pointer for the | 
|  | 77 | /// specified bucket will be non-null.  Otherwise, it will be null.  In either | 
|  | 78 | /// case, the FullHashValue field of the bucket will be set to the hash value | 
|  | 79 | /// of the string. | 
| Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 80 | unsigned StringMapImpl::LookupBucketFor(StringRef Name) { | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 81 | unsigned HTSize = NumBuckets; | 
| Chris Lattner | 2376346 | 2007-04-04 00:29:37 +0000 | [diff] [blame] | 82 | if (HTSize == 0) {  // Hash table unallocated so far? | 
|  | 83 | init(16); | 
|  | 84 | HTSize = NumBuckets; | 
|  | 85 | } | 
| Daniel Dunbar | 7766800 | 2009-10-17 18:21:06 +0000 | [diff] [blame] | 86 | unsigned FullHashValue = HashString(Name); | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 87 | unsigned BucketNo = FullHashValue & (HTSize-1); | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 88 | unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); | 
|  | 89 |  | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 90 | unsigned ProbeAmt = 1; | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 91 | int FirstTombstone = -1; | 
| Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 92 | while (true) { | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 93 | StringMapEntryBase *BucketItem = TheTable[BucketNo]; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 94 | // If we found an empty bucket, this key isn't in the table yet, return it. | 
| Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 95 | if (LLVM_LIKELY(!BucketItem)) { | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 96 | // If we found a tombstone, we want to reuse the tombstone instead of an | 
|  | 97 | // empty bucket.  This reduces probing. | 
|  | 98 | if (FirstTombstone != -1) { | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 99 | HashTable[FirstTombstone] = FullHashValue; | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 100 | return FirstTombstone; | 
|  | 101 | } | 
|  | 102 |  | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 103 | HashTable[BucketNo] = FullHashValue; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 104 | return BucketNo; | 
|  | 105 | } | 
|  | 106 |  | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 107 | if (BucketItem == getTombstoneVal()) { | 
|  | 108 | // Skip over tombstones.  However, remember the first one we see. | 
|  | 109 | if (FirstTombstone == -1) FirstTombstone = BucketNo; | 
| Benjamin Kramer | ffa24e0 | 2012-08-29 22:57:04 +0000 | [diff] [blame] | 110 | } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 111 | // If the full hash value matches, check deeply for a match.  The common | 
|  | 112 | // case here is that we are only looking at the buckets (for item info | 
|  | 113 | // being non-null and for the full hash value) not at the items.  This | 
|  | 114 | // is important for cache locality. | 
|  | 115 |  | 
| Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 116 | // Do the comparison like this because Name isn't necessarily | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 117 | // null-terminated! | 
|  | 118 | char *ItemStr = (char*)BucketItem+ItemSize; | 
| Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 119 | if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) { | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 120 | // We found a match! | 
|  | 121 | return BucketNo; | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // Okay, we didn't find the item.  Probe to the next bucket. | 
|  | 126 | BucketNo = (BucketNo+ProbeAmt) & (HTSize-1); | 
|  | 127 |  | 
|  | 128 | // Use quadratic probing, it has fewer clumping artifacts than linear | 
|  | 129 | // probing and has good cache behavior in the common case. | 
|  | 130 | ++ProbeAmt; | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 134 | /// FindKey - Look up the bucket that contains the specified key. If it exists | 
|  | 135 | /// in the map, return the bucket number of the key.  Otherwise return -1. | 
|  | 136 | /// This does not modify the map. | 
| Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 137 | int StringMapImpl::FindKey(StringRef Key) const { | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 138 | unsigned HTSize = NumBuckets; | 
| Chris Lattner | 2376346 | 2007-04-04 00:29:37 +0000 | [diff] [blame] | 139 | if (HTSize == 0) return -1;  // Really empty table? | 
| Daniel Dunbar | 7766800 | 2009-10-17 18:21:06 +0000 | [diff] [blame] | 140 | unsigned FullHashValue = HashString(Key); | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 141 | unsigned BucketNo = FullHashValue & (HTSize-1); | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 142 | unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); | 
|  | 143 |  | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 144 | unsigned ProbeAmt = 1; | 
| Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 145 | while (true) { | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 146 | StringMapEntryBase *BucketItem = TheTable[BucketNo]; | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 147 | // If we found an empty bucket, this key isn't in the table yet, return. | 
| Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 148 | if (LLVM_LIKELY(!BucketItem)) | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 149 | return -1; | 
|  | 150 |  | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 151 | if (BucketItem == getTombstoneVal()) { | 
|  | 152 | // Ignore tombstones. | 
| Benjamin Kramer | ffa24e0 | 2012-08-29 22:57:04 +0000 | [diff] [blame] | 153 | } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 154 | // If the full hash value matches, check deeply for a match.  The common | 
|  | 155 | // case here is that we are only looking at the buckets (for item info | 
|  | 156 | // being non-null and for the full hash value) not at the items.  This | 
|  | 157 | // is important for cache locality. | 
|  | 158 |  | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 159 | // Do the comparison like this because NameStart isn't necessarily | 
|  | 160 | // null-terminated! | 
|  | 161 | char *ItemStr = (char*)BucketItem+ItemSize; | 
| Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 162 | if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) { | 
| Chris Lattner | db08c1b | 2007-02-11 19:49:41 +0000 | [diff] [blame] | 163 | // We found a match! | 
|  | 164 | return BucketNo; | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | // Okay, we didn't find the item.  Probe to the next bucket. | 
|  | 169 | BucketNo = (BucketNo+ProbeAmt) & (HTSize-1); | 
|  | 170 |  | 
|  | 171 | // Use quadratic probing, it has fewer clumping artifacts than linear | 
|  | 172 | // probing and has good cache behavior in the common case. | 
|  | 173 | ++ProbeAmt; | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 177 | /// RemoveKey - Remove the specified StringMapEntry from the table, but do not | 
|  | 178 | /// delete it.  This aborts if the value isn't in the table. | 
|  | 179 | void StringMapImpl::RemoveKey(StringMapEntryBase *V) { | 
|  | 180 | const char *VStr = (char*)V + ItemSize; | 
| Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 181 | StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength())); | 
| Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 182 | (void)V2; | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 183 | assert(V == V2 && "Didn't find key?"); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | /// RemoveKey - Remove the StringMapEntry for the specified key from the | 
|  | 187 | /// table, returning it.  If the key is not in the table, this returns null. | 
| Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 188 | StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) { | 
| Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 189 | int Bucket = FindKey(Key); | 
| Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 190 | if (Bucket == -1) return nullptr; | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 191 |  | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 192 | StringMapEntryBase *Result = TheTable[Bucket]; | 
|  | 193 | TheTable[Bucket] = getTombstoneVal(); | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 194 | --NumItems; | 
|  | 195 | ++NumTombstones; | 
| Jakob Stoklund Olesen | 846f950 | 2011-03-30 18:32:51 +0000 | [diff] [blame] | 196 | assert(NumItems + NumTombstones <= NumBuckets); | 
|  | 197 |  | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 198 | return Result; | 
|  | 199 | } | 
|  | 200 |  | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 201 | /// RehashTable - Grow the table, redistributing values into the buckets with | 
|  | 202 | /// the appropriate mod-of-hashtable-size. | 
| David Blaikie | 16a9eab | 2014-06-23 18:28:53 +0000 | [diff] [blame] | 203 | unsigned StringMapImpl::RehashTable(unsigned BucketNo) { | 
| Jakob Stoklund Olesen | f587f44 | 2011-03-30 18:32:44 +0000 | [diff] [blame] | 204 | unsigned NewSize; | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 205 | unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); | 
| Jakob Stoklund Olesen | f587f44 | 2011-03-30 18:32:44 +0000 | [diff] [blame] | 206 |  | 
|  | 207 | // If the hash table is now more than 3/4 full, or if fewer than 1/8 of | 
|  | 208 | // the buckets are empty (meaning that many are filled with tombstones), | 
|  | 209 | // grow/rehash the table. | 
| Benjamin Kramer | 654a85e | 2015-02-23 16:41:36 +0000 | [diff] [blame] | 210 | if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) { | 
| Jakob Stoklund Olesen | f587f44 | 2011-03-30 18:32:44 +0000 | [diff] [blame] | 211 | NewSize = NumBuckets*2; | 
| Benjamin Kramer | 654a85e | 2015-02-23 16:41:36 +0000 | [diff] [blame] | 212 | } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <= | 
|  | 213 | NumBuckets / 8)) { | 
| Jakob Stoklund Olesen | f587f44 | 2011-03-30 18:32:44 +0000 | [diff] [blame] | 214 | NewSize = NumBuckets; | 
|  | 215 | } else { | 
| David Blaikie | 16a9eab | 2014-06-23 18:28:53 +0000 | [diff] [blame] | 216 | return BucketNo; | 
| Jakob Stoklund Olesen | f587f44 | 2011-03-30 18:32:44 +0000 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
| David Blaikie | 16a9eab | 2014-06-23 18:28:53 +0000 | [diff] [blame] | 219 | unsigned NewBucketNo = BucketNo; | 
| Chris Lattner | e15605c | 2007-02-11 08:20:35 +0000 | [diff] [blame] | 220 | // Allocate one extra bucket which will always be non-empty.  This allows the | 
|  | 221 | // iterators to stop at end. | 
| Serge Pavlov | 4500001 | 2018-02-15 09:45:59 +0000 | [diff] [blame] | 222 | StringMapEntryBase **NewTableArray = | 
|  | 223 | (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) + | 
|  | 224 | sizeof(unsigned)); | 
|  | 225 |  | 
| Matthias Braun | c20b338 | 2017-07-20 01:30:39 +0000 | [diff] [blame] | 226 | if (NewTableArray == nullptr) | 
|  | 227 | report_bad_alloc_error("Allocation of StringMap hash table failed."); | 
|  | 228 |  | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 229 | unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1); | 
|  | 230 | NewTableArray[NewSize] = (StringMapEntryBase*)2; | 
|  | 231 |  | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 232 | // Rehash all the items into their new buckets.  Luckily :) we already have | 
|  | 233 | // the hash values available, so we don't have to rehash any strings. | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 234 | for (unsigned I = 0, E = NumBuckets; I != E; ++I) { | 
|  | 235 | StringMapEntryBase *Bucket = TheTable[I]; | 
|  | 236 | if (Bucket && Bucket != getTombstoneVal()) { | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 237 | // Fast case, bucket available. | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 238 | unsigned FullHash = HashTable[I]; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 239 | unsigned NewBucket = FullHash & (NewSize-1); | 
| Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 240 | if (!NewTableArray[NewBucket]) { | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 241 | NewTableArray[FullHash & (NewSize-1)] = Bucket; | 
|  | 242 | NewHashArray[FullHash & (NewSize-1)] = FullHash; | 
| David Blaikie | 16a9eab | 2014-06-23 18:28:53 +0000 | [diff] [blame] | 243 | if (I == BucketNo) | 
|  | 244 | NewBucketNo = NewBucket; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 245 | continue; | 
|  | 246 | } | 
|  | 247 |  | 
| Chris Lattner | 77baa56 | 2007-02-11 20:58:00 +0000 | [diff] [blame] | 248 | // Otherwise probe for a spot. | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 249 | unsigned ProbeSize = 1; | 
|  | 250 | do { | 
|  | 251 | NewBucket = (NewBucket + ProbeSize++) & (NewSize-1); | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 252 | } while (NewTableArray[NewBucket]); | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 253 |  | 
|  | 254 | // Finally found a slot.  Fill it in. | 
| Benjamin Kramer | 46236ee | 2011-12-27 20:35:07 +0000 | [diff] [blame] | 255 | NewTableArray[NewBucket] = Bucket; | 
|  | 256 | NewHashArray[NewBucket] = FullHash; | 
| David Blaikie | 16a9eab | 2014-06-23 18:28:53 +0000 | [diff] [blame] | 257 | if (I == BucketNo) | 
|  | 258 | NewBucketNo = NewBucket; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 259 | } | 
|  | 260 | } | 
|  | 261 |  | 
| Chris Lattner | c770a02 | 2007-04-04 17:24:28 +0000 | [diff] [blame] | 262 | free(TheTable); | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 263 |  | 
|  | 264 | TheTable = NewTableArray; | 
|  | 265 | NumBuckets = NewSize; | 
| Jakob Stoklund Olesen | 846f950 | 2011-03-30 18:32:51 +0000 | [diff] [blame] | 266 | NumTombstones = 0; | 
| David Blaikie | 16a9eab | 2014-06-23 18:28:53 +0000 | [diff] [blame] | 267 | return NewBucketNo; | 
| Chris Lattner | 149e666 | 2006-10-29 23:42:03 +0000 | [diff] [blame] | 268 | } |