class bitmap
{
public:
bitmap(size_t maxNum) //初始化,根据最大值初始化数组大小
{
_v = new size_t[(maxNum >> 5) + 1]();
}
~bitmap()
{
if (nullptr != _v)
{
delete[] _v;
_v = nullptr;
}
}
void Set(size_t num) //添加num值 set 1
{
size_t index = num >> 5;
size_t pos = num % 32;
_v[index] |= (1 << pos);
}
void Reset(size_t num) //移除num值 set 0
{
size_t index = num >> 5;
size_t pos = num % 32;
_v[index] &= ~(1 << pos);
}
bool HasExisted(size_t num) //是否存在num值
{
size_t index = num >> 5;
size_t pos = num % 32;
if (_v[index] & (1 << pos))
{
return true;
}
return false;
}
private:
size_t * _v = nullptr;
};
bitmap算法
最新推荐文章于 2021-03-02 17:38:05 发布