From 99c06d1b66d26e3c39f9b3df93ee0a8cd36c162f Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 16 Feb 2022 16:07:07 +0300 Subject: [PATCH] 80 --- DtsodC/Makefile | 10 ++-- DtsodC/src/Hashtable/Hashtable.c | 96 ++++++++++++++++++++++++++------ DtsodC/src/Hashtable/Hashtable.h | 23 +++----- DtsodC/src/list.h | 85 ---------------------------- DtsodC/src/main.c | 6 -- DtsodC/src/tests/tests.c | 4 +- DtsodC/src/tests/tests.h | 2 +- 7 files changed, 97 insertions(+), 129 deletions(-) delete mode 100644 DtsodC/src/list.h diff --git a/DtsodC/Makefile b/DtsodC/Makefile index fdd3696..cf9f299 100644 --- a/DtsodC/Makefile +++ b/DtsodC/Makefile @@ -7,7 +7,7 @@ all: clear_c build test clear_c: clear clear_bin: - @echo "\e[36m-------------[clear_bin]---------------\e[0m" + @echo -e "\e[36m-------------[clear_bin]---------------\e[0m" touch $(OUTDIR)_.com rm $(OUTDIR)*.com @@ -16,14 +16,14 @@ clang: all CMPARGS= -Wall $(SRC) -o $(OUTFILE) build: - @echo "\e[36m-------------[build]---------------\e[0m" + @echo -e "\e[36m-------------[build]---------------\e[0m" $(CMP) -O1 -flto $(CMPARGS) build_dbg: - @echo "\e[36m-------------[build_dbg]---------------\e[0m" + @echo -e "\e[36m-------------[build_dbg]---------------\e[0m" $(CMP) -O0 $(CMPARGS).dbg test: - @echo "\e[36m-------------[test]----------------\e[0m" + @echo -e "\e[36m-------------[test]----------------\e[0m" $(OUTFILE) valgrind: clear_c build_dbg - @echo "\e[36m-----------[valgrind]--------------\e[0m" + @echo -e "\e[36m-----------[valgrind]--------------\e[0m" valgrind -s --read-var-info=yes --fullpath-after=DtsodC/ $(OUTFILE).dbg \ No newline at end of file diff --git a/DtsodC/src/Hashtable/Hashtable.c b/DtsodC/src/Hashtable/Hashtable.c index a154803..195ce16 100644 --- a/DtsodC/src/Hashtable/Hashtable.c +++ b/DtsodC/src/Hashtable/Hashtable.c @@ -1,24 +1,88 @@ #include "Hashtable.h" -/* -Hashtable Hashtable_create(uint16 height){ - Hashtable h={ - .height=height, - .rows=malloc(height*sizeof(Autoarr)) - }; - return h; + +define_Autoarr2(KeyValuePair) + +Hashtable* Hashtable_create(){ + Hashtable* ht=malloc(sizeof(Hashtable)); + ht->hein=HT_HEIGHTS[0]; + ht->rows=malloc(HT_HEIGHTS[0]*sizeof(Autoarr2(KeyValuePair))); + for(uint16 i;irows[i]=Autoarr2_create(KeyValuePair,4,16); + return ht; } -void Hashtable_clear(Hashtable* ht){ - for(uint16 i=0;iheight;i++) - Autoarr_clear(ht->rows+i); -} +static Autoarr2(KeyValuePair)* getrow(Hashtable* ht,char* key){ + uint16 rown=HT_HEIGHTS[ht->hein]%ihash(key); + if(rown>=HT_HEIGHTS[ht->hein]){ + ht->rows=realloc(ht->rows,HT_HEIGHTS[++ht->hein]*sizeof(Autoarr2(KeyValuePair))); + for(uint16 i=HT_HEIGHTS[ht->hein-1];ihein];i++) + ht->rows[i]=Autoarr2_create(KeyValuePair,4,16); + } + return ht->rows+rown; +} -void Hashtable_add_kvpair(Hashtable* ht, KeyValuePair pair){ - uint16 i=ht->height%ihash(pair.key); - Autoarr_add_kvpair(ht->rows[i],pair); +void Hashtable_free(Hashtable* ht){ + for(uint16 i=0;ihein];i++) + Autoarr2_clear(((Autoarr2(KeyValuePair)*)(ht->rows+i))); + free(ht->rows); + free(ht); +} + +void Hashtable_add_pair(Hashtable* ht, KeyValuePair pair){ + Autoarr2_add(getrow(ht,pair.key),pair); +} + +//returns length of string (including \0) +uint32 mystrlen(char* str){ + uint32 len=0; + while(*(str++)) len++; + return len++; +} + +//allocates new char[] and copies src there +char* mystrcpy(char* src){ + uint32 len=mystrlen(src); + char* dst=malloc(len*sizeof(char)); + while(--len<0) + dst[len]=src[len]; + return dst; +} + +//compares two strings, NullPtr-friendly +bool mystrcmp(char* key0, char* key1){ + if(!key0) return key1 ? 0 : 1; + else if(!key1) return 0; + while(*key0&&*key1){ + if(*key0!=*key1) return 0; + key0++; + key1++; + } + return 1; } void Hashtable_add(Hashtable* ht, char* key, Unitype value){ - Hashtable_add_kvpair(ht,(KeyValuePair){key,value}); + Hashtable_add_pair(ht,(KeyValuePair){key,value}); +} + +Unitype Hashtable_get(Hashtable* ht, char* key){ + Autoarr2(KeyValuePair)* ar=getrow(ht,key); + uint32 arlen=Autoarr2_length(ar); + for(uint32 i=0;i_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 7c1bebb..93f0c1d 100644 --- a/DtsodC/src/main.c +++ b/DtsodC/src/main.c @@ -6,11 +6,5 @@ int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); optime({test_all();}); - optime({ - Autoarr2(uint64) _ar2=Autoarr2_create(uint64,10000,20000); - Autoarr2(uint64)* ar2=&_ar2; - for(uint32 i=0;i