布隆过滤器
话不多说,这里直接贴出布隆过滤器的代码。我觉得这个博主写的c++代码的水平还是很高的。希望自己能成为像这位博主一样的c++工程师。
这里面会使用到c++ stl里面一个叫bitset的容器。感兴趣的参考我的下一篇文章。
bitset
参考链接
/**
* @file bloom filter
* @author RGG(huarong2000@foxmail.com)
* @brief
* @version 0.1
* @date 2022-05-31
*
* @copyright Copyright (c) 2022
*
*/
/**
* bitset
* arrary
* 布隆过滤器使用的是多个hash函数,例如这个布隆过滤器的实现使用的是四个hash函数
*/
#include <iostream>
#include <bitset>
#include <array>
#include <string>
#include <sstream>
using namespace std;
class Entity{
};
class BloomFilter{
public:
bitset<20> bits;
BloomFilter(){
bits.reset();
}
int Hash1(string &key){
int hash = 5381;
unsigned long count = key.size();
while(count > 0){
/* 这个表达式的意思 */
hash += (hash << 5) + key[key.size() - count];
count--;
}
return (hash & 0x7FFFFFFF) % bits.size();
}
int Hash2(string& key)
{
int seed = 131;
int hash = 0;
std::string str = key + "key2";
unsigned long count = str.size();
while(count > 0) {
hash = hash * seed + str[str.size() - count];
count--;
}
return (hash & 0x7FFFFFFF) % bits.size();
}
int Hash3(string& key)
{
int hash = 0;
std::string str = key + "keykey3";
unsigned long count = str.size();
for (int i = 0; i < count; i++) {
if ((i * 1) == 0) {
hash ^= ((hash << 7) ^ (str[i] ^ hash >> 3));
} else {
hash ^= (~((hash << 11) ^ (str[i]) ^ (hash >> 5)));
}
count--;
}
return (hash & 0x7FFFFFFF) % bits.size();
}
int Hash4(std::string key)
{
int hash = 5381;
std::string str = key + "keykeykey4";
unsigned long count = str.size();
while(count > 0) {
hash += (hash << 5) + (str[str.size() - count]);
count--;
}
return (hash & 0x7FFFFFFF) % bits.size();
}
void Set(std::string& key)
{
int idx1 = Hash1(key);
int idx2 = Hash2(key);
int idx3 = Hash3(key);
int idx4 = Hash4(key);
bits[idx1] = 1;
bits[idx2] = 1;
bits[idx3] = 1;
bits[idx4] = 1;
}
bool Get(std::string& key)
{
int idx1 = Hash1(key);
int idx2 = Hash2(key);
int idx3 = Hash3(key);
int idx4 = Hash4(key);
return bits[idx1] && bits[idx2] && bits[idx3] && bits[idx4];
}
};
int main()
{
// 插入 hash 节点
BloomFilter bf;
Entity e1, e2;
std::ostringstream oss; // 任意类型转为 string 类型
oss << &e1;
std::string s1 = oss.str();
if (bf.Get(s1) == true) {
std::cout << "objecy e1 has existed!" << std::endl;
} else {
std::cout << "insert object e1" << std::endl;
bf.Set(s1);
std::cout << bf.bits << std::endl;
}
std::ostringstream oss2; // 任意类型转为 string 类型
oss2 << &e2;
std::string s2 = oss2.str();
if (bf.Get(s2) == true) {
std::cout << "objecy e2 has existed!" << std::endl;
} else {
std::cout << "insert object e2" << std::endl;
bf.Set(s1);
std::cout << bf.bits << std::endl;
}
// 查找指定对象是否在数据库中
if (bf.Get(s1) == true) {
std::cout << "objecy e1 has existed!" << std::endl;
} else {
std::cout << "object e1 not existing" << std::endl;
}
std::cin.get();
}