【乾貨】位圖的實現與布隆過濾器

    位圖是用一個btye位來表示一個數據是否存在,再經過哈希函數肯定一個數據所在的位置,這樣處理會使當僅須要判斷一個數據在不在的時候大大的提升效率,縮小內存的使用,如一個數據爲int型,而一個int型的數據構成的位圖能表示32個數據的存在狀態。代碼實現以下:
算法

Bitmap.h:ide

#include<vector>
class BitMap
{
public:
	BitMap(size_t size)
		:_size(0)
	{
		Size(size);
	}
	void Set(size_t key)
	{
		size_t index = key / 32;
		size_t offset = key % 32;
		_map[index]=_map[index] | (1 << offset);
		++_size;
	}
	void Reset(size_t key)
	{
		size_t index = key / 32;
		size_t offset = key % 32;
		if ((_map[index] >> offset) & 1)
		{
			_map[index] = _map[index] & (~(1 << offset));
			++_size;
		}
	}
	void Size(size_t size)
	{
		_map.resize(size);
	}
	bool Touch(size_t key)
	{
		size_t index = key / 32;
		size_t offset = key % 32;
		if ((_map[index] >> offset) & 1)
			return true;
		return false;
	}
protected:
	size_t _size;
	vector<size_t> _map;
};

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

    這裏所說的映射函數咱們通常定義幾個,這樣就能夠加大避免衝突的概率,這裏我寫了個key爲string 類的布隆過濾器,僅供參考:
spa

BloomFilter.h:orm

#include"BitMap.h"
size_t BKDRHash(const char *str)//這裏定義了5個映射算法,僅供參考
{
	register size_t hash = 0;
	while (size_t ch = (size_t)*str++)
	{
		hash = hash * 131 + ch;           
	}
	return hash;
}
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 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 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 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;
}
class BloomFilter
{
public:
	BloomFilter(size_t size)
		:_capacity(size)
		, map(size)
	{}
	void Set(const string &key)
	{
		size_t index1 = BKDRHash(key.c_str())%_capacity;
		size_t index2 = SDBMHash(key.c_str()) % _capacity;
		size_t index3 = RSHash(key.c_str()) % _capacity;
		size_t index4 = APHash(key.c_str()) % _capacity;
		size_t index5 = JSHash(key.c_str()) % _capacity;
		map.Set(index1);
		map.Set(index2);
		map.Set(index3);
		map.Set(index4);
		map.Set(index5);
	}
	bool Touch(const string &key)
	{
		if (!map.Touch(BKDRHash(key.c_str()) % _capacity))
			return false;
		if (!map.Touch(SDBMHash(key.c_str()) % _capacity))
			return false;
		if (!map.Touch(RSHash(key.c_str()) % _capacity))
			return false;
		if (!map.Touch(APHash(key.c_str()) % _capacity))
			return false;
		if (!map.Touch(JSHash(key.c_str()) % _capacity))
			return false;
		return true;
	}
protected:
	size_t _capacity;
	BitMap map;
};

    若有疑問但願提出,有錯誤但願指正
htm

相關文章
相關標籤/搜索