From a6f83bab34adddd0742e149f8947f64dc91d2f4d Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sun, 6 Feb 2022 22:42:45 +0300 Subject: [PATCH] search tree is almost done --- DtsodC/Makefile | 7 ++- DtsodC/src/Autoarr/Autoarr.c | 4 +- DtsodC/src/DtsodParser/DtsodV24.c | 0 DtsodC/src/DtsodParser/DtsodV24.h | 0 DtsodC/src/SearchTree/SearchTree.c | 80 ++++++++++++++++++++------ DtsodC/src/SearchTree/SearchTree.h | 18 +++++- DtsodC/src/base/base.h | 11 +++- DtsodC/src/base/base_types.c | 12 ++++ DtsodC/src/base/base_types.h | 4 +- DtsodC/src/base/errors.h | 4 -- DtsodC/src/base/hash.c | 9 +++ DtsodC/src/base/{nsleep.c => msleep.c} | 5 +- DtsodC/src/main.c | 15 ++++- DtsodC/src/tests/test_all.c | 1 + DtsodC/src/tests/test_autoarr.c | 30 +++++----- DtsodC/src/tests/test_searchtree.c | 58 ++++++++++++++----- DtsodC/src/tests/tests.h | 6 ++ 17 files changed, 200 insertions(+), 64 deletions(-) create mode 100644 DtsodC/src/DtsodParser/DtsodV24.c create mode 100644 DtsodC/src/DtsodParser/DtsodV24.h create mode 100644 DtsodC/src/base/hash.c rename DtsodC/src/base/{nsleep.c => msleep.c} (52%) diff --git a/DtsodC/Makefile b/DtsodC/Makefile index 347f85f..aff39b5 100644 --- a/DtsodC/Makefile +++ b/DtsodC/Makefile @@ -8,17 +8,20 @@ std: clear_c std_build std_test clear_c: clear clear_bin: - @echo "\e[36m-----------------[clear_bin]-------------------\e[0m" + @echo "\e[36m-------------[clear_bin]---------------\e[0m" touch $(OUTDIR)_.com rm $(OUTDIR)*.com +clang: CMP=clang +clang: all + # using sdlib STDARGS=-D STDLIB -O std_build: @echo "\e[36m-------------[std_build]---------------\e[0m" $(CMP) $(STDARGS) $(SRC) -o $(OUTDIR)std_$(OUTFILE) std_test: - @echo "\e[36m----------------[std_test]------------------\e[0m" + @echo "\e[36m-------------[std_test]----------------\e[0m" $(OUTDIR)std_$(OUTFILE) # using cosmopolitan diff --git a/DtsodC/src/Autoarr/Autoarr.c b/DtsodC/src/Autoarr/Autoarr.c index d4cc9e1..788d15e 100644 --- a/DtsodC/src/Autoarr/Autoarr.c +++ b/DtsodC/src/Autoarr/Autoarr.c @@ -18,9 +18,7 @@ Autoarr Autoarr_create(uint16 _max_block_count, uint16 _max_block_length, base_t // creates new block if the current one is filled void __Autoarr_create_block(Autoarr *ar){ if (ar->curr_block_count>=ar->max_block_count) throw(ERR_MAXLENGTH); - //if (ar->curr_block_length==ar->max_block_length) - ar->curr_block_length=0; - //else throw("current block isn't filled"); + ar->curr_block_length=0; *(ar->values+ar->curr_block_count)=malloc(ar->max_block_length*typesize(ar->type)); ar->curr_block_count++; } diff --git a/DtsodC/src/DtsodParser/DtsodV24.c b/DtsodC/src/DtsodParser/DtsodV24.c new file mode 100644 index 0000000..e69de29 diff --git a/DtsodC/src/DtsodParser/DtsodV24.h b/DtsodC/src/DtsodParser/DtsodV24.h new file mode 100644 index 0000000..e69de29 diff --git a/DtsodC/src/SearchTree/SearchTree.c b/DtsodC/src/SearchTree/SearchTree.c index d2e4180..7033f64 100644 --- a/DtsodC/src/SearchTree/SearchTree.c +++ b/DtsodC/src/SearchTree/SearchTree.c @@ -1,16 +1,16 @@ #include "SearchTree.h" + #include "../tests/tests.h" STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); node->branches=NULL; - node->value.ptr=NULL; node->value.type=Null; + node->value.UInt64=0; return node; } - void STNode_free(STNode* node){ - ifNthrow(node); + if (!node) throw(ERR_NULLPTR); if(node->branches!=NULL){ for(uint8 n32 = 0;n32<8;n32++){ STNode*** ptrn32=(STNode***)node->branches[n32]; @@ -33,7 +33,8 @@ void STNode_free(STNode* node){ } free(node->branches); } - free(node->value.ptr); + if(node->value.type==UInt8Ptr|Int8Ptr) + free(node->value.VoidPtr); free(node); } @@ -51,22 +52,65 @@ uint8 combinei3(indexes3 i3){ return i3.n32*32+i3.n4*8; } - -// returns NULL or *STNode corresponding to the character -STNode* getcnode(STNode* node, uint8 c){ - indexes3 i3=splitindex(c); - ifNretN(node->branches); - STNode*** ptrn32=(STNode***)node->branches[i3.n32]; - ifNretN(ptrn32); - STNode** ptrn4=ptrn32[i3.n4]; - ifNretN(ptrn4); - return ptrn4[i3.rem]; -} - -void ST_push(STNode* node, const char* key, Unitype value){ +int16 globytes=0; +void ST_push(STNode* node_first, const char* key, Unitype value){ + if (!node_first) throw(ERR_NULLPTR); + int16 bytes=sizeof(Unitype); char c = *key; + STNode* node_last=node_first; for (uint16 i=0;c!='\0';){ - printf("[%u]%c ",i,c); + indexes3 i3=splitindex((uint8)c); + if(!node_last->branches){ + node_last->branches=(STNode****)malloc(8*sizeof(STNode*)); + bytes+=sizeof(void*)*8; + for(uint8 i=0;i<8;i++) + node_last->branches[i]=(STNode***)NULL; + } + STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; + if(!ptrn32){ + ptrn32=(STNode***)malloc(8*sizeof(STNode*)); + bytes+=sizeof(void*)*8; + for(uint8 i=0;i<8;i++) + ptrn32[i]=(STNode**)NULL; + node_last->branches[i3.n32]=ptrn32; + } + STNode** ptrn4=ptrn32[i3.n4]; + if(!ptrn4){ + ptrn4=(STNode**)malloc(4*sizeof(STNode*)); + bytes+=sizeof(void*)*4; + for(uint8 i=0;i<4;i++) + ptrn4[i]=(STNode*)NULL; + ptrn32[i3.n4]=ptrn4; + } + node_last=ptrn4[i3.rem]; + if(!node_last){ + node_last=STNode_create(); + bytes+=sizeof(STNode); + ptrn4[i3.rem]=node_last; + } c=*(key+(++i)); } + node_last->value=value; + globytes+=bytes; + dbg(globytes); +} + +const Unitype UnitypeNull={Null,.VoidPtr=NULL}; + +Unitype ST_pull(STNode* node_first, const char* key){ + if (!node_first) throw(ERR_NULLPTR); + char c = *key; + STNode* node_last=node_first; + for (uint16 i=0;c!='\0';){ + indexes3 i3=splitindex((uint8)c); + if(!node_last->branches) return UnitypeNull; + STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; + if(!ptrn32) return UnitypeNull; + STNode** ptrn4=ptrn32[i3.n4]; + if(!ptrn4) return UnitypeNull; + node_last=ptrn4[i3.rem]; + if(!node_last) return UnitypeNull; + c=*(key+(++i)); + } + return node_last->value; } diff --git a/DtsodC/src/SearchTree/SearchTree.h b/DtsodC/src/SearchTree/SearchTree.h index 11cc814..14c28e2 100644 --- a/DtsodC/src/SearchTree/SearchTree.h +++ b/DtsodC/src/SearchTree/SearchTree.h @@ -2,9 +2,22 @@ #include "../base/base.h" +// can store any base type typedef struct UniversalType{ base_type type; - void* ptr; + union { + int8 Int8; + uint8 UInt8; + int16 Int16; + uint16 UInt16; + int32 Int32; + uint32 UInt32; + int64 Int64; + uint64 UInt64; + float Float; + double Double; + void* VoidPtr; + }; } Unitype; typedef struct SearchTreeNode{ @@ -13,7 +26,8 @@ typedef struct SearchTreeNode{ } STNode; STNode* STNode_create(void); +//doesn't work! void STNode_free(STNode* node); void ST_push(STNode* node, const char* key, Unitype value); -Unitype ST_pull(STNode* node, const char* key); \ No newline at end of file +Unitype ST_pull(STNode* node, const char* key); diff --git a/DtsodC/src/base/base.h b/DtsodC/src/base/base.h index 59f6d8a..21e382d 100644 --- a/DtsodC/src/base/base.h +++ b/DtsodC/src/base/base.h @@ -1,3 +1,12 @@ +#pragma once + #include "std.h" #include "base_types.h" -#include "errors.h" \ No newline at end of file +#include "errors.h" + +// just sleeping function +// dont set 'milisec' > 1000 for good perfomance +void msleep(uint8 sec, uint16 milisec); + +//djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html +uint32 hash(char *str); \ No newline at end of file diff --git a/DtsodC/src/base/base_types.c b/DtsodC/src/base/base_types.c index 3ee7f65..3e6d34a 100644 --- a/DtsodC/src/base/base_types.c +++ b/DtsodC/src/base/base_types.c @@ -5,6 +5,10 @@ const char* typename(base_type t){ switch (t) { case Null: return "Null"; + case Double: return "Double"; + case Float: return "Float"; + case Bool: return "Bool"; + case Char: return "Char"; case Int8: return "Int8"; case UInt8: return "UInt8"; case Int16: return "Int16"; @@ -13,6 +17,14 @@ const char* typename(base_type t){ case UInt32: return "UInt32"; case Int64: return "Int64"; case UInt64: return "UInt64"; + case Int8Ptr: return "Int8Ptr"; + case UInt8Ptr: return "UInt8Ptr"; + case Int16Ptr: return "Int16Ptr"; + case UInt16Ptr: return "UInt16Ptr"; + case Int32Ptr: return "Int32Ptr"; + case UInt32Ptr: return "UInt32Ptr"; + case Int64Ptr: return "Int64Ptr"; + case UInt64Ptr: return "UInt64Ptr"; default: throw(ERR_WRONGTYPE); return "EEEEEE"; } } diff --git a/DtsodC/src/base/base_types.h b/DtsodC/src/base/base_types.h index 40910fc..5882fda 100644 --- a/DtsodC/src/base/base_types.h +++ b/DtsodC/src/base/base_types.h @@ -12,7 +12,9 @@ typedef uint32_t uint32; typedef int64_t int64; typedef uint64_t uint64; typedef enum base_type{ - Null, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Char, Bool + Null, Float, Double, Char, Bool, + UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, + UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr } __attribute__ ((__packed__)) base_type; const char* typename(base_type t); diff --git a/DtsodC/src/base/errors.h b/DtsodC/src/base/errors.h index 13100f6..2ca7919 100644 --- a/DtsodC/src/base/errors.h +++ b/DtsodC/src/base/errors.h @@ -14,7 +14,3 @@ void _throwstr(const char* errmesg, const char* srcfile, int line, const char* f CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \ CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \ printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__))) - -#define ifNthrow(PTR) if (!PTR) throw(ERR_NULLPTR) -#define ifNretN(PTR) if (!PTR) return NULL -#define ifNret(PTR) if (!PTR) return \ No newline at end of file diff --git a/DtsodC/src/base/hash.c b/DtsodC/src/base/hash.c new file mode 100644 index 0000000..da5ca47 --- /dev/null +++ b/DtsodC/src/base/hash.c @@ -0,0 +1,9 @@ +#include "base.h" + +uint32 hash(char *str){ + uint32 hash=5381; + char c; + while (c=*str++) + hash=((hash << 5) + hash) + c; //hash=hash*33^c + return hash; +} \ No newline at end of file diff --git a/DtsodC/src/base/nsleep.c b/DtsodC/src/base/msleep.c similarity index 52% rename from DtsodC/src/base/nsleep.c rename to DtsodC/src/base/msleep.c index 99a5c29..cfb1c23 100644 --- a/DtsodC/src/base/nsleep.c +++ b/DtsodC/src/base/msleep.c @@ -1,7 +1,6 @@ -#include "std.h" -#include "base_types.h" +#include "base.h" -void nsleep(uint8 sec, uint8 milisec){ +void msleep(uint8 sec, uint16 milisec){ if (sec>0) sleep(sec); if (milisec>0) diff --git a/DtsodC/src/main.c b/DtsodC/src/main.c index 7164e6c..58c8f89 100644 --- a/DtsodC/src/main.c +++ b/DtsodC/src/main.c @@ -9,8 +9,19 @@ int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); test_all(); - Unitype a={Null,NULL}; + /*Unitype a={Double,.Double=9}; STNode* node=STNode_create(); - ST_push(node,"aboba", a); + ST_push(node,"type", a); + ST_push(node,"time", a); + ST_push(node,"author_id", a); + ST_push(node,"channel_id", a); + ST_push(node,"message_id", a); + ST_push(node,"text", a); + ST_push(node,"files", a); + a=ST_pull(node,"");*/ + //STNode_free(node); + printf("%u\n", hash("000")); + printf("%u\n", hash("0000")); + printf("%u\n", hash("1111")); return 0; } diff --git a/DtsodC/src/tests/test_all.c b/DtsodC/src/tests/test_all.c index 3131a58..bccdd60 100644 --- a/DtsodC/src/tests/test_all.c +++ b/DtsodC/src/tests/test_all.c @@ -3,4 +3,5 @@ void test_all(void){ test_autoarr(); test_searchtree(); + printf("\e[96m---------------------------------------\n"); } \ No newline at end of file diff --git a/DtsodC/src/tests/test_autoarr.c b/DtsodC/src/tests/test_autoarr.c index 0202480..83e5a89 100644 --- a/DtsodC/src/tests/test_autoarr.c +++ b/DtsodC/src/tests/test_autoarr.c @@ -1,15 +1,15 @@ #include "tests.h" #include "../Autoarr/Autoarr.h" -void ardesc(Autoarr* ar){ - printf("\e[94m AUTOARR:%lu\n" - " type: %s\n" - " max_block_count: %u\n" - " curr_block_count: %u\n" - " max_block_length: %u\n" - " curr_block_length: %u\n" - " max_length: %u\n" - " curr_length: %u\n", +void printautoarr(Autoarr* ar){ + printf("\e[94mAUTOARR:%lu\n" + " type: %s\n" + " max_block_count: %u\n" + " curr_block_count: %u\n" + " max_block_length: %u\n" + " curr_block_length: %u\n" + " max_length: %u\n" + " curr_length: %u\n", sizeof(Autoarr), typename(ar->type), ar->max_block_count, @@ -25,7 +25,7 @@ void fillar(Autoarr* ar){ Autoarr_add_uint16(ar,i); } -void printar(Autoarr* ar){ +void printallval(Autoarr* ar){ for (uint16 i=0;imax_length;i++) printf("%u ", Autoarr_get_uint16(ar,i)); } @@ -33,11 +33,11 @@ void printar(Autoarr* ar){ void test_autoarr(){ printf("\e[96m------------[test_autoarr]-------------\n"); Autoarr ar=Autoarr_create(10,16,UInt16); - printf("\e[92m autoarr created\n\e[90m"); + printf("\e[92mautoarr created\n\e[90m"); fillar(&ar); - printar(&ar); - printf("\n\e[92m autoarr filled up\n"); - ardesc(&ar); + printallval(&ar); + printf("\n\e[92mautoarr filled up\n"); + printautoarr(&ar); Autoarr_clear(&ar); - printf("\e[92m autoarr cleared\n"); + printf("\e[92mautoarr cleared\n"); } diff --git a/DtsodC/src/tests/test_searchtree.c b/DtsodC/src/tests/test_searchtree.c index 5fb57da..a3d4efd 100644 --- a/DtsodC/src/tests/test_searchtree.c +++ b/DtsodC/src/tests/test_searchtree.c @@ -1,23 +1,55 @@ #include "tests.h" #include "../SearchTree/SearchTree.h" -void printn(STNode* node){ - printf("\e[94m STNode: %lu\n" - " branches: %p\n" - " value.type: %s\n" - " value.ptr: %p\n", - sizeof(STNode), - node->branches, - typename(node->value.type), - node->value.ptr - ); +void printuni(Unitype v){ + switch (v.type) { + case Null: printf("{%s}",typename(v.type));break; + case Double: printf("{%s:%lf}",typename(v.type),v.Double);break; + case Float: printf("{%s:%f}",typename(v.type),v.Float);break; + case Char: printf("{%s:%c}",typename(v.type),v.Int8);break; + case UInt8: + case UInt16: printf("{%s:%u}",typename(v.type),v.UInt16);break; + case UInt32: + case UInt64: printf("{%s:%lu}",typename(v.type),v.UInt64);break; + case Bool: + case Int8: + case Int16: printf("{%s:%d}",typename(v.type),v.Int16);break; + case Int32: + case Int64: printf("{%s:%ld}",typename(v.type),v.Int64);break; + case Int8Ptr: + case UInt8Ptr: + case Int16Ptr: + case UInt16Ptr: + case Int32Ptr: + case UInt32Ptr: + case Int64Ptr: + case UInt64Ptr: printf("{%s:%p}",typename(v.type),v.VoidPtr);break; + default: throw(ERR_WRONGTYPE);break; + } +} + +void printstnode(STNode* node){ + printf("\e[94mSTNode: %lu\n address: %p\n value: ",sizeof(STNode),node); + printuni(node->value); + // prints pointers to all existing branches + printf("\n branches: %p\n", node->branches); + if(node->branches) for(uint8 i=0;i<8;i++){ + printf(" \e[90m[%u]=%p\n",i,node->branches[i]); + for (uint8 ii = 0; ii < 8; ii++){ + if(node->branches[i]){ + printf(" \e[90m[%u][%u]=%p\n",i,ii,node->branches[i][ii]); + for (uint8 iii = 0; iii < 4; iii++) + if(node->branches[i][ii]) printf(" \e[90m[%u][%u][%u]=%p\n",i,ii,iii,node->branches[i][ii][iii]); + } + } + } } void test_searchtree(){ printf("\e[96m-----------[test_searchtree]-----------\n"); STNode* node=STNode_create(); - printf("\e[92m node created\n"); - printn(node); + printf("\e[92mnode created\n"); + printstnode(node); STNode_free(node); - printf("\e[92m node deleted\n"); + printf("\e[92mnode deleted\n"); } diff --git a/DtsodC/src/tests/tests.h b/DtsodC/src/tests/tests.h index 61303b9..97f403a 100644 --- a/DtsodC/src/tests/tests.h +++ b/DtsodC/src/tests/tests.h @@ -1,8 +1,14 @@ #pragma once #include "../base/std.h" +#include "../SearchTree/SearchTree.h" +#include "../Autoarr/Autoarr.h" void test_autoarr(void); +void printautoarr(Autoarr* ar); + void test_searchtree(void); +void printstnode(STNode* node); +void printuni(Unitype v); void test_all(void);