64
This commit is contained in:
@@ -144,7 +144,7 @@ void Autoarr_set_uint64(Autoarr *ar, uint32 index, uint64 element){
|
||||
*(*((uint64**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
|
||||
void Autoarr_free(Autoarr* ar){
|
||||
void Autoarr_clear(Autoarr* ar){
|
||||
switch (ar->type) {
|
||||
case Int8:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
@@ -1,10 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "base_types.h"
|
||||
#include "errors.h"
|
||||
#include "../base/base.h"
|
||||
|
||||
typedef struct Autoarr{
|
||||
typedef struct Autoarr{ // a collection with dynamic memory allocation
|
||||
base_type type; // type of data
|
||||
uint16 max_block_count; // max amount of blocks
|
||||
uint16 curr_block_count; // amount of blocks
|
||||
@@ -16,6 +14,7 @@ typedef struct Autoarr{
|
||||
} Autoarr;
|
||||
|
||||
Autoarr Autoarr_create(uint16 _max_block_count, uint16 _max_block_length, base_type _type);
|
||||
void Autoarr_clear(Autoarr* ar);
|
||||
|
||||
void Autoarr_add_int8(Autoarr *ar, int8 element);
|
||||
void Autoarr_add_uint8(Autoarr *ar, uint8 element);
|
||||
@@ -43,5 +42,3 @@ void Autoarr_set_int32(Autoarr *ar, uint32 index, int32 element);
|
||||
void Autoarr_set_uint32(Autoarr *ar, uint32 index, uint32 element);
|
||||
void Autoarr_set_int64(Autoarr *ar, uint32 index, int64 element);
|
||||
void Autoarr_set_uint64(Autoarr *ar, uint32 index, uint64 element);
|
||||
|
||||
void Autoarr_free(Autoarr* ar);
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
|
||||
72
DtsodC/src/SearchTree/SearchTree.c
Normal file
72
DtsodC/src/SearchTree/SearchTree.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "SearchTree.h"
|
||||
|
||||
STNode* STNode_create(){
|
||||
STNode* node=malloc(sizeof(STNode));
|
||||
node->branches=NULL;
|
||||
node->value.ptr=NULL;
|
||||
node->value.type=Null;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
void STNode_free(STNode* node){
|
||||
ifNthrow(node);
|
||||
if(node->branches!=NULL){
|
||||
for(uint8 n32 = 0;n32<8;n32++){
|
||||
STNode*** ptrn32=(STNode***)node->branches[n32];
|
||||
if(ptrn32!=NULL){
|
||||
for(uint8 n4 = 0;n4<8;n4++){
|
||||
STNode** ptrn4=ptrn32[n4];
|
||||
if (ptrn4!=NULL){
|
||||
for(uint8 rem=0;rem<4;rem++){
|
||||
STNode* ptrrem=ptrn4[rem];
|
||||
if(ptrrem!=NULL){
|
||||
STNode_free(ptrrem);
|
||||
free(ptrrem);
|
||||
}
|
||||
}
|
||||
free(ptrn4);
|
||||
}
|
||||
}
|
||||
free(ptrn32);
|
||||
}
|
||||
}
|
||||
free(node->branches);
|
||||
}
|
||||
free(node->value.ptr);
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
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){
|
||||
char c = *key;
|
||||
for (uint16 i=0;c!='\0';){
|
||||
printf("[%u]%c ",i,c);
|
||||
c=*(key+(++i));
|
||||
}
|
||||
}
|
||||
19
DtsodC/src/SearchTree/SearchTree.h
Normal file
19
DtsodC/src/SearchTree/SearchTree.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
typedef struct UniversalType{
|
||||
base_type type;
|
||||
void* ptr;
|
||||
} Unitype;
|
||||
|
||||
typedef struct SearchTreeNode{
|
||||
struct SearchTreeNode**** branches; //*STNode[8][8][4]
|
||||
Unitype value;
|
||||
} STNode;
|
||||
|
||||
STNode* STNode_create(void);
|
||||
void STNode_free(STNode* node);
|
||||
|
||||
void ST_push(STNode* node, const char* key, Unitype value);
|
||||
Unitype ST_pull(STNode* node, const char* key);
|
||||
25
DtsodC/src/SearchTree/SearchTree.md
Normal file
25
DtsodC/src/SearchTree/SearchTree.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# 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
|
||||
```
|
||||
3
DtsodC/src/base/base.h
Normal file
3
DtsodC/src/base/base.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "std.h"
|
||||
#include "base_types.h"
|
||||
#include "errors.h"
|
||||
@@ -13,7 +13,7 @@ const char* typename(base_type t){
|
||||
case UInt32: return "UInt32";
|
||||
case Int64: return "Int64";
|
||||
case UInt64: return "UInt64";
|
||||
default: throw(ERR_WRONGTYPE); break;
|
||||
default: throw(ERR_WRONGTYPE); return "EEEEEE";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,9 @@ typedef int32_t int32;
|
||||
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
|
||||
} base_type;
|
||||
Null, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Char, Bool
|
||||
} __attribute__ ((__packed__)) base_type;
|
||||
|
||||
const char* typename(base_type t);
|
||||
|
||||
|
||||
@@ -8,15 +8,19 @@ const char* errname(err_t err){
|
||||
case ERR_WRONGTYPE: return "ERR_WRONGTYPE";
|
||||
case ERR_WRONGINDEX: return "ERR_WRONGINDEX";
|
||||
case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED";
|
||||
case ERR_NULLPTR: return "ERR_NULLPTR";
|
||||
default: return "UNKNOWN_ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
void _throwint(int err, const char* srcfile, int line, const char* funcname){
|
||||
printf("\e[31m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err));
|
||||
exit(err);
|
||||
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[31m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
|
||||
printf("\e[91m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
|
||||
exit(255);
|
||||
}
|
||||
@@ -1,14 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum err_t {
|
||||
SUCCESS, ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED
|
||||
SUCCESS, //not an error
|
||||
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR
|
||||
} 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("NONE\n")))
|
||||
|
||||
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
|
||||
@@ -1,5 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "base_types.h"
|
||||
|
||||
|
||||
@@ -17,3 +17,4 @@
|
||||
|
||||
#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)
|
||||
@@ -1,12 +1,16 @@
|
||||
#include "base/std.h"
|
||||
#include "base/base_types.h"
|
||||
#include "base/errors.h"
|
||||
#include "base/Autoarr.h"
|
||||
#include "base/base.h"
|
||||
#include "tests/tests.h"
|
||||
#include "Autoarr/Autoarr.h"
|
||||
#include "SearchTree/SearchTree.h"
|
||||
|
||||
#define clrscr() printf("\e[1;1H\e[2J")
|
||||
|
||||
int main(){
|
||||
setlocale(LC_ALL, "en-US.Unicode");
|
||||
printf("\e[92mdtsod parser in c language!\e[94m\n");
|
||||
printf("\e[92mdtsod parser in c language!\e[97m\n");
|
||||
test_all();
|
||||
Unitype a={Null,NULL};
|
||||
STNode* node=STNode_create();
|
||||
ST_push(node,"aboba", a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "../base/base.h"
|
||||
|
||||
const wchar_t* slimak =
|
||||
U" ▄▄▄ \n"
|
||||
|
||||
6
DtsodC/src/tests/test_all.c
Normal file
6
DtsodC/src/tests/test_all.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "tests.h"
|
||||
|
||||
void test_all(void){
|
||||
test_autoarr();
|
||||
test_searchtree();
|
||||
}
|
||||
@@ -1,40 +1,43 @@
|
||||
#include "tests.h"
|
||||
#include "../base/Autoarr.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
|
||||
void ardesc(Autoarr* ar){
|
||||
printf("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",
|
||||
sizeof(Autoarr),
|
||||
typename(ar->type),
|
||||
ar->max_block_count,
|
||||
ar->curr_block_count,
|
||||
ar->max_block_length,
|
||||
ar->curr_block_length,
|
||||
ar->max_length,
|
||||
ar->curr_length);
|
||||
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",
|
||||
sizeof(Autoarr),
|
||||
typename(ar->type),
|
||||
ar->max_block_count,
|
||||
ar->curr_block_count,
|
||||
ar->max_block_length,
|
||||
ar->curr_block_length,
|
||||
ar->max_length,
|
||||
ar->curr_length);
|
||||
}
|
||||
|
||||
void fillar(Autoarr* ar){
|
||||
for (uint16 i=0;i<ar->max_length;i++)
|
||||
Autoarr_add_uint16(ar,i);
|
||||
ardesc(ar);
|
||||
}
|
||||
|
||||
void printar(Autoarr* ar){
|
||||
for (uint16 i=0;i<ar->max_length;i++)
|
||||
printf("%u ", Autoarr_get_uint16(&ar,i));
|
||||
printf("%u ", Autoarr_get_uint16(ar,i));
|
||||
}
|
||||
|
||||
void testar(){
|
||||
void test_autoarr(){
|
||||
printf("\e[96m------------[test_autoarr]-------------\n");
|
||||
Autoarr ar=Autoarr_create(10,16,UInt16);
|
||||
printf("\e[92m autoarr created\n\e[90m");
|
||||
fillar(&ar);
|
||||
printar(&ar);
|
||||
Autoarr_free(&ar);
|
||||
printf("\n\e[92m autoarr filled up\n");
|
||||
ardesc(&ar);
|
||||
Autoarr_clear(&ar);
|
||||
printf("\e[92m autoarr cleared\n");
|
||||
}
|
||||
|
||||
23
DtsodC/src/tests/test_searchtree.c
Normal file
23
DtsodC/src/tests/test_searchtree.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#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 test_searchtree(){
|
||||
printf("\e[96m-----------[test_searchtree]-----------\n");
|
||||
STNode* node=STNode_create();
|
||||
printf("\e[92m node created\n");
|
||||
printn(node);
|
||||
STNode_free(node);
|
||||
printf("\e[92m node deleted\n");
|
||||
}
|
||||
@@ -3,3 +3,6 @@
|
||||
#include "../base/std.h"
|
||||
|
||||
void test_autoarr(void);
|
||||
void test_searchtree(void);
|
||||
|
||||
void test_all(void);
|
||||
|
||||
Reference in New Issue
Block a user