summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-10-17 18:21:06 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-10-17 18:21:06 +0000
commit77668004c448dd95fb5ed2781f68e0f42a2868d7 (patch)
tree3f61528b7e8ca3029d5d19eec9b866050edd32a1 /llvm
parent1c0761d6e90bff86e4f5807017954be2debd6214 (diff)
downloadbcm5719-llvm-77668004c448dd95fb5ed2781f68e0f42a2868d7.tar.gz
bcm5719-llvm-77668004c448dd95fb5ed2781f68e0f42a2868d7.zip
Move StringMap's string has function into StringExtras.h
llvm-svn: 84344
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ADT/StringExtras.h14
-rw-r--r--llvm/lib/Support/StringMap.cpp19
2 files changed, 17 insertions, 16 deletions
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 3d1993c6b26..515cda4b298 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -16,6 +16,7 @@
#include "llvm/Support/DataTypes.h"
#include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/StringRef.h"
#include <cctype>
#include <cstdio>
#include <string>
@@ -225,6 +226,19 @@ void UnescapeString(std::string &Str);
/// doesn't satisfy std::isprint into an escape sequence.
void EscapeString(std::string &Str);
+/// HashString - Hash funtion for strings.
+///
+/// This is the Bernstein hash function.
+//
+// FIXME: Investigate whether a modified bernstein hash function performs
+// better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
+// X*33+c -> X*33^c
+static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
+ for (unsigned i = 0, e = Str.size(); i != e; ++i)
+ Result = Result * 33 + Str[i];
+ return Result;
+}
+
} // End llvm namespace
#endif
diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp
index 040308bbfd4..a729d3d6c09 100644
--- a/llvm/lib/Support/StringMap.cpp
+++ b/llvm/lib/Support/StringMap.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringExtras.h"
#include <cassert>
using namespace llvm;
@@ -46,20 +47,6 @@ void StringMapImpl::init(unsigned InitSize) {
}
-/// HashString - Compute a hash code for the specified string.
-///
-static unsigned HashString(const char *Start, const char *End) {
- // Bernstein hash function.
- unsigned int Result = 0;
- // TODO: investigate whether a modified bernstein hash function performs
- // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
- // X*33+c -> X*33^c
- while (Start != End)
- Result = Result * 33 + *Start++;
- Result = Result + (Result >> 5);
- return Result;
-}
-
/// LookupBucketFor - Look up the bucket that the specified string should end
/// up in. If it already exists as a key in the map, the Item pointer for the
/// specified bucket will be non-null. Otherwise, it will be null. In either
@@ -71,7 +58,7 @@ unsigned StringMapImpl::LookupBucketFor(const StringRef &Name) {
init(16);
HTSize = NumBuckets;
}
- unsigned FullHashValue = HashString(Name.begin(), Name.end());
+ unsigned FullHashValue = HashString(Name);
unsigned BucketNo = FullHashValue & (HTSize-1);
unsigned ProbeAmt = 1;
@@ -126,7 +113,7 @@ unsigned StringMapImpl::LookupBucketFor(const StringRef &Name) {
int StringMapImpl::FindKey(const StringRef &Key) const {
unsigned HTSize = NumBuckets;
if (HTSize == 0) return -1; // Really empty table?
- unsigned FullHashValue = HashString(Key.begin(), Key.end());
+ unsigned FullHashValue = HashString(Key);
unsigned BucketNo = FullHashValue & (HTSize-1);
unsigned ProbeAmt = 1;
OpenPOWER on IntegriCloud