stnode is a pointer hell

This commit is contained in:
Timerix22 2022-02-11 00:53:08 +03:00
parent f39efbb20d
commit 86bf4790a2
6 changed files with 49 additions and 79 deletions

View File

@ -18,7 +18,7 @@ void Hashtable_clear(Hashtable* ht){
ht->type=Null; ht->type=Null;
} }
void Hashtable_add_uni(Hashtable* ht,hash_t hash, Unitype val){ void Hashtable_add_uni(Hashtable* ht,uint32 hash, Unitype val){
} }

View File

@ -13,5 +13,3 @@ typedef struct Hashtable{
Hashtable Hashtable_create(uint16 height,my_type type); Hashtable Hashtable_create(uint16 height,my_type type);
void Hashtable_clear(Hashtable* ht); void Hashtable_clear(Hashtable* ht);
void Hashtable_add_uni(Hashtable* ht,hash_t hash, Unitype val);

View File

@ -1,3 +1,17 @@
#include "hash.h" #include "hash.h"
uint32 ihash(char *str){
uint32 hash=5381;
char c;
while(c=*(str++))
hash=((hash<<5)+hash)+c; //hash=hash*33^c
return hash;
}
uint64 lhash(char* str){
uint64 hash = 0;
int c;
while (c=*(str++))
hash=c+(hash<<6)+(hash<<16)-hash;
return hash;
}

View File

@ -2,7 +2,7 @@
#include "../base/base.h" #include "../base/base.h"
typedef uint32 hash_t;
//djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html //djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html
hash_t dhash(char *str); uint32 ihash(char *str);
//sdbm hash function
uint64 lhash(char* str);

View File

@ -9,7 +9,8 @@ STNode* STNode_create(){
return node; return node;
} }
void STNode_free(STNode* node){ uint8 nodn=0;
void STNode_free(STNode* node){dbg(0);nodn++;
if (!node) throw(ERR_NULLPTR); if (!node) throw(ERR_NULLPTR);
if(node->branches){ if(node->branches){
for(uint8 n32 = 0;n32<8;n32++){ for(uint8 n32 = 0;n32<8;n32++){
@ -30,11 +31,12 @@ void STNode_free(STNode* node){
} }
} }
free(node->branches); free(node->branches);
} }dbg(1);
//if value is not freed ptr //if value is not freed ptr
if(node->value.type>12 && node->value.type<21 && node->value.VoidPtr) if(node->value.type>12 && node->value.type<21 && node->value.VoidPtr)
free(node->value.VoidPtr); free(node->value.VoidPtr);dbg(2);printf("nodn %u\n",nodn);
free(node); printstnode(node);
free(node);dbg(3);nodn--;
} }
typedef struct {uint8 n32, n4, rem;} indexes3; typedef struct {uint8 n32, n4, rem;} indexes3;
@ -47,51 +49,34 @@ indexes3 splitindex(uint8 i){
}; };
} }
uint8 combinei3(indexes3 i3){
return i3.n32*32+i3.n4*8;
}
//int16 globytes=0;
void ST_push(STNode* node_first, const char* key, Unitype value){ void ST_push(STNode* node_first, const char* key, Unitype value){
if (!node_first) throw(ERR_NULLPTR); if (!node_first) throw(ERR_NULLPTR);
//int16 bytes=sizeof(Unitype); char c=*(key++);
char c = *key;
STNode* node_last=node_first; STNode* node_last=node_first;
for (uint16 i=0;c!='\0';){ while(c){
indexes3 i3=splitindex((uint8)c); indexes3 i3=splitindex((uint8)c);
if(!node_last->branches){ if(!node_last->branches){
node_last->branches=(STNode****)malloc(8*sizeof(STNode*)); node_last->branches=(STNode****)malloc(8*sizeof(STNode*));
//bytes+=sizeof(void*)*8;
for(uint8 i=0;i<8;i++) for(uint8 i=0;i<8;i++)
node_last->branches[i]=(STNode***)NULL; node_last->branches[i]=(STNode***)NULL;
} }
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; if(!node_last->branches[i3.n32]){
if(!ptrn32){ node_last->branches[i3.n32]=(STNode***)malloc(8*sizeof(STNode*));
ptrn32=(STNode***)malloc(8*sizeof(STNode*));
//bytes+=sizeof(void*)*8;
for(uint8 i=0;i<8;i++) for(uint8 i=0;i<8;i++)
ptrn32[i]=(STNode**)NULL; node_last->branches[i3.n32][i]=(STNode**)NULL;
node_last->branches[i3.n32]=ptrn32;
} }
STNode** ptrn4=ptrn32[i3.n4]; if(!node_last->branches[i3.n32][i3.n4]){
if(!ptrn4){ node_last->branches[i3.n32][i3.n4]=(STNode**)malloc(4*sizeof(STNode*));
ptrn4=(STNode**)malloc(4*sizeof(STNode*));
//bytes+=sizeof(void*)*4;
for(uint8 i=0;i<4;i++) for(uint8 i=0;i<4;i++)
ptrn4[i]=(STNode*)NULL; node_last->branches[i3.n32][i3.n4][i]=(STNode*)NULL;
ptrn32[i3.n4]=ptrn4; node_last->branches[i3.n32][i3.n4]=node_last->branches[i3.n32][i3.n4];
} }
node_last=ptrn4[i3.rem]; if(!node_last->branches[i3.n32][i3.n4][i3.rem])
if(!node_last){ node_last->branches[i3.n32][i3.n4][i3.rem]=STNode_create();
node_last=STNode_create(); node_last=node_last->branches[i3.n32][i3.n4][i3.rem];
//bytes+=sizeof(STNode); c=*(key++);
ptrn4[i3.rem]=node_last;
}
c=*(key+(++i));
} }
node_last->value=value; node_last->value=value;
//globytes+=bytes;
//dbg(globytes);
} }
const Unitype UnitypeNull={Null,.VoidPtr=NULL}; const Unitype UnitypeNull={Null,.VoidPtr=NULL};

View File

@ -1,59 +1,32 @@
#include "base/base.h" #include "base/base.h"
#include "tests/tests.h" #include "tests/tests.h"
#include "Autoarr/Autoarr.h" #include "Autoarr/Autoarr.h"
#include "SearchTree/SearchTree.h" #include "SearchTree/SearchTree.h"
#include "Hashtable/hash.c" #include "Hashtable/hash.h"
#define clrscr() printf("\e[1;1H\e[2J")
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");
/*test_all();
test_all();
Unitype a={Double,.Double=9}; Unitype a={Double,.Double=9};
STNode* node=STNode_create(); STNode* node=STNode_create();
ST_push(node,"type", a); ST_push(node,"type", a);
ST_push(node,"time", a); ST_push(node,"time", a);
ST_push(node,"author_id", a); ST_push(node,"author_id", a);
ST_push(node,"channel_id", a); ST_push(node,"channel_id", a);
ST_push(node,"message_id", a); ST_push(node,"message_id", a);
ST_push(node,"text", a); ST_push(node,"text", a);
ST_push(node,"files", a); ST_push(node,"files", a);
a=ST_pull(node,""); a=ST_pull(node,"");
STNode_free(node);
//STNode_free(node); printf("%u\n", ihash("0"));
printf("%lu\n", lhash("0"));
printf("%u\n", dhash("000")); printf("%u\n", ihash("1kakdkale210r"));
printf("%lu\n", lhash("1kakdkale210r"));*/
printf("%u\n", dhash("0000")); int** ptr2=NULL;
int a=4;
printf("%u\n", dhash("1111")); int* ptr=&a;
ptr2=&ptr;
printf("%p %p",ptr2, *ptr2);
return 0; return 0;
} }