2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file bloom.c Simple bloom filter implementation. */
14 void *adu_calloc(size_t size)
16 void *ret = malloc(size);
24 #define INFO_LOG printf
27 static inline uint64_t filter_bits(struct bloom *b)
29 return 1ULL << b->order;
33 * SuperFastHash, by Paul Hsieh.
34 * http://www.azillionmonkeys.com/qed/hash.html
38 #if (defined(__GNUC__) && defined(__i386__))
39 #define get16bits(d) (*((const uint16_t *) (d)))
41 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
42 +(uint32_t)(((const uint8_t *)(d))[0]) )
46 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
53 for (;len > 0; len--) {
54 hash += get16bits (data);
55 tmp = (get16bits (data+2) << 11) ^ hash;
56 hash = (hash << 16) ^ tmp;
57 data += 2*sizeof (uint16_t);
61 /* Handle end cases */
64 hash += get16bits (data);
66 hash ^= data[sizeof (uint16_t)] << 18;
70 hash += get16bits (data);
79 /* Force "avalanching" of final 127 bits */
89 static int test_and_set_bit(uint64_t bitnum, struct bloom *b)
91 uint64_t byte = bitnum / 8;
92 uint8_t bit = 1 << (bitnum % 8);
93 int ret = b->filter[byte] & bit;
95 b->filter[byte] |= bit;
103 * Insert data to the given bloom filter.
105 * This function computes \a k hashes from the given data where \a k is the
106 * number of hash functions of the filter \a b. Each hash value corresponds to
107 * a position in the bit array of the filter and each of these bits are being
108 * tested and set. If not all \a k bits were already set, the given data was
109 * not yet contained in the filter and the function returns non-zero.
111 * Otherwise either (a) the same data has already been inserted previously or
112 * (b) a hash collision occurred or (c) the \a k bits are set due to previous
113 * insertion of other data (i.e. a false positive occurred). It is impossible
114 * to distinguish these cases.
116 * \param data The data to insert.
117 * \param len Number of bytes of \a data.
118 * \param b The filter to insert to.
120 * \return Zero if the entry was already contained in the filter (or in case of
121 * false positives), non-zero otherwise.
123 int bloom_insert(const uint8_t *data, size_t len, struct bloom *b)
126 uint32_t h = 0xb11924e1; /* some arbitrary value */
128 for (i = 1; i < b->num_hash_functions; i++) {
129 uint64_t bitnum = super_fast_hash(data, len, h);
132 bitnum = bitnum << 32;
133 h = super_fast_hash(data, len, h);
136 bitnum &= filter_bits(b) - 1;
137 ret |= !test_and_set_bit(bitnum, b);
145 * Deallocate a bloom filter.
147 * \param b The filter to deallocate.
149 void bloom_free(struct bloom *b)
158 * Allocate and initialize a new bloom filter.
160 * \param order Use a filter containing 2^order bits.
161 * \param num_hash_functions Set that many bits in the filter per entry.
163 * \return This function either returns a freshly allocated and initialized
164 * bloom filter or does not return at all (if the underlying malloc() failed).
166 struct bloom *bloom_new(unsigned order, unsigned num_hash_functions)
168 struct bloom *b = adu_calloc(sizeof(*b));
170 INFO_LOG("allocating bloom filter (order: %u, %u hash functions)\n",
171 order, num_hash_functions);
173 b->num_hash_functions = num_hash_functions;
174 b->filter = adu_calloc(filter_bits(b) / 8);
180 int bloom_insert_string(const char *str, struct bloom *b)
182 uint32_t len = strlen(str);
184 return bloom_insert((const uint8_t *)str, len, b);
187 void add_stdin(struct bloom *b)
190 unsigned false_positives = 0;
192 while (fgets(buf, sizeof(buf) - 1, stdin)) {
193 size_t len = strlen(buf);
196 if (buf[len - 1] == '\n')
198 if (bloom_insert_string(buf, b))
201 INFO_LOG("filter contains %llu entries\n", b->num_entries);
202 printf("%u possible false positives\n", false_positives);
205 int main(int argc, char **argv)
210 printf("usage: $0 j k\n");
211 printf("j: set bloom filter size to m=2^j bits\n");
212 printf("k: # of hash functions to use\n");
215 b = bloom_new(atoi(argv[1]), atoi(argv[2]));
217 INFO_LOG("%u%% of bits are set\n", (unsigned)
218 (b->num_set_bits * 100ULL / filter_bits(b)));
222 #endif /* TEST_BLOOM */