This commit is contained in:
Timerix22 2022-02-16 16:07:07 +03:00
parent 8886f3acb4
commit 99c06d1b66
7 changed files with 97 additions and 129 deletions

View File

@ -7,7 +7,7 @@ all: clear_c build test
clear_c: clear_c:
clear clear
clear_bin: clear_bin:
@echo "\e[36m-------------[clear_bin]---------------\e[0m" @echo -e "\e[36m-------------[clear_bin]---------------\e[0m"
touch $(OUTDIR)_.com touch $(OUTDIR)_.com
rm $(OUTDIR)*.com rm $(OUTDIR)*.com
@ -16,14 +16,14 @@ clang: all
CMPARGS= -Wall $(SRC) -o $(OUTFILE) CMPARGS= -Wall $(SRC) -o $(OUTFILE)
build: build:
@echo "\e[36m-------------[build]---------------\e[0m" @echo -e "\e[36m-------------[build]---------------\e[0m"
$(CMP) -O1 -flto $(CMPARGS) $(CMP) -O1 -flto $(CMPARGS)
build_dbg: build_dbg:
@echo "\e[36m-------------[build_dbg]---------------\e[0m" @echo -e "\e[36m-------------[build_dbg]---------------\e[0m"
$(CMP) -O0 $(CMPARGS).dbg $(CMP) -O0 $(CMPARGS).dbg
test: test:
@echo "\e[36m-------------[test]----------------\e[0m" @echo -e "\e[36m-------------[test]----------------\e[0m"
$(OUTFILE) $(OUTFILE)
valgrind: clear_c build_dbg 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 valgrind -s --read-var-info=yes --fullpath-after=DtsodC/ $(OUTFILE).dbg

View File

@ -1,24 +1,88 @@
#include "Hashtable.h" #include "Hashtable.h"
/*
Hashtable Hashtable_create(uint16 height){ define_Autoarr2(KeyValuePair)
Hashtable h={
.height=height, Hashtable* Hashtable_create(){
.rows=malloc(height*sizeof(Autoarr)) Hashtable* ht=malloc(sizeof(Hashtable));
}; ht->hein=HT_HEIGHTS[0];
return h; ht->rows=malloc(HT_HEIGHTS[0]*sizeof(Autoarr2(KeyValuePair)));
for(uint16 i;i<HT_HEIGHTS[0];i++)
ht->rows[i]=Autoarr2_create(KeyValuePair,4,16);
return ht;
} }
void Hashtable_clear(Hashtable* ht){ static Autoarr2(KeyValuePair)* getrow(Hashtable* ht,char* key){
for(uint16 i=0;i<ht->height;i++) uint16 rown=HT_HEIGHTS[ht->hein]%ihash(key);
Autoarr_clear(ht->rows+i); 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];i<HT_HEIGHTS[ht->hein];i++)
ht->rows[i]=Autoarr2_create(KeyValuePair,4,16);
}
return ht->rows+rown;
} }
void Hashtable_add_kvpair(Hashtable* ht, KeyValuePair pair){ void Hashtable_free(Hashtable* ht){
uint16 i=ht->height%ihash(pair.key); for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
Autoarr_add_kvpair(ht->rows[i],pair); 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){ 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<arlen;i++){
KeyValuePair p=Autoarr2_get(ar,i);
if(mystrcmp(key,p.key)) return p.value;
}
return (Unitype){.type=Null,.VoidPtr=NULL};
}
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){
return (KeyValuePair){
.key=key,
.value=Hashtable_get(ht,key)
};
}
bool Hashtable_try_get(Hashtable* ht, char* key,Unitype* output){
Unitype u=Hashtable_get(ht,key);
*output=u;
return u.type==Null;
} }
*/

View File

@ -4,22 +4,17 @@
#include "../Autoarr/Autoarr2.h" #include "../Autoarr/Autoarr2.h"
#include "hash.h" #include "hash.h"
typedef struct Hashtable{
uint16 height; // amount of rows
Autoarr2(Unitype)* rows; // Autoarr[height]s
} Hashtable;
Hashtable Hashtable_create(uint16 height);
void Hashtable_clear(Hashtable* ht);
typedef struct KeyValuePair{ typedef struct KeyValuePair{
char* key; char* key;
Unitype value; Unitype value;
} KeyValuePair; } KeyValuePair;
const uint16 Hashtable_HTINY=61; declare_Autoarr2(KeyValuePair)
const uint16 Hashtable_HSMALL=631;
const uint16 Hashtable_HMED=3889; typedef struct Hashtable{
const uint16 Hashtable_HLARGE=19441; uint8 hein; //height=HT_HEIGHTS[hein]
const uint16 Hashtable_HMAX=65521; Autoarr2(KeyValuePair)* rows; // Autoarr[height]
} Hashtable;
// amount of rows
const uint16 HT_HEIGHTS[]={61,631,3889,19441,65521};

View File

@ -1,85 +0,0 @@
#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)

View File

@ -6,11 +6,5 @@ int main(){
setlocale(LC_ALL, "en-US.Unicode"); setlocale(LC_ALL, "en-US.Unicode");
printf("\e[92mdtsod parser in c language!\e[97m\n"); printf("\e[92mdtsod parser in c language!\e[97m\n");
optime({test_all();}); optime({test_all();});
optime({
Autoarr2(uint64) _ar2=Autoarr2_create(uint64,10000,20000);
Autoarr2(uint64)* ar2=&_ar2;
for(uint32 i=0;i<Autoarr2_max_length(ar2);i++)
Autoarr2_add(ar2,8);
});
return 0; return 0;
} }

View File

@ -29,7 +29,7 @@ void printuni(Unitype v){
} }
void test_all(void){ void test_all(void){
test_searchtree(); optime({test_searchtree();});
test_autoarr2(); optime({test_autoarr2();});
printf("\e[96m---------------------------------------\n"); printf("\e[96m---------------------------------------\n");
} }

View File

@ -14,5 +14,5 @@ void test_autoarr2(void);
clock_t start=clock();\ clock_t start=clock();\
(codeblock);\ (codeblock);\
clock_t stop=clock();\ clock_t stop=clock();\
printf("\e[96m%li\n",(stop-start));\ printf("\e[93m%li\n",(stop-start));\
}) })