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.
long long unsigned *bitmaps;
size_t n_bitmaps;
size_t bitmaps_allocated;
- unsigned next_entry;
};
/* Bitmaps are only meant to store relatively small numbers
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 ++) {
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;
}
bitmask = 1;
}
- b->next_entry = BITMAP_END;
+ i->idx = BITMAP_END;
return false;
}
***/
#include "macro.h"
+#include "hashmap.h"
typedef struct Bitmap Bitmap;
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);
}
static int dns_packet_append_types(DnsPacket *p, Bitmap *types, size_t *start) {
+ Iterator i;
uint8_t window = 0;
uint8_t len = 0;
uint8_t bitmaps[32] = {};
saved_size = p->size;
- BITMAP_FOREACH(n, types) {
+ BITMAP_FOREACH(n, types, i) {
uint8_t entry;
assert(n <= 0xffff);
static char *format_types(Bitmap *types) {
_cleanup_strv_free_ char **strv = NULL;
_cleanup_free_ char *str = NULL;
+ Iterator i;
unsigned type;
int r;
- BITMAP_FOREACH(type, types) {
+ BITMAP_FOREACH(type, types, i) {
if (dns_type_to_string(type)) {
r = strv_extend(&strv, strdup(dns_type_to_string(type)));
if (r < 0)
int main(int argc, const char *argv[]) {
_cleanup_bitmap_free_ Bitmap *b = NULL;
+ Iterator it;
unsigned n = (unsigned) -1, i = 0;
b = bitmap_new();
assert_se(bitmap_set(b, 1) == 0);
assert_se(bitmap_set(b, 256) == 0);
- BITMAP_FOREACH(n, b) {
+ BITMAP_FOREACH(n, b, it) {
assert_se(n == i);
if (i == 0)
i = 1;
i = 0;
- BITMAP_FOREACH(n, b) {
+ BITMAP_FOREACH(n, b, it) {
assert_se(n == i);
if (i == 0)
i = 1;