API定义
/** *bloom_filter.h * *bloom filter算法的API定义和说明。 *这套API没有考虑错误处理,不包含特定hash函数的实现。 */ #ifndef BLOOM_FILTER_H_INCLUDED #define BLOOM_FILTER_H_INCLUDED /** *bloom filter算法核心类型 * *核心类型用于定义bloom filter实例,其中包含算法所需的必要数据结构。 *API用户无法也不应该直接访问bloom_filter结构体。 */ typedef struct bloom_filter *bf_core_type; /** *hash回调函数类型 * *hash函数实现者应该对element元素计算hash值。 *实现者不需要在返回前特意对hash结果模m(m含义见bf_create)。 */ typedef unsigned long (*bf_hash_handler_type)(void *element); /** *创建bloom_filter实例 * *为bloom_filter实例分配内存并做部分必要的初始化,这是bloom_filter实例生命周期的开始。 *handlers数组包含k个hash函数指针,由API用户自己分配。m设置bloom_filter实例内部记录表比特位长度。 *handlers数组在bloom_filter实例的生命周期内必须始终存在,并且最终由API用户自己负责释放。 *紧接着必须调用bf_clear。 */ bf_core_type bf_create(bf_hash_handler_type handlers[], unsigned long k, unsigned long m); /** *释放bf实例 * *这是bf实例生命周期的终结。 *这里不会释放bf_create中传入的handlers数组。 */ void bf_destroy(bf_core_type bf); /** *清空bf实例中已有的元素 * *至少应在bf_create之后调用一次。 */ void bf_clear(bf_core_type bf); /** *向bf实例添加元素element */ void bf_add(bf_core_type bf, void *element); /** *查询元素element是否在bf实例中 * *bf中存在element则返回1,否则返回0。 *根据bloom filter算法,返回的结果有一定的错误率。 */ int bf_has(bf_core_type bf, void *element); #endif // BLOOM_FILTER_H_INCLUDED /** *bloom_filter.h * *bloom f