简易布隆过滤器

布隆过滤器 Bloom Filter

一、原理

如果想判断一个元素是不是在一个集合里,一般想到的是将集合中所有元素保存起来,然后通过比较确定。链表、树、散列表(又叫哈希表,Hash table)等等数据结构都是这种思路。但是随着集合中元素的增加,我们需要的存储空间越来越大。同时检索速度也越来越慢。

Bloom Filter 是一种空间效率很高的随机数据结构,Bloom filter 可以看做是对 bit-map 的扩展, 它的原理是:

当一个元素被加入集合时,通过 K 个 Hash 函数将这个元素映射成一个位阵列(Bit array)中的 K 个点,把它们置为 1。检索时,我们只要看看这些点是不是都是 1 就(大约)知道集合中有没有它了:

  • 如果这些点有任何一个 0,则被检索元素一定不在
  • 如果都是 1,则被检索元素很可能在。

二、优点

它的优点是空间效率查询时间都远远超过一般的算法,布隆过滤器存储空间和插入 / 查询时间都是常数O(k)。另外, 散列函数相互之间没有关系,方便由硬件并行实现。布隆过滤器不需要存储元素本身,在某些对保密要求非常严格的场合有优势。

三、缺点

但是布隆过滤器的缺点和优点一样明显。误算率是其中之一。随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。

(误判补救方法是:再建立一个小的白名单,存储那些可能被误判的信息。)

另外,一般情况下不能从布隆过滤器中删除元素. 我们很容易想到把位数组变成整数数组,每插入一个元素相应的计数器加 1, 这样删除元素时将计数器减掉就可以了。然而要保证安全地删除元素并非如此简单。首先我们必须保证删除的元素的确在布隆过滤器里面. 这一点单凭这个过滤器是无法保证的。另外计数器回绕也会造成问题

四、实现如下:

#pragma once
#include<iostream>
#include<vector>

using namespace std;
class BitMap
{
public:
	BitMap(size_t len)
		:_size(0)
	{
		_array.resize(len/32+1,0);
	}
	void Set(size_t num)
	{
		size_t index=num>>5;
		//size_t index=num/31;
		size_t count=num%32;
		_array[index]|=1<<count;
		++_size;
	}
	void Reset(size_t num)
	{
		size_t index=num>>5;
		size_t count=num%32;
		_array[index]&=~(1<<count);
		--_size;
	}
		bool Find(size_t num)
	{
		size_t index = num>>5;
		size_t count = num%32;

		return _array[index]&(1<<count);
	}

	void Clear()
	{
		_array.assign(_array.size(), 0);
		_size = 0;
	}

private:
	vector<int> _array;
	size_t _size;
};
struct HashFunc1
{
	HashFunc1()
	{}
	size_t operator()(string s)
	{
		const char * begin=s.c_str();
		size_t hash=0;
		while(*begin)
		{
			hash+=*begin;
		    begin++;
		}
		return hash%13;
	}

};
struct HashFunc2
{
	HashFunc2()
	{}
	size_t operator()(string s)
	{
		const char * begin=s.c_str();
		size_t hash=0;
		while(*begin)
		{
			hash+=*begin;
		    begin++;
		}
		return hash%65;
	}
};
struct HashFunc3
{
	HashFunc3()
	{}
	size_t operator()(string s)
	{
		const char * begin=s.c_str();
		size_t hash=0;
		while(*begin)
		{
			hash+=*begin;
		    begin++;
		}
		return hash%131;
	}
};
struct HashFunc4
{
	HashFunc4()
	{}
	size_t operator()(string s)
	{
		const char * begin=s.c_str();
		size_t hash=0;
		while(*begin)
		{
			hash+=*begin;
		    begin++;
		}
		return hash%98;
	}
};
struct HashFunc5
{
	HashFunc5()
	{}
	size_t operator()(string s)
	{
		const char * begin=s.c_str();
		size_t hash=0;
		while(*begin)
		{
			hash+=*begin;
		    begin++;
		}
		return hash%56;
	}
};
template<class K=string,class _HashFunc1=HashFunc1,\
         class _HashFunc2=HashFunc2,\
         class _HashFunc3=HashFunc3,\
         class _HashFunc4=HashFunc4,\
         class _HashFunc5=HashFunc5>  //仿函数,重载圆括号函数构成仿函数

class bloom
{
public:
	bloom(size_t len)  //布隆过滤器不支持删除
		:_bitmap(len)
		,_capacity(len)
	{}
	 void Set(string s)
	 {
 		 _bitmap.Set(_hash1(s)%_capacity);
		 _bitmap.Set(_hash2(s)%_capacity);
		 _bitmap.Set(_hash3(s)%_capacity);
		 _bitmap.Set(_hash4(s)%_capacity);
		 _bitmap.Set(_hash5(s)%_capacity);
	 }
	bool Find(string s)
	{
		if(_bitmap.Find(_hash1(s)%_capacity)==0)
		{
			return false;
		}
		if(_bitmap.Find(_hash2(s)%_capacity)==0)
		{
			return false;
		}
		if(_bitmap.Find(_hash3(s)%_capacity)==0)
		{
			return false;
		}
		if(_bitmap.Find(_hash4(s)%_capacity)==0)
		{
			return false;
		}
		if(_bitmap.Find(_hash5(s)%_capacity)==0)
		{
			return false;
		}

	}
private:
		BitMap _bitmap;
	    size_t _capacity;
		HashFunc1 _hash1;
		HashFunc1 _hash2;
		HashFunc1 _hash3;
		HashFunc1 _hash4;
		HashFunc1 _hash5;

};

#include"bloom.h"
int main()
{
	bloom<string,HashFunc1,HashFunc2,HashFunc3,HashFunc4,HashFunc5> b(-1);
	b.Set("we are young");
	b.Set("I Love You");
	b.Set("always");
	cout<<b.Find("we are young")<<endl;
	cout<<b.Find("always")<<endl;
	cout<<b.Find("I Like You")<<endl;
	system("pause");
	return 0;
}

五、结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值