diff --git a/DtsodC/Makefile b/DtsodC/Makefile index ba77c07..76d2281 100644 --- a/DtsodC/Makefile +++ b/DtsodC/Makefile @@ -14,13 +14,16 @@ clear_bin: clang: CMP=clang clang: all -CMPARGS=-O0 -g -Wall -Werror +CMPARGS= -Wall $(SRC) -o $(OUTFILE) build: @echo "\e[36m-------------[build]---------------\e[0m" - $(CMP) $(CMPARGS) $(SRC) -o $(OUTFILE) + $(CMP) -O2 $(CMPARGS) +build_dbg: + @echo "\e[36m-------------[build_dbg]---------------\e[0m" + $(CMP) -O0 $(CMPARGS).dbg test: @echo "\e[36m-------------[test]----------------\e[0m" $(OUTFILE) -valgrind: clear_c build +valgrind: clear_c build_dbg @echo "\e[36m-----------[valgrind]--------------\e[0m" - valgrind -s --read-var-info=yes --fullpath-after=DtsodC/ $(OUTFILE) \ No newline at end of file + valgrind -s --read-var-info=yes --fullpath-after=DtsodC/ $(OUTFILE).dbg \ No newline at end of file diff --git a/DtsodC/src/Autoarr/Autoarr.c b/DtsodC/src/Autoarr/Autoarr.c index bb5bc85..627870d 100644 --- a/DtsodC/src/Autoarr/Autoarr.c +++ b/DtsodC/src/Autoarr/Autoarr.c @@ -12,7 +12,6 @@ Autoarr Autoarr_create(uint16 _max_block_count, uint16 _max_block_length, my_typ .values=malloc(_max_block_count*sizeof(void*)) }; *ar.values=malloc(_max_block_length*typesize(ar.type)); - printf("%p %p\n",ar.values, *ar.values); return ar; } diff --git a/DtsodC/src/Autoarr/Autoarr2.c b/DtsodC/src/Autoarr/Autoarr2.c new file mode 100644 index 0000000..c95c954 --- /dev/null +++ b/DtsodC/src/Autoarr/Autoarr2.c @@ -0,0 +1,13 @@ +#include "Autoarr2.h" + +define_Autoarr2(uint8) +define_Autoarr2(int8) +define_Autoarr2(uint16) +define_Autoarr2(int16) +define_Autoarr2(uint32) +define_Autoarr2(int32) +define_Autoarr2(uint64) +define_Autoarr2(int64) +define_Autoarr2(float) +define_Autoarr2(double) +define_Autoarr2(Unitype) diff --git a/DtsodC/src/Autoarr/Autoarr2.h b/DtsodC/src/Autoarr/Autoarr2.h new file mode 100644 index 0000000..c5b96c5 --- /dev/null +++ b/DtsodC/src/Autoarr/Autoarr2.h @@ -0,0 +1,17 @@ +#pragma once + +#include "Autoarr2_declare.h" +#include "Autoarr2_define.h" + +declare_Autoarr2(uint8) +declare_Autoarr2(int8) +declare_Autoarr2(uint16) +declare_Autoarr2(int16) +declare_Autoarr2(uint32) +declare_Autoarr2(int32) +declare_Autoarr2(uint64) +declare_Autoarr2(int64) +declare_Autoarr2(float) +declare_Autoarr2(double) +declare_Autoarr2(Unitype) + diff --git a/DtsodC/src/Autoarr/Autoarr2_declare.h b/DtsodC/src/Autoarr/Autoarr2_declare.h new file mode 100644 index 0000000..f36e08f --- /dev/null +++ b/DtsodC/src/Autoarr/Autoarr2_declare.h @@ -0,0 +1,48 @@ +#pragma once + +#include "../base/base.h" + +#define declare_Autoarr2(type)\ +\ +struct Autoarr2_##type;\ +\ +typedef struct {\ + void (*add)(struct Autoarr2_##type* ar, type element);\ + type (*get)(struct Autoarr2_##type* ar, uint32 index);\ + void (*set)(struct Autoarr2_##type* ar, uint32 index, type element);\ + void (*clear)(struct Autoarr2_##type* ar);\ +} __functions_list_t_##type;\ +\ +typedef struct Autoarr2_##type{\ + uint16 blocks_count;\ + uint16 max_blocks_count;\ + uint16 block_length;\ + uint16 max_block_length;\ + type** values;\ + __functions_list_t_##type* functions;\ +} Autoarr2_##type;\ +\ +void __Autoarr2_add_##type(Autoarr2_##type* ar, type element);\ +type __Autoarr2_get_##type(Autoarr2_##type* ar, uint32 index);\ +void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element);\ +void __Autoarr2_clear_##type(Autoarr2_##type* ar);\ +Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_block_length); + +#define Autoarr2(type) Autoarr2_##type + +#define Autoarr2_add(autoarr, element)\ + autoarr->functions->add(autoarr, element) +#define Autoarr2_get(autoarr, index)\ + autoarr->functions->get(autoarr,index) +#define Autoarr2_set(autoarr, index, element)\ + autoarr->functions->set(autoarr, index, element) +#define Autoarr2_clear(autoarr)\ + autoarr->functions->clear(autoarr) +#define Autoarr2_create(type, max_blocks_count, max_block_length)\ + __Autoarr2_create_##type(max_blocks_count, max_block_length) + +#define Autoarr2_length(autoarr) \ + (uint32)(!autoarr->blocks_count ? 0 : \ + autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length) +#define Autoarr2_max_length(autoarr)\ + (uint32)(autoarr->max_block_length*autoarr->max_blocks_count) diff --git a/DtsodC/src/Autoarr/Autoarr2_define.h b/DtsodC/src/Autoarr/Autoarr2_define.h new file mode 100644 index 0000000..2a5973e --- /dev/null +++ b/DtsodC/src/Autoarr/Autoarr2_define.h @@ -0,0 +1,58 @@ +#pragma once + +#include "../base/base.h" + +#define define_Autoarr2(type)\ +\ +void __Autoarr2_add_##type(Autoarr2_##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 __Autoarr2_get_##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){\ + if(index>=Autoarr2_length(ar)) throw(ERR_WRONGINDEX);\ + ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ +}\ +\ +void __Autoarr2_clear_##type(Autoarr2_##type* ar){\ + for(uint16 i=0; iblocks_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={\ + &__Autoarr2_add_##type,\ + &__Autoarr2_get_##type,\ + &__Autoarr2_set_##type,\ + &__Autoarr2_clear_##type\ +};\ +\ +Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_block_length){\ + return (Autoarr2_##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\ + };\ +} diff --git a/DtsodC/src/Autoarr/new.h b/DtsodC/src/Autoarr/new.h deleted file mode 100644 index bdf5aff..0000000 --- a/DtsodC/src/Autoarr/new.h +++ /dev/null @@ -1,73 +0,0 @@ -#include "../base/base.h" - -#ifndef AUTOARR_H -#define AUTOARR_H - -#define Autoarr_length(autoarr) (uint32)(autoarr->max_block_length*(autoarr->max_blocks_count-1)+autoarr->block_length) -#define Autoarr_max_length(autoarr) (uint32)(autoarr->max_block_length*autoarr->max_blocks_count) - -#define define_Autoarr(type) \ -typedef struct Autoarr_##type{ \ - uint16 blocks_count; \ - uint16 max_blocks_count; \ - uint16 block_length; \ - uint16 max_block_length; \ - type** values; \ - void (*Autoarr_add)(struct Autoarr_##type* ar, type element); \ - type (*Autoarr_get)(struct Autoarr_##type* ar, uint32 index); \ - void (*Autoarr_set)(struct Autoarr_##type* ar, uint32 index, type element); \ -} 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]; \ -} \ -\ -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; \ -} \ -\ -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, \ - .Autoarr_add=Autoarr_add_##type, \ - .Autoarr_get=Autoarr_get_##type, \ - .Autoarr_set=Autoarr_set_##type \ - }; \ -} \ -\ -void Autoarr_clear_##type(Autoarr_##type* ar){ \ - for(uint16 i=0; iblocks_count;i++) \ - free(ar->values[i]); \ - free(ar->values); \ - ar->values=NULL; \ - ar->blocks_count=0; \ - ar->block_length=0; \ -} - -#define Autoarr(type) Autoarr_##type -#define Autoarr_create(type, max_blocks_count, max_block_length) \ - Autoarr_create_##type(max_blocks_count, max_block_length) - -#endif diff --git a/DtsodC/src/SearchTree/SearchTree.c b/DtsodC/src/SearchTree/SearchTree.c index 3d69c5c..be88281 100644 --- a/DtsodC/src/SearchTree/SearchTree.c +++ b/DtsodC/src/SearchTree/SearchTree.c @@ -39,10 +39,10 @@ void STNode_free(STNode* node){ case Int64Ptr: case UInt64Ptr: free(node->value.VoidPtr); break; - case AutoarrPtr: + /*case AutoarrPtr: Autoarr_clear((Autoarr*)node->value.VoidPtr); free(node->value.VoidPtr); - break; + break;*/ case STNodePtr: STNode_free((STNode*)node->value.VoidPtr); break; diff --git a/DtsodC/src/base/base.h b/DtsodC/src/base/base.h index 2d123d0..20104d1 100644 --- a/DtsodC/src/base/base.h +++ b/DtsodC/src/base/base.h @@ -4,6 +4,5 @@ #include "types.h" #include "errors.h" -// just sleeping function -// dont set 'milisec' > 1000 for good perfomance -void msleep(uint8 sec, uint16 milisec); +// sleep function based on std nanosleep() +void fsleep(float sec); diff --git a/DtsodC/src/base/fsleep.c b/DtsodC/src/base/fsleep.c new file mode 100644 index 0000000..36206e0 --- /dev/null +++ b/DtsodC/src/base/fsleep.c @@ -0,0 +1,8 @@ +#include "base.h" + +void fsleep(float sec){ + struct timespec t; + t.tv_sec=sec+0.0001f; + t.tv_nsec=(sec-t.tv_sec)*1000000000; + nanosleep(&t, NULL); +} diff --git a/DtsodC/src/base/msleep.c b/DtsodC/src/base/msleep.c deleted file mode 100644 index cfb1c23..0000000 --- a/DtsodC/src/base/msleep.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "base.h" - -void msleep(uint8 sec, uint16 milisec){ - if (sec>0) - sleep(sec); - if (milisec>0) - usleep(milisec*1000); -} diff --git a/DtsodC/src/base/std.h b/DtsodC/src/base/std.h index e3d7ce0..e8b4c7d 100644 --- a/DtsodC/src/base/std.h +++ b/DtsodC/src/base/std.h @@ -6,9 +6,7 @@ #include #include #include -#include #include -#include #define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N) diff --git a/DtsodC/src/base/types.c b/DtsodC/src/base/types.c index 212ab2d..0e1dec8 100644 --- a/DtsodC/src/base/types.c +++ b/DtsodC/src/base/types.c @@ -1,4 +1,3 @@ -#include "std.h" #include "types.h" #include "errors.h" diff --git a/DtsodC/src/base/types.h b/DtsodC/src/base/types.h index f0742e3..64fabd2 100644 --- a/DtsodC/src/base/types.h +++ b/DtsodC/src/base/types.h @@ -1,7 +1,6 @@ #pragma once #include "std.h" -#include "errors.h" typedef int8_t int8; typedef uint8_t uint8; @@ -15,7 +14,7 @@ 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, - UniversalType, AutoarrPtr, STNodePtr + UniversalType, STNodePtr } __attribute__ ((__packed__)) my_type; //returns type name diff --git a/DtsodC/src/list.h b/DtsodC/src/list.h new file mode 100644 index 0000000..0ac9851 --- /dev/null +++ b/DtsodC/src/list.h @@ -0,0 +1,85 @@ + +#define define_list(type) \ +\ + struct _list_##type; \ + \ + typedef struct \ + { \ + bool (*is_empty)(const struct _list_##type*); \ + size_t (*size)(const struct _list_##type*); \ + const type (*front)(const struct _list_##type*); \ + void (*push_front)(struct _list_##type*, type); \ + } _list_functions_##type; \ + \ + typedef struct _list_elem_##type \ + { \ + type _data; \ + struct _list_elem_##type* _next; \ + } list_elem_##type; \ + \ + typedef struct _list_##type \ + { \ + size_t _size; \ + list_elem_##type* _first; \ + list_elem_##type* _last; \ + _list_functions_##type* _functions; \ + } List_##type; \ + \ + List_##type* new_list_##type(); \ + bool list_is_empty_##type(const List_##type* list); \ + size_t list_size_##type(const List_##type* list); \ + const type list_front_##type(const List_##type* list); \ + void list_push_front_##type(List_##type* list, type elem); \ + \ + bool list_is_empty_##type(const List_##type* list) \ + { \ + return list->_size == 0; \ + } \ + \ + size_t list_size_##type(const List_##type* list) \ + { \ + return list->_size; \ + } \ + \ + const type list_front_##type(const List_##type* list) \ + { \ + return list->_first->_data; \ + } \ + \ + void list_push_front_##type(List_##type* list, type elem) \ + { \ + printf("%x",elem);\ + } \ + \ + _list_functions_##type _list_funcs_##type = { \ + &list_is_empty_##type, \ + &list_size_##type, \ + &list_front_##type, \ + &list_push_front_##type, \ + }; \ + \ + List_##type* new_list_##type() \ + { \ + List_##type* res = (List_##type*) malloc(sizeof(List_##type)); \ + res->_size = 0; \ + res->_first = NULL; \ + res->_functions = &_list_funcs_##type; \ + return res; \ + } + +#define List(type) \ + List_##type + +#define new_list(type) \ + new_list_##type() +#define is_empty(collection) \ + collection->_functions->is_empty(collection) + +#define size(collection) \ + collection->_functions->size(collection) + +#define front(collection) \ + collection->_functions->front(collection) + +#define push_front(collection, elem) \ + collection->_functions->push_front(collection, elem) diff --git a/DtsodC/src/main.c b/DtsodC/src/main.c index adc8834..b7d0bd4 100644 --- a/DtsodC/src/main.c +++ b/DtsodC/src/main.c @@ -1,10 +1,26 @@ #include "base/base.h" #include "tests/tests.h" +#include "Autoarr/Autoarr.h" +#include "Autoarr/Autoarr2.h" int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); //test_all(); - test_new(); + //test_autoarr2(); + long start=clock(); + Autoarr2(uint64) _ar2=Autoarr2_create(uint64,10000,20000); + Autoarr2(uint64)* ar2=&_ar2; + for(uint32 i=0;imax_length;i++) + Autoarr_add_uint64(ar,8); + stop=clock(); + printf("\e[96m%li\n",stop-start); return 0; } diff --git a/DtsodC/src/tests/new.c b/DtsodC/src/tests/new.c deleted file mode 100644 index 6fdd7e2..0000000 --- a/DtsodC/src/tests/new.c +++ /dev/null @@ -1,46 +0,0 @@ -#include "tests.h" -#include "../Autoarr/new.h" - -#ifndef Aaaa -#define Aaaa -define_Autoarr(uint16) -#endif - -void printautoarr(Autoarr(uint16)* ar){ - printf("\e[94mAUTOARR:%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)); -} - -void fillar(Autoarr(uint16)* ar){ - for (uint16 i=0;icurr_length); } -void fillar(Autoarr* ar){ +static void fillar(Autoarr* ar){ for (uint16 i=0;imax_length;i++) Autoarr_add_uint16(ar,i); } - -void printallval(Autoarr* ar){ +static void resetar(Autoarr* ar){ for (uint16 i=0;imax_length;i++) + Autoarr_set_uint16(ar,i,ar->max_length-i-1); +} + +static void printallval(Autoarr* ar){ + printf("\e[90m"); + for (uint16 i=0;icurr_length;i++) printf("%u ", Autoarr_get_uint16(ar,i)); + printf("\n"); } void test_autoarr(){ @@ -35,9 +41,12 @@ void test_autoarr(){ Autoarr ar=Autoarr_create(10,16,UInt16); printf("\e[92mautoarr created\n\e[90m"); fillar(&ar); - printallval(&ar); printf("\n\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"); } diff --git a/DtsodC/src/tests/test_autoarr2.c b/DtsodC/src/tests/test_autoarr2.c new file mode 100644 index 0000000..541eff3 --- /dev/null +++ b/DtsodC/src/tests/test_autoarr2.c @@ -0,0 +1,50 @@ +#include "tests.h" +#include "../Autoarr/Autoarr2.h" + +static void printautoarr(Autoarr2(uint16)* ar){ + printf("\e[94mAUTOARR:%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(Autoarr2(uint16)), + ar->max_blocks_count, + ar->blocks_count, + ar->max_block_length, + ar->block_length, + Autoarr2_max_length(ar), + Autoarr2_length(ar)); +} + +static void fillar(Autoarr2(uint16)* ar){ + for (uint16 i=0;i