X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=bloom.c;h=ec12d512d942d17a7aa6c8edd55b73b2e5f5b60e;hp=2aff535e833f75ba178bfcbf0524e0bf19d0cca7;hb=3a51a868784e755c34f19a16970118936fbf299f;hpb=4074e7034641cbbbfc861961d216d58a503d0aee diff --git a/bloom.c b/bloom.c index 2aff535..ec12d51 100644 --- a/bloom.c +++ b/bloom.c @@ -1,8 +1,10 @@ -#include -#include -#include -#include -#include +/* + * Copyright (C) 2008 Andre Noll + * + * Licensed under the GPL v2. For licencing details see COPYING. + */ + +/** \file bloom.c Simple bloom filter implementation. */ #include "adu.h" #include "string.h" @@ -32,13 +34,14 @@ static inline uint64_t filter_bits(struct bloom *b) * http://www.azillionmonkeys.com/qed/hash.html */ +/** \cond */ #if (defined(__GNUC__) && defined(__i386__)) #define get16bits(d) (*((const uint16_t *) (d))) #else #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ +(uint32_t)(((const uint8_t *)(d))[0]) ) #endif - +/** \endcond */ static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash) { @@ -96,7 +99,28 @@ static int test_and_set_bit(uint64_t bitnum, struct bloom *b) return ret; } -int bloom_test_and_insert(const uint8_t *data, size_t len, struct bloom *b) +/** + * Insert data to the given bloom filter. + * + * This function computes \a k hashes from the given data where \a k is the + * number of hash functions of the filter \a b. Each hash value corresponds to + * a position in the bit array of the filter and each of these bits are being + * tested and set. If not all \a k bits were already set, the given data was + * not yet contained in the filter and the function returns non-zero. + * + * Otherwise either (a) the same data has already been inserted previously or + * (b) a hash collision occurred or (c) the \a k bits are set due to previous + * insertion of other data (i.e. a false positive occurred). It is impossible + * to distinguish these cases. + * + * \param data The data to insert. + * \param len Number of bytes of \a data. + * \param b The filter to insert to. + * + * \return Zero if the entry was already contained in the filter (or in case of + * false positives), non-zero otherwise. + */ +int bloom_insert(const uint8_t *data, size_t len, struct bloom *b) { int i, ret = 0; uint32_t h = 0xb11924e1; /* some arbitrary value */ @@ -153,11 +177,11 @@ struct bloom *bloom_new(unsigned order, unsigned num_hash_functions) #ifdef TEST_BLOOM -int bloom_test_and_insert_string(const char *str, struct bloom *b) +int bloom_insert_string(const char *str, struct bloom *b) { uint32_t len = strlen(str); - return bloom_test_and_insert((const uint8_t *)str, len, b); + return bloom_insert((const uint8_t *)str, len, b); } void add_stdin(struct bloom *b) @@ -171,7 +195,7 @@ void add_stdin(struct bloom *b) continue; if (buf[len - 1] == '\n') buf[len - 1] = '\0'; - if (bloom_test_and_insert_string(buf, b)) + if (bloom_insert_string(buf, b)) false_positives++; } INFO_LOG("filter contains %llu entries\n", b->num_entries);