search tree is almost done

This commit is contained in:
Timerix22 2022-02-06 22:42:45 +03:00
parent a9d10b0895
commit a6f83bab34
17 changed files with 200 additions and 64 deletions

View File

@ -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

View File

@ -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++;
}

View File

View File

View File

@ -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;
}

View File

@ -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);
Unitype ST_pull(STNode* node, const char* key);

View File

@ -1,3 +1,12 @@
#pragma once
#include "std.h"
#include "base_types.h"
#include "errors.h"
#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);

View File

@ -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";
}
}

View File

@ -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);

View File

@ -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

9
DtsodC/src/base/hash.c Normal file
View File

@ -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;
}

View File

@ -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)

View File

@ -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;
}

View File

@ -3,4 +3,5 @@
void test_all(void){
test_autoarr();
test_searchtree();
printf("\e[96m---------------------------------------\n");
}

View File

@ -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;i<ar->max_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");
}

View File

@ -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");
}

View File

@ -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);