Add list (extra) foreach

This commit is contained in:
Gregory Lirent 2022-06-05 18:36:48 +03:00
parent 3598394391
commit 3e681aadd8
2 changed files with 30 additions and 6 deletions

View File

@ -18,4 +18,6 @@ extern size_t libcdsb_list_count(const vtype_list* s, const void* value, vtype
extern ssize_t libcdsb_list_get(vtype_value* x, vtype_list* s, ssize_t index, _Bool cut);
extern int list_foreach(const vtype_list* x, int (*callback)(void* value, ssize_t index, vtype type));
#endif /* LIBCDSB_EXTRA_LIST_H */

View File

@ -28,7 +28,6 @@ ssize_t libcdsb_list_get(val_t* x, list_t* s, ssize_t i, _Bool cut) {
ldir_t dir;
lnode_t* c;
size_t n;
int cmp;
if (i < 0) {
n = (i = ~i);
@ -137,18 +136,18 @@ _Bool libcdsb_list_update(list_t* x, ssize_t i, const void* v, vtype t, int ins)
if (is_null(c)) {
x->first = x->last = c = calloc(sizeof(*c), 1);
} else if (ins) {
lnode_t *x = malloc(sizeof(*x));
lnode_t *v = malloc(sizeof(*v));
dir = (ins < 0) ? LD_PREV : LD_NEXT;
ldir_dir(x, dir) = ldir_dir(c, dir);
ldir_inv(x, dir) = c;
ldir_dir(v, dir) = ldir_dir(c, dir);
ldir_inv(v, dir) = c;
c = x;
c = v;
if (!is_null(ldir_dir(c, dir))) {
ldir_inv(ldir_dir(c, dir), dir) = c;
}
} else ldir_inv((lnode_t*)x, dir) = c;
ldir_dir(ldir_inv(c, dir), dir) = c;
@ -159,3 +158,26 @@ _Bool libcdsb_list_update(list_t* x, ssize_t i, const void* v, vtype t, int ins)
return true;
}
/*#####################################################################################################################*/
int list_foreach(const vtype_list* x, int (*callback)(void* value, ssize_t index, vtype type)) {
lnode_t* c;
size_t n;
int r;
c = x->first;
n = 0;
while (!is_null(c)) {
if ((r = callback(vnode_peek(&c->node, c->type), n, c->type)) != 0)
return r;
c = c->next;
++n;
}
return 0;
}