llvm.org GIT mirror llvm / 4dee7fd
Move StringMap's string has function into StringExtras.h git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84344 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
2 changed file(s) with 17 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
1515
1616 #include "llvm/Support/DataTypes.h"
1717 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/StringRef.h"
1819 #include
1920 #include
2021 #include
224225 /// doesn't satisfy std::isprint into an escape sequence.
225226 void EscapeString(std::string &Str);
226227
228 /// HashString - Hash funtion for strings.
229 ///
230 /// This is the Bernstein hash function.
231 //
232 // FIXME: Investigate whether a modified bernstein hash function performs
233 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
234 // X*33+c -> X*33^c
235 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
236 for (unsigned i = 0, e = Str.size(); i != e; ++i)
237 Result = Result * 33 + Str[i];
238 return Result;
239 }
240
227241 } // End llvm namespace
228242
229243 #endif
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringExtras.h"
1415 #include
1516 using namespace llvm;
1617
4445 TheTable[NumBuckets].Item = (StringMapEntryBase*)2;
4546 }
4647
47
48 /// HashString - Compute a hash code for the specified string.
49 ///
50 static unsigned HashString(const char *Start, const char *End) {
51 // Bernstein hash function.
52 unsigned int Result = 0;
53 // TODO: investigate whether a modified bernstein hash function performs
54 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
55 // X*33+c -> X*33^c
56 while (Start != End)
57 Result = Result * 33 + *Start++;
58 Result = Result + (Result >> 5);
59 return Result;
60 }
6148
6249 /// LookupBucketFor - Look up the bucket that the specified string should end
6350 /// up in. If it already exists as a key in the map, the Item pointer for the
7057 init(16);
7158 HTSize = NumBuckets;
7259 }
73 unsigned FullHashValue = HashString(Name.begin(), Name.end());
60 unsigned FullHashValue = HashString(Name);
7461 unsigned BucketNo = FullHashValue & (HTSize-1);
7562
7663 unsigned ProbeAmt = 1;
125112 int StringMapImpl::FindKey(const StringRef &Key) const {
126113 unsigned HTSize = NumBuckets;
127114 if (HTSize == 0) return -1; // Really empty table?
128 unsigned FullHashValue = HashString(Key.begin(), Key.end());
115 unsigned FullHashValue = HashString(Key);
129116 unsigned BucketNo = FullHashValue & (HTSize-1);
130117
131118 unsigned ProbeAmt = 1;