80
This commit is contained in:
parent
8886f3acb4
commit
99c06d1b66
@ -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
|
||||
@ -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;i<HT_HEIGHTS[0];i++)
|
||||
ht->rows[i]=Autoarr2_create(KeyValuePair,4,16);
|
||||
return ht;
|
||||
}
|
||||
|
||||
void Hashtable_clear(Hashtable* ht){
|
||||
for(uint16 i=0;i<ht->height;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];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){
|
||||
uint16 i=ht->height%ihash(pair.key);
|
||||
Autoarr_add_kvpair(ht->rows[i],pair);
|
||||
void Hashtable_free(Hashtable* ht){
|
||||
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];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<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;
|
||||
}
|
||||
*/
|
||||
@ -4,22 +4,17 @@
|
||||
#include "../Autoarr/Autoarr2.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{
|
||||
char* key;
|
||||
Unitype value;
|
||||
} KeyValuePair;
|
||||
|
||||
const uint16 Hashtable_HTINY=61;
|
||||
const uint16 Hashtable_HSMALL=631;
|
||||
const uint16 Hashtable_HMED=3889;
|
||||
const uint16 Hashtable_HLARGE=19441;
|
||||
const uint16 Hashtable_HMAX=65521;
|
||||
declare_Autoarr2(KeyValuePair)
|
||||
|
||||
typedef struct Hashtable{
|
||||
uint8 hein; //height=HT_HEIGHTS[hein]
|
||||
Autoarr2(KeyValuePair)* rows; // Autoarr[height]
|
||||
} Hashtable;
|
||||
|
||||
// amount of rows
|
||||
const uint16 HT_HEIGHTS[]={61,631,3889,19441,65521};
|
||||
|
||||
@ -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)
|
||||
@ -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<Autoarr2_max_length(ar2);i++)
|
||||
Autoarr2_add(ar2,8);
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ void printuni(Unitype v){
|
||||
}
|
||||
|
||||
void test_all(void){
|
||||
test_searchtree();
|
||||
test_autoarr2();
|
||||
optime({test_searchtree();});
|
||||
optime({test_autoarr2();});
|
||||
printf("\e[96m---------------------------------------\n");
|
||||
}
|
||||
@ -14,5 +14,5 @@ void test_autoarr2(void);
|
||||
clock_t start=clock();\
|
||||
(codeblock);\
|
||||
clock_t stop=clock();\
|
||||
printf("\e[96m%li\n",(stop-start));\
|
||||
printf("\e[93m%li\n",(stop-start));\
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user