设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 手机 数据 公司
当前位置: 首页 > 服务器 > 安全 > 正文

【数据结构】位图BitMap与布隆过滤器BloomFilter(2)

发布时间:2021-03-30 21:46 所属栏目:53 来源:网络整理
导读:????二、布隆过滤器 #ifndef?__BLOOMFILTER_H__#define?__BLOOMFILTER_H__#include?iostreamusing?namespace?std;#includestring#include"BitMap.h"#include?"Common.h"templateclass?K?=?string,class?HashFunc1?=

????二、布隆过滤器

#ifndef?__BLOOMFILTER_H__
#define?__BLOOMFILTER_H__
#include?<iostream>
using?namespace?std;
#include<string>
#include"BitMap.h"
#include?"Common.h"


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?=?0)
????{
????????_capacity?=?GetPrimeSize(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);//设置为第多少位的数,然后调用位图的Set设置成第几个字节的第几位
????????_bitMap.Set(index2%_capacity);
????????_bitMap.Set(index3%_capacity);
????????_bitMap.Set(index4%_capacity);
????????_bitMap.Set(index5%_capacity);
????}

????bool?Test(const?K&?key)
????{
????????size_t?index1?=?HashFunc1()(key);
????????if?(!(_bitMap.Test(index1%_capacity)))//为1不一定存在,为0肯定不存在
????????????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?=?HashFunc4()(key);
????????if?(!(_bitMap.Test(index5%_capacity)))
????????????return?false;

????????return?true;
????}

protected:
????BitMap?_bitMap;
????size_t?_capacity;
};


void?TestBloomFilter()
{
????BloomFilter<>?bf(50);
????bf.Set("臧");
????bf.Set("静");
????bf.Set("比特");
????bf.Set("peter");
????bf.Set("徐");
????bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html");
????bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528155.html");

????cout?<<?"Exist?->:"?<<?bf.Test("臧")?<<?endl;
????cout?<<?"Exist?->:"?<<?bf.Test("静")?<<?endl;
????cout?<<?"Exist?->:"?<<?bf.Test("peter")?<<?endl;
????cout?<<?"Exist?->:"?<<?bf.Test("徐航")?<<?endl;
????cout?<<?"Exist?->:"?<<?bf.Test("http://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html")?<<?endl;
????cout?<<?"Exist?->:"?<<?bf.Test("http://www.cnblogs.com/-clq/archive/2012/05/31/25288153.html")?<<?endl;

}

#endif?//__BLOOMFILTER_H__

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读