位图法

一、定义

       位图法就是bitmap的缩写。所谓bitmap,就是用每一位来存放某种状态,适用于大规模数据,但数据状态又不是很多的情况。通常是用来判断某个数据存不存在的。在STL中有一个bitset容器,其实就是位图法,引用bitset介绍:
A bitset is a special container class that is designed to store bits (elements with only two possible values: 0 or 1, true or false, ...).The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.

二、数据结构

unsigned int bit[N];
在这个数组里面,可以存储 N * sizeof(int)个数据,但是最大的数只能是N * sizeof(int) - 1。假如,我们要存储的数据范围为0-15,则我们只需要使得N=1,这样就可以把数据存进去。如下图:

数据为【5,1,7,15,0,4,6,10】,则存入这个结构中的情况为

三、相关操作

1,写入数据

定义一个数组: unsigned char bit[8 * 1024]; 这样做,能存 8K*8=64K 个 unsigned short 数据。 bit 存放的字节位置和位位置(字节 0~8191 ,位 0~7 )

比如写 1234 ,字节序: 1234/8 = 154; 位序: 1234 &0b111 = 2 ,那么 1234 放在 bit 的下标 154 字节处,把该字节的 2 号位( 0~7)置为 1

字节位置: int nBytePos =1234/8 = 154;

位位置:   int nBitPos = 1234 & 7 = 2;

[cpp]  view plain copy
  1. // 把数组的 154 字节的 2 位置为 1  
  2. unsigned short val = 1<<nBitPos;  
  3. bit[nBytePos] = bit[nBytePos] |val;  // 写入 1234 得到arrBit[154]=0b00000100  

  再比如写入 1236 ,

字节位置: int nBytePos =1236/8 = 154;

位位置:   int nBitPos = 1236 & 7 = 4

[cpp]  view plain copy
  1. // / 把数组的 154 字节的 4 位置为 1  
  2. val = 1<<nBitPos;  
  3. arrBit[nBytePos] = arrBit[nBytePos] |val;  // 再写入 1236 得到arrBit[154]=0b00010100  
函数实现:
[cpp]  view plain copy
  1. #define SHIFT 5    
  2. #define MAXLINE 32    
  3. #define MASK 0x1F    
  4. void setbit(int *bitmap, int i){    
  5.     bitmap[i >> SHIFT] |= (1 << (i & MASK));    
  6. }  

2,读指定位

[cpp]  view plain copy
  1. bool getbit(int *bitmap1, int i){    
  2.     return bitmap1[i >> SHIFT] & (1 << (i & MASK));    
  3. }   

四、位图法的缺点

  1. 可读性差
  2. 位图存储的元素个数虽然比一般做法多,但是存储的元素大小受限于存储空间的大小。位图存储性质:存储的元素个数等于元素的最大值。比如, 1K 字节内存,能存储 8K 个值大小上限为 8K 的元素。(元素值上限为 8K ,这个局限性很大!)比如,要存储值为 65535 的数,就必须要 65535/8=8K 字节的内存。要就导致了位图法根本不适合存 unsigned int 类型的数(大约需要 2^32/8=5 亿字节的内存)。
  3. 位图对有符号类型数据的存储,需要 2 位来表示一个有符号元素。这会让位图能存储的元素个数,元素值大小上限减半。 比如 8K 字节内存空间存储 short 类型数据只能存 8K*4=32K 个,元素值大小范围为 -32K~32K 。

五、位图法的应用

  1、给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中
  首先,将这40亿个数字存储到bitmap中,然后对于给出的数,判断是否在bitmap中即可。
2、使用位图法判断整形数组是否存在重复
      遍历数组,一个一个放入bitmap,并且检查其是否在bitmap中出现过,如果没出现放入,否则即为重复的元素。
       3、使用位图法进行整形数组排序
      首先遍历数组,得到数组的最大最小值,然后根据这个最大最小值来缩小bitmap的范围。这里需要注意对于int的负数,都要转化为unsigned int来处理,而且取位的时候,数字要减去最小值。
       4、在2.5亿个整数中找出不重复的整数,注,内存不足以容纳这2.5亿个整数
      参 考的一个方法是:采用2-Bitmap(每个数分配2bit,00表示不存在,01表示出现一次,10表示多次,11无意义)。其实,这里可以使用两个普 通的Bitmap,即第一个Bitmap存储的是整数是否出现,如果再次出现,则在第二个Bitmap中设置即可。这样的话,就可以使用简单的1- Bitmap了。

六、实现

要求在http://blog.csdn.net/w397090770/article/details/7388319里面

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <cstdio>  
  4. #include <cstring>  
  5. #include <fstream>  
  6. #include <string>  
  7. #include <vector>  
  8. #include <algorithm>  
  9. #include <iterator>  
  10.   
  11. #define SHIFT 5  
  12. #define MAXLINE 32  
  13. #define MASK 0x1F  
  14.   
  15. using namespace std;  
  16.   
  17. //  w397090770    
  18. //  wyphao.2007@163.com    
  19. //  2012.11.29  
  20.   
  21. void setbit(int *bitmap, int i){  
  22.     bitmap[i >> SHIFT] |= (1 << (i & MASK));  
  23. }  
  24.   
  25. bool getbit(int *bitmap1, int i){  
  26.     return bitmap1[i >> SHIFT] & (1 << (i & MASK));  
  27. }  
  28.   
  29. size_t getFileSize(ifstream &in, size_t &size){  
  30.     in.seekg(0, ios::end);  
  31.     size = in.tellg();  
  32.     in.seekg(0, ios::beg);  
  33.     return size;  
  34. }  
  35.   
  36. char * fillBuf(const char *filename){  
  37.     size_t size = 0;  
  38.     ifstream in(filename);  
  39.     if(in.fail()){  
  40.         cerr<< "open " << filename << " failed!" << endl;  
  41.         exit(1);  
  42.     }  
  43.     getFileSize(in, size);    
  44.       
  45.     char *buf = (char *)malloc(sizeof(char) * size + 1);  
  46.     if(buf == NULL){  
  47.         cerr << "malloc buf error!" << endl;  
  48.         exit(1);  
  49.     }  
  50.       
  51.     in.read(buf, size);  
  52.     in.close();  
  53.     buf[size] = '\0';  
  54.     return buf;  
  55. }  
  56. void setBitMask(const char *filename, int *bit){  
  57.     char *buf, *temp;  
  58.     temp = buf = fillBuf(filename);  
  59.     char *p = new char[11];  
  60.     int len = 0;  
  61.     while(*temp){  
  62.         if(*temp == '\n'){  
  63.             p[len] = '\0';  
  64.             len = 0;  
  65.             //cout<<p<<endl;  
  66.             setbit(bit, atoi(p));  
  67.         }else{  
  68.             p[len++] = *temp;  
  69.         }  
  70.         temp++;  
  71.     }  
  72.     delete buf;  
  73. }  
  74.   
  75. void compareBit(const char *filename, int *bit, vector<int> &result){  
  76.     char *buf, *temp;  
  77.     temp = buf = fillBuf(filename);  
  78.     char *p = new char[11];  
  79.     int len = 0;  
  80.     while(*temp){  
  81.         if(*temp == '\n'){  
  82.             p[len] = '\0';  
  83.             len = 0;  
  84.             if(getbit(bit, atoi(p))){  
  85.                 result.push_back(atoi(p));  
  86.             }  
  87.         }else{  
  88.             p[len++] = *temp;  
  89.         }  
  90.         temp++;  
  91.     }  
  92.     delete buf;  
  93. }  
  94.   
  95. int main(){  
  96.     vector<int> result;  
  97.     unsigned int MAX = (unsigned int)(1 << 31);  
  98.     unsigned int size = MAX >> 5;  
  99.     int *bit1;  
  100.   
  101.     bit1 = (int *)malloc(sizeof(int) * (size + 1));  
  102.     if(bit1 == NULL){  
  103.         cerr<<"Malloc bit1 error!"<<endl;  
  104.         exit(1);  
  105.     }  
  106.   
  107.     memset(bit1, 0, size + 1);  
  108.     setBitMask("file1", bit1);  
  109.     compareBit("file2", bit1, result);  
  110.     delete bit1;  
  111.       
  112.     cout<<result.size();  
  113.     sort(result.begin(), result.end());  
  114.     vector< int >::iterator   it = unique(result.begin(), result.end());  
  115.   
  116.     ofstream    of("result");  
  117.     ostream_iterator<int> output(of, "\n");  
  118.     copy(result.begin(), it, output);  
  119.       
  120.     return 0;  
  121. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值