-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix4_search.c
131 lines (122 loc) · 2.8 KB
/
radix4_search.c
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
#include <stdio.h>
#include <string.h>
typedef struct Radix4Node{
struct Radix4Node* children[4];
int flag;//为1代表到达字符串的结尾
}Redix4Node,* Redix4ptr;
typedef struct Statistic{
int mem;
int cmpnum;
int nodeNum;
}Statistic;
Statistic global_stats;
void* bupt_malloc(size_t );
Redix4ptr newNode();
Redix4ptr Redix4Insert(Redix4ptr,int);
Redix4ptr Find(Redix4ptr ptr, int key);
Redix4ptr Find(Redix4ptr ptr, int key){
return ptr->children[key];
}
Redix4ptr newNode(){
global_stats.nodeNum++;
Redix4ptr node = (Redix4ptr)bupt_malloc(sizeof(Redix4Node));
for (int i = 0; i < 4; i++){
node->children[i] =NULL;
}
node->flag = 0;
return node;
}
Redix4ptr Redix4Insert(Redix4ptr ptr, int key){
if (!ptr->children[key]){
ptr->children[key] = newNode();
}
return ptr->children[key];
}
void* bupt_malloc(size_t size){
if (size <= 0) {
return NULL;
}
global_stats.mem += size;
return malloc(size);
}
int main(){
global_stats.cmpnum = 0;
global_stats.mem = 0;
global_stats.nodeNum = 0;
FILE *fp;
Redix4ptr head = newNode();
if ((fp = fopen("./patterns-127w.txt", "r")) == NULL){
return -1;
}
int flag = 0;
while (!feof(fp)){
char strLine[100] = { '\0' };
fgets(strLine, 100, fp);
int i = 0;
Redix4ptr pos = head;
while (strLine[i] != '\n'&&strLine[i] != '\0'){
int bit1 = (strLine[i] & 192)>>6;
pos = Redix4Insert(pos, bit1);
int bit2 = (strLine[i] & 48) >>4;
pos = Redix4Insert(pos, bit2);
int tmp = strLine[i] << 4;
int bit3 = (strLine[i] &12) >> 2;
pos = Redix4Insert(pos, bit3);
int bit4 = strLine[i] &3;
pos = Redix4Insert(pos, bit4);
i++;
}
pos->flag = 1;
}
fclose(fp);
printf("节点个数:%d\n", global_stats.nodeNum);
if ((fp = fopen("./words-98w.txt", "r")) == NULL){
return -1;
}
int wordsNum = 0;
int wordsSuccessNum = 0;
while (!feof(fp)){
wordsNum++;
char strLine[100] = { '\0' };
fgets(strLine, 100, fp);
//将字符串转为int
int i = 0;
Redix4ptr pos = head;
int length = 0;
while (strLine[length] != '\n'&&strLine[length] != '\0'){
length++;
}
while (strLine[i] != '\n'&&strLine[i] != '\0'){
int bit1 = (strLine[i] & 192) >> 6;
pos = Find(pos, bit1);
global_stats.cmpnum++;
if (!pos)
break;
int bit2 = (strLine[i] & 48) >> 4;
pos = Find(pos, bit2);
global_stats.cmpnum++;
if (!pos)
break;
int bit3 = (strLine[i] & 12) >> 2;
pos = Find(pos, bit3);
global_stats.cmpnum++;
if (!pos)
break;
int bit4 = strLine[i] & 3;
pos = Find(pos, bit4);
global_stats.cmpnum++;
if (!pos)
break;
i++;
}
if (i == length && pos->flag){
wordsSuccessNum++;
}
}
fclose(fp);
printf("树结构占用内存量%d\n", global_stats.mem);
printf("words总个数%d\n", wordsNum);
printf("成功检索的word总个数%d\n", wordsSuccessNum);
printf("字符比较次数 %d\n", global_stats.cmpnum / 1000);
printf("\n");
}