From cb57dd41595adddb08095298bb1ed258c8ea4877 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Thu, 16 Jul 2015 14:06:11 +0200 Subject: bitmap: use external iterator Reuse the Iterator object from hashmap.h and expose a similar API. This allows us to do { Iterator i; unsigned n; BITMAP_FOREACH(n, b, i) { Iterator j; unsigned m; BITMAP_FOREACH(m, b, j) { ... } } } without getting confused. Requested by David. --- src/basic/bitmap.c | 20 ++++++-------------- src/basic/bitmap.h | 8 ++++---- 2 files changed, 10 insertions(+), 18 deletions(-) (limited to 'src/basic') diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c index d865e2fc6e..d559be1bbb 100644 --- a/src/basic/bitmap.c +++ b/src/basic/bitmap.c @@ -27,7 +27,6 @@ struct Bitmap { long long unsigned *bitmaps; size_t n_bitmaps; size_t bitmaps_allocated; - unsigned next_entry; }; /* Bitmaps are only meant to store relatively small numbers @@ -149,22 +148,15 @@ void bitmap_clear(Bitmap *b) { b->bitmaps[i] = 0; } -void bitmap_rewind(Bitmap *b) { - if (!b) - return; - - b->next_entry = 0; -} - -bool bitmap_next(Bitmap *b, unsigned *n) { +bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) { long long bitmask; unsigned offset, rem; - if (!b && b->next_entry == BITMAP_END) + if (!b && i->idx == BITMAP_END) return false; - offset = BITMAP_NUM_TO_OFFSET(b->next_entry); - rem = BITMAP_NUM_TO_REM(b->next_entry); + offset = BITMAP_NUM_TO_OFFSET(i->idx); + rem = BITMAP_NUM_TO_REM(i->idx); bitmask = 1 << rem; for (; offset < b->n_bitmaps; offset ++) { @@ -172,7 +164,7 @@ bool bitmap_next(Bitmap *b, unsigned *n) { for (; bitmask; bitmask <<= 1, rem ++) { if (b->bitmaps[offset] & bitmask) { *n = BITMAP_OFFSET_TO_NUM(offset, rem); - b->next_entry = *n + 1; + i->idx = *n + 1; return true; } @@ -183,7 +175,7 @@ bool bitmap_next(Bitmap *b, unsigned *n) { bitmask = 1; } - b->next_entry = BITMAP_END; + i->idx = BITMAP_END; return false; } diff --git a/src/basic/bitmap.h b/src/basic/bitmap.h index 92bee51a2c..2874bc99f7 100644 --- a/src/basic/bitmap.h +++ b/src/basic/bitmap.h @@ -22,6 +22,7 @@ ***/ #include "macro.h" +#include "hashmap.h" typedef struct Bitmap Bitmap; @@ -37,13 +38,12 @@ bool bitmap_isset(Bitmap *b, unsigned n); bool bitmap_isclear(Bitmap *b); void bitmap_clear(Bitmap *b); -void bitmap_rewind(Bitmap *b); -bool bitmap_next(Bitmap *b, unsigned *n); +bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n); bool bitmap_equal(Bitmap *a, Bitmap *b); -#define BITMAP_FOREACH(n, b) \ - for (bitmap_rewind(b); bitmap_next((b), &(n)); ) +#define BITMAP_FOREACH(n, b, i) \ + for ((i).idx = 0; bitmap_iterate((b), &(i), (unsigned*)&(n)); ) DEFINE_TRIVIAL_CLEANUP_FUNC(Bitmap*, bitmap_free); -- cgit v1.2.3-54-g00ecf