diff --git a/include/extra/array.h b/include/extra/array.h index fd662a9..f720a78 100644 --- a/include/extra/array.h +++ b/include/extra/array.h @@ -17,6 +17,6 @@ extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index, 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 libcdsb_array_foreach(const vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) LIBCDSB_nt__ LIBCDSB_nn12__; +extern int libcdsb_array_foreach(const vtype_array* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) LIBCDSB_nt__ LIBCDSB_nn13__; #endif /* LIBCDSB_EXTRA_ARRAY_H */ diff --git a/include/extra/list.h b/include/extra/list.h index 6ef3c89..a227b0e 100644 --- a/include/extra/list.h +++ b/include/extra/list.h @@ -19,6 +19,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, int (*callback)(void* value, ssize_t index, vtype type)) LIBCDSB_nt__ LIBCDSB_nn12__; +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__; #endif /* LIBCDSB_EXTRA_LIST_H */ diff --git a/include/extra/set.h b/include/extra/set.h index 80fabf9..7f37db5 100644 --- a/include/extra/set.h +++ b/include/extra/set.h @@ -11,6 +11,6 @@ extern _Bool libcdsb_vset_find (vtype_value* x, vtype_set* s, const void* value, vtype type, _Bool cut); extern _Bool libcdsb_vset_insert(vtype_set* x, const void* value, vtype type); -extern int libcdsb_vset_foreach(const vtype_set* x, int (*callback)(const void* value, vtype type)) LIBCDSB_nt__ LIBCDSB_nn12__; +extern int libcdsb_vset_foreach(const vtype_set* x, void* data, int (*callback)(const void* value, vtype type, void* data)) LIBCDSB_nt__ LIBCDSB_nn13__; #endif /* LIBCDSB_EXTRA_SET_H */ diff --git a/src/array/extra.c b/src/array/extra.c index 211d423..4486800 100644 --- a/src/array/extra.c +++ b/src/array/extra.c @@ -76,7 +76,7 @@ ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) { /*#####################################################################################################################*/ -int libcdsb_array_foreach(const vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) { +int libcdsb_array_foreach(const vtype_array* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) { void* p; void* e; @@ -88,7 +88,7 @@ int libcdsb_array_foreach(const vtype_array* x, int (*callback)(void* value, ssi n = 0; while (p < e) { - if ((r = callback(p, n, x->type))) + if ((r = callback(p, n, x->type, data))) return r; p += vtype_size(x->type); diff --git a/src/list/extra.c b/src/list/extra.c index 570b679..9e8405c 100644 --- a/src/list/extra.c +++ b/src/list/extra.c @@ -163,7 +163,7 @@ _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, int (*callback)(void* value, ssize_t index, vtype type)) { +int libcdsb_list_foreach(const vtype_list* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) { lnode_t* c; size_t n; @@ -173,7 +173,7 @@ int libcdsb_list_foreach(const vtype_list* x, int (*callback)(void* value, ssize n = 0; while (!is_null(c)) { - if ((r = callback(vnode_peek(&c->node, c->type), n, c->type)) != 0) + if ((r = callback(vnode_peek(&c->node, c->type), n, c->type, data)) != 0) return r; c = c->next; ++n; diff --git a/src/set/extra.c b/src/set/extra.c index 3c3be91..9acc2d1 100644 --- a/src/set/extra.c +++ b/src/set/extra.c @@ -76,17 +76,23 @@ _Bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) { /*#####################################################################################################################*/ -int libcdsb_vset_foreach(const vtype_set* x, int (*callback)(const void* value, vtype type)) { - rbiter_t i; - int r; +int libcdsb_vset_foreach(const set_t* x, void* data, int (*callback)(const void* value, vtype type, void* data)) { + stack_t s = { .prev = 0, .value = x->root }; + int r = 0; rbnode_t* c; - if (rbiter_init(&i, &x->root, 0)) { - while (!rbnode_is_empty(c = rbiter_next(&i))) { - if ((r = callback(vnode_peek(&c->value, x->type), x->type))) - return r; + if (rbnode_is_empty(x->root)) return 0; + + while ((c = stack_pop(&s))) { + if ((r = callback(vnode_peek(&c->value, x->type), x->type, data))) { + stack_flush(&s); + break; } + if (!rbnode_is_empty(c->left)) + stack_push(&s, c->left); + if (!rbnode_is_empty(c->right)) + stack_push(&s, c->right); } - return 0; + return r; }