1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_INVBLOOM_H
3 #define CCAN_INVBLOOM_H
4 #include <ccan/short_types/short_types.h>
5 #include <ccan/tal/tal.h>
11 s32 *count; /* [n_elems] */
12 u8 *idsum; /* [n_elems][id_size] */
16 * invbloom_new - create a new invertable bloom lookup table
17 * @ctx: context to tal() from, or NULL.
18 * @type: type to place into the buckets (must not contain padding)
19 * @n_elems: number of entries in table
20 * @salt: 32 bit seed for table
22 * Returns a new table, which can be freed with tal_free().
24 #define invbloom_new(ctx, type, n_elems, salt) \
25 invbloom_new_((ctx), sizeof(type), (n_elems), (salt))
26 struct invbloom *invbloom_new_(const tal_t *ctx,
28 size_t n_elems, u32 salt);
32 * invbloom_insert - add a new element
33 * @ib: the invertable bloom lookup table.
36 * This is guaranteed to be the inverse of invbloom_delete.
38 void invbloom_insert(struct invbloom *ib, const void *elem);
41 * invbloom_delete - remove an element
42 * @ib: the invertable bloom lookup table.
45 * Note that this doesn't check the element was previously added (as
46 * that can not be done in general anyway).
48 * This is guaranteed to be the inverse of invbloom_delete.
50 void invbloom_delete(struct invbloom *ib, const void *elem);
53 * invbloom_get - check if an element is (probably) in the table.
54 * @ib: the invertable bloom lookup table.
57 * This may return a false negative if the table is too full.
59 * It will only return a false positive if deletions and insertions
60 * don't match, and have managed to produce a result which matches the
61 * element. This is much less likely.
63 bool invbloom_get(const struct invbloom *ib, const void *elem);
66 * invbloom_extract - try to recover an entry added to a bloom lookup table.
67 * @ctx: the context to tal() the return value from.
68 * @ib: the invertable bloom lookup table.
70 * This may be able to recover (and delete) an element which was
71 * invbloom_insert()ed into the table (and not deleted). This will
72 * not work if the table is too full.
74 * It may return a bogus element if deletions and insertions don't
77 void *invbloom_extract(const tal_t *ctx, struct invbloom *ib);
80 * invbloom_extract_negative - try to recover an entry deleted from a bloom lookup table.
81 * @ctx: the context to tal() the return value from.
82 * @ib: the invertable bloom lookup table.
84 * This may be able to recover (and insert/undo) an element which was
85 * invbloom_delete()ed from the table (and not inserted). This will
86 * not work if the table is too full.
88 * It may return a bogus element if deletions and insertions don't
91 void *invbloom_extract_negative(const tal_t *ctx, struct invbloom *ib);
94 * invbloom_subtract - return the differences of two IBLTs.
95 * @ib1: the invertable bloom lookup table to alter
96 * @ib2: the invertable bloom lookup table to subtract.
98 * This produces exactly the same result as if a new table had all the
99 * elements only in @ib1 inserted, then all the elements onlt in @ib2
102 * ie. if @ib1 and @ib2 are similar, the result may be a usable by
103 * invbloom_extract and invbloom_extract_negative.
105 void invbloom_subtract(struct invbloom *ib1, const struct invbloom *ib2);
108 * invbloom_empty - is an invertable bloom lookup table completely clean?
109 * @ib: the invertable bloom lookup table
111 * This is always true if @ib has had the same elements inserted and
112 * deleted. It is far less likely to be true if different ones were
113 * deleted than inserted.
115 bool invbloom_empty(const struct invbloom *ib);
116 #endif /* CCAN_INVBLOOM_H */