布隆过滤器

实现完整代码

/***************************************************/
//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);
}

运行结果:
在这里插入图片描述


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值