布隆過濾器(Bloom Filter)

  布隆過濾器(Bloom Filter)是1970年由布隆提出的。它實際上是一個很長的二進制向量和一系列隨機映射函數。布隆過濾器可以用於檢索一個元素是否在一個集合中。它的優點是空間效率和查詢時間都遠遠超過一般的算法,缺點是有一定的誤識別率和刪除困難。

       如果想要判斷一個元素是不是在一個集合裏,一般想到的是將所有元素保存起來,然後通過比較確定。鏈表,樹等等數據結構都是這種思路. 但是隨着集合中元素的增加,我們需要的存儲空間越來越大,檢索速度也越來越慢(O(n),O(logn))。不過世界上還有一種叫作散列表(又叫哈希表,Hash table)的數據結構。它可以通過一個Hash函數將一個元素映射成一個位陣列(Bit array)中的一個點。這樣一來,我們只要看看這個點是不是1就可以知道集合中有沒有它了。這就是布隆過濾器的基本思想【詳見百度百科】

     總的來說:布隆過濾器就是利用哈希算法+位圖實現的。

    所以布隆過濾器的底層我們就用一個位圖封裝。

     對哈希算法不熟悉的可以看博主博客http://helloleex.blog.51cto.com/10728491/1770568

     如果對位圖不太熟悉的可以查看博主的博客http://helloleex.blog.51cto.com/10728491/1772827

#pragma once
#include<string>
#include"Bit_Map.h"
#include"HashFunc.h"
using namespace std;

//5個哈希函數的仿函數
template<class K = string,
class HashFunc1 = _HashFunc1<K>,
class HashFunc2 = _HashFunc2<K>,
class HashFunc3 = _HashFunc3<K>, 
class HashFunc4 = _HashFunc4<K>, 
class HashFunc5 = _HashFunc5<K>>
class BloomFilter
{
public:
	BloomFilter(size_t size)
	{
		//size_t newsize = _GetNextPrime(size);
		//_capacity = newsize;
		//size不一定是素數,在素數表中取一個素數開一個素數大的空間
		_capacity = _bm.Resize(size);
	}
	void Set(const K& key)
	{
		size_t index1 = HashFunc1()(key);
		size_t index2 = HashFunc2()(key);
		size_t index3 = HashFunc3()(key);
		size_t index4 = HashFunc4()(key);
		size_t index5 = HashFunc5()(key);
		_bm.Set(index1);
		_bm.Set(index2);
		_bm.Set(index3);
		_bm.Set(index4);
		_bm.Set(index5);
	}

	//布隆過濾器不能執行刪除操作,因爲有不同的哈希算法,可能不同的key
	//標記在相同的位上,刪除會把別人的記錄給刪除了,影響正確性。
	//void Reset(const K& key);

	//測試存在不一定存在,不存在一定不存在
	bool Test(const K& key)
	{
		size_t index1 = HashFunc1()(key);
		size_t index2 = HashFunc2()(key);
		size_t index3 = HashFunc3()(key);
		size_t index4 = HashFunc4()(key);
		size_t index5 = HashFunc5()(key);
		if (_bm.Test(index1) || _bm.Test(index2) ||
			_bm.Test(index3) || _bm.Test(index4) ||
			_bm.Test(index5))
			return true;
		else
			return false;
	}
protected:
	BitMap _bm;
	size_t _capacity;
};
HashFunc.h
//5個高效的哈希算法,都是大神發明的。
#pragma once
template<class T>
//BKDR Hash Function是一種簡單快捷的hash算法,
//也是Java目前採用的字符串的Hash算法(累乘因子爲31)
struct _HashFunc1
{
	size_t operator()(const T& str)
	{
		register size_t hash = 0;
		const char* tmp = str.c_str();
		while (size_t ch = (size_t)*tmp++)
		{
			hash = hash * 131 + ch;
		}
		return hash;
	}
};
template<class T>
//RS Hash Function。因Robert Sedgwicks在其《Algorithms in C》一書中展示而得名。
struct _HashFunc2
{
	size_t operator()(const T& str)
	{
		register size_t hash = 0;
		size_t magic = 63689;
		const char* tmp = str.c_str();
		while (size_t ch = (size_t)*tmp++)
		{
			hash = hash * magic + ch;
			magic *= 378551;
		}
		return hash;
	}
}; template<class T>
//AP Hash Function 由Arash Partow發明的一種hash算法。
struct _HashFunc3
{
	size_t operator()(const T& str)
	{
		register size_t hash = 0;
		size_t ch;
		const char* tmp = str.c_str();
		for (long i = 0; ch = (size_t)*tmp++; i++)
		{
			if ((i & 1) == 0)
			{
				hash ^= ((hash << 7) ^ ch ^ (hash >> 3));
			}
			else
			{
				hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));
			}
		}
		return hash;
	}
}; 

template<class T>
// DJB Hash Function 2。由Daniel J. Bernstein 發明的另一種hash算法。
struct _HashFunc4
{
	size_t operator()(const T& str)
	{
		const char* tmp = str.c_str();
		if (!*tmp)   // 這是由本人添加,以保證空字符串返回哈希值0  
			return 0;
		register size_t hash = 5381;
		while (size_t ch = (size_t)*tmp++)
		{
			hash = hash * 33 ^ ch;
		}
		return hash;
	}
}; 

template<class T>
//JS Hash Function 。由Justin Sobel發明的一種hash算法。
struct _HashFunc5
{
	size_t operator()(const T& str)
	{
		const char* tmp = str.c_str();
		if (!*tmp)        // 這是由本人添加,以保證空字符串返回哈希值0  
			return 0;
		register size_t hash = 1315423911;
		while (size_t ch = (size_t)*tmp++)
		{
			hash ^= ((hash << 5) + ch + (hash >> 2));
		}
		return hash;
	}
};

     

    布隆過濾器是有誤識別率的,雖然機率很低很低,但是如果5個哈希函數其中有一兩個誤識別了,就會出現錯誤。

     而且布隆過濾器是不支持刪除Reset的。因爲有很機率會刪除其他哈希函數所標識的記錄,誤識別機率大大增高。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章