부 릉 필터

“test.cpp”
<strong><span style="font-size:18px;">#include <iostream>
using namespace std;
#include "BloomFilter.h"
#include <string>

void test()
{
	char* str1 = "1ile:///C:/Users/xjh/AppData/Local/Temp/360zip$Temp/360$0/%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%93%88%E5%B8%8C%E7%AE%97%E6%B3%95/%E5%90%84%E7%A7%8D%E5%AD%97%E7%AC%A6%E4%B8%B2Hash%E5%87%BD%E6%95%B0%20-%20clq%20-%20%E5%8D%9A%E5%AE%A2%E5%9B%AD.html";
	char* str2 = "2ile:///C:/Users/xjh/AppData/Local/Temp/360zip$Temp/360$0/%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%93%88%E5%B8%8C%E7%AE%97%E6%B3%95/%E5%90%84%E7%A7%8D%E5%AD%97%E7%AC%A6%E4%B8%B2Hash%E5%87%BD%E6%95%B0%20-%20clq%20-%20%E5%8D%9A%E5%AE%A2%E5%9B%AD.html";
	char* str3 = "3ile:///C:/Users/xjh/AppData/Local/Temp/360zip$Temp/360$0/%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%93%88%E5%B8%8C%E7%AE%97%E6%B3%95/%E5%90%84%E7%A7%8D%E5%AD%97%E7%AC%A6%E4%B8%B2Hash%E5%87%BD%E6%95%B0%20-%20clq%20-%20%E5%8D%9A%E5%AE%A2%E5%9B%AD.html";
	char* str4 = "4ile:///C:/Users/xjh/AppData/Local/Temp/360zip$Temp/360$0/%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%93%88%E5%B8%8C%E7%AE%97%E6%B3%95/%E5%90%84%E7%A7%8D%E5%AD%97%E7%AC%A6%E4%B8%B2Hash%E5%87%BD%E6%95%B0%20-%20clq%20-%20%E5%8D%9A%E5%AE%A2%E5%9B%AD.html";
	
	BloomFiter<> bf(5);
	bf.SetBloom(str1);
	bf.SetBloom(str2);
	bf.SetBloom(str3);
	cout<<bf.TestBloom(str1)<<endl;
	cout<<bf.TestBloom(str2)<<endl;
	cout<<bf.TestBloom(str3)<<endl;
	cout<<bf.TestBloom(str4)<<endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}</span></strong>

“BitMap.h”
<strong><span style="font-size:18px;">#pragma once
#include<vector>

class BitMap
{
public:
	//range   
	BitMap(size_t range)
	{
		//size_t     ,        
		//  5     32
		_bitmap.resize((range>>5) + 1);
	}

	void Set(size_t x)//0->1
	{
		//         
		size_t index = x/32;//   x>>5

		//         
		size_t num = x%32;

		_bitmap[index] |= (1<<num);
	}

	void Reset(size_t x)//1->0
	{
		size_t index = x/32;
		size_t num = x%32;

		_bitmap[index] &= (~(1<< num));
	}

	bool Test(size_t x)//x    
	{
		size_t index = x/32;
		size_t num = x%32;

		int ret = _bitmap[index] & (1<<num);

		if (ret)
		{
			return true;
		}
		else
			return false;
	}
private:
	vector<size_t> _bitmap;	
};</span></strong>

“BloomFlter.h”
<strong><span style="font-size:18px;">#pragma  once
#include "BitMap.h"


struct _HashFunc1
{
	size_t BKDRHash(const char* str)  
	{  
		register size_t hash = 0;  
		while (size_t ch = (size_t)*str++)  
		{         
			hash = hash * 131 + ch;   //      31、131、1313、13131、131313..         
		}  
		return hash;  
	}  
	size_t operator()(const string& str)
	{
		return BKDRHash(str.c_str());
	}
};

struct _HashFunc2
{
	size_t SDBMHash(const char *str)  
	{  
		register size_t hash = 0;  
		while (size_t ch = (size_t)*str++)  
		{  
			hash = 65599 * hash + ch;         
			//hash = (size_t)ch + (hash << 6) + (hash << 16) - hash;  
		}  
		return hash;  
	}  
	size_t operator()(const string& str)
	{
		return SDBMHash(str.c_str());
	}
};

struct _HashFunc3
{
	size_t RSHash(const char *str)  
	{  
		register size_t hash = 0;  
		size_t magic = 63689;     
		while (size_t ch = (size_t)*str++)  
		{  
			hash = hash * magic + ch;  
			magic *= 378551;  
		}  
		return hash;  
	}  
	size_t operator()(const string& str)
	{
		return RSHash(str.c_str());
	}
};
 
struct _HashFunc4
{
	size_t APHash(const char *str)  
	{  
		register size_t hash = 0;  
		size_t ch;  
		for (long i = 0; ch = (size_t)*str++; i++)  
		{  
			if ((i & 1) == 0)  
			{  
				hash ^= ((hash << 7) ^ ch ^ (hash >> 3));  
			}  
			else  
			{  
				hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));  
			}  
		}  
		return hash;  
	}  
	size_t operator()(const string& str)
	{
		return APHash(str.c_str());
	}
};


struct _HashFunc5
{
	size_t JSHash(const char *str)  
	{  
		if(!*str)        //        ,            0  
			return 0;  
		register size_t hash = 1315423911;  
		while (size_t ch = (size_t)*str++)  
		{  
			hash ^= ((hash << 5) + ch + (hash >> 2));  
		}  
		return hash;  
	}  
	size_t operator()(const string& str)
	{
		return JSHash(str.c_str());
	}
};

template<class K = string,
class HashFunc1 = _HashFunc1,
class HashFunc2 = _HashFunc2,
class HashFunc3 = _HashFunc3,
class HashFunc4 = _HashFunc4,
class HashFunc5 = _HashFunc5
>
class BloomFiter
{
public:
	BloomFiter(int num)
		:_map(num * 5)
		,_range(num * 5)
	{}
	void SetBloom(const K& key)
	{
		//      ,         1

		size_t Hash1 = HashFunc1()(key) % _range;
		_map.Set(Hash1);

		size_t Hash2 = HashFunc2()(key) % _range;
		_map.Set(Hash2);

		size_t Hash3 = HashFunc3()(key) % _range;
		_map.Set(Hash3);

		size_t Hash4 = HashFunc4()(key) % _range;
		_map.Set(Hash4);

		size_t Hash5 = HashFunc5()(key) % _range;
		_map.Set(Hash5);

		cout<<Hash1<<" ";
		cout<<Hash2<<" ";
		cout<<Hash3<<" ";
		cout<<Hash4<<" ";
		cout<<Hash5<<endl;;
	}
	bool TestBloom(const K& key)
	{
		HashFunc1 hf1;
		if (_map.Test(hf1(key) % _range) == false)
		{
			return false;
		}
		HashFunc2 hf2;
		if (_map.Test(hf2(key) % _range) == false)
		{
			return false;
		}
		HashFunc3 hf3;
		if (_map.Test(hf3(key) % _range) == false)
		{
			return false;
		}
		HashFunc4 hf4;
		if (_map.Test(hf4(key) % _range) == false)
		{
			return false;
		}
		HashFunc5 hf5;
		if (_map.Test(hf5(key) % _range) == false)
		{
			return false;
		}

		return true;
	}
private:
	BitMap _map;
	size_t _range;
};</span></strong>

좋은 웹페이지 즐겨찾기