Add array (extra) foreach

This commit is contained in:
Gregory Lirent 2022-06-05 20:01:10 +03:00
parent 4c9cd1525d
commit 355c822e61
2 changed files with 28 additions and 2 deletions

View File

@ -12,7 +12,9 @@
extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn2__;
extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern ssize_t libcdsb_array_push( vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern ssize_t libcdsb_array_push( vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern int array_foreach(vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) LIBCDSB_nt__ LIBCDSB_nn12__;
#endif /* LIBCDSB_EXTRA_ARRAY_H */

View File

@ -73,3 +73,27 @@ ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) {
return i;
}
/*#####################################################################################################################*/
int array_foreach(vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) {
void* p;
void* e;
size_t n;
int r;
p = x->mem;
e = x->mem + x->size*vtype_size(x->type);
n = 0;
while (p < e) {
if ((r = callback(p, n, x->type)))
return r;
p += vtype_size(x->type);
++n;
}
return 0;
}