位图(BitMap)

位图

引入:2.给40亿个不重复的无符号整数,没排过序。给一个无符号整数,如何快速判断一个数是否在这40亿个数中。 【腾讯】

思路:如果内存够的话,40亿个整型使用位图存储需要500M左右的空间。

分析:位图只适合判断,查找数据是否存在!

如图解释:


在代码中,使用的是无符号整型数据,32个二进制位,开辟数组时,一个数组元素是一个32位的整型数据,位图的思想,则这32位二进制位就可以表示32位

数,原本一个数组元素只能存一个数据,40亿个数,内存将会吃不消,查找也相当困难,位图使得一个数据用一个二进制位表示,一个无符号整型的数组元素

就可以表示32个数据,40亿个数据,有位图的方式存,会很节省空间,同时查找效率也会得到提高!

以下是代码:

#ifndef _BIT_MAP_H
#define _BIT_MAP_H

#include<iostream>
#include<vector>
using namespace std;

/*
*一个数据32位,40亿个整数,每个整数需用一位表示,40亿位就完事
*/

class BitMap
{
public:
	BitMap()
		:_size(0)
	{}

	BitMap(size_t size)
		:_size(0)
	{
		_array.resize((size>>5)+1);  //多少个数据,一个数据占32位,加一是至少一个数据
	}

	bool Set(size_t num)
	{
		size_t index = num >> 5;     //计算在哪个数据上
		size_t n = num % 32;

		if (_array[index] & (1 << (31 - n)))   //移位问题
		{
			cout << "有数据" << endl;
			return false;
		}
		else
		{
			size_t a = 1 << (31 - n);
			_array[index] |= a;
			++_size;
			return true;
		}
	}

	bool ReSet(size_t num)   //删除一个数 之后重置
	{
		size_t index = num >> 5;
		size_t n = num % 32;

		if (_array[index] & (1 << (31 - n)))   //数存在 删除
		{
			_array[index] &= (~(1 << (31 - n)));
			--_size;
			return true;
		}
		else
		{
			return false;  //不存在这个数
		}
	}

private:
	vector<size_t> _array; //数组
	size_t _size;          //位图中数据个数
};

#endif


void Test()
{
	BitMap bm(65);

	for (int i = 0; i < 32; ++i)
	{
		bm.Set(i);
	}

	bm.ReSet(0);
}
功能分析:

1.将数据写进位图,也就是设置,位图中的哪一个二进制位具体表示哪一个数据,数据个数也是动态开辟,实时保证位数足够;

2.删除数据,也就是重置位图中相应位;

3.查找都是同删除数据,找到在删;


赐教!

posted @ 2016-06-21 14:26  Li_Ning  阅读(818)  评论(0编辑  收藏  举报