mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
0a4ea07daa
`bloom_t` is defined as a struct. `_t` can mislead the user to think of bloom_t as a typedef (see our coding conventions) instead of a struct. Thus, I modified `struct bloom_t` to be a *typedefed* struct. Another solution would be to rename bloom_t to sth. like bloom_s everywhere and use `struct bloom_s` instead of `bloom_t`.
96 lines
2.2 KiB
C
96 lines
2.2 KiB
C
/**
|
|
* Bloom filter implementation
|
|
*
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
|
*
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
* directory for more details.
|
|
*
|
|
* @file
|
|
* @author Jason Linehan <patientulysses@gmail.com>
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
|
*
|
|
*/
|
|
|
|
#include <limits.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "bloom.h"
|
|
|
|
#define SETBIT(a,n) (a[n/CHAR_BIT] |= (1<<(n%CHAR_BIT)))
|
|
#define GETBIT(a,n) (a[n/CHAR_BIT] & (1<<(n%CHAR_BIT)))
|
|
#define ROUND(size) ((size + CHAR_BIT - 1) / CHAR_BIT)
|
|
|
|
bloom_t *bloom_new(size_t size, size_t num_hashes, ...)
|
|
{
|
|
bloom_t *bloom;
|
|
va_list hashes;
|
|
size_t n;
|
|
|
|
/* Allocate Bloom filter container */
|
|
if (!(bloom = malloc(sizeof(bloom_t)))) {
|
|
return NULL;
|
|
}
|
|
|
|
/* Allocate Bloom array */
|
|
if (!(bloom->a = calloc(ROUND(size), sizeof(char)))) {
|
|
free(bloom);
|
|
return NULL;
|
|
}
|
|
|
|
/* Allocate Bloom filter hash function pointers */
|
|
if (!(bloom->hash = (hashfp_t *)malloc(num_hashes * sizeof(hashfp_t)))) {
|
|
free(bloom->a);
|
|
free(bloom);
|
|
return NULL;
|
|
}
|
|
|
|
/* Assign hash functions to pointers in the Bloom filter */
|
|
va_start(hashes, num_hashes);
|
|
|
|
for (n = 0; n < num_hashes; n++) {
|
|
bloom->hash[n] = va_arg(hashes, hashfp_t);
|
|
}
|
|
|
|
va_end(hashes);
|
|
|
|
/*
|
|
* Record the number of hash functions (k) and the number of bytes
|
|
* in the Bloom array (m).
|
|
*/
|
|
bloom->k = num_hashes;
|
|
bloom->m = size;
|
|
|
|
return bloom;
|
|
}
|
|
|
|
void bloom_del(bloom_t *bloom)
|
|
{
|
|
free(bloom->a);
|
|
free(bloom->hash);
|
|
free(bloom);
|
|
}
|
|
|
|
void bloom_add(bloom_t *bloom, const uint8_t *buf, size_t len)
|
|
{
|
|
for (size_t n = 0; n < bloom->k; n++) {
|
|
uint32_t hash = bloom->hash[n](buf, len);
|
|
SETBIT(bloom->a, (hash % bloom->m));
|
|
}
|
|
}
|
|
|
|
bool bloom_check(bloom_t *bloom, const uint8_t *buf, size_t len)
|
|
{
|
|
for (size_t n = 0; n < bloom->k; n++) {
|
|
uint32_t hash = bloom->hash[n](buf, len);
|
|
|
|
if (!(GETBIT(bloom->a, (hash % bloom->m)))) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true; /* ? */
|
|
}
|