bitmap海量数据的快速查找和去重————————————

题目描述

给你一个文件,里面包含40亿个整数,写一个算法找出该文件中不包含的一个整数, 假设你有1GB内存可用。html

若是你只有10MB的内存呢?算法

解题思路

对于40亿个整数,若是直接用int数组来表示的大约要用4010^84B=16GB,超出了内存要求,这里数组

咱们能够用bitmap来解决,bitmap基本思想是一位表示一个整数,好比咱们有6个数据:post

1
7 3 1 5 6 4

 

假设bitmap容量为8,当插入7时 bit[7]=1,以此类推测试

bit[3]=1ui

bit[1]=1spa

bit[5]=1.net

……code

bit[4]=1htm

这样咱们查询5,只须要查看bit[5]==1侧存在,不然不存在。

这样一个位表明一个数据,那40一个数据大概要4010^8bit = 0.5GB,知足内存要求。

实现细节

首先咱们用int来表示:int bmap[1+N/32]; //N是总数,N=40亿,一个int32bit

而后咱们插入一个整数val,要先计算val位于数组bmap中的索引:index = val/32;

好比整数33,index=33/32=1,第33位于数组中的index=1

好比整数67,index=67/32=2,位于数组中index=2

而后在计算在这个index中的位置,由于数组中的每一个元素有32位

33,index=1,在1中的位置为33%32=1

67,index=2,在2中的位置为67%32=3

而后就是标识这个位置为1:

bmap[val/32] |= (1<<(val%32));

33: bmap[1] != (1<<1);//xxxxxx 1 x,红丝位置被置为1

67: bmap[2] != (1<<3);//xxxx 1 xxx

代码

1
2
3
4
5
6
 
void setVal(int val)
{
bmap[val/ 32] |= ( 1<<(val% 32));
//bmap[val>>5] != (val&0x1F);//这个更快?
}

 

怎样检测整数是否存在?

好比咱们检测33,一样咱们须要计算index,以及在index元素中的位置

33: index = 1, 在bmap[1]中的位置为 1,只须要检测这个位置是否为1

bmp[1] &(1<<1),这样是1返回true,否侧返回false

67:bmp[2]&(1<<3)

127:bmp[3]&(1<<31)

代码:

1
2
3
4
5
bool testVal(int val)
{
return bmap[val/ 32] & ( 1<<(val% 32));
//return bmap[val>>5] & (val&0x1F);
}

 

下面是完整测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const int N = MaxN;
const int BitLen = 32;
int bmap[ 1+N/BitLen];
 
void setVal(int val)
{
bmap[val/BitLen] |= ( 1<<(val%BitLen));
}
 
bool testVal(int val)
{
return bmap[val/BitLen] & ( 1<<(val%BitLen));
}
 
void funTest()
{
int a[] = { 1, 2, 3, 4, 6, 7};
 
for ( int i= 0; i< 6; ++i)
{
setVal(a[i]);
}
 
std::cout << testVal( 5) << std::endl;
return 0;
}

 

如今咱们来看若是内存要求是10MB呢?

这固然不能用bitmap来直接计算。由于从40亿数据找出一个不存在的数据,咱们能够将这么多的数据分红许多块, 好比每个块的大小是1000,那么第一块保存的就是0到999的数,第2块保存的就是1000 到1999的数……

实际上咱们并不保存这些数,而是给每个块设置一个计数器。 这样每读入一个数,咱们就在它所在的块对应的计数器加1。

处理结束以后, 咱们找到一个块,它的计数器值小于块大小(1000), 说明了这一段里面必定有数字是文件中所不包含的。而后咱们单独处理这个块便可。接下来咱们就能够用Bit Map算法了。咱们再遍历一遍数据, 把落在这个块的数对应的位置1(咱们要先把这个数归约到0到blocksize之间)。 最后咱们找到这个块中第一个为0的位,其对应的数就是一个没有出如今该文件中的数。)

代码以下(一个测试的代码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const int N = 1000;
const int BITLEN = 32;
const int BLOCK_SIZE = 100;
 
int Bucket[ 1+N/BLOCK_SIZE]={ 0};
int BitMap[ 1+BLOCK_SIZE/BITLEN] = { 0};
 
void test()
{
//生成测试数据
freopen( "test.txt", "w", stdout);
for ( int i= 0; i< 1000; ++i)
{
if (i == 127)
{
printf( "0\n");
continue;
}
printf( "%d\n", i);
}
fclose(stdout);
 
//读入测试数据
freopen( "test.txt", "r", stdin);
int Value;
while (scanf( "%d", &Value) != EOF)
{
++Bucket[Value/BLOCK_SIZE]; //测试数据分段累计
}
fclose(stdin);
 
//找出累计计数小于BLOCK_SIZE的
int Start=- 1, i;
for (i= 0; i< 1+N/BLOCK_SIZE; ++i)
{
if (Bucket[i] < BLOCK_SIZE)
{
Start = i*BLOCK_SIZE;
break;
}
}
if (i == 1+N/BLOCK_SIZE || Bucket[N/BLOCK_SIZE]== 0 && i==N/BLOCK_SIZE) return;
int End = Start + BLOCK_SIZE- 1;
 
//在不知足的那段用bitmap来检测
freopen( "test.txt", "r", stdin);
while (scanf( "%d", &Value) != EOF)
{
if (Value >= Start && Value <= End) //Value必须知足在那段
{
int Temp = Value - Start;
BitMap[Temp/BITLEN] |= ( 1<<(Temp%BITLEN));
}
}
fclose(stdin);
 
//找出不存在的数
freopen( "re.txt", "w", stdout);
bool Found = false;
for ( int i= 0; i< 1+BLOCK_SIZE/BITLEN; ++i)
{
for ( int k= 0; k < BITLEN; ++k)
{
if ((BitMap[i] & ( 1<<k)) == 0)
{
printf( "%d ", i*BITLEN+k+Start);
Found = true;
break;
}
}
if (Found) break;
}
fclose(stdout);
}

 

参考:http://hawstein.com/posts/12.3.html

关于数据的去重这里有一遍很好的文章是用bitmap来实现:

http://blog.csdn.net/hguisu/article/details/7880288

相关文章
相关标签/搜索