1 Star 0 Fork 0

石海涛/练习代码

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
BloomFilter.h 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2023-07-30 19:03 +08:00 . 仓库回退
#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;
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/FriendOfJustic/test.git
git@gitee.com:FriendOfJustic/test.git
FriendOfJustic
test
练习代码
master

搜索帮助