simplified Autoarr/Hashtable _foreach

This commit is contained in:
Timerix22 2023-05-20 01:40:16 +06:00
parent d7136055d9
commit 461aef3a00
19 changed files with 78 additions and 71 deletions

View File

@ -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
}

View File

@ -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=&el; \
if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \
ktDescriptor_##type.freeMembers(members_ptr); \
})); \
); \
} \
__Autoarr_##type##_freeWithoutMembers(ar, freePtr);\
} \

View File

@ -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;
}

View File

@ -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
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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( );

View File

@ -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

View File

@ -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)

View File

@ -21,7 +21,7 @@ i64 _autoarrVsVector(u16 blockCount, u16 blockLength){
}
void test_autoarrVsVector(){
optime(__func__, 1, ({
optime(__func__, 1,
kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n");
_autoarrVsVector(4, 16);
_autoarrVsVector(16, 64);
@ -30,5 +30,5 @@ void test_autoarrVsVector(){
_autoarrVsVector(32, 1024);
_autoarrVsVector(256, 256);
_autoarrVsVector(1024, 1024);
}));
);
}

View File

@ -36,7 +36,7 @@ static void printallval(Autoarr(u16)* ar){
}
void test_autoarr(){
optime("test_autoarr",1,({
optime("test_autoarr",1,
kprintf("\e[96m------------[test_autoarr]------------\n");
Autoarr(u16)* ar=Autoarr_create(u16,10,16);
kprintf("\e[92mautoarr created\n");
@ -49,5 +49,5 @@ void test_autoarr(){
printallval(ar);
Autoarr_free(ar, true);
kprintf("\e[92mautoarr deleted\n");
}));
);
}

View File

@ -16,50 +16,50 @@ const char text[]=
void print_dtsod(Hashtable* dtsod){
kprintf("\e[92m");
Hashtable_foreach(dtsod, p,({
Hashtable_foreach(dtsod, p,
printkvp(p);
if(p.value.typeId==ktid_ptrName(Hashtable)){
kprintf(": {\n");
Hashtable* sub=p.value.VoidPtr;
Hashtable_foreach(sub, _p,({
Hashtable_foreach(sub, _p,
kprintf(" ");
printkvp(_p);
kprintf("\n");
}));
);
kprintf("}");
}
kprintf("\n");
}));
);
}
void test_dtsod(){
optime(__func__,1,({
optime(__func__,1,
kprintf("\e[96m-------------[test_dtsod]-------------\n");
Hashtable* dtsod;
char* s;
optime("deserialize",1,({
optime("deserialize",1,
tryLast(DtsodV24_deserialize(text),r)
dtsod=r.value.VoidPtr;
}));
);
print_dtsod(dtsod);
optime("serialize",1,({
optime("serialize",1,
tryLast(DtsodV24_serialize(dtsod),r)
s=r.value.VoidPtr;
}));
);
DtsodV24_free(dtsod);
kprintf("\e[92m%s",s);
optime("reserialize",10,({
optime("reserialize",10,
tryLast(DtsodV24_deserialize(s),r)
dtsod=r.value.VoidPtr;
free(s);
tryLast(DtsodV24_serialize(dtsod),rr)
s=rr.value.VoidPtr;
DtsodV24_free(dtsod);
}));
);
free(s);
}));
);
}

View File

@ -8,39 +8,39 @@
char data[]="iojihiojopijiugbjmoihftytryfdrh";
#define test_hashfunc(hasht, hashf)({ \
#define test_hashfunc(hasht, hashf) { \
kprintf("\e[94mfunction: \e[92m" #hashf "\n"); \
hasht h=0; \
optime("speed test", 1, ({ \
optime("speed test", 1, \
for(u32 i=0; i<SPEED_TESTS; i++) \
h=hashf(h, data, sizeof(data)); \
})); \
); \
/*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/ \
Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768); \
optime("collision test",1,({ \
optime("collision test",1, \
u32 collisions=0; \
for(u64 i=0;i< COLLISION_TESTS;i++){ \
hasht h=hashb(hashf, (u8*)&i, sizeof(i)); \
bool col=false; \
Autoarr_foreach(hashes,e,({ \
Autoarr_foreach(hashes,e, \
if(e==h) { \
col=true; \
break; \
} \
})); \
); \
if(col) collisions++; \
else Autoarr_add(hashes,h); \
} \
kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS); \
})); \
); \
Autoarr_free(hashes, true); \
kprintf("\e[96m--------------------------------------\n"); \
})
}
void test_hash_functions(){
optime("test_hash_functions",1,({
optime("test_hash_functions",1,
kprintf("\e[96m--------[test_hash_functions]---------\n");
test_hashfunc(u32, hash_crc32);
test_hashfunc(u32, hash_sdbm32);
}));
);
}

View File

@ -62,7 +62,7 @@ Unitype gett(Hashtable* ht){
void test_hashtable(){
optime("test_hashtable",1,({
optime("test_hashtable",1,
kprintf("\e[96m-----------[test_hashtable]-----------\n");
Hashtable* ht=Hashtable_create();
kprintf("\e[92mhashtable created\n");
@ -73,5 +73,5 @@ void test_hashtable(){
print_hashtable(ht);
Hashtable_free(ht);
kprintf("\e[92mhashtable freed\n");
}));
);
}

View File

@ -2,7 +2,7 @@
#include "../src/random/krandom.h"
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){ \
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO) { \
kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n"); \
void* s= ALG##_init(0); \
u##VALUE_SIZE r=ALG##_next(s); \
@ -20,7 +20,7 @@
}
void test_rng_algorithms(){
optime("test_rng_algorithms",1,({
optime("test_rng_algorithms",1,
kprintf("\e[96m--------[test_rng_algorithms]---------\n");
// for ALG32
// xoroshiro64
@ -41,5 +41,5 @@ void test_rng_algorithms(){
test_alg(xoshiro256starstar, 64, 11091344671253066420ULL)
// splitmix64
test_alg(splitmix64, 64, 16294208416658607535ULL)
}));
);
}

View File

@ -30,7 +30,7 @@ Maybe a(){ try(b(),_,;) return MaybeNull; }
void test_safethrow(){
kprintf("\e[96m-----------[test_safethrow]-----------\n");
optime("test_safethrow", 1, ({
optime("test_safethrow", 1,
Maybe e=test_maybe();
kprintf("\e[94mthrow_error:\n\e[92m");
printMaybe(e);
@ -39,5 +39,5 @@ void test_safethrow(){
e=a();
printMaybe(e);
Maybe_free(e);
}));
);
}

View File

@ -23,7 +23,7 @@ void printstnode(STNode* node){
}
void test_searchtree(){
optime("test_searchtree",1,({
optime("test_searchtree",1,
kprintf("\e[96m-----------[test_searchtree]----------\n");
STNode* node=STNode_create();
kprintf("\e[92mnode created\n");
@ -70,5 +70,5 @@ void test_searchtree(){
printstnode(node);
STNode_free(node);
kprintf("\e[92mnode deleted\n");
}));
);
}

View File

@ -2,7 +2,7 @@
#include "../src/String/string.h"
void test_string(){
optime(__func__,1,({
optime(__func__,1,
kprintf("\e[96m-------------[test_string]------------\n");
char c[]="0123456789abcdef";
string s={.ptr=c, .length=cptr_length(c)};
@ -10,5 +10,5 @@ void test_string(){
char* p=string_extract(s);
kprintf("\e[94mstring_extract() -> \e[92m\"%s\"\n",p);
free(p);
}));
);
}

View File

@ -21,7 +21,7 @@ void test_type_system();
inline void test_all(){
kprintf("\e[97mkerep tests are starting!\n");
optime(__func__, 1, ({
optime(__func__, 1,
test_type_system();
test_string();
test_safethrow();
@ -35,7 +35,7 @@ inline void test_all(){
test_hashtable();
test_dtsod();
kprintf("\e[96m--------------------------------------\e[0m\n");
}));
);
}
#define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T))