universal hashb() and hashs() macros
This commit is contained in:
parent
e63ce573ff
commit
104dde3f74
@ -39,12 +39,10 @@ void Hashtable_expand(Hashtable* ht){
|
|||||||
uint32 arlen=Autoarr_length(ar);
|
uint32 arlen=Autoarr_length(ar);
|
||||||
for(uint32 k=0;k<arlen;k++){
|
for(uint32 k=0;k<arlen;k++){
|
||||||
KVPair p=Autoarr_get(ar,k);
|
KVPair p=Autoarr_get(ar,k);
|
||||||
uint16 newrown=hashs_sdbm32(p.key)%HT_HEIGHTS[ht->hein];
|
uint16 newrown=hashs(hash_sdbm32, p.key)%HT_HEIGHTS[ht->hein];
|
||||||
Autoarr(KVPair)* newar=newrows[newrown];
|
Autoarr(KVPair)* newar=newrows[newrown];
|
||||||
Autoarr_add(newar,p);
|
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);
|
Autoarr_free(ar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,10 +51,10 @@ void Hashtable_expand(Hashtable* ht){
|
|||||||
}
|
}
|
||||||
|
|
||||||
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
|
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]];
|
Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
|
||||||
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
|
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]];
|
ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
|
||||||
return ar;
|
return ar;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,13 +6,11 @@ extern "C" {
|
|||||||
|
|
||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
|
|
||||||
uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len);
|
#define hashb(FUNC, BUF, LEN) FUNC(0xFFFFFFFF, BUF, LEN)
|
||||||
#define hashb_sdbm32(BUF, LEN) hash_sdbm32(0xFFFFFFFF, BUF, LEN)
|
#define hashs(FUNC, STR) FUNC(0xFFFFFFFF, STR, cptr_length(STR))
|
||||||
#define hashs_sdbm32(STR) hash_sdbm32(0xFFFFFFFF, STR, cptr_length(STR))
|
|
||||||
|
|
||||||
|
uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len);
|
||||||
uint32 hash_crc32(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
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,18 +2,18 @@
|
|||||||
#include "../src/Hashtable/hash.h"
|
#include "../src/Hashtable/hash.h"
|
||||||
#include "../src/Autoarr/Autoarr.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)\
|
#define test_hashfunc(hasht, hashf, tests_n)\
|
||||||
optime(#hashf,1,({\
|
optime(#hashf,1,({\
|
||||||
printf("\e[94mfunction: \e[92m" #hashf "\n");\
|
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);\
|
Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768);\
|
||||||
uint32 collisions=0;\
|
uint32 collisions=0;\
|
||||||
for(uint32 i=0;i<tests_n;i++){\
|
for(uint32 i=0;i<tests_n;i++){\
|
||||||
hasht h=hashf((uint8*)&i, 4);\
|
hasht h=hashb(hashf, (uint8*)&i, 4);\
|
||||||
bool col=false;\
|
bool col=false;\
|
||||||
Autoarr_foreach(hashes,e,({\
|
Autoarr_foreach(hashes,e,({\
|
||||||
if(e==h) {\
|
if(e==h) {\
|
||||||
@ -31,7 +31,7 @@ char __buf[]="iojihiojopijiugbjmoihftytryfdrh";
|
|||||||
void test_hash_functions(){
|
void test_hash_functions(){
|
||||||
optime("test_hash_functions",1,({
|
optime("test_hash_functions",1,({
|
||||||
printf("\e[96m--------[test_hash_functions]---------\n");
|
printf("\e[96m--------[test_hash_functions]---------\n");
|
||||||
test_hashfunc(uint32, hashb_crc32, 10000);
|
test_hashfunc(uint32, hash_crc32, AMOUNT_OF_TESTS);
|
||||||
test_hashfunc(uint32, hashb_sdbm32, 10000);
|
test_hashfunc(uint32, hash_sdbm32, AMOUNT_OF_TESTS);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user