Update list (extra) foreach implementation

This commit is contained in:
Gregory Lirent 2022-06-08 09:57:57 +03:00
parent 472639a681
commit 659c329880
2 changed files with 32 additions and 9 deletions

View File

@ -6,11 +6,14 @@
#ifndef LIBCDSB_EXTRA_LIST_H
#define LIBCDSB_EXTRA_LIST_H
typedef int (*list_foreach_callback)(void* value, ssize_t index, vtype type, void* data);
#define list_get_by_index(x, s, index) libcdsb_list_get(x, s, index, 0)
#define list_pop_by_index(x, s, index) libcdsb_list_get(x, s, index, 1)
#define list_remove_by_index(s, index) libcdsb_list_get(0, s, index, 1)
#define list_foreach libcdsb_list_foreach
#define list_foreach(x, data, callback) libcdsb_list_foreach(x, data, callback, 0)
extern ssize_t libcdsb_list_find (vtype_value* x, vtype_list* s, const void* value, vtype type, _Bool reverse, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn2__;
extern _Bool libcdsb_list_update(vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction) LIBCDSB_nt__ LIBCDSB_nn1__;
@ -19,6 +22,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) LIBCDSB_nt__ LIBCDSB_nn2__;
extern int libcdsb_list_foreach(const vtype_list* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) LIBCDSB_nt__ LIBCDSB_nn13__;
extern int libcdsb_list_foreach(vtype_list* x, void* data, list_foreach_callback, _Bool flush) LIBCDSB_nt__ LIBCDSB_nn13__;
#endif /* LIBCDSB_EXTRA_LIST_H */

View File

@ -163,20 +163,40 @@ _Bool libcdsb_list_update(list_t* x, ssize_t i, const void* v, vtype t, int ins)
/*#####################################################################################################################*/
int libcdsb_list_foreach(const vtype_list* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) {
int libcdsb_list_foreach(vtype_list* x, void* data, list_foreach_callback callback, _Bool flush) {
lnode_t* n;
lnode_t* c;
size_t n;
size_t i;
int r;
c = x->first;
n = 0;
i = 0;
while (!is_null(c)) {
if ((r = callback(vnode_peek(&c->node, c->type), n, c->type, data)) != 0)
return r;
c = c->next;
++n;
if ((r = callback(vnode_peek(&c->node, c->type), i, c->type, data)) != 0)
break;
n = c->next;
if (flush) {
vnode_free(&c->node, c->type);
free(c);
}
c = n;
++i;
}
if (flush) {
while(!is_null(c)) {
n = c->next;
vnode_free(&c->node, c->type);
free(c);
c = n;
}
memset(x, 0, sizeof(*x));
}
return 0;