实现完整代码
/***************************************************/
//BloomFilter.h
#include "BitMap.h"
#include "Common.h"
typedef int(*PSTRTINT)(const char*);
typedef struct BloomFilter
{
BitMap _bmp;
PSTRTINT _HashFunc[5];
int _size;
}BloomFilter;
//初始化布隆过滤器
void InitBloomFilter(BloomFilter* bf, int capacity, PSTRTINT* HashFunc);
//向布隆过滤器中插入元素
void Insert(BloomFilter* bf, const char* str);
//检查元素是否在布隆过滤器中
int IsBloomFilter(BloomFilter* bf, const char* str);
//看布隆过滤器中有多少元素
int SizeBloomFilter(BloomFilter* bf);
//销毁布隆过滤器
void DestroyBloomFilter(BloomFilter* bf);
/***************************************************/
//BloomFilter.c
//初始化布隆过滤器
void InitBloomFilter(BloomFilter* bf, int capacity, PSTRTINT* HashFunc)
{
assert(bf);
InitBitMap(&bf->_bmp, capacity * 5);
for (int i = 0; i < 5; i++)
bf->_HashFunc[i] = HashFunc[i];
bf->_size = 0;
}
//向布隆过滤器中插入元素
void Insert(BloomFilter* bf, const char* str)
{
assert(bf);
int hashAddr1, hashAddr2, hashAddr3, hashAddr4, hashAddr5;
hashAddr1 = bf->_HashFunc[0](str) % bf->_bmp._capacity;
SetBitMap(&bf->_bmp, hashAddr1);
hashAddr2 = bf->_HashFunc[1](str) % bf->_bmp._capacity;
SetBitMap(&bf->_bmp, hashAddr2);
hashAddr3 = bf->_HashFunc[2](str) % bf->_bmp._capacity;
SetBitMap(&bf->_bmp, hashAddr3);
hashAddr4 = bf->_HashFunc[3](str) % bf->_bmp._capacity;
SetBitMap(&bf->_bmp, hashAddr4);
hashAddr5 = bf->_HashFunc[4](str) % bf->_bmp._capacity;
SetBitMap(&bf->_bmp, hashAddr5);
bf->_size++;
}
//检查元素是否在布隆过滤器中
//不在--->一定是准确的
//在--->不一定准确
int IsBloomFilter(BloomFilter* bf, const char* str)
{
assert(bf);
int hashAddr1, hashAddr2, hashAddr3, hashAddr4, hashAddr5;
hashAddr1 = bf->_HashFunc[0](str) % bf->_bmp._capacity;
if (0 == TestBitMap(&bf->_bmp, hashAddr1))
return 0;
hashAddr2 = bf->_HashFunc[1](str) % bf->_bmp._capacity;
if (0 == TestBitMap(&bf->_bmp, hashAddr2))
return 0;
hashAddr3 = bf->_HashFunc[2](str) % bf->_bmp._capacity;
if (0 == TestBitMap(&bf->_bmp, hashAddr3))
return 0;
hashAddr4 = bf->_HashFunc[3](str) % bf->_bmp._capacity;
if (0 == TestBitMap(&bf->_bmp, hashAddr4))
return 0;
hashAddr5 = bf->_HashFunc[4](str) % bf->_bmp._capacity;
if (0 == TestBitMap(&bf->_bmp, hashAddr4))
return 0;
return 1;
}
//看布隆过滤器中有多少元素
int SizeBloomFilter(BloomFilter* bf)
{
assert(bf);
return bf->_size;
}
//销毁布隆过滤器
void DestroyBloomFilter(BloomFilter* bf)
{
assert(bf);
DestroyBitMap(&bf->_bmp);
}
void TestBloomFilter()
{
BloomFilter bf;
PSTRTINT hashFuncArr[] = { StrToINT1, StrToINT2, StrToINT3, StrToINT4, StrToINT5 };
InitBloomFilter(&bf, 100, hashFuncArr);
Insert(&bf, "赵");
Insert(&bf, "钱");
Insert(&bf, "孙");
Insert(&bf, "李");
Insert(&bf, "周");
printf("%d\n", SizeBloomFilter(&bf));
if (IsBloomFilter(&bf, "周"))
printf("周 在其中\n");
else
printf("周 不在其中\n");
if (IsBloomFilter(&bf, "王"))
printf("王 在其中\n");
else
printf("王 不在其中\n");
DestroyBloomFilter(&bf);
}
/***************************************************/
//布隆过滤器字符串转换方法
unsigned int StrToINT1(const char *str);
unsigned int StrToINT2(const char *str);
unsigned int StrToINT3(const char *str);
unsigned int StrToINT4(const char *str);
unsigned int StrToINT5(const char *str);
unsigned int StrToINT1(const char *str)
{
unsigned int hash = 0;
while (*str)
{
// equivalent to: hash = 65599*hash + (*str++);
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
// RS Hash Function
unsigned int StrToINT2(const char *str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
while (*str)
{
hash = hash * a + (*str++);
a *= b;
}
return (hash & 0x7FFFFFFF);
}
// JS Hash Function
unsigned int StrToINT3(const char *str)
{
unsigned int hash = 1315423911;
while (*str)
{
hash ^= ((hash << 5) + (*str++) + (hash >> 2));
}
return (hash & 0x7FFFFFFF);
}
// ELF Hash Function
unsigned int StrToINT4(const char *str)
{
unsigned int hash = 0;
unsigned int x = 0;
while (*str)
{
hash = (hash << 4) + (*str++);
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}
return (hash & 0x7FFFFFFF);
}
// BKDR Hash Function
unsigned int StrToINT5(const char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
运行结果: