Bloom filter library. More...
Bloom filter library.
Files | |
file | bloom.h |
Bloom filter API. | |
Data Structures | |
struct | bloom_t |
bloom_t bloom filter object More... | |
Typedefs | |
typedef uint32_t(* | hashfp_t) (const uint8_t *, int len) |
hash function to use in thee filter | |
Functions | |
void | bloom_init (bloom_t *bloom, size_t size, uint8_t *bitfield, hashfp_t *hashes, int hashes_numof) |
Initialize a Bloom Filter. More... | |
void | bloom_del (bloom_t *bloom) |
Delete a Bloom filter. More... | |
void | bloom_add (bloom_t *bloom, const uint8_t *buf, size_t len) |
Add a string to a Bloom filter. More... | |
bool | bloom_check (bloom_t *bloom, const uint8_t *buf, size_t len) |
Determine if a string is in the Bloom filter. More... | |
void bloom_add | ( | bloom_t * | bloom, |
const uint8_t * | buf, | ||
size_t | len | ||
) |
Add a string to a Bloom filter.
CAVEAT Once a string has been added to the filter, it cannot be "removed"!
bloom | Bloom filter |
buf | string to add |
len | the length of the string buf |
bool bloom_check | ( | bloom_t * | bloom, |
const uint8_t * | buf, | ||
size_t | len | ||
) |
Determine if a string is in the Bloom filter.
The string 's' is hashed once for each of the 'k' hash functions, as though we were planning to add it to the filter. Instead of adding it however, we examine the bit that we would have set, and consider its value.
If the bit is 1 (set), the string we are hashing may be in the filter, since it would have set this bit when it was originally hashed. However, it may also be that another string just happened to produce a hash value that would also set this bit. That would be a false positive. This is why we have k > 1, so we can minimize the likelihood of false positives occurring.
If every bit corresponding to every one of the k hashes of our query string is set, we can say with some probability of being correct that the string we are holding is indeed "in" the filter. However, we can never be sure.
If, however, as we hash our string and peek at the resulting bit in the filter, we find the bit is 0 (not set)... well now, that's different. In this case, we can say with absolute certainty that the string we are holding is not in the filter, because if it were, this bit would have to be set.
In this way, the Bloom filter can answer NO with absolute surety, but can only speak a qualified YES.
bloom | Bloom filter |
buf | string to check |
len | the length of the string buf |
void bloom_del | ( | bloom_t * | bloom | ) |
Delete a Bloom filter.
bloom | The condemned |
void bloom_init | ( | bloom_t * | bloom, |
size_t | size, | ||
uint8_t * | bitfield, | ||
hashfp_t * | hashes, | ||
int | hashes_numof | ||
) |
Initialize a Bloom Filter.
bloom | bloom_t to initialize |
size | size of the bloom filter in bits |
bitfield | underlying bitfield of the bloom filter |
hashes | array of hashes |
hashes_numof | number of elements in hashes |
bitfield
MUST be large enough to hold size
bits.