Extend the foreach implementation

This commit is contained in:
2022-06-07 21:42:11 +03:00
parent b6a97576e4
commit 0922870d04
6 changed files with 21 additions and 15 deletions
+14 -8
View File
@@ -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;
}