代码拉取完成,页面将自动刷新
#pragma once
#include"bitset.h"
#include<string>
namespace sht
{
struct BKDRHash
{
size_t operator()(const std::string& key)
{
size_t hash = 0;
for (auto ch : key)
{
hash *= 131;
hash += ch;
}
return hash;
}
};
struct APHash
{
size_t operator()(const std::string& key)
{
unsigned int hash = 0;
int i = 0;
for (auto ch : key)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ (ch) ^ (hash >> 3));
}
else
{
hash ^= (~((hash << 11) ^ (ch) ^ (hash >> 5)));
}
++i;
}
return hash;
}
};
struct DJBHash
{
size_t operator()(const std::string& key)
{
unsigned int hash = 5381;
for (auto ch : key)
{
hash += (hash << 5) + ch;
}
return hash;
}
};
struct JSHash
{
size_t operator()(const std::string& s)
{
size_t hash = 1315423911;
for (auto ch : s)
{
hash ^= ((hash << 5) + ch + (hash >> 2));
}
return hash;
}
};
template<class k,size_t N=100,class Hash1=BKDRHash,class Hash2=DJBHash,class Hash3=JSHash,class Hash4=APHash>
class BloomFilter
{
public:
void set(const k& x)
{
size_t H1 = Hash1()(x)%sz;
size_t H2 = Hash2()(x) % sz;
size_t H3 = Hash3()(x) % sz;
size_t H4 = Hash4()(x) % sz;
bitset.set(H1);
bitset.set(H2);
bitset.set(H3);
bitset.set(H4);
return;
}
bool test(const k& x)
{
size_t H1 = Hash1()(x) % sz;
size_t H2 = Hash2()(x) % sz;
size_t H3 = Hash3()(x) % sz;
size_t H4 = Hash4()(x) % sz;
if (bitset.test(H1) == false)
return false;
if (bitset.test(H2) == false)
return false;
if (bitset.test(H3) == false)
return false;
if (bitset.test(H4) == false)
return false;
return true;
}
private:
sht::Bitset< N*6> bitset;
int sz = N * 6;
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。