StringMap.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //===--- StringMap.cpp - String Hash table map implementation -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the StringMap class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/StringMap.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include <cassert>
  16. using namespace llvm;
  17. StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
  18. ItemSize = itemSize;
  19. // If a size is specified, initialize the table with that many buckets.
  20. if (InitSize) {
  21. init(InitSize);
  22. return;
  23. }
  24. // Otherwise, initialize it with zero buckets to avoid the allocation.
  25. TheTable = 0;
  26. NumBuckets = 0;
  27. NumItems = 0;
  28. NumTombstones = 0;
  29. }
  30. void StringMapImpl::init(unsigned InitSize) {
  31. assert((InitSize & (InitSize-1)) == 0 &&
  32. "Init Size must be a power of 2 or zero!");
  33. NumBuckets = InitSize ? InitSize : 16;
  34. NumItems = 0;
  35. NumTombstones = 0;
  36. TheTable = (ItemBucket*)calloc(NumBuckets+1, sizeof(ItemBucket));
  37. // Allocate one extra bucket, set it to look filled so the iterators stop at
  38. // end.
  39. TheTable[NumBuckets].Item = (StringMapEntryBase*)2;
  40. }
  41. /// LookupBucketFor - Look up the bucket that the specified string should end
  42. /// up in. If it already exists as a key in the map, the Item pointer for the
  43. /// specified bucket will be non-null. Otherwise, it will be null. In either
  44. /// case, the FullHashValue field of the bucket will be set to the hash value
  45. /// of the string.
  46. unsigned StringMapImpl::LookupBucketFor(const StringRef &Name) {
  47. unsigned HTSize = NumBuckets;
  48. if (HTSize == 0) { // Hash table unallocated so far?
  49. init(16);
  50. HTSize = NumBuckets;
  51. }
  52. unsigned FullHashValue = HashString(Name);
  53. unsigned BucketNo = FullHashValue & (HTSize-1);
  54. unsigned ProbeAmt = 1;
  55. int FirstTombstone = -1;
  56. while (1) {
  57. ItemBucket &Bucket = TheTable[BucketNo];
  58. StringMapEntryBase *BucketItem = Bucket.Item;
  59. // If we found an empty bucket, this key isn't in the table yet, return it.
  60. if (BucketItem == 0) {
  61. // If we found a tombstone, we want to reuse the tombstone instead of an
  62. // empty bucket. This reduces probing.
  63. if (FirstTombstone != -1) {
  64. TheTable[FirstTombstone].FullHashValue = FullHashValue;
  65. return FirstTombstone;
  66. }
  67. Bucket.FullHashValue = FullHashValue;
  68. return BucketNo;
  69. }
  70. if (BucketItem == getTombstoneVal()) {
  71. // Skip over tombstones. However, remember the first one we see.
  72. if (FirstTombstone == -1) FirstTombstone = BucketNo;
  73. } else if (Bucket.FullHashValue == FullHashValue) {
  74. // If the full hash value matches, check deeply for a match. The common
  75. // case here is that we are only looking at the buckets (for item info
  76. // being non-null and for the full hash value) not at the items. This
  77. // is important for cache locality.
  78. // Do the comparison like this because Name isn't necessarily
  79. // null-terminated!
  80. char *ItemStr = (char*)BucketItem+ItemSize;
  81. if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
  82. // We found a match!
  83. return BucketNo;
  84. }
  85. }
  86. // Okay, we didn't find the item. Probe to the next bucket.
  87. BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
  88. // Use quadratic probing, it has fewer clumping artifacts than linear
  89. // probing and has good cache behavior in the common case.
  90. ++ProbeAmt;
  91. }
  92. }
  93. /// FindKey - Look up the bucket that contains the specified key. If it exists
  94. /// in the map, return the bucket number of the key. Otherwise return -1.
  95. /// This does not modify the map.
  96. int StringMapImpl::FindKey(const StringRef &Key) const {
  97. unsigned HTSize = NumBuckets;
  98. if (HTSize == 0) return -1; // Really empty table?
  99. unsigned FullHashValue = HashString(Key);
  100. unsigned BucketNo = FullHashValue & (HTSize-1);
  101. unsigned ProbeAmt = 1;
  102. while (1) {
  103. ItemBucket &Bucket = TheTable[BucketNo];
  104. StringMapEntryBase *BucketItem = Bucket.Item;
  105. // If we found an empty bucket, this key isn't in the table yet, return.
  106. if (BucketItem == 0)
  107. return -1;
  108. if (BucketItem == getTombstoneVal()) {
  109. // Ignore tombstones.
  110. } else if (Bucket.FullHashValue == FullHashValue) {
  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. // Do the comparison like this because NameStart isn't necessarily
  116. // null-terminated!
  117. char *ItemStr = (char*)BucketItem+ItemSize;
  118. if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
  119. // We found a match!
  120. return BucketNo;
  121. }
  122. }
  123. // Okay, we didn't find the item. Probe to the next bucket.
  124. BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
  125. // Use quadratic probing, it has fewer clumping artifacts than linear
  126. // probing and has good cache behavior in the common case.
  127. ++ProbeAmt;
  128. }
  129. }
  130. /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
  131. /// delete it. This aborts if the value isn't in the table.
  132. void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
  133. const char *VStr = (char*)V + ItemSize;
  134. StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
  135. V2 = V2;
  136. assert(V == V2 && "Didn't find key?");
  137. }
  138. /// RemoveKey - Remove the StringMapEntry for the specified key from the
  139. /// table, returning it. If the key is not in the table, this returns null.
  140. StringMapEntryBase *StringMapImpl::RemoveKey(const StringRef &Key) {
  141. int Bucket = FindKey(Key);
  142. if (Bucket == -1) return 0;
  143. StringMapEntryBase *Result = TheTable[Bucket].Item;
  144. TheTable[Bucket].Item = getTombstoneVal();
  145. --NumItems;
  146. ++NumTombstones;
  147. return Result;
  148. }
  149. /// RehashTable - Grow the table, redistributing values into the buckets with
  150. /// the appropriate mod-of-hashtable-size.
  151. void StringMapImpl::RehashTable() {
  152. unsigned NewSize = NumBuckets*2;
  153. // Allocate one extra bucket which will always be non-empty. This allows the
  154. // iterators to stop at end.
  155. ItemBucket *NewTableArray =(ItemBucket*)calloc(NewSize+1, sizeof(ItemBucket));
  156. NewTableArray[NewSize].Item = (StringMapEntryBase*)2;
  157. // Rehash all the items into their new buckets. Luckily :) we already have
  158. // the hash values available, so we don't have to rehash any strings.
  159. for (ItemBucket *IB = TheTable, *E = TheTable+NumBuckets; IB != E; ++IB) {
  160. if (IB->Item && IB->Item != getTombstoneVal()) {
  161. // Fast case, bucket available.
  162. unsigned FullHash = IB->FullHashValue;
  163. unsigned NewBucket = FullHash & (NewSize-1);
  164. if (NewTableArray[NewBucket].Item == 0) {
  165. NewTableArray[FullHash & (NewSize-1)].Item = IB->Item;
  166. NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash;
  167. continue;
  168. }
  169. // Otherwise probe for a spot.
  170. unsigned ProbeSize = 1;
  171. do {
  172. NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
  173. } while (NewTableArray[NewBucket].Item);
  174. // Finally found a slot. Fill it in.
  175. NewTableArray[NewBucket].Item = IB->Item;
  176. NewTableArray[NewBucket].FullHashValue = FullHash;
  177. }
  178. }
  179. free(TheTable);
  180. TheTable = NewTableArray;
  181. NumBuckets = NewSize;
  182. }