universal hashb() and hashs() macros

This commit is contained in:
2022-06-10 18:44:10 +03:00
parent e63ce573ff
commit 104dde3f74
3 changed files with 12 additions and 16 deletions

View File

@@ -39,12 +39,10 @@ void Hashtable_expand(Hashtable* ht){
uint32 arlen=Autoarr_length(ar);
for(uint32 k=0;k<arlen;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_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;
}

View File

@@ -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
}