位图(BitMap)&& 布隆过滤器(BloomFilter)

本文介绍了一种高效的数据查找方法,利用位图和布隆过滤器大幅降低内存占用,实现快速判断一个数是否存在于大量未排序的整数集合中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【面试题】给40亿个不重复无符号整数,没排过序。给一个无符号整数,如何快速判断一个数是否在这40亿个数中。

●  在看到这个题后最先想到的方法是遍历这40亿个数,依次进行判断,但此做法需要的内存很大,大约为15G(4000000000 * 4 ÷(1024*1024*1024)),可见此算法不可取。

 如果内存够的话,我们可以通过位图实现,位图一个数组每个数据的每个二进制位表示一个数据,每一位用0,1表示当前这个位置上是否存有值,同样是利用哈希存储的方法。此做法可以大大减少内存,对于此题是一个int类型就可以编程32个位,需要的内存空间从15G降到500M

具体实现如下:

#pragma
class BitMap//位图
{
public:
    BitMap()
    :_size(0)
    {}
    BitMap(size_t size)//size表示多少位,不是数据个数
    : _size(size)
    {//调整大小为size / 32 + 1即右移5位加1(加1:需要的大小要包含size,例如10%8=1,大小应为2)
    	_a.resize((size >> 5) + 1);
    }
    //位图中,注意1在移位时为左移num不是左移32-num;
    void Set(size_t x)//存入x位,置1
    {
    	size_t index = x >> 5;
    	size_t num = x % 32;//eg:x = 35,num = 3,则在位图中为_a[1]中设为001
    	++_size;
    	_a[index] |= 1 << num;//1左移3位,进行|使_a中对应处为
    }
    void Remove(size_t x)//删除x位,置0
    {
    	size_t index = x >> 5;
    	size_t num = x % 32;//eg:x = 35,num = 3,则在位图中为_a[1]中设为110
    	--_size;
    	_a[index] &= (~(1 << num));//1右移3位取反0,进行&使_a中对应处为0
    }
    bool Test(size_t x)//判断是否存在
    {
    	size_t index = x >> 5;
    	size_t num = x % 32;
    	if (_a[index] & (1 << num))//如果当前位为1,则存在
    	{
    		return true;
    	}
    	return false;
    }
    void Resize(size_t size)//重置大小
    {
    	_a.resize((size >> 5) + 1);
    }
    size_t Size()//返回位图的总位数
    {
    	return _size;
    }
    size_t Capacity()//返回int数据个数
    {
    	return _a.size();
    }
    void Print()
    {
    	for (size_t i = 0; i < _a.size(); i++)
    	{
    		cout << _a[i] << " " << endl;
    	}
    	cout << endl;
    }
private:
	vector<size_t> _a;
	size_t _size;
};

void TestBitMap()
{
	BitMap bm(65);
	bm.Set(3);
	bm.Set(4);
	bm.Set(5);
	bm.Print();
	cout << "is 4 EXTST? " << bm.Test(4) << endl;
	cout << "is 8 EXTST? " << bm.Test(8) << endl;

	bm.Remove(4);
	cout << "is 4 EXTST? " << bm.Test(4) << endl;
	bm.Print();

	cout << "size: " << bm.Size() << endl;
	cout << "capacity: " << bm.Capacity() << endl;
	bm.Resize(100);
	cout << "capacity: " << bm.Capacity() << endl;
}

●  Bloom Filter 是一种空间效率很高的随机数据结构,Bloom filter 可以看做是对 bit-map 的扩展, 它的原理是:

      当一个元素被加入集合时,通过 K 个 Hash函数将这个元素映射成一个位阵列(Bit array)中的 K 个点,把它们置为 1。检索时,只要看看这些点是不是都是 1 就知道集合中有没有它了。

1、如果这些点有任何一个 0,则被检索元素一定不在;

2、如果都是 1,则被检索元素可能在。

用了素数表和6个哈希算法:

#pragma

size_t _GetNextPrime(size_t size)//素数表:获取下一个素数
{
	const int _PrimeSize = 28;
	static const unsigned long _PrimeList[_PrimeSize] =
	{
		53ul, 97ul, 193ul, 389ul, 769ul,
		1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
		49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
		1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
		50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
		1610612741ul, 3221225473ul, 4294967291ul
	};
	for (size_t i = 0; i < _PrimeSize; ++i)
	{
		if (_PrimeList[i] > size)
		{
			return _PrimeList[i];
		}
		return _PrimeList[i - 1];
	}
	return _PrimeList[_PrimeSize];//如果size大于或等于素数表中数据,就返回表中最大数
}
//6种字符串哈希算法
template<class T>
size_t BKDRHash(const T * str)
{
	register size_t hash = 0;
	while (size_t ch = (size_t)*str++)
	{
		hash = hash * 131 + ch;   // 也可以乘以31、131、1313、13131、131313..         
	}
	return hash;
}
template<class T>
size_t SDBMHash(const T *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;
}
template<class T>
size_t RSHash(const T *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;
}
template<class T>
size_t APHash(const T *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;
}
template<class T>
size_t JSHash(const T *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;
}
template<class T>
size_t DEKHash(const T* str)
{
	if (!*str)        // 以保证空字符串返回哈希值0  
		return 0;
	register size_t hash = 1315423911;
	while (size_t ch = (size_t)*str++)
	{
		hash = ((hash << 5) ^ (hash >> 27)) ^ ch;
	}
	return hash;
}
//6个仿函数分别进行6种字符串算法的调用
template<class T>
struct _HashFunc1
{
	size_t operator()(const T& str)
	{
		return BKDRHash(str.c_str());
	}
};
template<class T>
struct _HashFunc2
{
	size_t operator()(const T& str)
	{
		return SDBMHash(str.c_str());
	}
}; 
template<class T>
struct _HashFunc3
{
	size_t operator()(const T& str)
	{
		return RSHash(str.c_str());
	}
}; 
template<class T>
struct _HashFunc4
{
	size_t operator()(const T& str)
	{
		return APHash(str.c_str());
	}
}; 
template<class T>
struct _HashFunc5
{
	size_t operator()(const T& str)
	{
		return JSHash(str.c_str());
	}
};
template<class T>
struct _HashFunc6
{
	size_t operator()(const T& str)
	{
		return DEKHash(str.c_str());
	}
};

布隆过滤器具体实现如下:

#define _CRT_SECURE_NO_WARNINGS 1

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 HashFunc6 = _HashFunc6<K>>
class BloomFilter
{
public:
	BloomFilter(size_t size = 0)
	{
		_capacity = _GetNextPrime(size);
		_bm.Resize(_capacity);//调用BitMap的Resize调整大小
	}
	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);
		size_t index6 = HashFunc6()(key); 

		_bm.Set(index1 % _capacity);//设置的位为index1 % _capacity,调用BitMap的Set
		_bm.Set(index2 % _capacity);
		_bm.Set(index3 % _capacity);
		_bm.Set(index4 % _capacity);
		_bm.Set(index5 % _capacity);
		_bm.Set(index6 % _capacity);
	}
	bool Test(const K& key)
	{
		//只要存在一个为0就不存在,否则存在
		size_t index1 = HashFunc1()(key);
		if (!_bm.Test(index1 % _capacity))
			return false;

		size_t index2 = HashFunc2()(key);
		if(!_bm.Test(index2 % _capacity))
			return false;

		size_t index3 = HashFunc3()(key);
		if(!_bm.Test(index3 % _capacity))
			return false;

		size_t index4 = HashFunc4()(key);
		if(!_bm.Test(index4 % _capacity))
			return false;

		size_t index5 = HashFunc5()(key);
		if(!_bm.Test(index5 % _capacity))
			return false;

		size_t index6 = HashFunc6()(key);
		if(!_bm.Test(index6 % _capacity))
			return false;
		return true;
	}
private:
	BitMap _bm;
	size_t _capacity;
};
void TestBloomFilter()
{
	BloomFilter<> bf(50);
	bf.Set("Scen");
	bf.Set("斯洛");
	bf.Set("http://blog.51cto.com/user_index.php?action=addblog_new&job=modify&tid=1773181");
	cout << "exist? " << bf.Test("Scen") << endl;
	cout << "exist? " << bf.Test("Scenluo") << endl;
	cout << "exist? " << bf.Test("斯洛") << endl;
	cout << "exist? " << bf.Test("http://blog.51cto.com/user_index.php?action=addblog_new&job=modify&tid=1773181") << endl;
	cout << "exist? " << bf.Test("http://blog.51cto.com/user_index.php?action=addblog_new&job=modify&tid=1773131") << endl;

}

布隆过滤器的缺陷:

1、误算率(False Positive)是其中之一。

      随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。所以我们用多个哈希表去存储一个数据。那么问题又来了,我们是多用一些呢,还是少用一些。如果多用哈希表的话,如上面的题,一个哈希就需要500M,那么放的越多是不是越占内存啊。如果太少的话是不是误算率就高啊,所以取个适中的。我的程序实现是取了六个哈希表。

2、如果当前位置为0肯定不存在,但是为1不一定存在。

一般布隆过滤器只支持设计,不支持删除。可以通过引用计数,但空间浪费较大。

本文出自 “Scen” 博客,请务必保留此出处http://10741357.blog.51cto.com/10731357/1773181

<think>好的,用户想了解C++中位图布隆过滤器的相关知识及总结,我需要结合提供的引用资料来组织回答。首先,我应该先明确位图布隆过滤器的基本概念,然后讨论它们的实现和应用,最后总结它们的优缺点和应用场景。 首先看位图部分。根据引用[3],位图用于处理海量数据的存在性判断,比如40亿个整数去重。位图通过每个bit位表示一个数据是否存在,节省空间。需要提到位图的数据结构设计,比如用vector<bool>或直接操作比特位。实现部分可能需要给出C++的代码示例,比如设置某位为1,清除为0,检查存在性。应用方面包括快速查找、去重、排序等,引用[2]和[3]都有提到这些应用。 然后是布隆过滤器。根据引用[4],布隆过滤器是概率型数据结构,用于判断元素可能存在或一定不存在。它使用多个哈希函数映射到不同的位,引用[3]和[4]都提到了这一点。实现部分需要说明插入和查找的过程,以及为什么不能删除,或者如何支持删除(可能需要引用计数)。应用方面,引用[5]提到了缓存穿透的解决方案,防止数据库被无效查询压垮。此外,分布式系统和爬虫中的URL去重也是常见应用。 需要注意布隆过滤器的优缺点,比如空间效率高但存在误判率,无法删除元素等。引用[4]和[5]都有相关描述。可能需要比较位图布隆过滤器的适用场景,比如位图适合精确判断且数据范围小的情况,而布隆过滤器适合大数据量且允许一定误判的情况。 代码实现部分,位图可以用bitset或手动操作位,布隆过滤器则需要多个哈希函数和位图的组合。需要确保示例代码正确,比如展示如何设置位,检查存在性,以及布隆过滤器的插入和查询步骤。 最后总结部分,需要将位图布隆过滤器的核心点对比,强调它们在海量数据处理中的重要性,并结合引用中的内容,如引用[1]提到的结合位图布隆过滤器的方案,可能可以提到这种混合结构在优化空间和查询速度上的优势。 现在需要检查是否有遗漏的引用内容,比如引用[1]提到结合位图布隆过滤器的优点,这可能是在实际应用中的优化策略,需要在总结部分提到。同时,用户要求生成相关问题,需要根据内容设计相关问题,比如应用场景、实现细节、优缺点比较等。 需要注意回答的结构,先讲位图,再讲布隆过滤器,每个部分包括概念、实现、应用,最后总结对比。确保引用标识正确添加,例如在提到布隆过滤器概念时加上[^4],在缓存穿透时引用[^5]等。</think>### C++位图布隆过滤器总结 #### 一、位图Bitmap) **1. 概念与实现** 位图通过每个比特位(bit)表示一个数据的存在性(0/1),适用于海量数据的快速存在性判断[^3]。例如,40亿个整数(约16GB)用位图仅需约500MB内存。 **C++实现示例**: ```cpp class Bitmap { private: std::vector<uint32_t> bits; // 使用uint32_t数组存储位 public: Bitmap(size_t range) : bits((range >> 5) + 1, 0) {} // 计算需要多少32位块 void set(size_t x) { size_t index = x >> 5; // 确定数组下标 size_t offset = x % 32; // 确定比特位偏移 bits[index] |= (1 << offset); } bool test(size_t x) { size_t index = x >> 5; size_t offset = x % 32; return (bits[index] & (1 << offset)) != 0; } }; ``` **2. 应用场景** - **数据去重**:如统计40亿整数中不重复的数字。 - **快速查询**:判断IP是否在黑名单中。 - **排序**:对有限范围的整数进行非比较排序[^2]。 --- #### 二、布隆过滤器(Bloom Filter) **1. 概念与实现** 布隆过滤器通过$k$个哈希函数将元素映射到多个位,若所有对应位为1则“可能存在”,否则“一定不存在”[^4]。牺牲一定准确性换取极高的空间效率。 **C++实现核心逻辑**: ```cpp class BloomFilter { private: Bitmap bitmap; size_t k; // 哈希函数数量 public: BloomFilter(size_t size, size_t k) : bitmap(size), k(k) {} void add(const std::string& key) { for (size_t i = 0; i < k; ++i) { size_t hash = std::hash<std::string>{}(key + std::to_string(i)); bitmap.set(hash % bitmap.size()); } } bool contains(const std::string& key) { for (size_t i = 0; i < k; ++i) { size_t hash = std::hash<std::string>{}(key + std::to_string(i)); if (!bitmap.test(hash % bitmap.size())) return false; } return true; // 可能存在(有一定误判率) } }; ``` **2. 应用场景** - **缓存穿透防护**:拦截不存在的数据请求,保护数据库。 - **分布式系统**:减少节点间冗余数据传输。 - **爬虫URL去重**:避免重复抓取已处理的页面[^3]。 **3. 优缺点** - **优点**:空间效率高,查询时间$O(k)$,适合海量数据[^4]。 - **缺点**:误判率随元素增加而上升,且不支持删除(除非引入计数布隆过滤器)。 --- #### 三、对比与总结 | **特性** | **位图** | **布隆过滤器** | |------------------|------------------------------|------------------------------| | **数据范围** | 适用于整数且范围较小 | 支持任意数据类型 | | **误判率** | 无 | 有(可调整哈希函数数量优化) | | **删除支持** | 直接修改位即可 | 需额外结构(如计数位图) | | **典型场景** | 精确存在性判断 | 允许误判的存在性预筛 | **混合优化方案**:结合位图布隆过滤器,例如用位图处理高频数据,布隆过滤器处理低频数据,提升整体性能[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值