Autoarr_create and Autoarr_free signatures changed
This commit is contained in:
parent
a9860691ab
commit
5fd395d7eb
@ -13,11 +13,7 @@ define_Autoarr(double)
|
||||
define_Autoarr(Unitype)
|
||||
|
||||
// right func to clear array of unitype values
|
||||
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){
|
||||
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)
|
||||
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++)
|
||||
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);
|
||||
void Autoarr_free_Unitype(Autoarr(Unitype)* ar){
|
||||
Autoarr_foreach(ar, u,Unitype_free(u));
|
||||
Autoarr_free(ar);
|
||||
}
|
||||
|
||||
@ -20,13 +20,13 @@ declare_Autoarr(double)
|
||||
declare_Autoarr(Unitype)
|
||||
|
||||
// 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)({\
|
||||
if(ar->blocks_count>0) {\
|
||||
typeof(**ar->values) elem;\
|
||||
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];\
|
||||
(codeblock);\
|
||||
}\
|
||||
|
||||
@ -5,33 +5,31 @@ extern "C" {
|
||||
#include "Autoarr.h"
|
||||
#include "../Hashtable/KeyValuePair.h"
|
||||
|
||||
EXPORT void CALL kerep_Autoarr_KeyValuePair_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_KeyValuePair** output){
|
||||
*output=malloc(sizeof(Autoarr_KeyValuePair));
|
||||
**output=Autoarr_create(KeyValuePair, max_blocks_count, max_block_length);
|
||||
EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_KVPair** output){
|
||||
*output=Autoarr_create(KVPair, max_blocks_count, max_block_length);
|
||||
}
|
||||
|
||||
EXPORT void CALL kerep_Autoarr_KeyValuePair_free(Autoarr_KeyValuePair* ar){
|
||||
Autoarr_clear(ar);
|
||||
free(ar);
|
||||
EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){
|
||||
Autoarr_free_KVPair(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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -5,13 +5,11 @@ extern "C" {
|
||||
#include "Autoarr.h"
|
||||
|
||||
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){
|
||||
Autoarr_clear(ar);
|
||||
free(ar);
|
||||
Autoarr_free_Unitype(ar);
|
||||
}
|
||||
|
||||
EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){
|
||||
|
||||
@ -15,7 +15,7 @@ typedef struct {\
|
||||
type (*get)(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 (*clear)(struct Autoarr_##type* ar);\
|
||||
void (*_free)(struct Autoarr_##type* ar);\
|
||||
} __functions_list_t_##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_getptr_##type(Autoarr_##type* ar, uint32 index);\
|
||||
void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\
|
||||
void __Autoarr_clear_##type(Autoarr_##type* ar);\
|
||||
Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);
|
||||
void __Autoarr_free_##type(Autoarr_##type* ar);\
|
||||
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);
|
||||
|
||||
#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)
|
||||
#define Autoarr_set(autoarr, index, element)\
|
||||
autoarr->functions->set(autoarr, index, element)
|
||||
#define Autoarr_clear(autoarr)\
|
||||
autoarr->functions->clear(autoarr)
|
||||
#define Autoarr_free(autoarr)\
|
||||
autoarr->functions->_free(autoarr)
|
||||
#define Autoarr_create(type, max_blocks_count, max_block_length)\
|
||||
__Autoarr_create_##type(max_blocks_count, max_block_length)
|
||||
|
||||
@ -55,13 +55,13 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block
|
||||
#define Autoarr_max_length(autoarr)\
|
||||
(uint32)(autoarr->max_block_length*autoarr->max_blocks_count)
|
||||
|
||||
#define Autoarr_remove(AR){\
|
||||
#define Autoarr_pop(AR){\
|
||||
if(AR->block_length==1){\
|
||||
AR->blocks_count--;\
|
||||
AR->block_length=AR->max_block_length;\
|
||||
free(AR->values[AR->blocks_count]);\
|
||||
}\
|
||||
else AR->block_length--;\
|
||||
AR->blocks_count--;\
|
||||
AR->block_length=AR->max_block_length;\
|
||||
free(AR->values[AR->blocks_count]);\
|
||||
}\
|
||||
else AR->block_length--;\
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -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;\
|
||||
}\
|
||||
\
|
||||
void __Autoarr_clear_##type(Autoarr_##type* ar){\
|
||||
void __Autoarr_free_##type(Autoarr_##type* ar){\
|
||||
for(uint16 i=0; i<ar->blocks_count;i++)\
|
||||
free(ar->values[i]); \
|
||||
free(ar->values);\
|
||||
ar->values=NULL;\
|
||||
ar->blocks_count=0;\
|
||||
ar->block_length=0;\
|
||||
free(ar);\
|
||||
}\
|
||||
\
|
||||
__functions_list_t_##type __functions_list_##type={\
|
||||
@ -53,11 +51,12 @@ __functions_list_t_##type __functions_list_##type={\
|
||||
&__Autoarr_get_##type,\
|
||||
&__Autoarr_getptr_##type,\
|
||||
&__Autoarr_set_##type,\
|
||||
&__Autoarr_clear_##type\
|
||||
&__Autoarr_free_##type\
|
||||
};\
|
||||
\
|
||||
Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\
|
||||
return (Autoarr_##type){\
|
||||
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\
|
||||
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type));\
|
||||
*ar=(Autoarr_##type){\
|
||||
.max_blocks_count=max_blocks_count,\
|
||||
.blocks_count=0,\
|
||||
.max_block_length=max_block_length,\
|
||||
@ -65,6 +64,7 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block
|
||||
.values=NULL,\
|
||||
.functions=&__functions_list_##type\
|
||||
};\
|
||||
return ar;\
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -1,9 +1,17 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
void StringBuilder_free(StringBuilder* b){
|
||||
Autoarr_free(b);
|
||||
}
|
||||
|
||||
void StringBuilder_pop(StringBuilder* b){
|
||||
Autoarr_pop(b);
|
||||
}
|
||||
|
||||
void StringBuilder_append_char(StringBuilder* b, char c){
|
||||
Autoarr_add(b,c);
|
||||
}
|
||||
|
||||
@ -8,7 +8,9 @@ extern "C" {
|
||||
|
||||
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_cptr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_string(StringBuilder* b, string s);
|
||||
|
||||
@ -130,20 +130,19 @@ Maybe __ReadValue(DeserializeSharedData* shared);
|
||||
Maybe __ReadString(DeserializeSharedData* shared){
|
||||
char c;
|
||||
bool prevIsBackslash=false;
|
||||
StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL);
|
||||
StringBuilder* b=&_b;
|
||||
StringBuilder* b=StringBuilder_create(STRB_BC,STRB_BL);
|
||||
|
||||
while ((c=*++text)){
|
||||
if(c=='"') {
|
||||
if(prevIsBackslash) {
|
||||
// replacing <\"> with <">
|
||||
Autoarr_remove(b);
|
||||
StringBuilder_pop(b);
|
||||
StringBuilder_append_char(b,c);
|
||||
prevIsBackslash=false;
|
||||
}
|
||||
else {
|
||||
char* str=StringBuilder_build(b);
|
||||
Autoarr_clear(b);
|
||||
StringBuilder_free(b);
|
||||
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)
|
||||
|
||||
Maybe __ReadList(DeserializeSharedData* shared){
|
||||
Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype)));
|
||||
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
||||
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
||||
readingList=true;
|
||||
|
||||
while (true){
|
||||
try(ReadValue(), val,{
|
||||
Autoarr_clear(list);
|
||||
Autoarr_free_Unitype(list);
|
||||
free(list);
|
||||
})
|
||||
Autoarr_add(list,val.value);
|
||||
@ -327,8 +325,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
|
||||
list=(Autoarr(Unitype)*)lu.VoidPtr;
|
||||
}
|
||||
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));
|
||||
}
|
||||
Autoarr_add(list,val.value);
|
||||
|
||||
@ -53,8 +53,8 @@ EXPORT void CALL kerep_DtsodV24_height(Hashtable* dtsod, uint16* heigth){
|
||||
*heigth=Hashtable_height(dtsod);
|
||||
}
|
||||
|
||||
EXPORT void CALL kerep_DtsodV24_getrow(Hashtable* dtsod, uint16 h, Autoarr_KeyValuePair** row){
|
||||
*row=dtsod->rows+h;
|
||||
EXPORT void CALL kerep_DtsodV24_getrow(Hashtable* dtsod, uint16 h, Autoarr_KVPair** row){
|
||||
*row=dtsod->rows[h];
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -64,7 +64,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
||||
try(AppendValue(e),__,;);
|
||||
addc(',');
|
||||
}));
|
||||
Autoarr_remove(b);
|
||||
StringBuilder_pop(b);
|
||||
addc('\n');
|
||||
tabs--;
|
||||
AppendTabs();
|
||||
@ -106,9 +106,9 @@ Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){
|
||||
}
|
||||
|
||||
Maybe DtsodV24_serialize(Hashtable* dtsod){
|
||||
StringBuilder sb=StringBuilder_create(STRB_BC,STRB_BL);
|
||||
try(__serialize(&sb,0,dtsod),__, Autoarr_clear((&sb)));
|
||||
char* str=StringBuilder_build(&sb);
|
||||
Autoarr_clear((&sb));
|
||||
StringBuilder* sb=StringBuilder_create(STRB_BC,STRB_BL);
|
||||
try(__serialize(sb,0,dtsod),__, StringBuilder_free((sb)));
|
||||
char* str=StringBuilder_build(sb);
|
||||
StringBuilder_free((sb));
|
||||
return SUCCESS(UniPtr(CharPtr, str));
|
||||
}
|
||||
|
||||
@ -11,16 +11,15 @@ static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||
Hashtable* Hashtable_create(){
|
||||
Hashtable* ht=malloc(sizeof(Hashtable));
|
||||
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++)
|
||||
ht->rows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL);
|
||||
ht->rows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
|
||||
return ht;
|
||||
}
|
||||
|
||||
void Hashtable_free(Hashtable* ht){
|
||||
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++){
|
||||
Autoarr_KeyValuePair_clear(ht->rows+i);
|
||||
}
|
||||
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
|
||||
Autoarr_free_KVPair(ht->rows[i]);
|
||||
free(ht->rows);
|
||||
free(ht);
|
||||
}
|
||||
@ -30,34 +29,39 @@ uint16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; }
|
||||
|
||||
void Hashtable_expand(Hashtable* ht){
|
||||
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++)
|
||||
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++){
|
||||
Autoarr(KeyValuePair)* ar=ht->rows+i;
|
||||
Autoarr(KVPair)* ar=ht->rows[i];
|
||||
uint32 arlen=Autoarr_length(ar);
|
||||
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];
|
||||
Autoarr(KeyValuePair)* newar=newrows+newrown;
|
||||
Autoarr(KVPair)* newar=newrows[newrown];
|
||||
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);
|
||||
ht->rows=newrows;
|
||||
}
|
||||
|
||||
Autoarr(KeyValuePair)* getrow(Hashtable* ht, char* key, bool can_expand){
|
||||
Autoarr(KeyValuePair)* ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein];
|
||||
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
|
||||
Autoarr(KVPair)* ar=ht->rows[ihash(key)%HT_HEIGHTS[ht->hein]];
|
||||
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){
|
||||
void Hashtable_add_pair(Hashtable* ht, KVPair p){
|
||||
Autoarr_add(getrow(ht,p.key,true),p);
|
||||
}
|
||||
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
|
||||
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);
|
||||
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;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
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));
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/* void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){
|
||||
/* void Hashtable_set_pair(Hashtable* ht, KVPair p){
|
||||
if(Hashtable_try_get(ht,p.key, NULL)){
|
||||
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ extern "C" {
|
||||
|
||||
typedef struct Hashtable{
|
||||
uint8 hein; // height=HT_HEIGHTS[hein]
|
||||
Autoarr(KeyValuePair)* rows; // Autoarr[height]
|
||||
Autoarr(KVPair)** rows; // Autoarr[height]
|
||||
} Hashtable;
|
||||
|
||||
Hashtable* Hashtable_create();
|
||||
@ -19,32 +19,32 @@ void Hashtable_free(Hashtable* ht);
|
||||
// amount of rows
|
||||
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
|
||||
#define KVPair(key,value) (KeyValuePair){key,value}
|
||||
#define KVPair(key,value) (KVPair){key,value}
|
||||
|
||||
//
|
||||
// don't add pairs with the same keys,
|
||||
// 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);
|
||||
|
||||
// returns null or pointer to value in hashtable
|
||||
Unitype* Hashtable_getptr(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);
|
||||
|
||||
// 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);
|
||||
|
||||
#define Hashtable_foreach(HT, EL, codeblock)({\
|
||||
uint16 hmax=Hashtable_height(HT);\
|
||||
for(uint16 h=0; h<hmax; h++){\
|
||||
Autoarr(KeyValuePair)* AR=HT->rows+h;\
|
||||
Autoarr(KVPair)* AR=HT->rows[h];\
|
||||
Autoarr_foreach(AR, EL, codeblock);\
|
||||
}\
|
||||
})
|
||||
|
||||
@ -1,25 +1,21 @@
|
||||
#include "KeyValuePair.h"
|
||||
|
||||
define_Autoarr(KeyValuePair)
|
||||
define_Autoarr(KVPair)
|
||||
|
||||
|
||||
// proper way to clear a KVP
|
||||
void KeyValuePair_free(KeyValuePair p){
|
||||
void KVPair_free(KVPair p){
|
||||
free(p.key);
|
||||
Unitype_free(p.value);
|
||||
}
|
||||
|
||||
// func for KVP array clearing
|
||||
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){
|
||||
for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++)
|
||||
for(uint16 elemI=0; elemI < ar->max_block_length; elemI++)
|
||||
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 Autoarr_free_KVPair(Autoarr_KVPair* ar){
|
||||
Autoarr_foreach(ar,k,KVPair_free(k));
|
||||
Autoarr_free(ar);
|
||||
}
|
||||
|
||||
void printkvp(KeyValuePair p){
|
||||
void printkvp(KVPair p){
|
||||
printf("{\"%s\", ",p.key);
|
||||
printuni(p.value);
|
||||
printf("}");
|
||||
|
||||
@ -7,20 +7,20 @@ extern "C" {
|
||||
#include "../base/base.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
|
||||
typedef struct KeyValuePair{
|
||||
typedef struct KVPair{
|
||||
char* key;
|
||||
Unitype value;
|
||||
} KeyValuePair;
|
||||
} KVPair;
|
||||
|
||||
declare_Autoarr(KeyValuePair)
|
||||
declare_Autoarr(KVPair)
|
||||
|
||||
// proper way to clear a KVP
|
||||
void KeyValuePair_free(KeyValuePair p);
|
||||
void KVPair_free(KVPair p);
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@ -1,6 +1,3 @@
|
||||
# kerep
|
||||
|
||||
My library written in C
|
||||
|
||||
# TODO
|
||||
change return type of Autoarr_create() and StringBuilder_create() to pointers
|
||||
24
base/types.c
24
base/types.c
@ -80,36 +80,34 @@ void Unitype_free(Unitype u){
|
||||
STNode_free(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt8Ptr:
|
||||
Autoarr_clear(((Autoarr(int8)*)u.VoidPtr));
|
||||
__Autoarr_free_int8(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt8Ptr:
|
||||
Autoarr_clear(((Autoarr(uint8)*)u.VoidPtr));
|
||||
case AutoarrUInt8Ptr:
|
||||
__Autoarr_free_uint8(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt16Ptr:
|
||||
Autoarr_clear(((Autoarr(int16)*)u.VoidPtr));
|
||||
__Autoarr_free_int16(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt16Ptr:
|
||||
Autoarr_clear(((Autoarr(uint16)*)u.VoidPtr));
|
||||
__Autoarr_free_uint16(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt32Ptr:
|
||||
Autoarr_clear(((Autoarr(int32)*)u.VoidPtr));
|
||||
__Autoarr_free_int32(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt32Ptr:
|
||||
Autoarr_clear(((Autoarr(uint32)*)u.VoidPtr));
|
||||
__Autoarr_free_uint32(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt64Ptr:
|
||||
Autoarr_clear(((Autoarr(int64)*)u.VoidPtr));
|
||||
__Autoarr_free_int64(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt64Ptr:
|
||||
Autoarr_clear(((Autoarr(uint64)*)u.VoidPtr));
|
||||
__Autoarr_free_uint64(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUnitypePtr:
|
||||
Autoarr_Unitype_clear(u.VoidPtr);
|
||||
free((Autoarr(Unitype)*)u.VoidPtr);
|
||||
Autoarr_free_Unitype(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrKVPairPtr:
|
||||
Autoarr_KeyValuePair_clear(u.VoidPtr);
|
||||
free((Autoarr(KeyValuePair)*)u.VoidPtr);
|
||||
Autoarr_free_KVPair(u.VoidPtr);
|
||||
break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
|
||||
@ -38,16 +38,16 @@ static void printallval(Autoarr(uint16)* ar){
|
||||
void test_autoarr(){
|
||||
optime("test_autoarr",1,({
|
||||
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");
|
||||
fillar(&ar);
|
||||
fillar(ar);
|
||||
printf("\e[92mautoarr filled up\n");
|
||||
printautoarr(&ar);
|
||||
printallval(&ar);
|
||||
resetar(&ar);
|
||||
printautoarr(ar);
|
||||
printallval(ar);
|
||||
resetar(ar);
|
||||
printf("\e[92mautoarr values reset\n");
|
||||
printallval(&ar);
|
||||
Autoarr_clear(((&ar)));
|
||||
printallval(ar);
|
||||
Autoarr_free(ar);
|
||||
printf("\e[92mautoarr cleared\n");
|
||||
}));
|
||||
}
|
||||
|
||||
@ -63,26 +63,5 @@ void test_dtsod(){
|
||||
}));
|
||||
|
||||
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);
|
||||
}));
|
||||
}
|
||||
@ -20,7 +20,7 @@ void printrowgraph(Hashtable* ht){
|
||||
for(uint32 i=0; i<lgs_l; i++)
|
||||
lgs[i]=0;
|
||||
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);
|
||||
lgs[l]++;
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "../Hashtable/KeyValuePair.h"
|
||||
|
||||
EXPORT void CALL test_marshalling(char* text, KeyValuePair** kptr){
|
||||
KeyValuePair* k=malloc(sizeof(KeyValuePair));
|
||||
EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
|
||||
KVPair* k=malloc(sizeof(KVPair));
|
||||
k->key="message";
|
||||
char* tc=cptr_copy(text);
|
||||
Unitype u={.VoidPtr=tc, .type=CharPtr};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user