位图

位图数据结构,就是用一块内存区域的每个比特表示一个对象的数据结构。

叫做 bitmap 或者 bitplane。

优点是速度快,内存空间占用小,能表示大范围的数据。

《Programming Pearls》里面举了一个例子,假设要对0到一千万范围内的、没有重复元素的正整数排序,

则利用位图数据结构很合适。


要使用位图数据结构,就必须熟悉位操作。

以下是已经写好的操作位图数据结构的实用函数:

[cpp]  view plain copy
  1. #define BITS_PER_BYTE 8  
  2. #define BITS_PER_INT (BITS_PER_BYTE * 4)  
  3. // set the bit of the int specified by index to 1.  
  4. // the index is zero-based, counting from the left(the higher end)  
  5. inline void pearl_int_set(int* data, int index)  
  6. {  
  7.     int mask = 1 << (BITS_PER_INT - 1 - index);  
  8.     *data |= mask;  
  9. }  
  10.   
  11. // test whether a bit of the int specified by index is 1.  
  12. // the index is zero-based, counting from the left(the higher end)  
  13. inline int pearl_int_test(int* data, int index)  
  14. {  
  15.     int mask = 1 << (BITS_PER_INT - 1 - index);  
  16.     return (*data) & mask;  
  17. }  
  18.   
  19. // set a bit of the int specified by bit_index to 0.  
  20. // the index is zero-based, counting from the left(the higher end)  
  21. inline void pearl_int_clear(int *data, int bit_index)  
  22. {  
  23.     int mask = ~(1 << (BITS_PER_INT - 1 - bit_index));  
  24.     *data &= mask;  
  25. }  
  26.   
  27. // get the right(lower) part of the int.  
  28. inline int pearl_int_right(int *data, int count)  
  29. {  
  30.     int mask = 1;  
  31.     while(count > 0) {  
  32.         mask = mask << 1 + 1;  
  33.     }  
  34.     return *data & mask;      
  35. }  
  36.   
  37. // get the left(upper) part of the int value  
  38. inline int pearl_int_left(int *data, int count)  
  39. {  
  40.     int mask = 1;  
  41.     count = BITS_PER_BYTE - count;  
  42.     while (count > 0) {  
  43.         mask = mask << 1 + 1;  
  44.     }  
  45.   
  46.     mask = ~mask;  
  47.     return *data & mask;  
  48. }  
  49.   
  50. // set a bit of the speicified memory area to 1.  
  51. // @warning this function does NOT perform error checking.  
  52. inline void pearl_set(int *bitplane, int index)  
  53. {  
  54.     int offset = index / BITS_PER_INT;  
  55.     int pos = index - offset * BITS_PER_INT;  
  56.   
  57.     pearl_int_set(bitplane + offset, pos);  
  58. }  
  59.   
  60. // set a bit of the specified memory area to 0.  
  61. // @warning this function does NOT perform error checking.  
  62. inline void pearl_clear(int *bitplane, int index)  
  63. {  
  64.     int offset = index / BITS_PER_INT;  
  65.     int pos = index - offset * BITS_PER_INT;  
  66.   
  67.     pearl_int_clear(bitplane + offset, pos);  
  68. }  
  69.   
  70. // test if a bit of the specified memory area is 1.  
  71. // @warning this function does NOT perform error checking.  
  72. inline int pearl_test(int *bitplane, int index)  
  73. {  
  74.     int offset = index / BITS_PER_INT;  
  75.     int pos = index - offset * BITS_PER_INT;  
  76.   
  77.     return pearl_int_test(bitplane + offset, pos);  
  78. }  


约定

所有比特的编号方法是,从低字节的高位比特位开始,第一个bit为0,最后一个bit为 n-1。

例如,假设 int arr[4] 表示一块内存区域,共16个字节,共128个比特,第1个整数 arr[0] 的高比特(从左边数,第1个字节,第1个比特)为第0个比特。

因此,上面程序与机器是大端还是小端模式无关。

图解:

编号:  |0123456------->-------

              |00000000 | 00000000 | 00000000 | 00000000 | 00000000 | 00000000 | 00000000 | 00000000 |....

              |<-------------------arr[0]--------------------------->|<---------------arr[1]--------------------------------->....


用法举例:

[cpp]  view plain copy
  1. int main(int argc, char* argv[]) {  
  2.     int* bitplane = (int*)malloc(sizeof(int)*SIZE);  
  3.     pearl_set(bitplane, n); // 0 <= n <= SIZE * 4 * 8 - 1  
  4.     if ( pearl_test(bitplane, n) ) {  
  5.         printf("bit(%d) is set\n", n);  
  6.     }  
  7.     pearl_clear(bitplane, n);  
  8.     if ( pearl_test(bitplane, n) ) {  
  9.         printf("bit(%d) is cleared\n", n);  
  10.     }  
  11.     return 0;  
  12. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值