hashtable passes the test!

This commit is contained in:
Timerix22 2022-02-17 00:52:41 +03:00
parent c7a3fb10b5
commit a8dd8de77b
4 changed files with 24 additions and 12 deletions

View File

@ -28,14 +28,27 @@ void Hashtable_free(Hashtable* ht){
uint32 Hashtable_height(Hashtable* ht){ return HT_HEIGHTS[ht->hein]; } uint32 Hashtable_height(Hashtable* ht){ return HT_HEIGHTS[ht->hein]; }
Autoarr2(KeyValuePair)* getrow(Hashtable* ht, char* key){
uint16 rown=HT_HEIGHTS[ht->hein]%ihash(key); void Hashtable_resize(Hashtable* ht){
if(rown>=HT_HEIGHTS[ht->hein]){ Autoarr2(KeyValuePair)* newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr2(KeyValuePair)));
ht->rows=realloc(ht->rows,HT_HEIGHTS[++ht->hein]*sizeof(Autoarr2(KeyValuePair))); for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
for(uint16 i=HT_HEIGHTS[ht->hein-1];i<HT_HEIGHTS[ht->hein];i++)
ht->rows[i]=Autoarr2_create(KeyValuePair,4,16); ht->rows[i]=Autoarr2_create(KeyValuePair,4,16);
for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
Autoarr2(KeyValuePair)* ar=ht->rows+i;
for(uint16 k=0;k<Autoarr2_length(ar);k++){
KeyValuePair p=Autoarr2_get(ar,k);
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein];
Autoarr2(KeyValuePair)* newar=newrows+newrown;
Autoarr2_add(newar,p);
} }
}
ht->rows=newrows;
}
Autoarr2(KeyValuePair)* getrow(Hashtable* ht, char* key){
uint16 rown=ihash(key)%HT_HEIGHTS[ht->hein];
if(rown>=HT_HEIGHTS[ht->hein])
Hashtable_resize(ht);
return ht->rows+rown; return ht->rows+rown;
} }

View File

@ -9,11 +9,10 @@ uint32 mystrlen(char* str){
//allocates new char[] and copies src there //allocates new char[] and copies src there
char* mystrcpy(char* src){ char* mystrcpy(char* src){
uint32 len=mystrlen(src);dbg(len); uint32 len=mystrlen(src);
char* dst=malloc(len*sizeof(char)); char* dst=malloc(len*sizeof(char));
while(len-->0) while(len-->0)
dst[len]=src[len]; dst[len]=src[len];
printf("dst: %s\n",dst);
return dst; return dst;
} }

View File

@ -6,8 +6,8 @@
int main(){ int main(){
setlocale(LC_ALL, "en-US.Unicode"); setlocale(LC_ALL, "en-US.Unicode");
printf("\e[92mdtsod parser in c language!\e[97m\n"); printf("\e[92mdtsod parser in c language!\e[97m\n");
//optime("test_all",1,{test_all();}); optime("test_all",1,{test_all();});
test_hashtable(); //test_hashtable();
printf("\e[0m\n"); printf("\e[0m\n");
return 0; return 0;
} }

View File

@ -14,7 +14,7 @@ void print_hashtable(Hashtable* ht){
void hashtable_fill(Hashtable* ht){ void hashtable_fill(Hashtable* ht){
char* key=malloc(20); char* key=malloc(20);
for(uint32 i=0;i<255;i++){ for(uint32 i=0;i<100;i++){
sprintf(key,"key__%u",i); sprintf(key,"key__%u",i);
Hashtable_add(ht,key,Uni(UInt32,i)); Hashtable_add(ht,key,Uni(UInt32,i));
} }
@ -23,7 +23,7 @@ void hashtable_fill(Hashtable* ht){
void hashtable_printval(Hashtable* ht){ void hashtable_printval(Hashtable* ht){
char* key=malloc(20); char* key=malloc(20);
for(uint32 i=0;i<255;i++){ for(uint32 i=0;i<100;i++){
sprintf(key,"key__%u",i); sprintf(key,"key__%u",i);
printuni(Hashtable_get(ht,key)); printuni(Hashtable_get(ht,key));
printf(" "); printf(" ");