#pragma once
#include <vector>
class BitMap
{
public:
BitMap()
:_size(0)
{}
void Resize(size_t size)
{
_a.resize((size>>5) + 1);
}
void Set(size_t x)
{
size_t index = x>>5;
size_t n = x % 32;
_a[index] |= (1<<n);
++_size;
}
void Reset(size_t x)
{
size_t index = x>>5;
size_t n = x % 32;
_a[index] &= (~(1<<n));
--_size;
}
bool Test(size_t x)
{
size_t index = x>>5;
size_t n = x % 32;
return _a[index] & (1<<n);
}
size_t Size()
{
return _size;
}
protected:
vector<size_t> _a;
size_t _size;
};
void BitMapTest()
{
BitMap bitmap;
bitmap.Resize(65);
bitmap.Set(65);
bitmap.Set(1);
bitmap.Set(4);
bitmap.Set(45);
cout<<"65?"<<bitmap.Test(65)<<endl;
cout<<"1?"<<bitmap.Test(1)<<endl;
cout<<"4?"<<bitmap.Test(4)<<endl;
cout<<"45?"<<bitmap.Test(45)<<endl;
cout<<"25?"<<bitmap.Test(25)<<endl;
cout<<endl<<"Reset(65)"<<endl;
bitmap.Reset(65);
cout<<"65?"<<bitmap.Test(65)<<endl;
cout<<endl<<"Size:"<<bitmap.Size()<<endl;
}
#include <iostream>
using namespace std;
//#include "HashTablesBucket.h"
#include "BitMap.h"
int main()
{
//TestDic();
//Test();
BitMapTest();
return 0;
}
转载于:https://blog.51cto.com/zgw285763054/1786883