summaryrefslogtreecommitdiff
path: root/src/libdatastruct/gensetdyn_iter.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
commit3534b428629be185e096be99e3bd5fdfe32d5544 (patch)
tree210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libdatastruct/gensetdyn_iter.c
downloadskalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libdatastruct/gensetdyn_iter.c')
-rw-r--r--src/libdatastruct/gensetdyn_iter.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libdatastruct/gensetdyn_iter.c b/src/libdatastruct/gensetdyn_iter.c
new file mode 100644
index 0000000..216d42b
--- /dev/null
+++ b/src/libdatastruct/gensetdyn_iter.c
@@ -0,0 +1,26 @@
+/* ISC license. */
+
+#include <skalibs/bitarray.h>
+#include <skalibs/functypes.h>
+#include <skalibs/gensetdyn.h>
+
+unsigned int gensetdyn_iter (gensetdyn_ref g, iterfunc_t_ref f, void *stuff)
+{
+ /*
+ XXX: we may be called by a freeing function, so we cannot alloc -
+ XXX: so pray that the bitarray fits in the stack.
+ */
+ unsigned char bits[bitarray_div8(g->storage.len)] ;
+ unsigned int i = 0, j = 0, n = 0, m = gensetdyn_n(g) ;
+ register unsigned int *fl = genalloc_s(unsigned int, &g->freelist) ;
+ register unsigned int sp = genalloc_len(unsigned int, &g->freelist) ;
+ bitarray_setn(bits, 0, g->storage.len) ;
+
+ for (; i < sp ; i++) bitarray_clear(bits, fl[i]) ;
+ for (i = 0 ; (i < g->storage.len) && (j < m) ; i++) if (bitarray_peek(bits, i))
+ {
+ j++ ;
+ if ((*f)(gensetdyn_p(g, i), stuff)) n++ ;
+ }
+ return n ;
+}