X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Finvbloom%2Finvbloom.c;h=d34ec1feeadded58b431be5e8aaa70bf9bc748ff;hb=ed6dd33e06c0e8f1c4dd006e0b70d9f2d6ba6c09;hp=bda23462ae5d64ed91d7c1cd22966a19c40d0d0c;hpb=eaab3d3e5cef9a612e688cf2b0d099277ade145a;p=ccan diff --git a/ccan/invbloom/invbloom.c b/ccan/invbloom/invbloom.c index bda23462..d34ec1fe 100644 --- a/ccan/invbloom/invbloom.c +++ b/ccan/invbloom/invbloom.c @@ -34,7 +34,7 @@ struct invbloom *invbloom_new_(const tal_t *ctx, void invbloom_singleton_cb_(struct invbloom *ib, void (*cb)(struct invbloom *, - size_t bucket, void *), + size_t bucket, bool, void *), void *data) { ib->singleton = cb; @@ -51,7 +51,7 @@ static u8 *idsum_ptr(const struct invbloom *ib, size_t bucket) return (u8 *)ib->idsum + bucket * ib->id_size; } -static void check_for_singleton(struct invbloom *ib, size_t bucket) +static void check_for_singleton(struct invbloom *ib, size_t bucket, bool before) { if (!ib->singleton) return; @@ -59,7 +59,7 @@ static void check_for_singleton(struct invbloom *ib, size_t bucket) if (ib->count[bucket] != 1 && ib->count[bucket] != -1) return; - ib->singleton(ib, bucket, ib->singleton_data); + ib->singleton(ib, bucket, before, ib->singleton_data); } static void add_to_bucket(struct invbloom *ib, size_t n, const u8 *id) @@ -67,12 +67,14 @@ static void add_to_bucket(struct invbloom *ib, size_t n, const u8 *id) size_t i; u8 *idsum = idsum_ptr(ib, n); + check_for_singleton(ib, n, true); + ib->count[n]++; for (i = 0; i < ib->id_size; i++) idsum[i] ^= id[i]; - check_for_singleton(ib, n); + check_for_singleton(ib, n, false); } static void remove_from_bucket(struct invbloom *ib, size_t n, const u8 *id) @@ -80,11 +82,13 @@ static void remove_from_bucket(struct invbloom *ib, size_t n, const u8 *id) size_t i; u8 *idsum = idsum_ptr(ib, n); + check_for_singleton(ib, n, true); + ib->count[n]--; for (i = 0; i < ib->id_size; i++) idsum[i] ^= id[i]; - check_for_singleton(ib, n); + check_for_singleton(ib, n, false); } void invbloom_insert(struct invbloom *ib, const void *id) @@ -141,7 +145,7 @@ static void *extract(const tal_t *ctx, struct invbloom *ib, int count) if (ib->count[i] != count) continue; - id = tal_dup(ctx, u8, idsum_ptr(ib, i), ib->id_size, 0); + id = tal_dup_arr(ctx, u8, idsum_ptr(ib, i), ib->id_size, 0); return id; } return NULL; @@ -175,12 +179,15 @@ void invbloom_subtract(struct invbloom *ib1, const struct invbloom *ib2) assert(ib1->id_size == ib2->id_size); assert(ib1->salt == ib2->salt); + for (i = 0; i < ib1->n_elems; i++) + check_for_singleton(ib1, i, true); + for (i = 0; i < ib1->n_elems * ib1->id_size; i++) ib1->idsum[i] ^= ib2->idsum[i]; for (i = 0; i < ib1->n_elems; i++) { ib1->count[i] -= ib2->count[i]; - check_for_singleton(ib1, i); + check_for_singleton(ib1, i, false); } }