diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 637bc74..992ef5e 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -39,12 +39,10 @@ void Hashtable_expand(Hashtable* ht){ uint32 arlen=Autoarr_length(ar); for(uint32 k=0;khein]; + uint16 newrown=hashs(hash_sdbm32, p.key)%HT_HEIGHTS[ht->hein]; Autoarr(KVPair)* newar=newrows[newrown]; Autoarr_add(newar,p); } - // it is a feature, not a bug - // no need to free kvpair keys and values, they just moved to new autoarrs Autoarr_free(ar); } @@ -53,10 +51,10 @@ void Hashtable_expand(Hashtable* ht){ } Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){ - uint32 hash=hashs_sdbm32(key); + uint32 hash=hashs(hash_sdbm32, key); Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]]; if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar)) - optime("expand",1,(Hashtable_expand(ht))); + Hashtable_expand(ht); ar=ht->rows[hash%HT_HEIGHTS[ht->hein]]; return ar; } diff --git a/src/Hashtable/hash.h b/src/Hashtable/hash.h index d424de9..753ed11 100644 --- a/src/Hashtable/hash.h +++ b/src/Hashtable/hash.h @@ -6,13 +6,11 @@ extern "C" { #include "../base/base.h" -uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len); -#define hashb_sdbm32(BUF, LEN) hash_sdbm32(0xFFFFFFFF, BUF, LEN) -#define hashs_sdbm32(STR) hash_sdbm32(0xFFFFFFFF, STR, cptr_length(STR)) +#define hashb(FUNC, BUF, LEN) FUNC(0xFFFFFFFF, BUF, LEN) +#define hashs(FUNC, STR) FUNC(0xFFFFFFFF, STR, cptr_length(STR)) +uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len); uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len); -#define hashb_crc32(BUF, LEN) hash_crc32(0xFFFFFFFF, BUF, LEN) -#define hashs_crc32(STR) hash_crc32(0xFFFFFFFF, STR, cptr_length(STR)) #if __cplusplus } diff --git a/tests/test_hash_functions.c b/tests/test_hash_functions.c index b1ec0b7..46a9658 100644 --- a/tests/test_hash_functions.c +++ b/tests/test_hash_functions.c @@ -2,18 +2,18 @@ #include "../src/Hashtable/hash.h" #include "../src/Autoarr/Autoarr.h" -#define AMOUNT_OF_TESTS 400000 +#define AMOUNT_OF_TESTS 10000 -char __buf[]="iojihiojopijiugbjmoihftytryfdrh"; +char data[]="iojihiojopijiugbjmoihftytryfdrh"; #define test_hashfunc(hasht, hashf, tests_n)\ optime(#hashf,1,({\ printf("\e[94mfunction: \e[92m" #hashf "\n");\ - printf("\e[94mhash of \"%s\": \e[92m%x\n",__buf, hashf(__buf,cptr_length(__buf)));\ + printf("\e[94mhash of \"%s\": \e[92m%x\n",data, hashs(hashf,data));\ Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768);\ uint32 collisions=0;\ for(uint32 i=0;i