简单布隆过滤器实现、布隆过滤器扩展
来源:互联网 发布:sql分页查询语句join 编辑:程序博客网 时间:2024/05/16 05:06
布隆过滤器
1.实现一个简单的布隆过滤器。
如果想要判断一个元素是不是在一个集合里,一般想到的是将所有元素保存起来,然后通过比较确定。链表,树等等数据结构都是这种思路.
但是随着集合中元素的增加,我们需要的存储空间越来越大,检索速度也越来越慢(O(n),O(logn))。不过世界上还有一种叫作散列表(又叫哈希表,Hash
table)的数据结构。它可以通过一个Hash函数将一个元素映射成一个位阵列(Bit
array)中的一个点。这样一来,我们只要看看这个点是不是1就可以知道集合中有没有它了。这就是布隆过滤器的基本思想。
优点
相比于其它的数据结构,布隆过滤器在空间和时间方面都有巨大的优势。布隆过滤器存储空间和插入/查询时间都是常数。另外,
Hash函数相互之间没有关系,方便由硬件并行实现。布隆过滤器不需要存储元素本身,在某些对保密要求非常严格的场合有优势。
布隆过滤器可以表示全集,其它任何数据结构都不能; k和m相同,使用同一组Hash函数的两个布隆过滤器的交并差运算可以使用位操作进行。
布隆过滤器
缺点
但是布隆过滤器的缺点和优点一样明显。误算率是其中之一。随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。
另外,一般情况下不能从布隆过滤器中删除元素。我们很容易想到把位列阵变成整数数组,每插入一个元素相应的计数器加1,
这样删除元素时将计数器减掉就可以了。然而要保证安全的删除元素并非如此简单。首先我们必须保证删除的元素的确在布隆过滤器里面.
这一点单凭这个过滤器是无法保证的。另外计数器回绕也会造成问题。 在降低误算率方面,有不少工作,使得出现了很多布隆过滤器的变种。
代码实现:
#include<iostream>#include"BitSet.h"using namespace std;//布隆过滤器//实现思路: 位图+hash//hash 确定位置进行定位template<class K>struct _HashFunc1{ size_t BKDRHash(const char *str) { register size_t hash = 0; while (size_t ch = (size_t)*str++) { hash = hash * 131 + ch; // 也可以乘以31、131、1313、13131、131313.. } return hash; } size_t operator()(const string &s) { return BKDRHash(s.c_str()); }};template<class K>struct _HashFunc2{ size_t SDBMHash(const char *str) { register size_t hash = 0; while (size_t ch = (size_t)*str++) { hash = 65599 * hash + ch; //hash = (size_t)ch + (hash << 6) + (hash << 16) - hash; } return hash; } size_t operator()(const string &s) { return SDBMHash(s.c_str()); }};template<class K>struct _HashFunc3{ size_t RSHash(const char *str) { if (!*str) return 0; register size_t hash = 1315423911; while (size_t ch = (size_t)*str++) { hash ^= ((hash << 5) + ch + (hash >> 2)); } return hash; } size_t operator()(const string &s) { return RSHash(s.c_str()); }};template<class K>struct _HashFunc4{ size_t RSHash(const char *str) { register size_t hash = 0; size_t magic = 63689; while (size_t ch = (size_t)*str++) { hash = hash * magic + ch; magic *= 378551; } return hash; } size_t operator()(const string&s) { return RSHash(s.c_str()); }};template<class K>struct _HashFunc5{ size_t RSHash(const char *str) { register size_t hash = 0; size_t ch; for (long i = 0; ch = (size_t)*str++; i++) { if ((i & 1) == 0) { hash ^= ((hash << 7) ^ ch ^ (hash >> 3)); } else { hash ^= (~((hash << 11) ^ ch ^ (hash >> 5))); } } return hash; } size_t operator()(const string &s) { return RSHash(s.c_str()); }};template<class K=string,class HashFunc1=_HashFunc1<K>,class HashFunc2=_HashFunc2<K>,class HashFunc3=_HashFunc3<K>,class HashFunc4=_HashFunc4<K>,class HashFunc5 =_HashFunc5<K >>class BloomFilter{public: BloomFilter(size_t size) { _capacity = _GetNextPrime(size); _bitmap.Resize(_capacity); } void Set(const K &key) { size_t index1 = HashFunc1()(key); size_t index2 = HashFunc2()(key); size_t index3 = HashFunc3()(key); size_t index4 = HashFunc4()(key); size_t index5 = HashFunc5()(key); _bitmap.Set(index1%_capacity); _bitmap.Set(index2%_capacity); _bitmap.Set(index3%_capacity); _bitmap.Set(index4%_capacity); _bitmap.Set(index5%_capacity); } bool IsIn(const K&key) { size_t index1 = HashFunc1()(key); if (!_bitmap.Test(index1%_capacity)) { return false; } size_t index2 = HashFunc2()(key); if (!_bitmap.Test(index2%_capacity)) { return false; } size_t index3 = HashFunc3()(key); if (!_bitmap.Test(index3%_capacity)) { return false; } size_t index4 = HashFunc4()(key); if (!_bitmap.Test(index4%_capacity)) { return false; } size_t index5 = HashFunc5()(key); if (!_bitmap.Test(index5%_capacity)) { return false; } return true; }protected: unsigned long _GetNextPrime(unsigned long num) { const int _PrimeSize = 28; static const unsigned long _PrimeList[_PrimeSize] = { 53ul, 97ul, 193ul, 389ul, 769ul, 1543ul, 3079ul, 6151ul, 12289ul, 24593ul, 49157ul, 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul, 3221225473ul, 4294967291ul }; size_t pos = 0; while (pos < _PrimeSize) { if (_PrimeList[pos] > num) { break; } ++pos; } return _PrimeList[pos]; }private: BitSet _bitmap; size_t _capacity;};void Test2(){ BitSet bitmap(60); for (int i = 4; i < 10; i++) { bitmap.Set(i); } for (int i = 0; i < 20; i++) { cout << i << ":" << bitmap.Test(i) << endl; }}void Test1(){ BloomFilter<>bf(30); string s11("www"); bf.Set(s11); bf.Set("张三"); bf.Set("王五"); bf.Set("李四"); cout << bf.IsIn("布隆过滤器") << endl; cout << bf.IsIn("ww") << endl; cout << bf.IsIn("www") << endl; cout << bf.IsIn("王五") << endl;}int main(){ Test1(); return 0;}
2.–布隆过滤器的扩展:如何扩展BloomFilter使得它支持删除元素的操作?请设计实现一个支持删除的布隆过滤器.
//支持Reset的bloomfliter template<typename K=string ,class HashFunc1=_HashFunc1 , class HashFunc2 = _HashFunc2 , class HashFunc3= _HashFunc3 , class HashFunc4= _HashFunc4 , class HashFunc5 = _HashFunc5> class BloomFilter { public: BloomFilter(size_t range) { _bitmap.resize(range*5); //为了减少误判,提高精度,用5个位置来表示一个数 } void Set(const K& key) //要设置为1,必须将5个位置都设置 { size_t index1 = HashFunc1()(key) % _bitmap.size(); size_t index2 = HashFunc2()(key) % _bitmap.size(); size_t index3 = HashFunc3()(key) % _bitmap.size(); size_t index4 = HashFunc4()(key) % _bitmap.size(); size_t index5 = HashFunc5()(key) % _bitmap.size(); _bitmap[index1]++; _bitmap[index2]++; _bitmap[index3]++; _bitmap[index4]++; _bitmap[index5]++; } bool ReSet(const K& key) //采用引用计数的方式复位 { size_t index1 = HashFunc1()(key) % _bitmap.size(); size_t index2 = HashFunc2()(key) % _bitmap.size(); size_t index3 = HashFunc3()(key) % _bitmap.size(); size_t index4 = HashFunc4()(key) % _bitmap.size(); size_t index5 = HashFunc5()(key) % _bitmap.size(); if (_bitmap[index1] == 0 || _bitmap[index2] == 0 || _bitmap[index3] == 0 || _bitmap[index4] == 0 || _bitmap[index5] == 0) //只要有一个为0,说明这个key不存在 return false; //要是都不为0,才减一 _bitmap[index1]--; _bitmap[index2]--; _bitmap[index3]--; _bitmap[index4]--; _bitmap[index5]--; return true; } bool Test(const K& key) { size_t index1 = HashFunc1()(key) % _bitmap.size(); size_t index2 = HashFunc2()(key) % _bitmap.size(); size_t index3 = HashFunc3()(key) % _bitmap.size(); size_t index4 = HashFunc4()(key) % _bitmap.size(); size_t index5 = HashFunc5()(key) % _bitmap.size(); //只有五个位置都为1,才存在 if (_bitmap[index1] != 0 && _bitmap[index2] != 0 && _bitmap[index3] != 0 && _bitmap[index4] != 0 && _bitmap[index5] != 0) return true; return false; } private: vector<size_t> _bitmap; };
- 简单布隆过滤器实现、布隆过滤器扩展
- 【布隆过滤器】实现一个简单的布隆过滤器
- 【布隆过滤器】实现一个简单的布隆过滤器
- 实现布隆过滤器
- 简单的布隆过滤器
- 哈希表的扩展-布隆过滤器
- Python实现布隆过滤器
- 布隆过滤器java实现
- 布隆过滤器:实现代码
- 布隆过滤器及其实现
- 实现一个布隆过滤器
- 关于布隆过滤器
- 布隆过滤器
- 重写布隆过滤器
- 布隆过滤器
- 布隆过滤器
- 布隆过滤器
- 布隆过滤器
- 集成学习(Ensemble Learning)
- ODF光纤配线架
- Camera---获得摄像机的视口区域(转载大神雨松MOMO)
- Educational Codeforces Round 26 A. Text Volume
- Java上传Excel并解析
- 简单布隆过滤器实现、布隆过滤器扩展
- [转]深度学习如何入门?
- HDU 1892 See you~(二维树状数组 单点更新)
- window下eclipse ndk配置
- Kail Linux fluxion WiFi破解
- 百度传课-php2小时超音速入门
- 在大学老师布置的一些题目,欢迎各位大佬指正(6)
- 大数据学习笔记:编写Mapreduce代码并运行
- php增删改