DtsodC moved to separate repo

This commit is contained in:
Timerix22 2022-03-18 20:42:39 +03:00
parent 22cd18f733
commit 7f653adfd2
36 changed files with 0 additions and 1717 deletions

View File

@ -1,23 +0,0 @@
#include "Autoarr.h"
define_Autoarr(uint8)
define_Autoarr(int8)
define_Autoarr(uint16)
define_Autoarr(int16)
define_Autoarr(uint32)
define_Autoarr(int32)
define_Autoarr(uint64)
define_Autoarr(int64)
define_Autoarr(float)
define_Autoarr(double)
define_Autoarr(Unitype)
//right func to clear array of unitype values
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++)
Unitype_free(ar->values[blockI][elemI]);
for(uint32 elemI=0;elemI<ar->block_length;elemI++)
Unitype_free(ar->values[ar->blocks_count-1][elemI]);
Autoarr_clear(ar);
}

View File

@ -1,34 +0,0 @@
#pragma once
#include "Autoarr_declare.h"
#include "Autoarr_define.h"
declare_Autoarr(uint8)
declare_Autoarr(int8)
declare_Autoarr(uint16)
declare_Autoarr(int16)
declare_Autoarr(uint32)
declare_Autoarr(int32)
declare_Autoarr(uint64)
declare_Autoarr(int64)
declare_Autoarr(float)
declare_Autoarr(double)
declare_Autoarr(Unitype)
//right func to clear array of unitype values
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar);
#define Autoarr_foreach(ar,elem,codeblock)({\
if(ar->blocks_count>0) {\
typeof(**ar->values) elem;\
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)\
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++){dbg(5);\
elem=ar->values[blockI][elemI];\
(codeblock);\
}\
for(uint32 elemI=0;elemI<ar->block_length;elemI++){\
elem=ar->values[ar->blocks_count-1][elemI];\
(codeblock);\
}\
}\
})

View File

@ -1,61 +0,0 @@
#pragma once
#include "../base/base.h"
#define declare_Autoarr(type)\
\
struct Autoarr_##type;\
\
typedef struct {\
void (*add)(struct Autoarr_##type* ar, type element);\
type (*get)(struct Autoarr_##type* ar, uint32 index);\
type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\
void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\
void (*clear)(struct Autoarr_##type* ar);\
} __functions_list_t_##type;\
\
typedef struct Autoarr_##type{\
uint16 blocks_count;\
uint16 max_blocks_count;\
uint16 block_length;\
uint16 max_block_length;\
type** values;\
__functions_list_t_##type* functions;\
} Autoarr_##type;\
\
void __Autoarr_add_##type(Autoarr_##type* ar, type element);\
type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index);\
type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index);\
void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\
void __Autoarr_clear_##type(Autoarr_##type* ar);\
Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);
#define Autoarr(type) Autoarr_##type
#define Autoarr_add(autoarr, element)\
autoarr->functions->add(autoarr, element)
#define Autoarr_get(autoarr, index)\
autoarr->functions->get(autoarr,index)
#define Autoarr_getptr(autoarr, index)\
autoarr->functions->getptr(autoarr,index)
#define Autoarr_set(autoarr, index, element)\
autoarr->functions->set(autoarr, index, element)
#define Autoarr_clear(autoarr)\
autoarr->functions->clear(autoarr)
#define Autoarr_create(type, max_blocks_count, max_block_length)\
__Autoarr_create_##type(max_blocks_count, max_block_length)
#define Autoarr_length(autoarr) \
(uint32)(!autoarr->blocks_count ? 0 : \
autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length)
#define Autoarr_max_length(autoarr)\
(uint32)(autoarr->max_block_length*autoarr->max_blocks_count)
#define Autoarr_remove(AR){\
if(AR->block_length==1){\
AR->blocks_count--;\
AR->block_length=AR->max_block_length;\
free(AR->values[AR->blocks_count]);\
}\
else AR->block_length--;\
}

View File

@ -1,64 +0,0 @@
#pragma once
#include "../base/base.h"
#define define_Autoarr(type)\
\
void __Autoarr_add_##type(Autoarr_##type* ar, type element){\
if(!ar->values){\
ar->values=malloc(ar->max_blocks_count*sizeof(type*));\
goto create_block;\
}\
if(ar->block_length==ar->max_block_length){\
if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH);\
ar->block_length=0;\
create_block:\
ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type));\
ar->blocks_count++;\
}\
ar->values[ar->blocks_count-1][ar->block_length]=element;\
ar->block_length++;\
}\
\
type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index){\
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
return ar->values[index/ar->max_block_length][index%ar->max_block_length];\
}\
\
type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index){\
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\
}\
\
void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
}\
\
void __Autoarr_clear_##type(Autoarr_##type* ar){\
for(uint16 i=0; i<ar->blocks_count;i++)\
free(ar->values[i]); \
free(ar->values);\
ar->values=NULL;\
ar->blocks_count=0;\
ar->block_length=0;\
}\
\
__functions_list_t_##type __functions_list_##type={\
&__Autoarr_add_##type,\
&__Autoarr_get_##type,\
&__Autoarr_getptr_##type,\
&__Autoarr_set_##type,\
&__Autoarr_clear_##type\
};\
\
Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\
return (Autoarr_##type){\
.max_blocks_count=max_blocks_count,\
.blocks_count=0,\
.max_block_length=max_block_length,\
.block_length=0,\
.values=NULL,\
.functions=&__functions_list_##type\
};\
}

View File

@ -1,71 +0,0 @@
#include "StringBuilder.h"
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length){
return Autoarr_create(int8,max_blocks_count,max_block_length);
}
void StringBuilder_append_char(StringBuilder* b, char c){
Autoarr_add(b,c);
}
void StringBuilder_append_cptr(StringBuilder* b, char* s){
char c;
while((c=*s++))
Autoarr_add(b,c);
}
void StringBuilder_append_string(StringBuilder* b, string s){
while(s.length>0){
Autoarr_add(b,*s.ptr++);
s.length--;
}
}
void StringBuilder_append_int64(StringBuilder* b, int64 a){
uint8 i=0;
if(a==0){
Autoarr_add(b,'0');
}
else if(a<0){
Autoarr_add(b,'-');
a=-a;
}
char buf[24];
while(a!=0){
buf[i++]='0'+a%10;
a/=10;
}
string rev=string_reverse((string){buf,i});
StringBuilder_append_string(b,rev);
free(rev.ptr);
}
void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
uint8 i=0;
if(a==0){
Autoarr_add(b,'0');
}
char buf[24];
while(a!=0){
buf[i++]='0'+a%10;
a/=10;
}
string rev=string_reverse((string){buf,i});
StringBuilder_append_string(b,rev);
free(rev.ptr);
}
void StringBuilder_append_double(StringBuilder* b, double a){
char buf[32];
sprintf(buf,"%lf",a);
StringBuilder_append_cptr(b,buf);
}
char* StringBuilder_build(StringBuilder* b){
uint32 len=Autoarr_length(b);
char* str=malloc(len+1);
str[len]=0;
for(uint32 i=0;i<len;i++)
str[i]=Autoarr_get(b,i);
return str;
}

View File

@ -1,14 +0,0 @@
#pragma once
#include "Autoarr.h"
typedef Autoarr(int8) StringBuilder;
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cptr(StringBuilder* b, char* s);
void StringBuilder_append_string(StringBuilder* b, string s);
void StringBuilder_append_int64(StringBuilder* b, int64 a);
void StringBuilder_append_uint64(StringBuilder* b, uint64 a);
void StringBuilder_append_double(StringBuilder* b, double a);
char* StringBuilder_build(StringBuilder* b);

View File

@ -1,27 +0,0 @@
#include "DtsodV24.h"
//returns UniNull if key not found
Unitype DtsodV24_get(Hashtable* dtsod, char* key){
return Hashtable_get(dtsod, key);
}
//adds or sets value
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
Unitype* val=Hashtable_getptr(dtsod, key);
if(val) *val=value;
else Hashtable_add(dtsod, key, value);
}
//checks for dtsod contains value or dont
bool DtsodV24_contains(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key);
return val!=NULL;
}
//replaces value with UniNull if key exists in dtsod
bool DtsodV24_remove(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key);
if (!val) return false;
*val=UniNull;
return true;
}

View File

@ -1,24 +0,0 @@
#if __cplusplus
extern "c" {
#endif
#pragma once
#include "../base/base.h"
#include "../Hashtable/Hashtable.h"
//parses text to binary values
Hashtable* DtsodV24_deserialize(char* text);
//creates text representation of dtsod
char* DtsodV24_serialize(Hashtable* dtsod);
//returns value or UniNull if key not found
Unitype DtsodV24_get(Hashtable* dtsod, char* key);
//adds or sets value
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value);
//checks for dtsod contains value or dont
bool DtsodV24_contains(Hashtable* dtsod, char* key);
//replaces value with UniNull if key exists in dtsod
bool DtsodV24_remove(Hashtable* dtsod, char* key);
#if __cplusplus
}
#endif

View File

@ -1,261 +0,0 @@
#include "DtsodV24.h"
#include "../Autoarr/StringBuilder.h"
#define ARR_BC 8
#define ARR_BL 16
#define STRB_BC 64
#define STRB_BL 1024
Hashtable* __deserialize(char** _text, bool calledRecursively){
Hashtable* dict=Hashtable_create();
char* text=*_text;
char c;
bool partOfDollarList=false;
bool readingList=false;
void __throw_wrongchar(char* file, int line, char* fname,char _c){
char errBuf[]="unexpected <c> at:\n \""
"00000000000000000000000000000000\"";
errBuf[12]=_c;
for(uint8 i=0;i<32;i++)
errBuf[i+22]=*(text-16+i);
printf("\n\e[91m[%s:%d %s] throwed error: %s\n",file,line,fname,errBuf);
exit(128);
};
#define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C)
void SkipComment(){
while((c=*++text)!='\n')
if(!c) throw(ERR_ENDOFSTR);
};
string ReadName(){
string nameStr={text,0};
text--;
while ((c=*++text)) switch (c){
case ' ': case '\t':
case '\r': case '\n':
if(nameStr.length!=0)
throw_wrongchar(c);
nameStr.ptr++;
break;
case '=': case ';':
case '\'': case '"':
case '[': case ']':
case '{':
throw_wrongchar(c);
break;
case '#':
SkipComment();
if(nameStr.length!=0)
throw_wrongchar(c);
nameStr.ptr=text+1; //skips '\n'
break;
case '}':
if(!calledRecursively) throw_wrongchar(c);
if((*++text)!=';')
throw_wrongchar(c);
case ':':
return nameStr;
case '$':
if(nameStr.length!=0)
throw_wrongchar(c);
nameStr.ptr++;
partOfDollarList=true;
break;
default:
nameStr.length++;
break;
}
if(nameStr.length>0) throw(ERR_ENDOFSTR);
return nameStr;
};
Unitype ReadValue(){
//returns part of <text> without quotes
char* ReadString(){
bool prevIsBackslash=false;
StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL);
StringBuilder* b=&_b;
while ((c=*++text)){
if(c=='"') {
if(prevIsBackslash) {
//replacing <\"> with <">
Autoarr_remove(b);
StringBuilder_append_char(b,c);
}
else {
char* str=StringBuilder_build(b);
Autoarr_clear(b);
return str;
}
}
else {
prevIsBackslash= c=='\\' && !prevIsBackslash;
StringBuilder_append_char(b,c);
}
}
throw(ERR_ENDOFSTR);
return NULL;
};
Autoarr(Unitype)* ReadList(){
Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype)));
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
readingList=true;
while (true){
Autoarr_add(list,ReadValue());
if (!readingList) break;
}
return list;
};
Hashtable* ReadDtsod(){
++text; //skips '{'
return __deserialize(&text,true);
}
Unitype ParseValue(string str){
//printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCharPtr(str));
const string nullStr={"null",4};
const string trueStr={"true",4};
const string falseStr={"false",5};
switch(*str.ptr){
case 'n':
if(string_compare(str,nullStr))
return UniNull;
else throw_wrongchar(*str.ptr);
break;
case 't':
if(string_compare(str,trueStr))
return UniTrue;
else throw_wrongchar(*str.ptr);
break;
case 'f':
if(string_compare(str,falseStr))
return UniFalse;
else throw_wrongchar(*str.ptr);
break;
default:
switch(str.ptr[str.length-1]){
case 'f': {
char* _c=string_cpToCharPtr(str);
Unitype rez=Uni(Double,strtod(_c,NULL));
free(_c);
return rez;
}
case 'u': {
uint64 lu=0;
char* _c=string_cpToCharPtr(str);
sscanf(_c,"%lu",&lu);
free(_c);
return Uni(UInt64,lu);
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
int64 li=0;
char* _c=string_cpToCharPtr(str);
if(sscanf(_c,"%li",&li)!=1){
char err[64];
sprintf(err,"can't parse to int: <%s>",_c);
throw(err);
}
free(_c);
return Uni(Int64,li);
}
default:
throw_wrongchar(str.ptr[str.length-1]);
}
}
throw(ERR_ENDOFSTR);
return UniNull;
};
string valueStr={text+1,0};
Unitype value;
while ((c=*++text)) switch (c){
case ' ': case '\t':
case '\r': case '\n':
if(valueStr.length!=0)
throw_wrongchar(c);
valueStr.ptr++;
break;
case '=': case ':':
case '}': case '$':
throw_wrongchar(c);
break;
case '#':;
char _c=c;
SkipComment();
if(valueStr.length!=0)
throw_wrongchar(_c);
valueStr.ptr=text+1; //skips '\n'
break;
case '"':
if(valueStr.length!=0) throw_wrongchar(c);
value=UniPtr(CharPtr,ReadString());
break;
case '\'':
if(valueStr.length!=0) throw_wrongchar(c);
char valueChar=*++text;
if (*++text != '\'') throw("after <'> should be char");
value=Uni(Char,valueChar);
break;
case '[':
if(valueStr.length!=0) throw_wrongchar(c);
value=UniPtr(AutoarrUnitypePtr,ReadList());
case ']':
readingList=false;
break;
case '{':
if(valueStr.length!=0) throw_wrongchar(c);
value=UniPtr(HashtablePtr,ReadDtsod());
return value;
case ';':
case ',':
if(valueStr.length!=0)
value=ParseValue(valueStr);
return value;
default:
valueStr.length++;
break;
}
throw(ERR_ENDOFSTR);
return UniNull;
};
text--;
while((c=*++text)){
string name=ReadName();
if(name.length==0) //end of file or '}' in recursive call
goto END;
char* nameCPtr=string_cpToCharPtr(name);
Unitype value=ReadValue();
if(partOfDollarList){
Autoarr(Unitype)* list;
Unitype lu;
if(Hashtable_try_get(dict,nameCPtr, &lu)){
list=(Autoarr(Unitype)*)lu.VoidPtr;
}
else{
list=malloc(sizeof(Autoarr(Unitype)));
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list));
}
Autoarr_add(list,value);
}
else Hashtable_add(dict,nameCPtr,value);
}
END:
*_text=text;
return dict;
}
Hashtable* DtsodV24_deserialize(char* text) {
Hashtable* r=__deserialize(&text,false);
return r;
}

View File

@ -1,90 +0,0 @@
#include "DtsodV24.h"
#include "../Autoarr/StringBuilder.h"
//65536 max length!
#define STRB_BC 64
#define STRB_BL 1024
#define addc(B,C) StringBuilder_append_char(B,C)
void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
void AppendTabs(){
for(uint8 t=0; t<tabs; t++)
addc(b,'\t');
};
void AppendValue(Unitype u){
switch(u.type){
case Int64:
StringBuilder_append_int64(b,u.Int64);
break;
case UInt64:
StringBuilder_append_uint64(b,u.UInt64);
addc(b,'u');
break;
case Double:
StringBuilder_append_double(b,u.Double);
addc(b,'f');
break;
case CharPtr:
addc(b,'"');
char c;
while((c=*(char*)(u.VoidPtr++))){
if(c=='\"') addc(b,'\\');
addc(b,c);
}
addc(b,'"');
break;
case Char:
addc(b,'\'');
addc(b,u.Char);
addc(b,'\'');
break;
case Bool:
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
break;
case Null:
if(!u.VoidPtr) StringBuilder_append_cptr(b, "null");
else throw("Null-type pointer is not 0");
break;
case AutoarrUnitypePtr:
addc(b,'[');
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
addc(b,' ');
AppendValue(e);
addc(b,',');
}));
Autoarr_remove(b);
addc(b,' ');
addc(b,']');
break;
case HashtablePtr:
addc(b,'{');
addc(b,'\n');
__serialize(b,tabs+1,u.VoidPtr);
AppendTabs();
addc(b,'}');
break;
default: dbg((u.type)); throw(ERR_WRONGTYPE);
}
};
Hashtable_foreach(dtsod, p, ({
AppendTabs();
StringBuilder_append_cptr(b,p.key);
addc(b,':');
addc(b,' ');
AppendValue(p.value);
addc(b,';');
addc(b,'\n');
}));
}
char* DtsodV24_serialize(Hashtable* dtsod){
StringBuilder b=StringBuilder_create(STRB_BC,STRB_BL);
__serialize(&b,0,dtsod);
char* str=StringBuilder_build(&b);
Autoarr_clear((&b));
return str;
}

View File

@ -1,101 +0,0 @@
#include "Hashtable.h"
// amount of rows
#define HT_HEIN_MIN 0
#define HT_HEIN_MAX 5
static const uint16 HT_HEIGHTS[]={61,257,1021,4099,16381,65521};
#define ARR_BC 2
#define ARR_BL 8
Hashtable* Hashtable_create(){
Hashtable* ht=malloc(sizeof(Hashtable));
ht->hein=HT_HEIN_MIN;
ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KeyValuePair)));
for(uint16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++)
ht->rows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL);
return ht;
}
void Hashtable_free(Hashtable* ht){
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++){
Autoarr_KeyValuePair_clear(ht->rows+i);
}
free(ht->rows);
free(ht);
}
uint32 Hashtable_height(Hashtable* ht){ return HT_HEIGHTS[ht->hein]; }
void Hashtable_expand(Hashtable* ht){
if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH);
Autoarr(KeyValuePair)* newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KeyValuePair)));
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
newrows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL);
for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
Autoarr(KeyValuePair)* ar=ht->rows+i;
uint32 arlen=Autoarr_length(ar);
for(uint16 k=0;k<arlen;k++){
KeyValuePair p=Autoarr_get(ar,k);
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein];
Autoarr(KeyValuePair)* newar=newrows+newrown;
Autoarr_add(newar,p);
}
Autoarr_clear(ar);
}
free(ht->rows);
ht->rows=newrows;
}
Autoarr(KeyValuePair)* getrow(Hashtable* ht, char* key, bool can_expand){
Autoarr(KeyValuePair)* ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein];
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
optime("expand",1,(Hashtable_expand(ht)));
ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein];
return ar;
}
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){
Autoarr_add(getrow(ht,p.key,true),p);
}
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
Hashtable_add_pair(ht,KVPair(key,u));
}
//returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){
KeyValuePair* p=Autoarr_getptr(ar,i);
if(cptr_compare(key,p->key)) return &p->value;
}
return NULL;
}
Unitype Hashtable_get(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){
KeyValuePair p=Autoarr_get(ar,i);
if(cptr_compare(key,p.key)) return p.value;
}
return UniNull;
}
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){
return KVPair(key,Hashtable_get(ht,key));
}
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
Unitype u=Hashtable_get(ht,key);
*output=u;
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,KVPair(key,u)); } */

View File

@ -1,43 +0,0 @@
#pragma once
#include "../base/base.h"
#include "hash.h"
#include "KeyValuePair.h"
typedef struct Hashtable{
uint8 hein; //height=HT_HEIGHTS[hein]
Autoarr(KeyValuePair)* rows; // Autoarr[height]
} Hashtable;
Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht);
//amount of rows
uint32 Hashtable_height(Hashtable* ht);
//adds charptr and value to new KeyValuePair
//use cptr_copy() to create new string if needed
#define KVPair(key,value) (KeyValuePair){key,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);
//not implemented yet
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_set(Hashtable* ht, char* key, Unitype u);
#define Hashtable_foreach(HT, EL, codeblock)({\
uint16 hmax=Hashtable_height(HT);\
for(uint16 h=0; h<hmax; h++){\
Autoarr(KeyValuePair)* AR=HT->rows+h;\
Autoarr_foreach(AR, EL, codeblock);\
}\
})

View File

@ -1,26 +0,0 @@
#include "KeyValuePair.h"
define_Autoarr(KeyValuePair)
//proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p){
free(p.key);
Unitype_free(p.value);
}
//func for KVP array clearing
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){
for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++)
for(uint16 elemI=0; elemI < ar->max_block_length; elemI++)
KeyValuePair_free(ar->values[blockI][elemI]);
for(uint16 elemI=0; elemI < ar->block_length; elemI++)
KeyValuePair_free(ar->values[ar->blocks_count-1][elemI]);
Autoarr_clear(ar);
}
void printkvp(KeyValuePair p){
printf("{\"%s\", ",p.key);
printuni(p.value);
printf("}");
}

View File

@ -1,17 +0,0 @@
#include "../base/base.h"
#include "../Autoarr/Autoarr.h"
typedef struct KeyValuePair{
char* key;
Unitype value;
} KeyValuePair;
declare_Autoarr(KeyValuePair)
//proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p);
//func to clear KVP array
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar);
void printkvp(KeyValuePair p);

View File

@ -1,15 +0,0 @@
#include "hash.h"
uint32 ihash(char *str){
uint32 hash=5381;
for (char c=*str;c;c=*(++str))
hash=((hash<<5)+hash)+c;
return hash;
}
uint64 lhash(char* str){
uint64 hash = 0;
for (char c=*str;c;c=*(++str))
hash=c+(hash<<6)+(hash<<16)-hash;
return hash;
}

View File

@ -1,8 +0,0 @@
#pragma once
#include "../base/base.h"
//djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html
uint32 ihash(char *str);
//sdbm hash function
uint64 lhash(char* str);

View File

@ -1,33 +0,0 @@
SRC=$(wildcard [^tests]**/*.c)
TESTS=$(wildcard tests/*c) $(wildcard tests/**/*.c)
OUTDIR=.bin
OUTFILE=$(OUTDIR)/dtsodc.com
CMP=gcc
all: clear_c build test
clear_c:
clear
clear_bin:
@echo -e '\e[96m--------------[clear_bin]--------------\e[0m'
touch $(OUTDIR)/_.com
rm $(OUTDIR)/*.com
clang: CMP=clang
clang: all
CMPARGS= -Wall -Wno-discarded-qualifiers $(SRC) $(TESTS) -o $(OUTFILE)
build:
@echo -e '\n\e[96m----------------[build]----------------\e[0m'
@mkdir -p $(OUTDIR)
$(CMP) -O1 -flto $(CMPARGS)
build_dbg:
@echo -e '\n\e[96m--------------[build_dbg]--------------\e[0m'
@mkdir -p $(OUTDIR)
$(CMP) -O0 -g $(CMPARGS).dbg
test:
@echo -e '\n\e[96m----------------[test]-----------------\e[0m'
$(OUTFILE)
valgrind: clear_c build_dbg
@echo -e '\n\e[96m--------------[valgrind]---------------\e[0m'
valgrind -s --read-var-info=yes --track-origins=yes --fullpath-after=DtsodC/ \
--leak-check=full --show-leak-kinds=all $(OUTFILE).dbg

View File

@ -1,13 +0,0 @@
# DtsodC
DtsodV23 parser in C# works too slow, so i wrote V24 parser in C
<br>
## Building on Linux
**Required packages:** gcc
```bash
make build
````

View File

@ -1,91 +0,0 @@
#include "SearchTree.h"
STNode* STNode_create(){
STNode* node=malloc(sizeof(STNode));
node->branches=NULL;
node->value.type=Null;
node->value.UInt64=0;
return node;
}
void STNode_free(STNode* node){
if (!node) throw(ERR_NULLPTR);
if(node->branches){
for(uint8 n32 = 0;n32<8;n32++){
STNode*** ptrn32=(STNode***)node->branches[n32];
if(ptrn32){
for(uint8 n4 = 0;n4<8;n4++){
STNode** ptrn4=ptrn32[n4];
if (ptrn4){
for(uint8 rem=0;rem<4;rem++){
STNode* ptrrem=ptrn4[rem];
if(ptrrem)
STNode_free(ptrrem);
}
free(ptrn4);
}
}
free(ptrn32);
}
}
free(node->branches);
}
if(node->value.VoidPtr)
Unitype_free(node->value);
free(node);
}
typedef struct {uint8 n32, n4, rem;} indexes3;
indexes3 splitindex(uint8 i){
return (indexes3){
.n32=i/32,
.n4=i%32/4,
.rem=i%32%4,
};
}
void ST_push(STNode* node_first, const char* key, Unitype value){
if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first;
while(*key){
indexes3 i3=splitindex((uint8)*key);
if(!node_last->branches){
node_last->branches=(STNode****)malloc(8*sizeof(STNode*));
for(uint8 i=0;i<8;i++)
node_last->branches[i]=(STNode***)NULL;
}
if(!node_last->branches[i3.n32]){
node_last->branches[i3.n32]=(STNode***)malloc(8*sizeof(STNode*));
for(uint8 i=0;i<8;i++)
node_last->branches[i3.n32][i]=(STNode**)NULL;
}
if(!node_last->branches[i3.n32][i3.n4]){
node_last->branches[i3.n32][i3.n4]=(STNode**)malloc(4*sizeof(STNode*));
for(uint8 i=0;i<4;i++)
node_last->branches[i3.n32][i3.n4][i]=(STNode*)NULL;
}
if(!node_last->branches[i3.n32][i3.n4][i3.rem])
node_last->branches[i3.n32][i3.n4][i3.rem]=STNode_create();
node_last=node_last->branches[i3.n32][i3.n4][i3.rem];
key++;
}
node_last->value=value;
}
Unitype ST_pull(STNode* node_first, const char* key){
if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first;
while (*key){
indexes3 i3=splitindex((uint8)*key);
if(!node_last->branches) return UniNull;
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
if(!ptrn32) return UniNull;
STNode** ptrn4=ptrn32[i3.n4];
if(!ptrn4) return UniNull;
node_last=ptrn4[i3.rem];
if(!node_last) return UniNull;
key++;
}
return node_last->value;
}

View File

@ -1,14 +0,0 @@
#pragma once
#include "../base/base.h"
typedef struct SearchTreeNode{
struct SearchTreeNode**** branches; //*STNode[8][8][4]
Unitype value;
} STNode;
STNode* STNode_create();
void STNode_free(STNode* node);
void ST_push(STNode* node, const char* key, Unitype value);
Unitype ST_pull(STNode* node, const char* key);

View File

@ -1,25 +0,0 @@
# Search Tree
byte is dividing into indexes of 3d array of nodes
```
STNode.branches byte
┣━[0]━┳━[0]━┳━[0] 0
┃ ┃ ┣━...
┃ ┃ ┗━[3] 3
┃ ┣━...
┃ ┃
┃ ┗━[7]━┳━[0] 28
┃ ┣━...
┃ ┗━[3] 31
┣━...
┗━[7]━┳━[0]━┳━[0] 224
┃ ┣━...
┃ ┗━[3] 227
┣━...
┗━[7]━┳━[0] 252
┣━...
┗━[3] 255
```

View File

@ -1,17 +0,0 @@
#pragma once
#include "std.h"
#include "types.h"
#include "errors.h"
#include "mystr.h"
// executes codeblock and prints execution time
#define optime(opname,repeats,codeblock) ({\
struct timespec start, stop;\
clock_gettime(CLOCK_REALTIME, &start);\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
(codeblock);\
clock_gettime(CLOCK_REALTIME, &stop);\
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
})

View File

@ -1,27 +0,0 @@
#include "std.h"
#include "errors.h"
const char* errname(err_t err){
switch(err){
case SUCCESS: return "SUCCESS";
case ERR_MAXLENGTH: return "ERR_MAXLENGTH";
case ERR_WRONGTYPE: return "ERR_WRONGTYPE";
case ERR_WRONGINDEX: return "ERR_WRONGINDEX";
case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED";
case ERR_NULLPTR: return "ERR_NULLPTR";
case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
default: return "UNKNOWN_ERROR";
}
}
void _throwint(int err, const char* srcfile, int line, const char* funcname){
if(err){ // SUCCESS=0 is not an error
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err));
exit(err);
}
else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname);
}
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
exit(255);
}

View File

@ -1,16 +0,0 @@
#pragma once
typedef enum err_t {
SUCCESS, //not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t;
const char* errname(err_t err);
void _throwint(int err, const char* srcfile, int line, const char* funcname) ;
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname);
#pragma GCC diagnostic ignored "-Wint-conversion"
#define throw(E) \
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__)))

View File

@ -1,76 +0,0 @@
#include "base.h"
//returns length of string (including \0)
uint32 cptr_length(char* str){
uint32 len=0;
while(*(str++)) len++;
return ++len;
}
//allocates new char[] and copies src there
char* cptr_copy(char* src){
uint32 len=cptr_length(src);
char* dst=malloc(len*sizeof(char));
while(len-->0)
dst[len]=src[len];
return dst;
}
//compares two char buffers, NullPtr-friendly
bool cptr_compare(char* key0, char* key1){
if(!key0) return key1 ? false : true;
if(!key1) return false;
while(*key0&&*key1)
if(*key0++ != *key1++)
return false;
return true;
}
//multiplies char n times
char* char_multiply(char c, uint32 n){
char* rez=malloc(n+1);
rez[n]=0;
while(n-->0)
rez[n]=c;
return rez;
}
//copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCharPtr(string str){
char* cptr=malloc(str.length*sizeof(char)+1);
if(str.length==0) return NULL;
cptr[str.length]=0;
while(str.length-->0)
cptr[str.length]=str.ptr[str.length];
return cptr;
}
//copies cptr content (excluding '\0' at the end) to new string
string string_cpFromCharPtr(char* cptr){
if(!cptr) return stringNull;
string str;
str.length=cptr_length(cptr)-1;
str.ptr=malloc(str.length);
for(uint32 i=0;i<str.length;i++)
str.ptr[i]=cptr[i];
return str;
}
//compares two strings, NullPtr-friendly
bool string_compare(string str0, string str1){
if(str0.length!=str1.length) return false;
if(!str0.ptr) return str1.ptr ? false : true;
else if(!str1.ptr) return false;
while(str0.length-->0)
if(*str0.ptr++ != *str1.ptr++)
return false;
return true;
}
//creates new string which is reversed variant of <s>
string string_reverse(string s){
string r={malloc(s.length), s.length};
for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1];
return r;
}

View File

@ -1,36 +0,0 @@
#pragma once
#include "types.h"
//returns length of string (including \0)
uint32 cptr_length(char* str);
//allocates new char[] and copies src there
char* cptr_copy(char* src);
//compares two char buffers, NullPtr-friendly
bool cptr_compare(char* key0, char* key1);
//multiplies char n times
char* char_multiply(char c, uint32 n);
//my fixed length string struct
//doesn't store '\0' at the end
typedef struct string{
char* ptr; //char pointer
uint32 length; //amount of chars in ptr value
} string;
static const string stringNull={NULL,0};
//copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCharPtr(string str);
//copies cptr content (excluding '\0' at the end) to new string
string string_cpFromCharPtr(char* cptr);
//compares two strings, NullPtr-friendly
bool string_compare(string str0, string str1);
//creates new string which is reversed variant of <s>
string string_reverse(string s);

View File

@ -1,13 +0,0 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <locale.h>
#include <time.h>
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)
#define IFTYPE(X, T) __builtin_types_compatible_p(typeof(X), T)
#define dbg(N) printf("\e[95m%d\n",N)

View File

@ -1,130 +0,0 @@
#include "types.h"
#include "errors.h"
#include "../Autoarr/Autoarr.h"
#include "../Hashtable/Hashtable.h"
#include "../SearchTree/SearchTree.h"
const char* typename(my_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";
case UInt16: return "UInt16";
case Int32: return "Int32";
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";
case CharPtr: return "CharPtr";
case STNodePtr: return "STNodePtr";
case HashtablePtr: return "HashtablePtr";
case UniversalType: return "Unitype";
case AutoarrInt8Ptr: return "AutoarrInt8Ptr";
case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr";
case AutoarrInt16Ptr: return "AutoarrInt16Ptr";
case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr";
case AutoarrInt32Ptr: return "AutoarrInt32Ptr";
case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr";
case AutoarrInt64Ptr: return "AutoarrInt64Ptr";
case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr";
case AutoarrUnitypePtr: return "AutoarrUnitypePtr";
case AutoarrKVPairPtr: return "AutoarrKVPairPtr";
default: throw(ERR_WRONGTYPE); return "ERROR";
}
}
//frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u){
switch (u.type) {
case Null:
case Float:
case Double:
case Char:
case Bool:
case Int8:
case UInt8:
case Int16:
case UInt16:
case Int32:
case UInt32:
case Int64:
case UInt64:
break;
case Int8Ptr:
case UInt8Ptr:
case Int16Ptr:
case UInt16Ptr:
case Int32Ptr:
case UInt32Ptr:
case Int64Ptr:
case UInt64Ptr:
case CharPtr:
free(u.VoidPtr);
break;
case HashtablePtr:
Hashtable_free(u.VoidPtr);
break;
case STNodePtr:
STNode_free(u.VoidPtr);
break;
case AutoarrInt8Ptr:
Autoarr_clear(((Autoarr(int8)*)u.VoidPtr));
break;
case AutoarrUInt8Ptr:
Autoarr_clear(((Autoarr(uint8)*)u.VoidPtr));
break;
case AutoarrInt16Ptr:
Autoarr_clear(((Autoarr(int16)*)u.VoidPtr));
break;
case AutoarrUInt16Ptr:
Autoarr_clear(((Autoarr(uint16)*)u.VoidPtr));
break;
case AutoarrInt32Ptr:
Autoarr_clear(((Autoarr(int32)*)u.VoidPtr));
break;
case AutoarrUInt32Ptr:
Autoarr_clear(((Autoarr(uint32)*)u.VoidPtr));
break;
case AutoarrInt64Ptr:
Autoarr_clear(((Autoarr(int64)*)u.VoidPtr));
break;
case AutoarrUInt64Ptr:
Autoarr_clear(((Autoarr(uint64)*)u.VoidPtr));
break;
case AutoarrUnitypePtr:
Autoarr_Unitype_clear(u.VoidPtr);
free((Autoarr(Unitype)*)u.VoidPtr);
break;
case AutoarrKVPairPtr:
Autoarr_KeyValuePair_clear(u.VoidPtr);
free((Autoarr(KeyValuePair)*)u.VoidPtr);
break;
default: throw(ERR_WRONGTYPE);
}
}
void printuni(Unitype v){
switch (v.type) {
case Null: printf("{Null}");break;
case Double: printf("{%s : %lf}",typename(v.type),v.Double);break;
case Char: printf("{%s : '%c'}",typename(v.type),v.Char);break;
case Bool:
case UInt64: printf("{%s : %lu}",typename(v.type),v.UInt64);break;
case Int64: printf("{%s : %ld}",typename(v.type),v.Int64);break;
case CharPtr: printf("{%s : \"%s\"}",typename(v.type),(char*)v.VoidPtr);break;
default: printf("{%s : %p}",typename(v.type),v.VoidPtr);break;
}
}

View File

@ -1,52 +0,0 @@
#pragma once
#include "std.h"
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
typedef uint16_t uint16;
typedef int32_t int32;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;
typedef enum my_type{
Null, Float, Double, Char, Bool,
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
CharPtr, STNodePtr, HashtablePtr,
UniversalType,
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
AutoarrUnitypePtr, AutoarrKVPairPtr
} __attribute__ ((__packed__)) my_type;
//returns type name
const char* typename(my_type t);
// returns size of type in bytes
int8 typesize(my_type type);
// can store any base type
typedef struct Unitype{
my_type type;
union {
int64 Int64;
uint64 UInt64;
double Double;
char Char;
bool Bool;
void* VoidPtr;
};
} Unitype;
static const Unitype UniNull={Null,.VoidPtr=NULL};
static const Unitype UniTrue={Bool,.Bool=true};
static const Unitype UniFalse={Bool,.Bool=false};
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}
#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL}
//frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u);
void printuni(Unitype v);

View File

@ -1,19 +0,0 @@
#include "../base/base.h"
#include "tests.h"
void test_all(){
/* test_searchtree();
test_autoarr();
test_hashtable();
test_string(); */
test_dtsod();
printf("\e[96m---------------------------------------\e[0m\n");
}
int main(){
setlocale(LC_ALL, "en-US.Unicode");
printf("\e[92mdtsod parser in c language!\e[97m\n");
optime("test_all",1,{test_all();});
printf("\e[0m\n");
return 0;
}

View File

@ -1,52 +0,0 @@
#include "tests.h"
#include "../Autoarr/Autoarr.h"
static void printautoarr(Autoarr(uint16)* ar){
printf("\e[94mAutoarr(uint16): %lu\n"
" max_blocks_count: %u\n"
" blocks_count: %u\n"
" max_block_length: %u\n"
" block_length: %u\n"
" max_length: %u\n"
" length: %u\n",
sizeof(Autoarr(uint16)),
ar->max_blocks_count,
ar->blocks_count,
ar->max_block_length,
ar->block_length,
Autoarr_max_length(ar),
Autoarr_length(ar));
}
static void fillar(Autoarr(uint16)* ar){
for (uint16 i=0;i<Autoarr_max_length(ar);i++)
Autoarr_add(ar,i);
}
static void resetar(Autoarr(uint16)* ar){
for (uint16 i=0;i<Autoarr_max_length(ar);i++)
Autoarr_set(ar,i,Autoarr_max_length(ar)-i-1);
}
static void printallval(Autoarr(uint16)* ar){
printf("\e[90m");
for (uint16 i=0;i<Autoarr_length(ar);i++)
printf("%u ",Autoarr_get(ar,i));
printf("\n");
}
void test_autoarr(){
optime("test_autoarr",1,({
printf("\e[96m------------[test_autoarr]------------\n");
Autoarr(uint16) ar=Autoarr_create(uint16,10,16);
printf("\e[92mautoarr created\n");
fillar(&ar);
printf("\e[92mautoarr filled up\n");
printautoarr(&ar);
printallval(&ar);
resetar(&ar);
printf("\e[92mautoarr values reset\n");
printallval(&ar);
Autoarr_clear(((&ar)));
printf("\e[92mautoarr cleared\n");
}));
}

View File

@ -1,55 +0,0 @@
#include "tests.h"
#include "../DtsodParser/DtsodV24.h"
const char text[]=
"message: {\n"
" bool: false;"
" char: 'v';"
" int: -2515;"
" uint:#comment!\n 0u;"
" double: 965.557f;#another comment!\n"
" text: \"_$\\\"\\\\'''a ыыы000;2;=:%d;```\";\n"
"}; ";
void print_dtsod(Hashtable* dtsod){
printf("\e[92m");
Hashtable_foreach(dtsod, p,({
printkvp(p);
if(p.value.type==HashtablePtr){
printf(": {\n");
Hashtable* sub=p.value.VoidPtr;
Hashtable_foreach(sub, _p,({
printf(" ");
printkvp(_p);
printf("\n");
}));
printf("}");
}
printf("\n");
}));
}
void test_dtsod(){
optime(__func__,1,({
printf("\e[96m-------------[test_dtsod]-------------\n");
Hashtable* dtsod;
char* s=cptr_copy(text);
optime("deserialize",1,(dtsod=DtsodV24_deserialize(s)));
free(s);
print_dtsod(dtsod);
optime("serialize",1,(s=DtsodV24_serialize(dtsod)));
Hashtable_free(dtsod);
printf("\e[92m%s",s);
optime("reserialize",10,({
dtsod=DtsodV24_deserialize(s);
free(s);
s=DtsodV24_serialize(dtsod);
Hashtable_free(dtsod);
}));
free(s);
}));
}

View File

@ -1,71 +0,0 @@
#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 printrowgraph(Hashtable* ht){
printf("\e[94mrow length graph:\n");
uint16 lgs_l=1000;
uint32 lgs[lgs_l];
for(uint32 i=0; i<lgs_l; i++)
lgs[i]=0;
for(uint16 h=0;h<Hashtable_height(ht);h++){
Autoarr(KeyValuePair)* ar=ht->rows+h;
uint32 l=Autoarr_length(ar);
lgs[l]++;
}
for(uint32 i=0; i<lgs_l; i++)
if(lgs[i]>0) {
char* str0=char_multiply(' ',i>=100?0:(i>=10?1:2));
char* str1=char_multiply(' ',lgs[i]>=100?0:(lgs[i]>=10?1:2));
char* str2=char_multiply('#',lgs[i]/100);
printf("\e[94m length: \e[96m%u %s \e[94mfrequency: \e[96m%u %s \e[90m%s\n",i,str0,lgs[i],str1,str2);
free(str0);
free(str1);
free(str2);
}
}
void fill(Hashtable* ht){
for(uint32 i=0;i<100000;i++){
char* key=malloc(12);
sprintf(key,"key__%u",i);
Hashtable_add(ht,key,Uni(UInt64,i));
}
}
Unitype gett(Hashtable* ht){
char* key=malloc(12);
Unitype u;
for(uint32 i=0;i<100000;i++){
sprintf(key,"key__%u",i);
u=Hashtable_get(ht,key);
}
free(key);
return u;
}
void test_hashtable(){
optime("test_hashtable",1,({
printf("\e[96m-----------[test_hashtable]------------\n");
Hashtable* ht=Hashtable_create();
printf("\e[92mhashtable created\n");
print_hashtable(ht);
optime("fill",1,fill(ht));
optime("get",1,gett(ht));
printrowgraph(ht);
print_hashtable(ht);
Hashtable_free(ht);
printf("\e[92mhashtable freed\n");
}));
}

View File

@ -1,71 +0,0 @@
#include "tests.h"
#include "../SearchTree/SearchTree.h"
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]);
if(node->branches[i])
for (uint8 ii = 0; ii < 8; ii++){
printf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]);
if(node->branches[i][ii])
for (uint8 iii = 0; iii < 4; iii++)
printf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]);
}
}
}
void test_searchtree(){
optime("test_searchtree",1,({
printf("\e[96m-----------[test_searchtree]-----------\n");
STNode* node=STNode_create();
printf("\e[92mnode created\n");
printf("push:\e[94m\n ");
Unitype u={.type=Int64,.Int64=-3};
printuni(u);
ST_push(node,"type", u);
printf(" -> type\n ");
u=(Unitype){.type=Int64,.Int64=25};
printuni(u);
ST_push(node,"time", u);
printf(" -> time\n ");
u=(Unitype){.type=Double,.Double=-542.00600};
printuni(u);
ST_push(node,"author_id", u);
printf(" -> author_id\n ");
u=(Unitype){.type=Int64,.Int64=-31255};
printuni(u);
ST_push(node,"channel_id", u);
printf(" -> channel_id\n ");
u=(Unitype){.type=Double,.Double=32.2004};
printuni(u);
ST_push(node,"message_id", u);
printf(" -> message_id\n ");
u=(Unitype){.type=Int8Ptr,.VoidPtr=malloc(1)};
printuni(u);
ST_push(node,"text", u);
printf(" -> text\n");
printf("\e[92mpull:\e[94m");
printf("\n type -> ");
printuni(ST_pull(node,"type"));
printf("\n time -> ");
printuni(ST_pull(node,"time"));
printf("\n author_id -> ");
printuni(ST_pull(node,"author_id"));
printf("\n channel_id -> ");
printuni(ST_pull(node,"channel_id"));
printf("\n message_id -> ");
printuni(ST_pull(node,"message_id"));
printf("\n text -> ");
printuni(ST_pull(node,"text"));
printf("\n");
printf("\e[92mfirst node: ");
printstnode(node);
STNode_free(node);
printf("\e[92mnode deleted\n");
}));
}

View File

@ -1,16 +0,0 @@
#include "tests.h"
#include "../base/mystr.h"
void test_string(){
optime(__func__,1,({
printf("\e[96m-------------[test_string]-------------\n");
char c[]="0123456789abcdef";
string s=string_cpFromCharPtr(c);
printf("\e[92m\"%s\" -> string_cpFromCharPtr()\n",c);
if(s.length!=16) throw("string created with incorrect length");
char* p=string_cpToCharPtr(s);
printf("\e[92mstring_cpToCharPtr() -> \"%s\"\n",p);
free(p);
free(s.ptr);
}));
}

View File

@ -1,11 +0,0 @@
#pragma once
#include "../base/base.h"
void printuni(Unitype v);
void test_searchtree();
void test_autoarr();
void test_hashtable();
void test_string();
void test_dtsod();