用位图解决大数据存储
2015-12-05 17:21
148 查看
BitSet.h
#include<iostream> #include<assert.h> using namespace std; class BitSet { public: BitSet(size_t N) //N表示存储数据的范围 { _size = N / 32 + 1; _arr = new size_t[_size]; memset(_arr, 0, sizeof(size_t)*_size);//给_arr数组初始化全部为0 } ~BitSet() { if (_arr) { delete[] _arr; } } void Set(size_t num) { int index = num / 32;//index表示数据存储在数组的第几个位置 assert(index < _size); int pos = num % 32; //pos表示数组下标为index的某一位 _arr[index] |= 1 << (32 - pos); } void Reset(size_t num) { int index = num / 32; assert(index < _size); int pos = num % 32; _arr[index] &= ~(1 << (32 - pos)); } void Clear() { memset(_arr, 0, sizeof(size_t)*_size); } bool Test(size_t num)//测试num是否已经存在 { int index = num / 32; assert(index < _size); int pos = num % 32; if ((_arr[index] & 1 << (32-pos)) > 0) { return true; } return false; } private: size_t* _arr; size_t _size; };
main.cpp
#include"BitSet.h" void Test() { BitSet bt(-1); //将-1强转成4294967295 bt.Set(32); bt.Set(99); bt.Reset(32); bt.Test(32); //bt.Clear(); bt.Test(99); bt.Test(1); } int main() { Test(); getchar(); return 0; }
相关文章推荐
- 马尔可夫聚类算法(MCL)
- Timus Online Judge 1577 E-mail(字符串DP)
- SaltStack入门(二)Grains、NoteGroup和State
- 对数据的认识(二)
- Assertion failure in -[MASViewConstraint install]
- HDOJ--2674--N!Again
- fatal error LINK1123:failure during conversion to COFF:file invalid or corrupt
- AudioTrack 分析
- ORCAD层次化设计遇到Cannot descend from a schematic page that is not contained within the hierarchy
- svn安装错误解决(Service 'ServerSVN Server' failed to start,……)
- CRITICAL_SECTION http://blog.csdn.net/leowangzi/article/details/6572650
- process launch failed: Security
- MFC主线程使用WaitForSingleObject阻塞的问题 http://blog.csdn.net/sysprogram/article/details/17383455
- WaitForSingleObject http://www.cnblogs.com/shootingstars/archive/2004/07/15/24602.html
- CreateEvent http://blog.csdn.net/richerg85/article/details/7471426
- POSTTHREADMESSAGE http://blog.csdn.net/immcss/article/details/3790243
- OpenCV中cvWaitKey()函数注意事项
- suspend造成死锁的例子和原因分析 http://blog.csdn.net/magictong/article/details/4161571
- 线程天敌TerminateThread与SuspendThread http://blog.csdn.net/magictong/article/details/6304439
- 265. Paint House II