欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

【干貨】位圖的實現(xiàn)與布隆過濾器

    位圖是用一個btye位來表示一個數(shù)據(jù)是否存在,再通過哈希函數(shù)確定一個數(shù)據(jù)所在的位置,這樣處理會使當(dāng)僅需要判斷一個數(shù)據(jù)在不在的時候大大的提高效率,縮小內(nèi)存的使用,如一個數(shù)據(jù)為int型,而一個int型的數(shù)據(jù)構(gòu)成的位圖能表示32個數(shù)據(jù)的存在狀態(tài)。代碼實現(xiàn)如下:

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的涿州網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

Bitmap.h:

#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年由布隆提出的。它實際上是一個很長的二進制向量和一系列隨機映射函數(shù)。布隆過濾器可以用于檢索一個元素是否在一個集合中。它的優(yōu)點是空間效率和查詢時間都遠遠超過一般的算法,缺點是有一定的誤識別率和刪除困難。(百度百科)

    這里所說的映射函數(shù)我們一般定義幾個,這樣就可以加大避免沖突的幾率,這里我寫了個key為string 類的布隆過濾器,僅供參考:

BloomFilter.h:

#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;
};

    如有疑問希望提出,有錯誤希望指正

網(wǎng)站標題:【干貨】位圖的實現(xiàn)與布隆過濾器
標題來源:http://chinadenli.net/article6/ppsoig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站制作動態(tài)網(wǎng)站、云服務(wù)器、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
日韩美女偷拍视频久久| 国产亚洲精品岁国产微拍精品| 99亚洲综合精品成人网色播| 午夜视频免费观看成人| 欧美野外在线刺激在线观看 | 欧美精品在线观看国产| 99久久精品午夜一区二区| 日韩不卡一区二区视频| 日韩中文字幕免费在线视频| 黄片免费观看一区二区| 天海翼高清二区三区在线| av一区二区三区天堂| 亚洲视频在线观看免费中文字幕| 激情五月天免费在线观看| 欧美午夜一区二区福利视频| 日韩精品一区二区一牛| 在线日韩欧美国产自拍| 激情偷拍一区二区三区视频| 中文字幕一区二区久久综合| 婷婷亚洲综合五月天麻豆| 国产成人在线一区二区三区| 亚洲深夜精品福利一区| 亚洲男人的天堂色偷偷| 亚洲av在线视频一区| 狠狠亚洲丁香综合久久| 亚洲最新中文字幕一区 | 亚洲性生活一区二区三区| 亚洲香艳网久久五月婷婷| 欧美日韩亚洲国产av| 少妇人妻中出中文字幕| 色哟哟精品一区二区三区| 久久三级国外久久久三级| 亚洲欧美国产中文色妇| 夫妻激情视频一区二区三区| 好吊妞视频只有这里有精品| 最新国产欧美精品91| 国产亚州欧美一区二区| 欧美一级特黄大片做受大屁股| 偷拍偷窥女厕一区二区视频| 午夜福利视频日本一区| 国产女同精品一区二区|