-
Notifications
You must be signed in to change notification settings - Fork 5
/
WuManberOperation.c
162 lines (127 loc) · 4.32 KB
/
WuManberOperation.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "WuManberOperation.h"
#include "ShiftTable.h"
#include "ShiftTableOperation.h"
#include "PrefixTable.h"
#include "PrefixTableOperation.h"
void buildShiftTable(WuManber wuManber, char** patterns, int count) {
ShiftTableOperation tableOperation = ShiftTableOperationFactory();
int minLength = INT_MAX;
for(int i=0;i<count;i++) {
int length = strlen(patterns[i]);
if(minLength > length) {
minLength = length;
}
}
wuManber->minPatternLength = minLength;
printf(" - Min pattern length: %d\n\n", minLength);
ShiftTable table = ShiftTableFactory();
for(int i=0;i<count;i++) {
// block size = 2
for(int j=0;j<minLength-1;j++) {
int shift = minLength - (j + 1 + 1);
char subString[2] = {patterns[i][j], patterns[i][j + 1]};
tableOperation->put(table, subString, shift);
}
}
wuManber->shiftTable = table;
}
void buildPrefixTable(WuManber wuManber, char** patterns, int count) {
PrefixTableOperation tableOperation = PrefixTableOperationFactory();
PrefixTable table = PrefixTableFactory();
// block size = 2
// pattern length must >= 2
for(int i=0;i<count;i++) {
char prefix[2] = {patterns[i][0], patterns[i][1]};
tableOperation->push(table, prefix, patterns[i]);
}
wuManber->prefixTable = table;
}
MatchingResult search(WuManber wuManber, char* content) {
ShiftTableOperation shiftTableOperation = ShiftTableOperationFactory();
PrefixTableOperation prefixTableOperation = PrefixTableOperationFactory();
MatchingResult result = malloc(sizeof(MatchingResultStruct));
result->next = NULL;
MatchingResult head = result;
// if subString not in shiftTable, skip this value
int skip = wuManber->minPatternLength - 2 + 1;
int start = wuManber->minPatternLength - 1;
int contentLength = strlen(content);
while(start < contentLength) {
char subString[2] = {content[start - 1], content[start]};
int shift = shiftTableOperation->get(wuManber->shiftTable, subString);
// don't have this bad char
if(shift == -1) {
start += skip;
}
// match tail, lookup prefix table
else if(shift == 0) {
// get prefix index
int prefixStart = start - (wuManber->minPatternLength - 1);
// prefix
char prefix[2] = {content[prefixStart], content[prefixStart + 1]};
PatternNode node = prefixTableOperation->pop(wuManber->prefixTable, prefix);
// if has this prefix, match the whole pattern
while(node) {
int flag = 0;
// not need to match prefix, so i = 2
for(int i=2;i<+node->length;i++) {
if(node->pattern[i] != content[prefixStart + i]) {
flag = 1;
break;
}
}
if(!flag) {
MatchingResult res = malloc(sizeof(MatchingResultStruct));
res->pattern = malloc(node->length);
res->pattern = node->pattern;
res->start = prefixStart + 1;
res->end = prefixStart + node->length - 1 + 1;
res->next = NULL;
result->next = res;
result = res;
//printf("match %s at %d ~ %d\n", node->pattern, prefixStart, prefixStart + node->length - 1);
}
node = node->next;
}
start++;
}
// bad char shift
else {
start += shift;
}
}
return head;
}
void destroy(WuManber wuManber) {
// free shift table
free(wuManber->shiftTable);
// free prefix table
for(int i=0;i<65536;i++) {
if(wuManber->prefixTable[i]) {
PatternNode current = wuManber->prefixTable[i];
while(current) {
PatternNode temp = current;
current = current->next;
free(temp);
}
}
}
free(wuManber->prefixTable);
// free wumanber
free(wuManber);
}
WuManberOperation WuManberOperationFactory() {
static WuManberOperation wuManberOperation = NULL;
if(!wuManberOperation) {
wuManberOperation = malloc(sizeof(WuManberOperationStruct));
wuManberOperation->buildShiftTable = buildShiftTable;
wuManberOperation->buildPrefixTable = buildPrefixTable;
wuManberOperation->search = search;
wuManberOperation->destroy = destroy;
}
return wuManberOperation;
}