diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2015-01-09 16:10:40 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2015-01-09 16:10:40 +0000 |
commit | b59ce39f9779cf4b1ad423783b6a532f94a8e880 (patch) | |
tree | 4db0c2cc5de6b4fe5d01fb5cc762b6a74ce32d34 /src/libdatastruct/avlnode_iter.c | |
parent | 157ecd01d6cc4598ce4b0ec9bd477abbbf41d6c5 (diff) | |
download | skalibs-b59ce39f9779cf4b1ad423783b6a532f94a8e880.tar.xz |
Add cancellation to iterators over avltree(n) and genset(dyn)
Diffstat (limited to 'src/libdatastruct/avlnode_iter.c')
-rw-r--r-- | src/libdatastruct/avlnode_iter.c | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/libdatastruct/avlnode_iter.c b/src/libdatastruct/avlnode_iter.c index c6064f3..e1b17de 100644 --- a/src/libdatastruct/avlnode_iter.c +++ b/src/libdatastruct/avlnode_iter.c @@ -4,23 +4,27 @@ struct avlnode_iter_s { - avlnode *s ; + avlnode const *s ; unsigned int max ; + unsigned int cut ; avliterfunc_t_ref f ; void *p ; } ; -static int avlnode_iter_rec (struct avlnode_iter_s const *blah, unsigned int r, unsigned int h) +static unsigned int avlnode_iter_rec (struct avlnode_iter_s const *blah, unsigned int r, unsigned int h) { - return (r < blah->max) ? - avlnode_iter_rec(blah, blah->s[r].child[0], h+1) - && (*blah->f)(blah->s[r].data, h, blah->p) - && avlnode_iter_rec(blah, blah->s[r].child[1], h+1) - : 1 ; + if (r >= blah->max) return blah->max ; + { + unsigned int res = avlnode_iter_rec(blah, blah->s[r].child[0], h+1) ; + if (res != blah->max) return res ; + } + if (r == blah->cut) return blah->max ; + if (!(*blah->f)(blah->s[r].data, h, blah->p)) return r ; + return avlnode_iter_rec(blah, blah->s[r].child[1], h+1) ; } -int avlnode_iter (avlnode *s, unsigned int max, unsigned int r, avliterfunc_t_ref f, void *p) +unsigned int avlnode_iter_nocancel (avlnode *s, unsigned int max, unsigned int cut, unsigned int r, avliterfunc_t_ref f, void *p) { - struct avlnode_iter_s blah = { s, max, f, p } ; + struct avlnode_iter_s blah = { .s = s, .max = max, .cut = cut, .f = f, .p = p } ; return avlnode_iter_rec(&blah, r, 0) ; } |