-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_normal.cpp
133 lines (127 loc) · 3.07 KB
/
list_normal.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include<vector>
#include<iostream>
#include<string>
#include<sys/time.h>
using namespace std;
FILE *fi;
FILE *fp;
struct _INDEX {
unsigned int len;
unsigned int *arr;
} *idx;
int MAXARRS = 2000;
unsigned int i, alen;
unsigned int *aarr;
int j, n;
vector<int> strtoints(char* line) {
vector<int> arr;
int i = 0;
int num = 0;
while (line[i] == ' ' || (line[i] >= 48 && line[i] <= 57)) {
num = 0;
while (line[i] != ' ') {
num *= 10;
int tmp = line[i] - 48;
num += tmp;
i++;
}
i++;
arr.push_back(num);
}
return arr;
}
bool find(unsigned int e, _INDEX list) {
for (int i = 0;i < list.len;i++) {
if (e == list.arr[i]) {
return true;
}
}
return false;
}
int main() {
struct timeval t1, t2;
double timeuse=0;
fi = fopen("ExpIndex", "rb");
if (NULL == fi) {
printf("Can not open file ExpIndex!\n");
return 1;
}
idx = (struct _INDEX *)malloc(MAXARRS * sizeof(struct _INDEX));
if (NULL == idx) {
printf("Can not malloc %d bytes for idx!\n", MAXARRS * sizeof(struct _INDEX));
return 2;
}
j = 0;
while (1) {
fread(&alen, sizeof(unsigned int), 1, fi);
if (feof(fi)) break;
aarr = (unsigned int *)malloc(alen * sizeof(unsigned int));
if (NULL == aarr) {
printf("Can not malloc %d bytes for aarr!\n", alen * sizeof(unsigned short));
return 3;
}
for (i = 0;i < alen;i++) {
fread(&aarr[i], sizeof(unsigned int), 1, fi);
if (feof(fi)) break;
}
if (feof(fi)) break;
idx[j].len = alen;
idx[j].arr = aarr;
j++;
if (j >= MAXARRS) {
printf("Too many arrays(>=%d)!\n", MAXARRS);
break;
}
}
fclose(fi);
//现在已经有一个idx数组存储了这个倒排索引文件,idx[i].arr表示第i个关键词的倒排索引链表
//下面是query_list代表查询的二维数组,大概能到2000个关键词,所以上面的max可以设置为2000
fp = fopen("ExpQuery", "r");
vector<vector<int> > query_list;
vector<int> arr;
char* line = new char[100];
while ((fgets(line, 100, fp)) != NULL)
{
arr = strtoints(line);
query_list.push_back(arr);
}
fclose(fp);
gettimeofday(&t1, NULL);
//实现按表求交的平凡算法
vector<vector<unsigned int> > results;
int QueryNum = 100; //代表要处理的查询次数
for (int i = 0;i < QueryNum;i++) { //逐个处理查询请求
int TermNum = query_list[i].size(); //query_list[i]表示第i次查询所包含的关键词的集合
vector<unsigned int> result; //结果数组
vector<unsigned int> temp; //临时数组
//先把第一个列表作为基准拷贝到临时数组
for (int j = 0;j < idx[query_list[i][0]].len;j++) {
temp.push_back(idx[query_list[i][0]].arr[j]);
}
for (int j = 1;j < TermNum;j++) { //逐个表求交
for (int k = 0;k < temp.size();k++) {
if (!find(temp[k], idx[query_list[i][j]])) {
//没找到,不添加
}
else {
result.push_back(temp[k]);
}
}
//一次求交之后,result存储临时结果
temp = result;
result.clear();
}
result = temp;
cout << result.size() << endl;
results.push_back(result);
}
gettimeofday(&t2, NULL);
timeuse += (t2.tv_sec-t1.tv_sec) * 1000000 + t2.tv_usec-t1.tv_usec;
cout<<"time_use="<<timeuse<<endl;
for (j = 0;j < n;j++) free(idx[j].arr);
free(idx);
return 0;
}