手写一个布隆过滤器(Bloom Filter)

布隆过滤器

话不多说,这里直接贴出布隆过滤器的代码。我觉得这个博主写的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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

强大的RGG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值