Autoarr_create and Autoarr_free signatures changed

This commit is contained in:
Timerix22 2022-04-19 18:07:34 +03:00
parent a9860691ab
commit 5fd395d7eb
21 changed files with 125 additions and 152 deletions

View File

@ -13,11 +13,7 @@ define_Autoarr(double)
define_Autoarr(Unitype) define_Autoarr(Unitype)
// right func to clear array of unitype values // right func to clear array of unitype values
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){ void Autoarr_free_Unitype(Autoarr(Unitype)* ar){
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++) Autoarr_foreach(ar, u,Unitype_free(u));
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++) Autoarr_free(ar);
Unitype_free(ar->values[blockI][elemI]);
for(uint32 elemI=0;elemI<ar->block_length;elemI++)
Unitype_free(ar->values[ar->blocks_count-1][elemI]);
Autoarr_clear(ar);
} }

View File

@ -20,13 +20,13 @@ declare_Autoarr(double)
declare_Autoarr(Unitype) declare_Autoarr(Unitype)
// right func to clear array of unitype values // right func to clear array of unitype values
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar); void Autoarr_free_Unitype(Autoarr(Unitype)* ar);
#define Autoarr_foreach(ar,elem,codeblock)({\ #define Autoarr_foreach(ar,elem,codeblock)({\
if(ar->blocks_count>0) {\ if(ar->blocks_count>0) {\
typeof(**ar->values) elem;\ typeof(**ar->values) elem;\
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)\ for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)\
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++){dbg(5);\ for(uint32 elemI=0;elemI<ar->max_block_length;elemI++){\
elem=ar->values[blockI][elemI];\ elem=ar->values[blockI][elemI];\
(codeblock);\ (codeblock);\
}\ }\

View File

@ -5,33 +5,31 @@ extern "C" {
#include "Autoarr.h" #include "Autoarr.h"
#include "../Hashtable/KeyValuePair.h" #include "../Hashtable/KeyValuePair.h"
EXPORT void CALL kerep_Autoarr_KeyValuePair_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_KeyValuePair** output){ EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_KVPair** output){
*output=malloc(sizeof(Autoarr_KeyValuePair)); *output=Autoarr_create(KVPair, max_blocks_count, max_block_length);
**output=Autoarr_create(KeyValuePair, max_blocks_count, max_block_length);
} }
EXPORT void CALL kerep_Autoarr_KeyValuePair_free(Autoarr_KeyValuePair* ar){ EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){
Autoarr_clear(ar); Autoarr_free_KVPair(ar);
free(ar);
} }
EXPORT void CALL kerep_Autoarr_KeyValuePair_get(Autoarr_KeyValuePair* ar, uint32 index, KeyValuePair* output){ EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){
*output=Autoarr_get(ar, index); *output=Autoarr_get(ar, index);
} }
EXPORT void CALL kerep_Autoarr_KeyValuePair_add(Autoarr_KeyValuePair* ar, KeyValuePair element){ EXPORT void CALL kerep_Autoarr_KVPair_add(Autoarr_KVPair* ar, KVPair element){
Autoarr_add(ar, element); Autoarr_add(ar, element);
} }
EXPORT void CALL kerep_Autoarr_KeyValuePair_set(Autoarr_KeyValuePair* ar, uint32 index, KeyValuePair element){ EXPORT void CALL kerep_Autoarr_KVPair_set(Autoarr_KVPair* ar, uint32 index, KVPair element){
Autoarr_set(ar, index, element); Autoarr_set(ar, index, element);
} }
EXPORT void CALL kerep_Autoarr_KeyValuePair_length(Autoarr_KeyValuePair* ar, uint32* output){ EXPORT void CALL kerep_Autoarr_KVPair_length(Autoarr_KVPair* ar, uint32* output){
*output=Autoarr_length(ar); *output=Autoarr_length(ar);
} }
EXPORT void CALL kerep_Autoarr_KeyValuePair_max_length(Autoarr_KeyValuePair* ar, uint32* output){ EXPORT void CALL kerep_Autoarr_KVPair_max_length(Autoarr_KVPair* ar, uint32* output){
*output=Autoarr_max_length(ar); *output=Autoarr_max_length(ar);
} }

View File

@ -5,13 +5,11 @@ extern "C" {
#include "Autoarr.h" #include "Autoarr.h"
EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_Unitype** output){ EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_Unitype** output){
*output=malloc(sizeof(Autoarr_Unitype)); *output=Autoarr_create(Unitype, max_blocks_count, max_block_length);
**output=Autoarr_create(Unitype, max_blocks_count, max_block_length);
} }
EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){ EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){
Autoarr_clear(ar); Autoarr_free_Unitype(ar);
free(ar);
} }
EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){ EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){

View File

@ -15,7 +15,7 @@ typedef struct {\
type (*get)(struct Autoarr_##type* ar, uint32 index);\ type (*get)(struct Autoarr_##type* ar, uint32 index);\
type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\ type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\
void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\ void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\
void (*clear)(struct Autoarr_##type* ar);\ void (*_free)(struct Autoarr_##type* ar);\
} __functions_list_t_##type;\ } __functions_list_t_##type;\
\ \
typedef struct Autoarr_##type{\ typedef struct Autoarr_##type{\
@ -31,8 +31,8 @@ void __Autoarr_add_##type(Autoarr_##type* ar, type element);\
type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index);\ type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index);\
type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index);\ type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index);\
void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\ void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\
void __Autoarr_clear_##type(Autoarr_##type* ar);\ void __Autoarr_free_##type(Autoarr_##type* ar);\
Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length); Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);
#define Autoarr(type) Autoarr_##type #define Autoarr(type) Autoarr_##type
@ -44,8 +44,8 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block
autoarr->functions->getptr(autoarr,index) autoarr->functions->getptr(autoarr,index)
#define Autoarr_set(autoarr, index, element)\ #define Autoarr_set(autoarr, index, element)\
autoarr->functions->set(autoarr, index, element) autoarr->functions->set(autoarr, index, element)
#define Autoarr_clear(autoarr)\ #define Autoarr_free(autoarr)\
autoarr->functions->clear(autoarr) autoarr->functions->_free(autoarr)
#define Autoarr_create(type, max_blocks_count, max_block_length)\ #define Autoarr_create(type, max_blocks_count, max_block_length)\
__Autoarr_create_##type(max_blocks_count, max_block_length) __Autoarr_create_##type(max_blocks_count, max_block_length)
@ -55,7 +55,7 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block
#define Autoarr_max_length(autoarr)\ #define Autoarr_max_length(autoarr)\
(uint32)(autoarr->max_block_length*autoarr->max_blocks_count) (uint32)(autoarr->max_block_length*autoarr->max_blocks_count)
#define Autoarr_remove(AR){\ #define Autoarr_pop(AR){\
if(AR->block_length==1){\ if(AR->block_length==1){\
AR->blocks_count--;\ AR->blocks_count--;\
AR->block_length=AR->max_block_length;\ AR->block_length=AR->max_block_length;\

View File

@ -39,13 +39,11 @@ void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
}\ }\
\ \
void __Autoarr_clear_##type(Autoarr_##type* ar){\ void __Autoarr_free_##type(Autoarr_##type* ar){\
for(uint16 i=0; i<ar->blocks_count;i++)\ for(uint16 i=0; i<ar->blocks_count;i++)\
free(ar->values[i]); \ free(ar->values[i]); \
free(ar->values);\ free(ar->values);\
ar->values=NULL;\ free(ar);\
ar->blocks_count=0;\
ar->block_length=0;\
}\ }\
\ \
__functions_list_t_##type __functions_list_##type={\ __functions_list_t_##type __functions_list_##type={\
@ -53,11 +51,12 @@ __functions_list_t_##type __functions_list_##type={\
&__Autoarr_get_##type,\ &__Autoarr_get_##type,\
&__Autoarr_getptr_##type,\ &__Autoarr_getptr_##type,\
&__Autoarr_set_##type,\ &__Autoarr_set_##type,\
&__Autoarr_clear_##type\ &__Autoarr_free_##type\
};\ };\
\ \
Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\
return (Autoarr_##type){\ Autoarr_##type* ar=malloc(sizeof(Autoarr_##type));\
*ar=(Autoarr_##type){\
.max_blocks_count=max_blocks_count,\ .max_blocks_count=max_blocks_count,\
.blocks_count=0,\ .blocks_count=0,\
.max_block_length=max_block_length,\ .max_block_length=max_block_length,\
@ -65,6 +64,7 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block
.values=NULL,\ .values=NULL,\
.functions=&__functions_list_##type\ .functions=&__functions_list_##type\
};\ };\
return ar;\
} }
#if __cplusplus #if __cplusplus

View File

@ -1,9 +1,17 @@
#include "StringBuilder.h" #include "StringBuilder.h"
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length){ StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length){
return Autoarr_create(int8,max_blocks_count,max_block_length); return Autoarr_create(int8,max_blocks_count,max_block_length);
} }
void StringBuilder_free(StringBuilder* b){
Autoarr_free(b);
}
void StringBuilder_pop(StringBuilder* b){
Autoarr_pop(b);
}
void StringBuilder_append_char(StringBuilder* b, char c){ void StringBuilder_append_char(StringBuilder* b, char c){
Autoarr_add(b,c); Autoarr_add(b,c);
} }

View File

@ -8,7 +8,9 @@ extern "C" {
typedef Autoarr(int8) StringBuilder; typedef Autoarr(int8) StringBuilder;
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length); StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
void StringBuilder_free(StringBuilder* b);
void StringBuilder_pop(StringBuilder* b);
void StringBuilder_append_char(StringBuilder* b, char c); void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cptr(StringBuilder* b, char* s); void StringBuilder_append_cptr(StringBuilder* b, char* s);
void StringBuilder_append_string(StringBuilder* b, string s); void StringBuilder_append_string(StringBuilder* b, string s);

View File

@ -130,20 +130,19 @@ Maybe __ReadValue(DeserializeSharedData* shared);
Maybe __ReadString(DeserializeSharedData* shared){ Maybe __ReadString(DeserializeSharedData* shared){
char c; char c;
bool prevIsBackslash=false; bool prevIsBackslash=false;
StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL); StringBuilder* b=StringBuilder_create(STRB_BC,STRB_BL);
StringBuilder* b=&_b;
while ((c=*++text)){ while ((c=*++text)){
if(c=='"') { if(c=='"') {
if(prevIsBackslash) { if(prevIsBackslash) {
// replacing <\"> with <"> // replacing <\"> with <">
Autoarr_remove(b); StringBuilder_pop(b);
StringBuilder_append_char(b,c); StringBuilder_append_char(b,c);
prevIsBackslash=false; prevIsBackslash=false;
} }
else { else {
char* str=StringBuilder_build(b); char* str=StringBuilder_build(b);
Autoarr_clear(b); StringBuilder_free(b);
return SUCCESS(UniPtr(CharPtr,str)); return SUCCESS(UniPtr(CharPtr,str));
} }
} }
@ -153,18 +152,17 @@ Maybe __ReadString(DeserializeSharedData* shared){
} }
} }
safethrow(ERR_ENDOFSTR, Autoarr_clear(b)); safethrow(ERR_ENDOFSTR, StringBuilder_free(b));
} }
#define ReadString() __ReadString(shared) #define ReadString() __ReadString(shared)
Maybe __ReadList(DeserializeSharedData* shared){ Maybe __ReadList(DeserializeSharedData* shared){
Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype))); Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
readingList=true; readingList=true;
while (true){ while (true){
try(ReadValue(), val,{ try(ReadValue(), val,{
Autoarr_clear(list); Autoarr_free_Unitype(list);
free(list); free(list);
}) })
Autoarr_add(list,val.value); Autoarr_add(list,val.value);
@ -327,8 +325,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
list=(Autoarr(Unitype)*)lu.VoidPtr; list=(Autoarr(Unitype)*)lu.VoidPtr;
} }
else{ else{
list=malloc(sizeof(Autoarr(Unitype))); list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list)); Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list));
} }
Autoarr_add(list,val.value); Autoarr_add(list,val.value);

View File

@ -53,8 +53,8 @@ EXPORT void CALL kerep_DtsodV24_height(Hashtable* dtsod, uint16* heigth){
*heigth=Hashtable_height(dtsod); *heigth=Hashtable_height(dtsod);
} }
EXPORT void CALL kerep_DtsodV24_getrow(Hashtable* dtsod, uint16 h, Autoarr_KeyValuePair** row){ EXPORT void CALL kerep_DtsodV24_getrow(Hashtable* dtsod, uint16 h, Autoarr_KVPair** row){
*row=dtsod->rows+h; *row=dtsod->rows[h];
} }
#if __cplusplus #if __cplusplus

View File

@ -64,7 +64,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
try(AppendValue(e),__,;); try(AppendValue(e),__,;);
addc(','); addc(',');
})); }));
Autoarr_remove(b); StringBuilder_pop(b);
addc('\n'); addc('\n');
tabs--; tabs--;
AppendTabs(); AppendTabs();
@ -106,9 +106,9 @@ Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){
} }
Maybe DtsodV24_serialize(Hashtable* dtsod){ Maybe DtsodV24_serialize(Hashtable* dtsod){
StringBuilder sb=StringBuilder_create(STRB_BC,STRB_BL); StringBuilder* sb=StringBuilder_create(STRB_BC,STRB_BL);
try(__serialize(&sb,0,dtsod),__, Autoarr_clear((&sb))); try(__serialize(sb,0,dtsod),__, StringBuilder_free((sb)));
char* str=StringBuilder_build(&sb); char* str=StringBuilder_build(sb);
Autoarr_clear((&sb)); StringBuilder_free((sb));
return SUCCESS(UniPtr(CharPtr, str)); return SUCCESS(UniPtr(CharPtr, str));
} }

View File

@ -11,16 +11,15 @@ static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
Hashtable* Hashtable_create(){ Hashtable* Hashtable_create(){
Hashtable* ht=malloc(sizeof(Hashtable)); Hashtable* ht=malloc(sizeof(Hashtable));
ht->hein=HT_HEIN_MIN; ht->hein=HT_HEIN_MIN;
ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KeyValuePair))); ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*));
for(uint16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++) for(uint16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++)
ht->rows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL); ht->rows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
return ht; return ht;
} }
void Hashtable_free(Hashtable* ht){ void Hashtable_free(Hashtable* ht){
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++){ for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
Autoarr_KeyValuePair_clear(ht->rows+i); Autoarr_free_KVPair(ht->rows[i]);
}
free(ht->rows); free(ht->rows);
free(ht); free(ht);
} }
@ -30,34 +29,39 @@ uint16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; }
void Hashtable_expand(Hashtable* ht){ void Hashtable_expand(Hashtable* ht){
if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH); if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH);
Autoarr(KeyValuePair)* newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KeyValuePair)));
Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)));
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++) for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
newrows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL); newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){ for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
Autoarr(KeyValuePair)* ar=ht->rows+i; Autoarr(KVPair)* ar=ht->rows[i];
uint32 arlen=Autoarr_length(ar); uint32 arlen=Autoarr_length(ar);
for(uint16 k=0;k<arlen;k++){ for(uint16 k=0;k<arlen;k++){
KeyValuePair p=Autoarr_get(ar,k); KVPair p=Autoarr_get(ar,k);
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein]; uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein];
Autoarr(KeyValuePair)* newar=newrows+newrown; Autoarr(KVPair)* newar=newrows[newrown];
Autoarr_add(newar,p); Autoarr_add(newar,p);
} }
Autoarr_clear(ar); // it is a feature, not a bug
// no need to free kvpair keys and values, they just moved to new autoarrs
Autoarr_free(ar);
} }
free(ht->rows); free(ht->rows);
ht->rows=newrows; ht->rows=newrows;
} }
Autoarr(KeyValuePair)* getrow(Hashtable* ht, char* key, bool can_expand){ Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
Autoarr(KeyValuePair)* ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein]; Autoarr(KVPair)* ar=ht->rows[ihash(key)%HT_HEIGHTS[ht->hein]];
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar)) if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
optime("expand",1,(Hashtable_expand(ht))); optime("expand",1,(Hashtable_expand(ht)));
ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein]; ar=ht->rows[ihash(key)%HT_HEIGHTS[ht->hein]];
return ar; return ar;
} }
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){ void Hashtable_add_pair(Hashtable* ht, KVPair p){
Autoarr_add(getrow(ht,p.key,true),p); Autoarr_add(getrow(ht,p.key,true),p);
} }
void Hashtable_add(Hashtable* ht, char* key, Unitype u){ void Hashtable_add(Hashtable* ht, char* key, Unitype u){
@ -66,25 +70,25 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){
// returns null or pointer to value in hashtable // returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key){ Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false); Autoarr(KVPair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar); uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){ for(uint32 i=0;i<arlen;i++){
KeyValuePair* p=Autoarr_getptr(ar,i); KVPair* p=Autoarr_getptr(ar,i);
if(cptr_compare(key,p->key)) return &p->value; if(cptr_compare(key,p->key)) return &p->value;
} }
return NULL; return NULL;
} }
Unitype Hashtable_get(Hashtable* ht, char* key){ Unitype Hashtable_get(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false); Autoarr(KVPair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar); uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){ for(uint32 i=0;i<arlen;i++){
KeyValuePair p=Autoarr_get(ar,i); KVPair p=Autoarr_get(ar,i);
if(cptr_compare(key,p.key)) return p.value; if(cptr_compare(key,p.key)) return p.value;
} }
return UniNull; return UniNull;
} }
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){ KVPair Hashtable_get_pair(Hashtable* ht, char* key){
return KVPair(key,Hashtable_get(ht,key)); return KVPair(key,Hashtable_get(ht,key));
} }
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
@ -93,7 +97,7 @@ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
return u.type!=Null; return u.type!=Null;
} }
/* void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){ /* void Hashtable_set_pair(Hashtable* ht, KVPair p){
if(Hashtable_try_get(ht,p.key, NULL)){ if(Hashtable_try_get(ht,p.key, NULL)){
} }

View File

@ -10,7 +10,7 @@ extern "C" {
typedef struct Hashtable{ typedef struct Hashtable{
uint8 hein; // height=HT_HEIGHTS[hein] uint8 hein; // height=HT_HEIGHTS[hein]
Autoarr(KeyValuePair)* rows; // Autoarr[height] Autoarr(KVPair)** rows; // Autoarr[height]
} Hashtable; } Hashtable;
Hashtable* Hashtable_create(); Hashtable* Hashtable_create();
@ -19,32 +19,32 @@ void Hashtable_free(Hashtable* ht);
// amount of rows // amount of rows
uint16 Hashtable_height(Hashtable* ht); uint16 Hashtable_height(Hashtable* ht);
// adds charptr and value to new KeyValuePair // adds charptr and value to new KVPair
// use cptr_copy() to create new string if needed // use cptr_copy() to create new string if needed
#define KVPair(key,value) (KeyValuePair){key,value} #define KVPair(key,value) (KVPair){key,value}
// //
// don't add pairs with the same keys, // don't add pairs with the same keys,
// or something weird will happen // or something weird will happen
// //
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p); void Hashtable_add_pair(Hashtable* ht, KVPair p);
void Hashtable_add(Hashtable* ht, char* key, Unitype u); void Hashtable_add(Hashtable* ht, char* key, Unitype u);
// returns null or pointer to value in hashtable // returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key); Unitype* Hashtable_getptr(Hashtable* ht, char* key);
Unitype Hashtable_get(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key);
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key); KVPair Hashtable_get_pair(Hashtable* ht, char* key);
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
// not implemented yet // not implemented yet
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p); void Hashtable_set_pair(Hashtable* ht, KVPair p);
void Hashtable_set(Hashtable* ht, char* key, Unitype u); void Hashtable_set(Hashtable* ht, char* key, Unitype u);
#define Hashtable_foreach(HT, EL, codeblock)({\ #define Hashtable_foreach(HT, EL, codeblock)({\
uint16 hmax=Hashtable_height(HT);\ uint16 hmax=Hashtable_height(HT);\
for(uint16 h=0; h<hmax; h++){\ for(uint16 h=0; h<hmax; h++){\
Autoarr(KeyValuePair)* AR=HT->rows+h;\ Autoarr(KVPair)* AR=HT->rows[h];\
Autoarr_foreach(AR, EL, codeblock);\ Autoarr_foreach(AR, EL, codeblock);\
}\ }\
}) })

View File

@ -1,25 +1,21 @@
#include "KeyValuePair.h" #include "KeyValuePair.h"
define_Autoarr(KeyValuePair) define_Autoarr(KVPair)
// proper way to clear a KVP // proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p){ void KVPair_free(KVPair p){
free(p.key); free(p.key);
Unitype_free(p.value); Unitype_free(p.value);
} }
// func for KVP array clearing // func for KVP array clearing
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){ void Autoarr_free_KVPair(Autoarr_KVPair* ar){
for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++) Autoarr_foreach(ar,k,KVPair_free(k));
for(uint16 elemI=0; elemI < ar->max_block_length; elemI++) Autoarr_free(ar);
KeyValuePair_free(ar->values[blockI][elemI]);
for(uint16 elemI=0; elemI < ar->block_length; elemI++)
KeyValuePair_free(ar->values[ar->blocks_count-1][elemI]);
Autoarr_clear(ar);
} }
void printkvp(KeyValuePair p){ void printkvp(KVPair p){
printf("{\"%s\", ",p.key); printf("{\"%s\", ",p.key);
printuni(p.value); printuni(p.value);
printf("}"); printf("}");

View File

@ -7,20 +7,20 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#include "../Autoarr/Autoarr.h" #include "../Autoarr/Autoarr.h"
typedef struct KeyValuePair{ typedef struct KVPair{
char* key; char* key;
Unitype value; Unitype value;
} KeyValuePair; } KVPair;
declare_Autoarr(KeyValuePair) declare_Autoarr(KVPair)
// proper way to clear a KVP // proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p); void KVPair_free(KVPair p);
// func to clear KVP array // func to clear KVP array
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar); void Autoarr_free_KVPair(Autoarr_KVPair* ar);
void printkvp(KeyValuePair p); void printkvp(KVPair p);
#if __cplusplus #if __cplusplus
} }

View File

@ -1,6 +1,3 @@
# kerep # kerep
My library written in C My library written in C
# TODO
change return type of Autoarr_create() and StringBuilder_create() to pointers

View File

@ -80,36 +80,34 @@ void Unitype_free(Unitype u){
STNode_free(u.VoidPtr); STNode_free(u.VoidPtr);
break; break;
case AutoarrInt8Ptr: case AutoarrInt8Ptr:
Autoarr_clear(((Autoarr(int8)*)u.VoidPtr)); __Autoarr_free_int8(u.VoidPtr);
break; break;
case AutoarrUInt8Ptr: case AutoarrUInt8Ptr:
Autoarr_clear(((Autoarr(uint8)*)u.VoidPtr)); __Autoarr_free_uint8(u.VoidPtr);
break; break;
case AutoarrInt16Ptr: case AutoarrInt16Ptr:
Autoarr_clear(((Autoarr(int16)*)u.VoidPtr)); __Autoarr_free_int16(u.VoidPtr);
break; break;
case AutoarrUInt16Ptr: case AutoarrUInt16Ptr:
Autoarr_clear(((Autoarr(uint16)*)u.VoidPtr)); __Autoarr_free_uint16(u.VoidPtr);
break; break;
case AutoarrInt32Ptr: case AutoarrInt32Ptr:
Autoarr_clear(((Autoarr(int32)*)u.VoidPtr)); __Autoarr_free_int32(u.VoidPtr);
break; break;
case AutoarrUInt32Ptr: case AutoarrUInt32Ptr:
Autoarr_clear(((Autoarr(uint32)*)u.VoidPtr)); __Autoarr_free_uint32(u.VoidPtr);
break; break;
case AutoarrInt64Ptr: case AutoarrInt64Ptr:
Autoarr_clear(((Autoarr(int64)*)u.VoidPtr)); __Autoarr_free_int64(u.VoidPtr);
break; break;
case AutoarrUInt64Ptr: case AutoarrUInt64Ptr:
Autoarr_clear(((Autoarr(uint64)*)u.VoidPtr)); __Autoarr_free_uint64(u.VoidPtr);
break; break;
case AutoarrUnitypePtr: case AutoarrUnitypePtr:
Autoarr_Unitype_clear(u.VoidPtr); Autoarr_free_Unitype(u.VoidPtr);
free((Autoarr(Unitype)*)u.VoidPtr);
break; break;
case AutoarrKVPairPtr: case AutoarrKVPairPtr:
Autoarr_KeyValuePair_clear(u.VoidPtr); Autoarr_free_KVPair(u.VoidPtr);
free((Autoarr(KeyValuePair)*)u.VoidPtr);
break; break;
default: throw(ERR_WRONGTYPE); default: throw(ERR_WRONGTYPE);
} }

View File

@ -38,16 +38,16 @@ static void printallval(Autoarr(uint16)* ar){
void test_autoarr(){ void test_autoarr(){
optime("test_autoarr",1,({ optime("test_autoarr",1,({
printf("\e[96m------------[test_autoarr]------------\n"); printf("\e[96m------------[test_autoarr]------------\n");
Autoarr(uint16) ar=Autoarr_create(uint16,10,16); Autoarr(uint16)* ar=Autoarr_create(uint16,10,16);
printf("\e[92mautoarr created\n"); printf("\e[92mautoarr created\n");
fillar(&ar); fillar(ar);
printf("\e[92mautoarr filled up\n"); printf("\e[92mautoarr filled up\n");
printautoarr(&ar); printautoarr(ar);
printallval(&ar); printallval(ar);
resetar(&ar); resetar(ar);
printf("\e[92mautoarr values reset\n"); printf("\e[92mautoarr values reset\n");
printallval(&ar); printallval(ar);
Autoarr_clear(((&ar))); Autoarr_free(ar);
printf("\e[92mautoarr cleared\n"); printf("\e[92mautoarr cleared\n");
})); }));
} }

View File

@ -63,26 +63,5 @@ void test_dtsod(){
})); }));
free(s); free(s);
FILE* f=fopen("messages.dtsod", "r");
if(f==NULL){
perror("error ");
throw("can't open file");
}
char fbuf[65535];
uint32 i=0;
char cc;
while((cc=fgetc(f))!=EOF){
fbuf[i++]=cc;
}
fbuf[i]='\0';
fclose(f);
printf("read %u chars", i);
Maybe rrr=DtsodV24_deserialize(fbuf);
if(rrr.errmsg) {
throw(rrr.errmsg);
}
else dtsod=rrr.value.VoidPtr;
Hashtable_free(dtsod);
})); }));
} }

View File

@ -20,7 +20,7 @@ void printrowgraph(Hashtable* ht){
for(uint32 i=0; i<lgs_l; i++) for(uint32 i=0; i<lgs_l; i++)
lgs[i]=0; lgs[i]=0;
for(uint16 h=0;h<Hashtable_height(ht);h++){ for(uint16 h=0;h<Hashtable_height(ht);h++){
Autoarr(KeyValuePair)* ar=ht->rows+h; Autoarr(KVPair)* ar=ht->rows[h];
uint32 l=Autoarr_length(ar); uint32 l=Autoarr_length(ar);
lgs[l]++; lgs[l]++;
} }

View File

@ -1,7 +1,7 @@
#include "../Hashtable/KeyValuePair.h" #include "../Hashtable/KeyValuePair.h"
EXPORT void CALL test_marshalling(char* text, KeyValuePair** kptr){ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
KeyValuePair* k=malloc(sizeof(KeyValuePair)); KVPair* k=malloc(sizeof(KVPair));
k->key="message"; k->key="message";
char* tc=cptr_copy(text); char* tc=cptr_copy(text);
Unitype u={.VoidPtr=tc, .type=CharPtr}; Unitype u={.VoidPtr=tc, .type=CharPtr};