您的位置:首页 > 其它

布隆过滤器的简易实现

2016-05-08 16:41 465 查看
布隆过滤器(Bloom
Filter): 是由布隆(Burton
Howard Bloom)提出的。它实际上是由一个很长的二进制向量和一系列随机映射函数组成,布隆过滤器用于检索一个元素是否在一个集合中。底层是利用哈希表来实现的,它可以通过一个Hash函数将一个元素映射成一个位阵列(Bit
Array)中的一个点。这样一来,我们只要看看这个点是不是 1 就知道可以集合中有没有它了。这就是布隆过滤器的基本思想。

优点:空间效率和查询时间相比于其他数据结构有很大的优势

缺点:有一定的误识别率,删除困难

应用:

Google 著名的分布式数据库 Bigtable 使用了布隆过滤器来查找不存在的行或列,以减少磁盘查找的IO次数。

Squid 网页代理缓存服务器在cache digests中也使用了布隆过滤器。

Venti 文档存储系统也采用布隆过滤器来检测先前存储的数据。

SPIN 模型检测器也使用布隆过滤器在大规模验证问题时跟踪可达状态空间。

Google Chrome浏览器使用了布隆过滤器加速安全浏览服务。

实现代码:

bitmap的实现见另一篇博客(位图的实现):点击打开链接

BloomFilter.h

#pragma once

#include"bitmap.h"

size_t BKDRHash(const char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}

return (hash & 0x7FFFFFFF);
}

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 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 APHash(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 JSHash(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;
}

template<class K>
struct __HashFunc1
{
size_t operator()(const K& key)
{
return BKDRHash(key.c_str());
}
};

template<class K>
struct __HashFunc2
{
size_t operator()(const K& key)
{
return SDBMHash(key.c_str());
}
};

template<class K>
struct __HashFunc3
{
size_t operator()(const K& key)
{
return RSHash(key.c_str());
}
};

template<class K>
struct __HashFunc4
{
size_t operator()(const K& key)
{
return APHash(key.c_str());
}
};

template<class K>
struct __HashFunc5
{
size_t operator()(const K& key)
{
return JSHash(key.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:
Bitmap _bitmap;
size_t _capacity;
};

void Test()
{
BloomFilter<> bf(30);
bf.Set("布隆过滤器");
bf.Set("AAAAAAAAAAA");
bf.Set("qqqqqqqqq");

cout << bf.IsIn("布隆过滤器") << endl;
cout << bf.IsIn("ww") << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: