首页 > 其他 > 详细

BitMap实现

时间:2016-06-20 22:25:18      阅读:285      评论:0      收藏:0      [点我收藏+]

#pragma once


#include<vector>

using namespace std;


class BitMap

{

public:

BitMap()

:_size(0)

{}

BitMap(size_t size)

:_size(0)

{

_arrays.resize((size >> 5) + 1);//BitMap的大小

}

bool Set(size_t num)//在对应的index下的(1<<n)位设置为一

{

size_t index = num >> 5;

size_t n = num % 32;


if (_arrays[index] & (1 << n))

{

return false;

}

else

{

_arrays[index] |= (1 << n);

++_size;

return true;

}


}

bool ReSet(size_t num)//去掉标志位

{

size_t index = num >> 5;

size_t n = num % 32;


if (_arrays[index] & (1 << n))

{

_arrays[index] &= (~(1 << n));

--_size;

return true;

}

return false;

}

bool Test(size_t num)

{

size_t index = num >> 5;

size_t n = num % 32;

return _arrays[index] & (1 << n);

}

void Clear()

{

_arrays.assign(_arrays.size(), 0);

}

protected:

vector<size_t> _arrays;

size_t _size;

};

void Test1()

{

BitMap bm(65);

bm.Set(1);

bm.Set(4);

bm.Set(33);


cout << "1?" << bm.Test(1) << endl;

cout << "2?" << bm.Test(2) << endl;

cout << "4?" << bm.Test(4) << endl;

cout << "33?" << bm.Test(33) << endl;


bm.ReSet(33);

bm.ReSet(4);


cout << "1?" << bm.Test(1) << endl;

cout << "2?" << bm.Test(2) << endl;

cout << "4?" << bm.Test(4) << endl;

cout << "33?" << bm.Test(33) << endl;

}


本文出自 “落幕知客” 博客,请务必保留此出处http://zheng2048.blog.51cto.com/10612048/1791156

BitMap实现

原文:http://zheng2048.blog.51cto.com/10612048/1791156

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!