Bloom Filter算法实现

#include<iostream>
#include<fstream>
#include<boost/filesystem.hpp>
#include<boost/filesystem/path.hpp>
#include<list>
#include<vector>
#include "hashFun.h.h"
//#include<boost/uuid/uuid_generators.hpp>
//#include<boost/uuid/uuid.hpp>
//#include<boost/uuid/uuid_io.hpp>


/*
* bloom.h
*
*  Created on: 2012-2-22
*      Author: xiaojay
*/


#ifndef BLOOM_H_
#define BLOOM_H_




class Bloom
{
public:
Bloom(int size, std::vector<HashFun*> hashfunclist);
~Bloom();
void add(const char * text);
bool check(const char * text);


private:
const static int CHARBITSIZE = 8;
int size;
char * arr;
std::vector<HashFun*> hashfunclist;
inline void setbit(long pos);
inline bool getbit(long pos);
};
#endif 






Bloom::Bloom(int size, std::vector<HashFun*> hashfunclist)
{
assert(hashfunclist.size()>0);
this->size = size;
this->hashfunclist = hashfunclist;
this->arr = new char[size];
}


Bloom::~Bloom()
{
if (this->arr != NULL)
{
delete this->arr;
}
}


void Bloom::add(const char * text)
{
int nfunc = hashfunclist.size();
long code = 0;
for (int i = 0; i<nfunc; i++)
{
code = hashfunclist.at(i)->gethashval(text);


if (code / CHARBITSIZE>size) return;
else
{
setbit(code);
}
}
}


bool Bloom::check(const char * text)
{
int nfunc = hashfunclist.size();
long code = 0;
for (int i = 0; i<nfunc; i++)
{
code = hashfunclist.at(i)->gethashval(text);
if (code / CHARBITSIZE>size) 
return false;
else
{
if (getbit(code))
continue;
else 
return false;
}
}
return true;
}


inline void Bloom::setbit(long code)
{
arr[code / CHARBITSIZE] |= (1 << (code%CHARBITSIZE));
}


inline bool Bloom::getbit(long code)
{
if (!(arr[code / CHARBITSIZE] & (1 << (code%CHARBITSIZE))))
{
return false;
}
return true;
}
class HashFunA : public HashFun
{
public:
virtual long gethashval(const char * key)
{
unsigned int h = 0;
while (*key) h ^= (h << 5) + (h >> 2) + (unsigned char)*key++;
return h % 80000;
}
};
class HashFunB : public HashFun
{
public:
virtual long gethashval(const char * key)
{
unsigned int h = 0;
while (*key) h = (unsigned char)*key++ + (h << 6) + (h << 16) - h;
return h % 80000;
}
};
using namespace std;
int main()
{

HashFunA *funa = new HashFunA();
HashFunB *funb = new HashFunB();
vector<HashFun*> hashfunclist;
hashfunclist.push_back(funa);
hashfunclist.push_back(funb);


/*
* Create Bloom object with two parameters :
* size of the store array and list of hash functions
*/
Bloom bloom(10000, hashfunclist);


///Add some words to bloom filter


bloom.add("hello");
bloom.add("world");
bloom.add("ipad");
bloom.add("iphone4");
bloom.add("ipod");
bloom.add("apple");
bloom.add("banana");
bloom.add("hello");


/*
* Test
*/
char word[20];
while (true)
{
cout << "Please input a word : " << endl;
cin >> word;
if (bloom.check(word))
{
cout << "Word :" << word << " has been set in bloom filter." << endl;
}
else
{
cout << "Word :" << word << " not exist !" << endl;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值