位圖:源代碼

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

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

	BitMap(size_t len)
		:_size(0)
	{
		size_t size = len/32+1;
		_array.resize(size, 0);
	}

	void Set(size_t num)
	{
		//size_t index = num/32;
		size_t index = num>>5;
		size_t count = num%32;

		_array[index] |= 1<<count;

		++_size;
	}

	void ReSet(size_t num)
	{
		size_t index = num>>5;
		size_t count = num%32;
		_array[index] &= ~(1<<count);

		--_size;

	}

	bool Test(size_t num)
	{
		size_t index = num>>5;
		size_t count = num%32;

		return _array[index]&(1<<count);
	}

	void Clear()
	{
		_array.assign(_array.size(), 0);
		_size = 0;
	}

private:
	vector<int> _array;
	size_t _size;
};

void Test1()
{
	BitMap bm(-1);

	bm.Set(100);
	bm.Set(9999);
	bm.Set(111111);

	cout<<"100?"<<bm.Test(100)<<endl;
	cout<<"99?"<<bm.Test(99)<<endl;
	cout<<"111111?"<<bm.Test(111111)<<endl;
	cout<<"101?"<<bm.Test(101)<<endl;
	cout<<"9999?"<<bm.Test(9999)<<endl;

	bm.ReSet(100);
	cout<<"100?"<<bm.Test(100)<<endl;
}

int main()
{
	Test1();

	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章