simplified Autoarr/Hashtable _foreach
This commit is contained in:
@@ -23,20 +23,20 @@ Autoarr_declare(u64)
|
||||
|
||||
Autoarr_declare(Unitype)
|
||||
|
||||
#define Autoarr_foreach(ar,elem,codeblock)({ \
|
||||
#define Autoarr_foreach(ar, elem, codeblock...) { \
|
||||
if(ar->blocks_count>0) { \
|
||||
typeof(**ar->values) elem; \
|
||||
for(u16 blockI=0;blockI<ar->blocks_count-1;blockI++) \
|
||||
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){ \
|
||||
elem=ar->values[blockI][elemI]; \
|
||||
(codeblock); \
|
||||
{ codeblock; } \
|
||||
} \
|
||||
for(u16 elemI=0;elemI<ar->block_length;elemI++){ \
|
||||
elem=ar->values[ar->blocks_count-1][elemI]; \
|
||||
(codeblock); \
|
||||
{ codeblock; } \
|
||||
} \
|
||||
} \
|
||||
})
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ void __Autoarr_##type##_freeWithoutMembers(Autoarr_##type* ar, bool freePtr){ \
|
||||
\
|
||||
void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr){ \
|
||||
if(ktDescriptor_##type.freeMembers!=NULL) { \
|
||||
Autoarr_foreach(ar, el, ({ \
|
||||
Autoarr_foreach(ar, el, \
|
||||
void* members_ptr=⪙ \
|
||||
if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \
|
||||
ktDescriptor_##type.freeMembers(members_ptr); \
|
||||
})); \
|
||||
); \
|
||||
} \
|
||||
__Autoarr_##type##_freeWithoutMembers(ar, freePtr);\
|
||||
} \
|
||||
|
||||
@@ -55,12 +55,12 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
||||
AppendTabs();
|
||||
addc('[');
|
||||
tabs++;
|
||||
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
|
||||
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e,
|
||||
addc('\n');
|
||||
AppendTabs();
|
||||
try(AppendValue(e),__,;);
|
||||
addc(',');
|
||||
}));
|
||||
);
|
||||
StringBuilder_rmchar(b);
|
||||
addc('\n');
|
||||
tabs--;
|
||||
@@ -75,11 +75,11 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
||||
else if(u.typeId==ktid_ptrName(Hashtable)){
|
||||
// check hashtable is blank
|
||||
bool hashtableNotBlank=false;
|
||||
Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({
|
||||
Hashtable_foreach(((Hashtable*)u.VoidPtr), __,
|
||||
hashtableNotBlank=true;
|
||||
if(__.key) {} // weird way to disable warning
|
||||
break;
|
||||
}));
|
||||
);
|
||||
|
||||
if(hashtableNotBlank){
|
||||
addc('\n');
|
||||
@@ -109,7 +109,7 @@ Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod){
|
||||
};
|
||||
SerializeSharedData* shared=&_shared;
|
||||
|
||||
Hashtable_foreach(dtsod, p, ({
|
||||
Hashtable_foreach(dtsod, p,
|
||||
AppendTabs();
|
||||
StringBuilder_append_cptr(b,p.key);
|
||||
addc(':');
|
||||
@@ -117,7 +117,7 @@ Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod){
|
||||
try(AppendValue(p.value),__,;);
|
||||
addc(';');
|
||||
addc('\n');
|
||||
}));
|
||||
);
|
||||
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
@@ -33,13 +33,13 @@ Unitype* Hashtable_getPtr(Hashtable* ht, char* key);
|
||||
Unitype Hashtable_get(Hashtable* ht, char* key);
|
||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
|
||||
|
||||
#define Hashtable_foreach(HT, EL, codeblock)({ \
|
||||
#define Hashtable_foreach(HT, EL, codeblock...) { \
|
||||
u16 hmax=Hashtable_height(HT); \
|
||||
for(u16 h=0; h<hmax; h++){ \
|
||||
Autoarr(KVPair)* AR=HT->rows[h]; \
|
||||
Autoarr_foreach(AR, EL, codeblock); \
|
||||
} \
|
||||
})
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ extern "C" {
|
||||
#define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE)
|
||||
|
||||
#define LinkedList_create(TYPE) LinkedList_##TYPE##_create()
|
||||
#define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); })
|
||||
#define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); })
|
||||
|
||||
|
||||
void LinkedList_addToBeginning(void* _llist, void* _new_node);
|
||||
|
||||
@@ -13,9 +13,9 @@ void complete_buf(StringBuilder* b){
|
||||
if(!len) return;
|
||||
string str={.length=len, .ptr=malloc(len)};
|
||||
u32 i=0;
|
||||
Autoarr_foreach(b->curr_buf, c, ({
|
||||
Autoarr_foreach(b->curr_buf, c,
|
||||
str.ptr[i++]=c;
|
||||
}));
|
||||
);
|
||||
Autoarr_add(b->compl_bufs,str);
|
||||
Autoarr_free(b->curr_buf, true);
|
||||
b->curr_buf=Autoarr_create(i8,BL_C,BL_L);
|
||||
@@ -47,17 +47,17 @@ void StringBuilder_free(StringBuilder* b){
|
||||
string StringBuilder_build(StringBuilder* b){
|
||||
complete_buf(b);
|
||||
u32 len=0;
|
||||
Autoarr_foreach(b->compl_bufs, cs, ({
|
||||
Autoarr_foreach(b->compl_bufs, cs,
|
||||
len+=cs.length;
|
||||
}));
|
||||
);
|
||||
string str= { .length=len, .ptr=malloc(len+1) };
|
||||
str.ptr[len]='\0';
|
||||
u32 i=0;
|
||||
Autoarr_foreach(b->compl_bufs, cs, ({
|
||||
Autoarr_foreach(b->compl_bufs, cs,
|
||||
for(u32 n=0;n<cs.length;n++)
|
||||
str.ptr[i++]=cs.ptr[n];
|
||||
free(cs.ptr);
|
||||
}));
|
||||
);
|
||||
StringBuilder_free(b);
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ void printMaybe(Maybe e);
|
||||
|
||||
#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.value=UniNull, .errmsg=ERRMSG}
|
||||
|
||||
#define __EXIT(ERRMSG) ({ kprintf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
|
||||
#define __EXIT(ERRMSG) ({ kprintf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
|
||||
|
||||
char* __doNothing(char* a);
|
||||
char* __unknownErr( );
|
||||
|
||||
@@ -19,23 +19,23 @@
|
||||
/// executes codeblock and prints execution time
|
||||
/// u64 op_i is counter of the internal loop
|
||||
/// uses non-standard high-precision clock
|
||||
#define optime(opname,repeats,codeblock) ({ \
|
||||
#define optime(opname, repeats, codeblock...) { \
|
||||
struct timespec start, stop; \
|
||||
clock_gettime(CLOCK_REALTIME, &start); \
|
||||
for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
|
||||
(codeblock); \
|
||||
{ codeblock; } \
|
||||
clock_gettime(CLOCK_REALTIME, &stop); \
|
||||
f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000; \
|
||||
__optime_print(opname,t) \
|
||||
})
|
||||
__optime_print(opname,t); \
|
||||
}
|
||||
#else
|
||||
/// uses standard low precision clock
|
||||
#define optime(opname,repeats,codeblock) ({ \
|
||||
#define optime(opname, repeats, codeblock...) { \
|
||||
clock_t start=clock(); \
|
||||
for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
|
||||
(codeblock); \
|
||||
{ codeblock; } \
|
||||
clock_t stop=clock(); \
|
||||
f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000; \
|
||||
__optime_print(opname,t) \
|
||||
})
|
||||
__optime_print(opname,t); \
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -50,6 +50,7 @@ void kprintf(const char* format, ...){
|
||||
va_start(vl, format);
|
||||
u32 i=0;
|
||||
for(char c=format[i++]; c!=0; c=format[i++]){
|
||||
// value format specifiers
|
||||
if(c=='%'){
|
||||
char* argstr=NULL;
|
||||
bool l=false;
|
||||
@@ -112,8 +113,11 @@ void kprintf(const char* format, ...){
|
||||
fputs(argstr, stdout);
|
||||
free(argstr);
|
||||
}
|
||||
} else if(c=='\e'){
|
||||
}
|
||||
// escape sequences
|
||||
else if(c=='\e'){
|
||||
IFWIN(
|
||||
/* WINDOWS */
|
||||
({
|
||||
if((c=format[i++])=='['){
|
||||
u8 colorUnix=0;
|
||||
@@ -135,9 +139,12 @@ void kprintf(const char* format, ...){
|
||||
}
|
||||
}
|
||||
}),
|
||||
/* UNIX */
|
||||
putc(c,stdout);
|
||||
);
|
||||
} else {
|
||||
}
|
||||
// common characters
|
||||
else {
|
||||
putc(c,stdout);
|
||||
}
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
|
||||
Reference in New Issue
Block a user