

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<time.h>
using namespace std;
namespace mybitset //处理整数 STL容器 bitset
{
//按位申请空间
template<size_t N>
class bitset
{
public:
bitset()
{
_vc.resize(N / 8 + 1, 0);//多开一个字节,如要开20个比特位,不加一只有2个字节
}
//该比特位设置成 1,在
void set(size_t x)
{
//在第几个char对象里
size_t i = x / 8;
//锁定在char对象里第几个比特位。
size_t j = x % 8;
_vc[i] |= (1 << j);
}
//将该数字设置成不存在
void reset(size_t x)
{
size_t i = x / 8;
size_t j = x % 8;
_vc[i] &= (~(1 << j));
}
//探测该数字在不在
bool test(size_t x)
{
size_t i = x / 8;
size_t j = x % 8;
return (_vc[i] & (1 << j)) == 0 ? false : true;
}
private:
vector<char> _vc;
};
struct HashFunc1
{
//BKDR Hash Function
size_t operator() (const string& str)
{
size_t hash = 0;
for (size_t i = 0; i < str.size(); i++)
{
hash *= 131;
hash += str[i];
}
return hash;
}
};
struct HashFunc2
{
//SDBM Hash Function
size_t operator() (const string& str)
{
size_t hash = 0;
for (size_t i = 0; i < str.size(); i++)
{
hash *= 65599;
hash += str[i];
}
return hash;
}
};
struct HashFunc3
{
//RS Hash Function
size_t operator() (const string& str)
{
size_t hash = 0;
size_t magic = 63689;
for (size_t i = 0; i < str.size(); i++)
{
hash *= magic;
hash += str[i];
magic *= 378551;
}
return hash;
}
};
//N要插入元素的个数
template<size_t N
,class Hash1 = HashFunc1
,class Hash2= HashFunc2
, class Hash3 = HashFunc3>
class Bloomfilter
{
public:
void set(const string& str)
{
size_t h1 = Hash1()(str) % len;
size_t h2 = Hash2()(str) % len;
size_t h3 = Hash3()(str) % len;
/*size_t h1 = Hash1()(str);
size_t h2 = Hash2()(str);
size_t h3 = Hash3()(str);*/
cout << h1 << " " << h2 << " " << h3 << endl;
_bf.set(h1);
_bf.set(h2);
_bf.set(h3);
}
private:
bitset<6 * N> _bf;
size_t len = 6 * N;
};
}
bool test(const string& str)
{
size_t h1 = Hash1()(str) % len;
size_t h2 = Hash2()(str) % len;
size_t h3 = Hash3()(str) % len;
if (_bf.test(h1) == false)
{
return false;
}
if (_bf.test(h2) == false)
{
return false;
}
if (_bf.test(h3) == false)
{
return false;
}
return true;
}
void TestBloomFilter()
{
/*BloomFilter<100> bf;
bf.Set("张三");
bf.Set("李四");
bf.Set("牛魔王");
bf.Set("红孩儿");
cout << bf.Test("张三") << endl;
cout << bf.Test("李四") << endl;
cout << bf.Test("牛魔王") << endl;
cout << bf.Test("红孩儿") << endl;
cout << bf.Test("孙悟空") << endl;
cout << bf.Test("二郎神") << endl;
cout << bf.Test("猪八戒") << endl;*/
BloomFilter<100> bf;
size_t N = 100;
std::vector<std::string> v1;
for (size_t i = 0; i < N; ++i)
{
std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html";
url += std::to_string(1234 + i);
v1.push_back(url);//字符串处理加入到数组里
}
for (auto& str : v1)
{
bf.Set(str);
}
for (auto& str : v1)
{
cout << bf.Test(str) << endl;
}
cout << endl << endl;
std::vector<std::string> v2;
for (size_t i = 0; i < N; ++i)
{
std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html";
url += std::to_string(6789 + i);
v2.push_back(url);
}
size_t n2 = 0;
for (auto& str : v2)
{
if (bf.Test(str))
{
++n2;
}
}
cout << "相似字符串误判率:" << (double)n2 / (double)N << endl;
std::vector<std::string> v3;
for (size_t i = 0; i < N; ++i)
{
//std::string url = "https://www.baidu.com/s?wd=ln2&rsv_spt=1&rsv_iqid=0xc1c7784f000040b1&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_dl=tb&rsv_enter=1&rsv_sug3=8&rsv_sug1=7&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=ln2&rsp=5&inputT=4576&rsv_sug4=5211";
//std::string url = "https://zhidao.baidu.com/question/1945717405689377028.html?fr=iks&word=ln2&ie=gbk&dyTabStr=MCw0LDMsMiw2LDEsNSw3LDgsOQ==";
std::string url = "https://www.cnblogs.com/-clq/archive/2012/01/31/2333247.html";
url += std::to_string(6789 + i);
v3.push_back(url);
}
size_t n3 = 0;
for (auto& str : v3)
{
if (bf.Test(str))
{
++n3;
}
}
cout << "不相似字符串误判率:" << (double)n3 / (double)N << endl;
}

思路:

void test()
{
Bloomfilter<2> bf1;
bf1.set("https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html");
}
void test1()
{
Bloomfilter<2> bf1;
//bf1.set("https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html");
cout << bf1.test("https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html") << endl;
}