[llvm-commits] [llvm] r84344 - in /llvm/trunk: include/llvm/ADT/StringExtras.h lib/Support/StringMap.cpp

Daniel Dunbar daniel at zuster.org
Sat Oct 17 11:21:06 PDT 2009


Author: ddunbar
Date: Sat Oct 17 13:21:06 2009
New Revision: 84344

URL: http://llvm.org/viewvc/llvm-project?rev=84344&view=rev
Log:
Move StringMap's string has function into StringExtras.h

Modified:
    llvm/trunk/include/llvm/ADT/StringExtras.h
    llvm/trunk/lib/Support/StringMap.cpp

Modified: llvm/trunk/include/llvm/ADT/StringExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringExtras.h?rev=84344&r1=84343&r2=84344&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/StringExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/StringExtras.h Sat Oct 17 13:21:06 2009
@@ -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 @@
 /// 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

Modified: llvm/trunk/lib/Support/StringMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringMap.cpp?rev=84344&r1=84343&r2=84344&view=diff

==============================================================================
--- llvm/trunk/lib/Support/StringMap.cpp (original)
+++ llvm/trunk/lib/Support/StringMap.cpp Sat Oct 17 13:21:06 2009
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringExtras.h"
 #include <cassert>
 using namespace llvm;
 
@@ -46,20 +47,6 @@
 }
 
 
-/// 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 @@
     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 @@
 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;





More information about the llvm-commits mailing list