Add array (extra) foreach
This commit is contained in:
parent
4c9cd1525d
commit
355c822e61
@ -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 */
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user