some Hashtable improvements from another branch
This commit is contained in:
parent
20a60fec60
commit
2a6ad6e207
@ -7,9 +7,7 @@ Unitype DtsodV24_get(Hashtable* dtsod, char* key){
|
||||
|
||||
// adds or sets value
|
||||
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
|
||||
Unitype* val=Hashtable_getptr(dtsod, key);
|
||||
if(val) *val=value;
|
||||
else Hashtable_add(dtsod, key, value);
|
||||
Hashtable_addOrSet(dtsod,key,value);
|
||||
}
|
||||
|
||||
// checks for dtsod contains value or dont
|
||||
|
||||
@ -53,19 +53,17 @@ void Hashtable_expand(Hashtable* ht){
|
||||
}
|
||||
|
||||
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
|
||||
Autoarr(KVPair)* ar=ht->rows[ihash(key)%HT_HEIGHTS[ht->hein]];
|
||||
uint32 hash=ihash(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)));
|
||||
ar=ht->rows[ihash(key)%HT_HEIGHTS[ht->hein]];
|
||||
ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
|
||||
return ar;
|
||||
}
|
||||
|
||||
|
||||
void Hashtable_add_pair(Hashtable* ht, KVPair p){
|
||||
Autoarr_add(getrow(ht,p.key,true),p);
|
||||
}
|
||||
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
|
||||
Hashtable_add_pair(ht,KVPair(key,u));
|
||||
KVPair p={ .key=key, .value=u };
|
||||
Autoarr_add(getrow(ht,key,true),p);
|
||||
}
|
||||
|
||||
// returns null or pointer to value in hashtable
|
||||
@ -88,18 +86,15 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
|
||||
}
|
||||
return UniNull;
|
||||
}
|
||||
KVPair Hashtable_get_pair(Hashtable* ht, char* key){
|
||||
return KVPair(key,Hashtable_get(ht,key));
|
||||
}
|
||||
|
||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
||||
Unitype u=Hashtable_get(ht,key);
|
||||
*output=u;
|
||||
return u.type!=Null;
|
||||
}
|
||||
|
||||
/* void Hashtable_set_pair(Hashtable* ht, KVPair p){
|
||||
if(Hashtable_try_get(ht,p.key, NULL)){
|
||||
|
||||
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
|
||||
Unitype* val=Hashtable_getptr(ht, key);
|
||||
if(val) *val=u;
|
||||
else Hashtable_add(ht, key, u);
|
||||
}
|
||||
}
|
||||
void Hashtable_set(Hashtable* ht, char* key, Unitype u){ Hashtable_set_pair(ht,KVPair(key,u)); } */
|
||||
|
||||
@ -19,28 +19,19 @@ void Hashtable_free(Hashtable* ht);
|
||||
// amount of rows
|
||||
uint16 Hashtable_height(Hashtable* ht);
|
||||
|
||||
// adds charptr and value to new KVPair
|
||||
// use cptr_copy() to create new string if needed
|
||||
#define KVPair(key,value) (KVPair){key,value}
|
||||
|
||||
//
|
||||
// don't add pairs with the same keys,
|
||||
// or something weird will happen
|
||||
//
|
||||
void Hashtable_add_pair(Hashtable* ht, KVPair p);
|
||||
// if not sure, use Hashtable_addOrSet()
|
||||
void Hashtable_add(Hashtable* ht, char* key, Unitype u);
|
||||
|
||||
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u);
|
||||
|
||||
// returns null or pointer to value in hashtable
|
||||
Unitype* Hashtable_getptr(Hashtable* ht, char* key);
|
||||
|
||||
Unitype Hashtable_get(Hashtable* ht, char* key);
|
||||
KVPair Hashtable_get_pair(Hashtable* ht, char* key);
|
||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
|
||||
|
||||
// not implemented yet
|
||||
void Hashtable_set_pair(Hashtable* ht, KVPair p);
|
||||
void Hashtable_set(Hashtable* ht, char* key, Unitype u);
|
||||
|
||||
#define Hashtable_foreach(HT, EL, codeblock)({\
|
||||
uint16 hmax=Hashtable_height(HT);\
|
||||
for(uint16 h=0; h<hmax; h++){\
|
||||
|
||||
@ -30,10 +30,6 @@ void print_dtsod(Hashtable* dtsod){
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
void test_dtsod(){
|
||||
optime(__func__,1,({
|
||||
printf("\e[96m-------------[test_dtsod]-------------\n");
|
||||
|
||||
@ -36,28 +36,27 @@ void printrowgraph(Hashtable* ht){
|
||||
}
|
||||
}
|
||||
|
||||
void fill(Hashtable* ht){
|
||||
for(uint32 i=0;i<100000;i++){
|
||||
char* genkey(uint32 i){
|
||||
char* key=malloc(12);
|
||||
IFWIN(
|
||||
sprintf_s(key,12,"key_%u",i),
|
||||
sprintf(key,"key_%u",i)
|
||||
);
|
||||
Hashtable_add(ht,key,Uni(UInt64,i));
|
||||
return key;
|
||||
}
|
||||
|
||||
void fill(Hashtable* ht){
|
||||
for(uint32 i=0;i<100000;i++)
|
||||
Hashtable_add(ht,genkey(i),Uni(UInt64,i));
|
||||
}
|
||||
|
||||
Unitype gett(Hashtable* ht){
|
||||
char* key=malloc(12);
|
||||
Unitype u;
|
||||
for(uint32 i=0;i<100000;i++){
|
||||
IFWIN(
|
||||
sprintf_s(key,12,"key_%u",i),
|
||||
sprintf(key,"key_%u",i)
|
||||
);
|
||||
char* key=genkey(i);
|
||||
u=Hashtable_get(ht,key);
|
||||
}
|
||||
free(key);
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user