您的位置:首页 > 其它

位图

2016-03-27 14:41 288 查看
位图主要用来处理海量数据问题
//数据必须集中在一个范围内,且不能存负数
class BitMap//将数据存储在对应的位,用位来存储数据
{
public:
BitMap(size_t len)
{
int size = len >> 5;
if (len % 32)
_array.resize(size + 1);
else
_array.resize(size);
}
BitMap(size_t minLen, size_t maxLen)//如果用这种,求下标时(num-minLen)/32
{
int size = (maxLen-minLen+1) >> 5;
if ((maxLen - minLen + 1 )% 32)
_array.resize(size + 1);
else
_array.resize(size);
}
void Set(size_t num)
{
size_t index = num >> 5;
size_t count = num % 32;
_array[index] |= (1 << count);//将_array[index]第count位置为1,此处存储和大小端有关系
}
void ReSet(size_t num)
{
size_t index = num >> 5;
size_t count = num % 32;
_array[index] &= (!(1 << count));//将_array[index]第count位置为1,此处存储和大小端有关系
}
bool Test(size_t num)
{
size_t index = num >> 5;
size_t count = num % 32;
return  _array[index] &(1 << count);
}
private:
vector<int> _array;//用vector<char>不能存储相同的数,有限制,因为它只有0,1两个不同的位
};
void Test2()
{
BitMap bm(100);
bm.Set(0);
bm.Set(100);
bm.Set(9);
bm.Set(4);
bm.Set(77);
bm.Set(6);
cout << bm.Test(0) << " " << bm.Test(10) << " " << bm.Test(77)<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  位图