fix dependency check logic cause config always disabled
[lunaix-os.git] / lunaix-os / libs / hash.c
index b4e5c6f58910f57545aaaf0d36f4cd07722e7c4e..7182ab684523b1fa53180c336f9e0f0a17be23ba 100644 (file)
@@ -2,24 +2,27 @@
 #include <lunaix/compiler.h>
 
 /**
- * @brief Simple string hash function
+ * @brief Simple string hash function (sdbm)
  *
- * ref: https://stackoverflow.com/a/7666577
+ * ref: http://www.cse.yorku.ca/~oz/hash.html
+ * 
+ * sdbm has lower standard deviation in bucket distribution
+ * than djb2 (previously used) for low bucket size (16, 32).
  *
  * @param str
  * @return unsigned int
  */
-u32_t weak
+u32_t _weak
 strhash_32(const char* str, u32_t truncate_to)
 {
     if (!str)
         return 0;
 
-    u32_t hash = 5381;
+    u32_t hash = 0;
     int c;
 
     while ((c = *str++))
-        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
+        hash = (hash << 6) + (hash << 16) + c - hash;
 
     return hash >> (HASH_SIZE_BITS - truncate_to);
 }
\ No newline at end of file