-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaho-corasick-mt-serial.c
361 lines (270 loc) · 9.13 KB
/
aho-corasick-mt-serial.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/time.h>
/*
* Max number of states in the matching machine.
* Should be equal to the sum of the length of all keywords.
*/
#define MAXS 500
/* Characters in input alphabet */
#define MAXC 26
/* Number of strings to analyze */
#define HSTRN 1
/* Length of strings to analyze */
#define HSTRL 10
/* Number of strings to search for */
#define NSTRN 4
/* Length of strings to search for */
#define NSTRL 10
/*
* OUTPUT FUNCTION
* Bit i in this mask is one of the word with index i
* appears when the machine enters this state.
*/
int out[MAXS];
/* FAILURE FUNCTION */
int f[MAXS];
/* GOTO FUNCTION (TRIE) */
int g[MAXS][MAXC];
/* Structs that represent the Queue */
struct Queue {
struct QueueNode* head;
struct QueueNode* tail;
};
struct QueueNode {
int data;
struct QueueNode* next;
};
/* Function to initialize the Queue */
struct Queue* init();
/* Function to check if the Queue is empty */
bool is_empty(struct Queue* q);
/* Function to add an element to the Queue */
void enqueue(struct Queue* q, int data);
/* Function to remove the element at the head of the Queue */
int dequeue(struct Queue* q);
// Builds the string matching machine.
// arr - array of words. The index of each keyword is important:
// "out[state] & (1 << i)" is > 0 if we just found word[i]
// in the text.
// Returns the number of states that the built machine has.
// States are numbered 0 up to the return value - 1, inclusive.
int build_ac_graph(char needles[NSTRN][NSTRL])
{
// Initialize all values in g function as 0.
memset(out, 0, sizeof(out));
// Initialize all values in goto function as -1.
memset(g, -1, sizeof(g));
// Initially, we just have the 0 state
int states = 1;
// Construct values for goto function, i.e., fill g[][]
// This is same as building a Trie for arr[]
for (int i = 0; i < NSTRN; ++i) {
char* needle = needles[i];
int current_state = 0;
// Insert all characters of current word in arr[]
for (int j = 0; j < strlen(needle); ++j) {
int ch = needle[j] - 'a';
// Allocate a new node (create a new state) if a
// node for ch doesn't exist.
if (g[current_state][ch] == -1)
g[current_state][ch] = states++;
current_state = g[current_state][ch];
}
// Add current word in g function
out[current_state] |= (1 << i);
}
// For all characters which don't have an edge from
// root (or state 0) in Trie, add a goto edge to state
// 0 itself
for (int ch = 0; ch < MAXC; ++ch)
if (g[0][ch] == -1)
g[0][ch] = 0;
// Now, let's build the f function
// Initialize values in fail function
memset(f, -1, sizeof(f));
// Failure function is computed in breadth first order
// using a queue
struct Queue* q = init();
// Iterate over every possible input
for (int ch = 0; ch < MAXC; ch++) {
// All nodes of depth 1 have f function value
// as 0. For example, in above diagram we move to 0
// from states 1 and 3.
if (g[0][ch] != 0) {
f[g[0][ch]] = 0;
enqueue(q, g[0][ch]);
}
}
// Now queue has states 1 and 3
while (!is_empty(q)) {
// Remove the front state from queue
int state = dequeue(q);
// For the removed state, find f function for
// all those characters for which goto function is
// not defined.
for (int ch = 0; ch <= MAXC; ch++) {
// If goto function is defined for character 'ch'
// and 'state'
if (g[state][ch] != -1) {
// Find f state of removed state
int failure = f[state];
// Find the deepest node labeled by proper
// suffix of string from root to current
// state.
while (g[failure][ch] == -1)
failure = f[failure];
failure = g[failure][ch];
f[g[state][ch]] = failure;
// Merge g values
out[g[state][ch]] |= out[failure];
// Insert the next level node (of Trie) in Queue
enqueue(q, g[state][ch]);
}
}
}
return states;
}
/* Function to initialize the Queue */
struct Queue* init() {
struct Queue* q = (struct Queue*) malloc(sizeof(struct Queue));
q->head = NULL;
q->tail = NULL;
return q;
}
/* Function to check if the Queue is empty */
bool is_empty(struct Queue* q) {
if (q->head == NULL)
return true;
return false;
}
/* Function to add an element to the Queue */
void enqueue(struct Queue* q, int data) {
/* Create a new node for the Queue */
struct QueueNode* new_node = (struct QueueNode*) malloc(sizeof(int));
new_node->data = data;
new_node->next = NULL;
/* Special case: first element of the Queue */
if (q->tail == NULL) {
q->head = q->tail = new_node;
return;
}
/* Add at the tail of the Queue */
q->tail->next = new_node;
q->tail = new_node;
}
/* Function to remove the element at the head of the Queue */
int dequeue(struct Queue* q) {
struct QueueNode* temp = q->head;
int node = temp->data;
/* Set the head to the second element */
q->head = temp->next;
/* Special case: last element of the Queue */
if (q->head == NULL)
q->tail = NULL;
free(temp);
return node;
}
// Returns the next state the machine will transition to using goto
// and f functions.
// currentState - The current state of the machine. Must be between
// 0 and the number of states - 1, inclusive.
// nextInput - The next character that enters into the machine.
int find_next_state(int currentState, char nextInput) {
int answer = currentState;
int ch = nextInput - 'a';
/* If goto function is not defined, use f function */
while (g[answer][ch] == -1)
answer = f[answer];
return g[answer][ch];
}
/* This function finds all the occurrences */
void search(char haystacks[HSTRN][HSTRL], int* counters) {
/* Initialize current state */
int current_state = 0;
/* Iterate for each haystack */
for (int i = 0; i < HSTRN; i++) {
/* Traverse the text through the built machine to find all occurrences */
for (int j = 0; j < strlen(haystacks[i]); j++) {
current_state = find_next_state(current_state, haystacks[i][j]);
/* If match not found, move to next state */
if (out[current_state] == 0)
continue;
/* Match found, update patterns counter. */
for (int k = 0; k < NSTRN; k++) {
if (out[current_state] & (1 << k)) {
counters[k] += 1;
}
}
}
}
}
// Driver program to test above
int main(int argc, char** argv) {
struct timeval begin, end;
char haystacks[HSTRN][HSTRL];
char needles[NSTRN][NSTRL];
int* counters = calloc(NSTRN, sizeof(int));
int counter = 0;
size_t size = 0;
char* buffer = NULL;
/* Start reading texts file */
FILE* texts = fopen(argv[1], "r");
if (texts == NULL) {
printf("Error opening texts file\n");
return -1;
}
while (!feof(texts)) {
counter = counter + 1;
if (getline(&buffer, &size, texts) != -1) {
/* Substitute the new line with an end of string */
buffer[strcspn(buffer, "\n")] = 0;
/* Copy the string to the new string */
strcpy(haystacks[counter - 1], buffer);
} else {
printf("Error while reading texts file\n");
return -1;
}
}
fclose(texts);
/* End reading texts file */
counter = 0;
size = 0;
buffer = NULL;
/* Start reading patterns file */
FILE* patterns = fopen(argv[2], "r");
if (patterns == NULL) {
printf("Error opening patterns file\n");
return -1;
}
while (!feof(patterns)) {
counter = counter + 1;
if (getline(&buffer, &size, patterns) != -1) {
/* Substitute the new line with an end of string */
buffer[strcspn(buffer, "\n")] = 0;
/* Copy the line to the new string */
strcpy(needles[counter - 1], buffer);
} else {
printf("Error while reading texts file\n");
return -1;
}
}
fclose(patterns);
/* End reading patterns file */
/* Building the AC graph */
build_ac_graph(needles);
/* Start searching time */
gettimeofday(&begin, 0);
search(haystacks, counters);
/* End searching time */
gettimeofday(&end, 0);
long seconds = end.tv_sec - begin.tv_sec;
long microseconds = end.tv_usec - begin.tv_usec;
double elapsed = seconds + microseconds*1e-6;
printf("Time measured: %.3f seconds.\n", elapsed);
for (int i = 0; i < NSTRN; i++)
printf("Needle %s found %d times\n", needles[i], counters[i]);
return 0;
}