From a25f5bd5f146de12c68b3a0bfdcb6805950b4db4 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 14 Feb 2022 23:24:33 +0300 Subject: [PATCH] 75 --- DtsodC/.vscode/settings.json | 4 +- DtsodC/src/Autoarr/new.h | 67 ++++++++++++++++++++++++++++++ DtsodC/src/Hashtable/Hashtable.c | 10 ++--- DtsodC/src/Hashtable/Hashtable.h | 6 +-- DtsodC/src/SearchTree/SearchTree.c | 1 + DtsodC/src/main.c | 2 +- DtsodC/src/tests/new.c | 17 ++++++++ DtsodC/src/tests/new.h | 1 + 8 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 DtsodC/src/Autoarr/new.h create mode 100644 DtsodC/src/tests/new.c create mode 100644 DtsodC/src/tests/new.h diff --git a/DtsodC/.vscode/settings.json b/DtsodC/.vscode/settings.json index 4a3e2d9..0e7b133 100644 --- a/DtsodC/.vscode/settings.json +++ b/DtsodC/.vscode/settings.json @@ -4,7 +4,9 @@ "cstdint": "c", "limits": "c", "cstdio": "c", - "cwchar": "c" + "cwchar": "c", + "compare": "c", + "type_traits": "c" }, "C_Cpp.default.defines": [], "C_Cpp.intelliSenseCacheSize": 10 diff --git a/DtsodC/src/Autoarr/new.h b/DtsodC/src/Autoarr/new.h new file mode 100644 index 0000000..e7a7369 --- /dev/null +++ b/DtsodC/src/Autoarr/new.h @@ -0,0 +1,67 @@ +#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(struct Autoarr_##type* ar, type element){ \ + if(!ar->values){ \ + ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \ + ar->values[0]=malloc(ar->max_block_length*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*typesize(ar->type)); \ + ar->blocks_count++; \ + } \ + ar->values[ar->blocks_count-1][ar->block_length]=element; \ + ar->block_length++; \ +} \ +\ +type Autoarr_get_##type(struct 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(struct 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) diff --git a/DtsodC/src/Hashtable/Hashtable.c b/DtsodC/src/Hashtable/Hashtable.c index 3da9dca..a154803 100644 --- a/DtsodC/src/Hashtable/Hashtable.c +++ b/DtsodC/src/Hashtable/Hashtable.c @@ -1,26 +1,24 @@ #include "Hashtable.h" - +/* Hashtable Hashtable_create(uint16 height){ Hashtable h={ .height=height, - .rows=malloc(height*sizeof(Autoarr*)) + .rows=malloc(height*sizeof(Autoarr)) }; return h; } void Hashtable_clear(Hashtable* ht){ for(uint16 i=0;iheight;i++) - Autoarr_clear(ht->rows[i]); - free(ht->rows); + Autoarr_clear(ht->rows+i); } void Hashtable_add_kvpair(Hashtable* ht, KeyValuePair pair){ uint16 i=ht->height%ihash(pair.key); - if(!ht->rows[i]) - ht->rows[i]=Autoarr_create(100,4,ht->type); Autoarr_add_kvpair(ht->rows[i],pair); } void Hashtable_add(Hashtable* ht, char* key, Unitype value){ Hashtable_add_kvpair(ht,(KeyValuePair){key,value}); } +*/ \ No newline at end of file diff --git a/DtsodC/src/Hashtable/Hashtable.h b/DtsodC/src/Hashtable/Hashtable.h index 823c936..11a6e34 100644 --- a/DtsodC/src/Hashtable/Hashtable.h +++ b/DtsodC/src/Hashtable/Hashtable.h @@ -6,10 +6,10 @@ typedef struct Hashtable{ uint16 height; // amount of rows - Autoarr** rows; // Autoarr[height]s + Autoarr* rows; // Autoarr[height]s } Hashtable; -Hashtable Hashtable_create(uint16 height,my_type type); +Hashtable Hashtable_create(uint16 height); void Hashtable_clear(Hashtable* ht); @@ -22,4 +22,4 @@ const uint16 Hashtable_HTINY=61; const uint16 Hashtable_HSMALL=631; const uint16 Hashtable_HMED=3889; const uint16 Hashtable_HLARGE=19441; -const uint16 Hashtable_HMAX=65536; +const uint16 Hashtable_HMAX=65521; diff --git a/DtsodC/src/SearchTree/SearchTree.c b/DtsodC/src/SearchTree/SearchTree.c index 3e23483..3d69c5c 100644 --- a/DtsodC/src/SearchTree/SearchTree.c +++ b/DtsodC/src/SearchTree/SearchTree.c @@ -1,4 +1,5 @@ #include "SearchTree.h" +#include "../Autoarr/Autoarr.h" STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); diff --git a/DtsodC/src/main.c b/DtsodC/src/main.c index f8596b1..d831adc 100644 --- a/DtsodC/src/main.c +++ b/DtsodC/src/main.c @@ -1,9 +1,9 @@ #include "base/base.h" #include "tests/tests.h" -#include "Autoarr/Autoarr.h" #include "SearchTree/SearchTree.h" #include "Hashtable/hash.h" + int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); diff --git a/DtsodC/src/tests/new.c b/DtsodC/src/tests/new.c new file mode 100644 index 0000000..66168c7 --- /dev/null +++ b/DtsodC/src/tests/new.c @@ -0,0 +1,17 @@ +#include "new.h" +#include "../Autoarr/new.h" +#include "../base/base.h" + +define_Autoarr(uint16) + +void test_new(){ + printf("\e[96m------------[test_autoarr]-------------\n"); + Autoarr(uint16) ar=Autoarr_create(uint16,10,16); + printf("\e[92mautoarr created\n\e[90m"); + fillar(&ar); + printallval(&ar); + printf("\n\e[92mautoarr filled up\n"); + printautoarr(&ar); + Autoarr_clear(&ar); + printf("\e[92mautoarr cleared\n"); +} \ No newline at end of file diff --git a/DtsodC/src/tests/new.h b/DtsodC/src/tests/new.h new file mode 100644 index 0000000..275a59e --- /dev/null +++ b/DtsodC/src/tests/new.h @@ -0,0 +1 @@ +void test_new(); \ No newline at end of file