7 #include "../crc/xxhash.h"
8 #include "../crc/murmur3.h"
16 #define BITS_PER_INDEX (sizeof(uint32_t) * 8)
17 #define BITS_INDEX_MASK (BITS_PER_INDEX - 1)
21 uint32_t (*fn)(const void *, uint32_t, uint32_t);
24 struct bloom_hash hashes[] = {
41 #define MIN_ENTRIES 1073741824UL
43 struct bloom *bloom_new(uint64_t entries)
48 b = malloc(sizeof(*b));
49 b->nentries = entries;
50 no_uints = (entries + BITS_PER_INDEX - 1) / BITS_PER_INDEX;
51 no_uints = max((unsigned long) no_uints, MIN_ENTRIES);
52 b->map = calloc(no_uints, sizeof(uint32_t));
61 void bloom_free(struct bloom *b)
67 static int __bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords,
70 uint32_t hash[N_HASHES];
73 for (i = 0; i < N_HASHES; i++) {
74 hash[i] = hashes[i].fn(data, nwords, hashes[i].seed);
75 hash[i] = hash[i] % b->nentries;
79 for (i = 0; i < N_HASHES; i++) {
80 const unsigned int index = hash[i] / BITS_PER_INDEX;
81 const unsigned int bit = hash[i] & BITS_INDEX_MASK;
83 if (b->map[index] & (1U << bit))
86 b->map[index] |= 1U << bit;
89 return was_set == N_HASHES;
92 int bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords)
94 return __bloom_check(b, data, nwords, 0);
97 int bloom_set(struct bloom *b, uint32_t *data, unsigned int nwords)
99 return __bloom_check(b, data, nwords, 1);