X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/d57ee3ae693448387e3022fdd07bd741b2db818a..871af48a7d8d1a8cca7b27e0e15d1dfa030bd172:/lunaix-os/libs/hash.c diff --git a/lunaix-os/libs/hash.c b/lunaix-os/libs/hash.c index dc1b3cc..7182ab6 100644 --- a/lunaix-os/libs/hash.c +++ b/lunaix-os/libs/hash.c @@ -1,24 +1,28 @@ -#include +#include +#include /** - * @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 */ -uint32_t -strhash_32(const char* str, uint32_t truncate_to) +u32_t _weak +strhash_32(const char* str, u32_t truncate_to) { if (!str) return 0; - uint32_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