Switch to a better hash functions for strings: Jenkins One-at-a-time hash
authorFlorian Festi <ffesti@redhat.com>
Thu, 16 Oct 2008 07:59:54 +0000 (09:59 +0200)
committerFlorian Festi <ffesti@redhat.com>
Fri, 24 Oct 2008 08:34:25 +0000 (10:34 +0200)
lib/rpmhash.c

index 5d5d6a7..34be313 100644 (file)
@@ -5,19 +5,20 @@
 
 #include "lib/rpmhash.h"
 
-unsigned int hashFunctionString(const char * string)
-{
-    char xorValue = 0;
-    char sum = 0;
-    short len;
-    int i;
-    const char * chp = string;
 
-    len = strlen(string);
-    for (i = 0; i < len; i++, chp++) {
-       xorValue ^= *chp;
-       sum += *chp;
-    }
+unsigned int hashFunctionString(const char * string) {
+    /* Jenkins One-at-a-time hash */
+
+    unsigned int hash = 0xe4721b68;
 
-    return ((((unsigned)len) << 16) + (((unsigned)sum) << 8) + xorValue);
+    while (*string != '\0') {
+      hash += *string;
+      hash += (hash << 10);
+      hash ^= (hash >> 6);
+      string++;
+    }
+    hash += (hash << 3);
+    hash ^= (hash >> 11);
+    hash += (hash << 15);
+    return hash;
 }