This commit is contained in:
Timerix22 2022-02-16 20:54:50 +03:00
parent 99c06d1b66
commit c7a3fb10b5
17 changed files with 260 additions and 178 deletions

View File

@ -7,7 +7,7 @@ all: clear_c build test
clear_c: clear_c:
clear clear
clear_bin: clear_bin:
@echo -e "\e[36m-------------[clear_bin]---------------\e[0m" @echo -e '\e[96m--------------[clear_bin]--------------\e[0m'
touch $(OUTDIR)_.com touch $(OUTDIR)_.com
rm $(OUTDIR)*.com rm $(OUTDIR)*.com
@ -16,14 +16,15 @@ clang: all
CMPARGS= -Wall $(SRC) -o $(OUTFILE) CMPARGS= -Wall $(SRC) -o $(OUTFILE)
build: build:
@echo -e "\e[36m-------------[build]---------------\e[0m" @echo -e '\n\e[96m----------------[build]----------------\e[0m'
$(CMP) -O1 -flto $(CMPARGS) $(CMP) -O1 -flto $(CMPARGS)
build_dbg: build_dbg:
@echo -e "\e[36m-------------[build_dbg]---------------\e[0m" @echo -e '\n\e[96m--------------[build_dbg]--------------\e[0m'
$(CMP) -O0 $(CMPARGS).dbg $(CMP) -O0 -g $(CMPARGS).dbg
test: test:
@echo -e "\e[36m-------------[test]----------------\e[0m" @echo -e '\n\e[96m----------------[test]-----------------\e[0m'
$(OUTFILE) $(OUTFILE)
valgrind: clear_c build_dbg valgrind: clear_c build_dbg
@echo -e "\e[36m-----------[valgrind]--------------\e[0m" @echo -e '\n\e[96m--------------[valgrind]---------------\e[0m'
valgrind -s --read-var-info=yes --fullpath-after=DtsodC/ $(OUTFILE).dbg valgrind -s --read-var-info=yes --track-origins=yes --fullpath-after=DtsodC/ \
--leak-check=full --show-leak-kinds=all $(OUTFILE).dbg

View File

@ -9,6 +9,7 @@ struct Autoarr2_##type;\
typedef struct {\ typedef struct {\
void (*add)(struct Autoarr2_##type* ar, type element);\ void (*add)(struct Autoarr2_##type* ar, type element);\
type (*get)(struct Autoarr2_##type* ar, uint32 index);\ type (*get)(struct Autoarr2_##type* ar, uint32 index);\
type* (*getptr)(struct Autoarr2_##type* ar, uint32 index);\
void (*set)(struct Autoarr2_##type* ar, uint32 index, type element);\ void (*set)(struct Autoarr2_##type* ar, uint32 index, type element);\
void (*clear)(struct Autoarr2_##type* ar);\ void (*clear)(struct Autoarr2_##type* ar);\
} __functions_list_t_##type;\ } __functions_list_t_##type;\
@ -24,6 +25,7 @@ typedef struct Autoarr2_##type{\
\ \
void __Autoarr2_add_##type(Autoarr2_##type* ar, type element);\ void __Autoarr2_add_##type(Autoarr2_##type* ar, type element);\
type __Autoarr2_get_##type(Autoarr2_##type* ar, uint32 index);\ type __Autoarr2_get_##type(Autoarr2_##type* ar, uint32 index);\
type* __Autoarr2_getptr_##type(Autoarr2_##type* ar, uint32 index);\
void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element);\ void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element);\
void __Autoarr2_clear_##type(Autoarr2_##type* ar);\ void __Autoarr2_clear_##type(Autoarr2_##type* ar);\
Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_block_length); Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_block_length);
@ -34,6 +36,8 @@ Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_blo
autoarr->functions->add(autoarr, element) autoarr->functions->add(autoarr, element)
#define Autoarr2_get(autoarr, index)\ #define Autoarr2_get(autoarr, index)\
autoarr->functions->get(autoarr,index) autoarr->functions->get(autoarr,index)
#define Autoarr2_getptr(autoarr, index)\
autoarr->functions->getptr(autoarr,index)
#define Autoarr2_set(autoarr, index, element)\ #define Autoarr2_set(autoarr, index, element)\
autoarr->functions->set(autoarr, index, element) autoarr->functions->set(autoarr, index, element)
#define Autoarr2_clear(autoarr)\ #define Autoarr2_clear(autoarr)\

View File

@ -25,6 +25,11 @@ type __Autoarr2_get_##type(Autoarr2_##type* ar, uint32 index){\
return ar->values[index/ar->max_block_length][index%ar->max_block_length];\ return ar->values[index/ar->max_block_length][index%ar->max_block_length];\
}\ }\
\ \
type* __Autoarr2_getptr_##type(Autoarr2_##type* ar, uint32 index){\
if(index>=Autoarr2_length(ar)) throw(ERR_WRONGINDEX);\
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\
}\
\
void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element){\ void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element){\
if(index>=Autoarr2_length(ar)) throw(ERR_WRONGINDEX);\ if(index>=Autoarr2_length(ar)) throw(ERR_WRONGINDEX);\
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
@ -42,6 +47,7 @@ void __Autoarr2_clear_##type(Autoarr2_##type* ar){\
__functions_list_t_##type __functions_list_##type={\ __functions_list_t_##type __functions_list_##type={\
&__Autoarr2_add_##type,\ &__Autoarr2_add_##type,\
&__Autoarr2_get_##type,\ &__Autoarr2_get_##type,\
&__Autoarr2_getptr_##type,\
&__Autoarr2_set_##type,\ &__Autoarr2_set_##type,\
&__Autoarr2_clear_##type\ &__Autoarr2_clear_##type\
};\ };\

View File

@ -2,16 +2,34 @@
define_Autoarr2(KeyValuePair) define_Autoarr2(KeyValuePair)
// amount of rows
static const uint16 HT_HEIGHTS[]={61,631,3889,19441,65521};
Hashtable* Hashtable_create(){ Hashtable* Hashtable_create(){
Hashtable* ht=malloc(sizeof(Hashtable)); Hashtable* ht=malloc(sizeof(Hashtable));
ht->hein=HT_HEIGHTS[0]; ht->hein=0;
ht->rows=malloc(HT_HEIGHTS[0]*sizeof(Autoarr2(KeyValuePair))); ht->rows=malloc(HT_HEIGHTS[0]*sizeof(Autoarr2(KeyValuePair)));
for(uint16 i;i<HT_HEIGHTS[0];i++) for(uint16 i=0;i<HT_HEIGHTS[0];i++)
ht->rows[i]=Autoarr2_create(KeyValuePair,4,16); ht->rows[i]=Autoarr2_create(KeyValuePair,4,16);
return ht; return ht;
} }
static Autoarr2(KeyValuePair)* getrow(Hashtable* ht,char* key){ void Hashtable_free(Hashtable* ht){
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++){
Autoarr2(KeyValuePair)* ar=(Autoarr2(KeyValuePair)*)(ht->rows+i);
for(uint32 i=0;i<Autoarr2_length(ar);i++)
free(Autoarr2_getptr(ar,i)->key);
Autoarr2_clear(ar);
}
free(ht->rows);
free(ht);
}
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); uint16 rown=HT_HEIGHTS[ht->hein]%ihash(key);
if(rown>=HT_HEIGHTS[ht->hein]){ if(rown>=HT_HEIGHTS[ht->hein]){
ht->rows=realloc(ht->rows,HT_HEIGHTS[++ht->hein]*sizeof(Autoarr2(KeyValuePair))); ht->rows=realloc(ht->rows,HT_HEIGHTS[++ht->hein]*sizeof(Autoarr2(KeyValuePair)));
@ -21,47 +39,28 @@ static Autoarr2(KeyValuePair)* getrow(Hashtable* ht,char* key){
return ht->rows+rown; return ht->rows+rown;
} }
void Hashtable_free(Hashtable* ht){ //copies string and value to new KeyValuePair
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++) KeyValuePair cpair(char* key, Unitype value){
Autoarr2_clear(((Autoarr2(KeyValuePair)*)(ht->rows+i))); return (KeyValuePair){.key=mystrcpy(key),.value=value};
free(ht->rows);
free(ht);
} }
void Hashtable_add_pair(Hashtable* ht, KeyValuePair pair){
Autoarr2_add(getrow(ht,pair.key),pair); void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){
Autoarr2_add(getrow(ht,p.key),p);
}
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
Hashtable_add_pair(ht,cpair(key,u));
} }
//returns length of string (including \0) //returns null or pointer to value in hashtable
uint32 mystrlen(char* str){ Unitype* Hashtable_getptr(Hashtable* ht, char* key){
uint32 len=0; Autoarr2(KeyValuePair)* ar=getrow(ht,key);
while(*(str++)) len++; uint32 arlen=Autoarr2_length(ar);
return len++; for(uint32 i=0;i<arlen;i++){
KeyValuePair* p=Autoarr2_getptr(ar,i);
if(mystrcmp(key,p->key)) return &p->value;
} }
return NULL;
//allocates new char[] and copies src there
char* mystrcpy(char* src){
uint32 len=mystrlen(src);
char* dst=malloc(len*sizeof(char));
while(--len<0)
dst[len]=src[len];
return dst;
}
//compares two strings, NullPtr-friendly
bool mystrcmp(char* key0, char* key1){
if(!key0) return key1 ? 0 : 1;
else if(!key1) return 0;
while(*key0&&*key1){
if(*key0!=*key1) return 0;
key0++;
key1++;
}
return 1;
}
void Hashtable_add(Hashtable* ht, char* key, Unitype value){
Hashtable_add_pair(ht,(KeyValuePair){key,value});
} }
Unitype Hashtable_get(Hashtable* ht, char* key){ Unitype Hashtable_get(Hashtable* ht, char* key){
@ -73,16 +72,18 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
} }
return (Unitype){.type=Null,.VoidPtr=NULL}; return (Unitype){.type=Null,.VoidPtr=NULL};
} }
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){ KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){
return (KeyValuePair){ return cpair(key,Hashtable_get(ht,key));
.key=key,
.value=Hashtable_get(ht,key)
};
} }
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
Unitype u=Hashtable_get(ht,key); Unitype u=Hashtable_get(ht,key);
*output=u; *output=u;
return u.type==Null; return u.type==Null;
} }
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){
if(Hashtable_try_get(ht,p.key, NULL)){
}
}
void Hashtable_set(Hashtable* ht, char* key, Unitype u){ Hashtable_set_pair(ht,cpair(key,u)); }

View File

@ -9,6 +9,9 @@ typedef struct KeyValuePair{
Unitype value; Unitype value;
} KeyValuePair; } KeyValuePair;
//copies string and value to new KeyValuePair
KeyValuePair cpair(char* key, Unitype value);
declare_Autoarr2(KeyValuePair) declare_Autoarr2(KeyValuePair)
typedef struct Hashtable{ typedef struct Hashtable{
@ -16,5 +19,24 @@ typedef struct Hashtable{
Autoarr2(KeyValuePair)* rows; // Autoarr[height] Autoarr2(KeyValuePair)* rows; // Autoarr[height]
} Hashtable; } Hashtable;
// amount of rows Hashtable* Hashtable_create();
const uint16 HT_HEIGHTS[]={61,631,3889,19441,65521}; void Hashtable_free(Hashtable* ht);
uint32 Hashtable_height(Hashtable* ht);
//copies string and value to new KeyValuePair
KeyValuePair cpair(char* key, Unitype value);
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_add(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);
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key);
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_set(Hashtable* ht, char* key, Unitype u);

View File

@ -3,7 +3,7 @@
uint32 ihash(char *str){ uint32 ihash(char *str){
uint32 hash=5381; uint32 hash=5381;
for (char c=*str;c;c=*(++str)) for (char c=*str;c;c=*(++str))
hash=((hash<<5)+hash)+c; //hash=hash*33^c hash=((hash<<5)+hash)+c;
return hash; return hash;
} }

View File

@ -3,6 +3,7 @@
#include "std.h" #include "std.h"
#include "types.h" #include "types.h"
#include "errors.h" #include "errors.h"
#include "mystr.h"
// sleep function based on std nanosleep() // sleep function based on std nanosleep()
void fsleep(float sec); void fsleep(float sec);

30
DtsodC/src/base/mystr.c Normal file
View File

@ -0,0 +1,30 @@
#include "base.h"
//returns length of string (including \0)
uint32 mystrlen(char* str){
uint32 len=0;
while(*(str++)) len++;
return ++len;
}
//allocates new char[] and copies src there
char* mystrcpy(char* src){
uint32 len=mystrlen(src);dbg(len);
char* dst=malloc(len*sizeof(char));
while(len-->0)
dst[len]=src[len];
printf("dst: %s\n",dst);
return dst;
}
//compares two strings, NullPtr-friendly
bool mystrcmp(char* key0, char* key1){
if(!key0) return key1 ? 0 : 1;
else if(!key1) return 0;
while(*key0&&*key1){
if(*key0!=*key1) return 0;
key0++;
key1++;
}
return 1;
}

14
DtsodC/src/base/mystr.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "types.h"
//returns length of string (including \0)
uint32 mystrlen(char* str);
//allocates new char[] and copies src there
char* mystrcpy(char* src);
//compares two strings, NullPtr-friendly
bool mystrcmp(char* key0, char* key1);

View File

@ -40,3 +40,5 @@ typedef struct Unitype{
void* VoidPtr; void* VoidPtr;
}; };
} Unitype; } Unitype;
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}

View File

@ -1,10 +1,13 @@
#include "base/base.h" #include "base/base.h"
#include "tests/tests.h" #include "tests/tests.h"
#include "Autoarr/Autoarr2.h" #include "Autoarr/Autoarr2.h"
#include "Hashtable/Hashtable.h"
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();}); //optime("test_all",1,{test_all();});
test_hashtable();
printf("\e[0m\n");
return 0; return 0;
} }

View File

@ -1,55 +0,0 @@
#pragma once
#include "../base/base.h"
const wchar_t* slimak =
U" ▄▄▄ \n"
"▄▀░▄░▀▄ \n"
"█░█▄▀░█ \n"
"█░▀▄▄▀█▄█▄▀ \n"
"▄▄█▄▄▄▄███▀ \n"
" ▄▄▄ \n"
" █▀▄▄░▀█ \n"
"█░█░▄▀░░█ \n"
"█░▀▄▄▄▀░█▄█▄▄▀ \n"
"▄▄█▄▄▄▄███▀▀ \n"
" ▄▄▄ \n"
" █▀▄▄░▀█ \n"
"█░█░▄▀░░█ \n"
"█░▀▄▄▄▀░█▄█▄▄▀ \n"
"▄▄█▄▄▄▄████▀▀ \n"
" ▄▄▄ \n"
" ▄▀░▄░▀▄ \n"
" █░█▄▀░█ \n"
" █░▀▄▄▀█▄█▄▀ \n"
" ▄▄█▄▄▄▄███▀ \n"
" ▄▄▄ \n"
" █▀▄▄░▀█ \n"
" █░█░▄▀░░█ \n"
" █░▀▄▄▄▀░█▄█▄▄▀ \n"
" ▄▄█▄▄▄▄███▀▀ \n"
" ▄▄▄ \n"
" █▀▄▄░▀█ \n"
" █░█░▄▀░░█ \n"
" █░▀▄▄▄▀░█▄█▄▄▀ \n"
" ▄▄█▄▄▄▄████▀▀ \n"
" ▄▄▄ \n"
" ▄▀░▄░▀▄ \n"
" █░█▄▀░█ \n"
" █░▀▄▄▀█▄█▄▀ \n"
" ▄▄█▄▄▄▄███▀ \n"
" ▄▄▄ \n"
" █▀▄▄░▀█ \n"
" █░█░▄▀░░█ \n"
" █░▀▄▄▄▀░█▄█▄▄▀\n"
" ▄▄█▄▄▄▄███▀▀ \n"
" ▄▄▄ \n"
" █▀▄▄░▀█ \n"
" █░█░▄▀░░█ \n"
" █░▀▄▄▄▀░█▄█▄▄▀\n"
" ▄▄█▄▄▄▄████▀▀ \n"
" ▄▄▄ \n"
" ▄▀░▄░▀▄ \n"
" █░█▄▀░█ \n"
" █░▀▄▄▀█▄█▄▀\n"
" ▄▄█▄▄▄▄███▀ \n";

View File

@ -2,7 +2,7 @@
#include "../Autoarr/Autoarr2.h" #include "../Autoarr/Autoarr2.h"
static void printautoarr(Autoarr2(uint16)* ar){ static void printautoarr(Autoarr2(uint16)* ar){
printf("\e[94mAUTOARR:%lu\n" printf("\e[94mAutoarr2(uint16): %lu\n"
" max_blocks_count: %u\n" " max_blocks_count: %u\n"
" blocks_count: %u\n" " blocks_count: %u\n"
" max_block_length: %u\n" " max_block_length: %u\n"
@ -35,7 +35,8 @@ static void printallval(Autoarr2(uint16)* ar){
} }
void test_autoarr2(){ void test_autoarr2(){
printf("\e[96m------------[test_autoarr2]-------------\n"); optime("test_autoarr2",1,({
printf("\e[96m------------[test_autoarr2]------------\n");
Autoarr2(uint16) ar=Autoarr2_create(uint16,10,16); Autoarr2(uint16) ar=Autoarr2_create(uint16,10,16);
printf("\e[92mautoarr created\n"); printf("\e[92mautoarr created\n");
fillar(&ar); fillar(&ar);
@ -47,4 +48,5 @@ void test_autoarr2(){
printallval(&ar); printallval(&ar);
Autoarr2_clear(((&ar))); Autoarr2_clear(((&ar)));
printf("\e[92mautoarr cleared\n"); printf("\e[92mautoarr cleared\n");
}));
} }

View File

@ -0,0 +1,46 @@
#include "tests.h"
#include "../Hashtable/Hashtable.h"
void print_hashtable(Hashtable* ht){
printf("\e[94mHashtable:%lu\n"
" hein: %u\n"
" height: %u\n"
" rows: %p\n",
sizeof(Hashtable),
ht->hein,
Hashtable_height(ht),
ht->rows);
}
void hashtable_fill(Hashtable* ht){
char* key=malloc(20);
for(uint32 i=0;i<255;i++){
sprintf(key,"key__%u",i);
Hashtable_add(ht,key,Uni(UInt32,i));
}
free(key);
}
void hashtable_printval(Hashtable* ht){
char* key=malloc(20);
for(uint32 i=0;i<255;i++){
sprintf(key,"key__%u",i);
printuni(Hashtable_get(ht,key));
printf(" ");
}
free(key);
}
void test_hashtable(void){
optime("test_hashtable",1,({
printf("\e[96m-----------[test_hashtable]------------\n");
Hashtable* ht=Hashtable_create();
print_hashtable(ht);
hashtable_fill(ht);
printf("\e[92mhashtable filled\n\e[90m");
hashtable_printval(ht);
Hashtable_free(ht);
printf("\n\e[92mhashtable freed\n");
}));
}

View File

@ -20,6 +20,7 @@ void printstnode(STNode* node){
} }
void test_searchtree(){ void test_searchtree(){
optime("test_searchtree",1,({
printf("\e[96m-----------[test_searchtree]-----------\n"); printf("\e[96m-----------[test_searchtree]-----------\n");
STNode* node=STNode_create(); STNode* node=STNode_create();
printf("\e[92mnode created\n"); printf("\e[92mnode created\n");
@ -66,4 +67,5 @@ void test_searchtree(){
printstnode(node); printstnode(node);
STNode_free(node); STNode_free(node);
printf("\e[92mnode deleted\n"); printf("\e[92mnode deleted\n");
}));
} }

View File

@ -29,7 +29,8 @@ void printuni(Unitype v){
} }
void test_all(void){ void test_all(void){
optime({test_searchtree();}); test_searchtree();
optime({test_autoarr2();}); test_autoarr2();
printf("\e[96m---------------------------------------\n"); test_hashtable();
printf("\e[96m---------------------------------------\e[0m\n");
} }

View File

@ -7,12 +7,14 @@ void printuni(Unitype v);
void test_all(void); void test_all(void);
void test_searchtree(void); void test_searchtree(void);
void test_autoarr2(void); void test_autoarr2(void);
void test_hashtable(void);
// executes codeblock and prints execution time // executes codeblock and prints execution time
// should be used like optime({foo();}), because just optime(foo()) works slower // should be used like optime({foo();}), because just optime(foo()) works slower
#define optime(codeblock) ({\ #define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\ clock_t start=clock();\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
(codeblock);\ (codeblock);\
clock_t stop=clock();\ clock_t stop=clock();\
printf("\e[93m%li\n",(stop-start));\ printf("\e[93moperation %s took \e[94m%ld \e[93mticks\n",opname,(stop-start));\
}) })