布隆过滤器与位图

代码

第一个是位图,第二个是布隆过滤器,第三个 Twobitmap,是能表示4个状态的位图,因为普通位图一个位只能表示一个状态。

ifndef _BITMAP_H
#define _BITMAP_H
#include<vector>
#include<iostream>
#include<assert.h>
using namespace std;
class bitmap
{
public:
    bitmap()
        :_bitmap(((unsigned)-1 >> 3) + 1)
    {}
    void Set(size_t x)
    {
        size_t index = x/8;
        size_t pos = x%8;
        try
        {
            _bitmap.at(index)|=(1<<pos);
        }
        catch (const std::out_of_range& err)
                 std::cerr<<"Out of Range error:"<<err.what()<<endl;
        }
    }
    void UnSet(size_t x)
    {
        size_t index = x/8;
        size_t pos = x%8;
        try
        {
            _bitmap.at(index)&=(~(1<<pos));
        }
        catch (const std::out_of_range& err)
        {
            std::cerr<<"Out of Range error:"<<err.what()<<endl;
        }
    }
    bool Test(size_t x)
    {
        size_t index = x/8;
        size_t pos = x%8;
        try
                try
        {
            return _bitmap.at(index)&(1<<pos);
        }
        catch (const std::out_of_range& err)
        {
            std::cerr<<"Out of Range error:"<<err.what()<<endl;
        }
        assert(false);
    }

private:
    std::vector<unsigned char> _bitmap;
};
class TwoBitMap
{
public:
    TwoBitMap()
            :_bits(((unsigned)-1 >> 3)*2 + 1)
    {

    }
    int GetCount(size_t x)
    {
        int index = x / 4;
        int bitpos = x % 4;
        return (_bits[index] & (3 << bitpos * 2)) >> (2 * bitpos);
    }
    void Add(size_t x)
    {
        int nums = GetCount(x);
        if (x < 2)
        {
            int index = x / 4;
            int bitpos = x % 4;
            _bits[index] += (1 << 2 * bitpos);
                     }
    }
private:
    vector<unsigned char>_bits;
};
template<class K,class HashFunc,class HashFunc2,class HashFunc3,class HashFunc4,class HashFunc5>
class BloomFile
{
  public:
      BloomFile()
          :_bitmap()
      {}
      void Set(const K & key )
      {
            size_t index = HashFunc()(key);
            _bitmap.Set(index);
            index = HashFunc2()(key);
            _bitmap.Set(index);
            index = HashFunc3()(key);
            _bitmap.Set(index);
            index = HashFunc4()(key);
            _bitmap.Set(index);
            index = HashFunc5()(key);
            _bitmap.Set(index);
      }
      bool Test(const K&key)
      {
          size_t index = 0;
          index = HashFunc()(key);
          if(!_bitmap.Test(index))
              return false;
          index = HashFunc2()(key);
          if(!_bitmap.Test(index))
              return false;
          index = HashFunc3()(key);
          if(!_bitmap.Test(index))
              return false;
          index = HashFunc4()(key);
          if(!_bitmap.Test(index))
              return false;
          index = HashFunc5()(key);
          if(!_bitmap.Test(index))
              return false;
          return true;
      }
  protected:
    bitmap _bitmap;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值