feat: re-implemented hash function with multiplication hash

main
Sara 2024-10-04 11:11:45 +02:00
parent 96323d7abc
commit bb201d5085
1 changed files with 2 additions and 2 deletions

View File

@ -2,10 +2,10 @@
#include "string.h" #include "string.h"
uintptr_t strnhash(const char* s, size_t n) { uintptr_t strnhash(const char* s, size_t n) {
static const size_t shift = sizeof(uintptr_t) * 8 - 4;
uintptr_t hash = 0; uintptr_t hash = 0;
static size_t const a = 33;
for(size_t i = 0; i < n; ++i) { for(size_t i = 0; i < n; ++i) {
hash = ((hash << 4) | s[i]) ^ (((uintptr_t)0xF << shift) & hash); hash = ((hash*a) + s[i]);
} }
return hash; return hash;
} }