From 99da265217ed69e0155bb80396d291719e82e517 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 23 Feb 2022 21:13:11 +0300 Subject: [PATCH] another small optimization --- DtsodC/src/DtsodParser/DtsodV24.c | 29 +++++++++++++++++++++++++++++ DtsodC/src/DtsodParser/DtsodV24.h | 2 ++ DtsodC/src/Hashtable/Hashtable.c | 9 ++------- DtsodC/src/Hashtable/Hashtable.h | 7 +++++-- DtsodC/src/tests/test_hashtable.c | 6 ++---- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/DtsodC/src/DtsodParser/DtsodV24.c b/DtsodC/src/DtsodParser/DtsodV24.c index 8363a10..5b2ba18 100644 --- a/DtsodC/src/DtsodParser/DtsodV24.c +++ b/DtsodC/src/DtsodParser/DtsodV24.c @@ -1,2 +1,31 @@ #include "DtsodV24.h" + +Hashtable* __parse(const char* text, bool called_recursively){ + Hashtable* dict=Hashtable_create(); + /* char c; + + char* ReadName(){ + + }; + + Unitype ReadValue(){ + + }; + + void SkipComment(){ + + }; + + + while ((c=*text)){ + char* n=ReadName(); + Unitype v=ReadValue(); + Hashtable_add(dict,n,v); + text++; + } */ + + return dict; +} + +Hashtable* DtsodV24_parse(const char* text) { return __parse(text,false); } diff --git a/DtsodC/src/DtsodParser/DtsodV24.h b/DtsodC/src/DtsodParser/DtsodV24.h index bc7d4f7..6a40bdd 100644 --- a/DtsodC/src/DtsodParser/DtsodV24.h +++ b/DtsodC/src/DtsodParser/DtsodV24.h @@ -1,3 +1,5 @@ #pragma once #include "../base/base.h" +#include "../Hashtable/Hashtable.h" +Hashtable* DtsodV24_parse(const char* text); diff --git a/DtsodC/src/Hashtable/Hashtable.c b/DtsodC/src/Hashtable/Hashtable.c index 9d14d09..8d9a5ec 100644 --- a/DtsodC/src/Hashtable/Hashtable.c +++ b/DtsodC/src/Hashtable/Hashtable.c @@ -62,11 +62,6 @@ Autoarr2(KeyValuePair)* getrow(Hashtable* ht, char* key, bool can_expand){ return ar; } -//copies string and value to new KeyValuePair -KeyValuePair cpair(char* key, Unitype value){ - return (KeyValuePair){.key=mystrcpy(key),.value=value}; -} - void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){ Autoarr2_add(getrow(ht,p.key,true),p); @@ -104,9 +99,9 @@ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ return u.type==Null; } -void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){ +/* 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,cpair(key,u)); } +void Hashtable_set(Hashtable* ht, char* key, Unitype u){ Hashtable_set_pair(ht,cpair(key,u)); } */ diff --git a/DtsodC/src/Hashtable/Hashtable.h b/DtsodC/src/Hashtable/Hashtable.h index 6b000aa..3de7438 100644 --- a/DtsodC/src/Hashtable/Hashtable.h +++ b/DtsodC/src/Hashtable/Hashtable.h @@ -22,10 +22,12 @@ typedef struct Hashtable{ Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); +//amount of rows uint32 Hashtable_height(Hashtable* ht); -//copies string and value to new KeyValuePair -KeyValuePair cpair(char* key, Unitype value); +//adds charptr and value to new KeyValuePair +//use mystrcpy() to create new string if needed +#define cpair(key,value) (KeyValuePair){key,value} void Hashtable_add_pair(Hashtable* ht, KeyValuePair p); @@ -38,5 +40,6 @@ 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); \ No newline at end of file diff --git a/DtsodC/src/tests/test_hashtable.c b/DtsodC/src/tests/test_hashtable.c index 569d143..3477100 100644 --- a/DtsodC/src/tests/test_hashtable.c +++ b/DtsodC/src/tests/test_hashtable.c @@ -42,21 +42,19 @@ void printrowgraph(Hashtable* ht){ } void fill(Hashtable* ht){ - char* key=malloc(20); for(uint32 i=0;i<100000;i++){ + char* key=malloc(12); sprintf(key,"key__%u",i); Hashtable_add(ht,key,Uni(UInt32,i)); } - free(key); } Unitype gett(Hashtable* ht){ - char* key=malloc(20); + char* key=malloc(12); Unitype u; for(uint32 i=0;i<100000;i++){ sprintf(key,"key__%u",i); u=Hashtable_get(ht,key); - //printuni(u); printf(" "); } free(key); return u;