-
Notifications
You must be signed in to change notification settings - Fork 0
/
ahocorasick.c
154 lines (139 loc) · 3.86 KB
/
ahocorasick.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
#include "ahocorasick.h"
#include "stdlib.h"
#include "unistd.h"
#include "stdio.h"
ac_node *ahocorasick_create_node()
{
ac_node *new_node = (ac_node *)malloc(sizeof(ac_node));
if (new_node == NULL)
{
return NULL;
}
new_node->root = 0;
new_node->end_of_word = 0;
new_node->index = -1;
new_node->failure = NULL;
for (int i = 0; i < MAX_CHARS; i++)
{
new_node->child[i] = NULL;
}
return new_node;
}
void ahocorasick_insert(ac_node *root, const unsigned char *word, const int index)
{
ac_node *current = root;
for (int i = 0; word[i] != '\0'; i++)
{
if (current->child[word[i]] == NULL)
{
current->child[word[i]] = ahocorasick_create_node();
}
current = current->child[word[i]];
}
current->end_of_word = 1;
current->index = index;
}
void ahocorasick_build_failure_links(ac_node *root)
{
int queue_capacity = 10; // Initial queue capacity
int queue_size = 0;
ac_node **queue = (ac_node **)malloc(queue_capacity * sizeof(ac_node *));
int front = 0, rear = 0;
queue[rear++] = root;
queue_size++;
while (front < rear)
{
ac_node *current = queue[front++];
for (int i = 0; i < MAX_CHARS; i++)
{
ac_node *child = current->child[i];
if (child && current == root)
{
child->failure = root;
}
else if (child)
{
ac_node *failure = current->failure;
while (failure && !failure->child[i])
{
failure = failure->failure;
}
child->failure = failure ? failure->child[i] : root;
}
if (child)
{
if (queue_size == queue_capacity)
{
queue_capacity *= 2;
queue = (ac_node **)realloc(queue, queue_capacity * sizeof(ac_node *));
}
queue[rear++] = child;
queue_size++;
}
}
}
free(queue);
}
int ahocorasick_find_matches(ac_node *root, const unsigned char *text, int **matchIndices)
{
ac_node *current = root;
int len = strlen((char *)text);
int matchIndicesCapacity = 10; // Initial capacity
int numMatches = 0;
*matchIndices = (int *)malloc(matchIndicesCapacity * sizeof(int));
for (int i = 0; i < len; i++)
{
while (current && !current->child[text[i]])
{
current = current->failure;
}
current = current ? current->child[text[i]] : root;
ac_node *temp = current;
while (temp && temp->end_of_word)
{
if (numMatches == matchIndicesCapacity)
{
matchIndicesCapacity *= 2;
*matchIndices = (int *)realloc(*matchIndices, matchIndicesCapacity * sizeof(int));
}
(*matchIndices)[numMatches++] = temp->index;
temp = temp->failure;
}
}
return numMatches;
}
void ahocorasick_free_trei(ac_node *current)
{
if (current == NULL)
{
return;
}
for (int i = 0; i < MAX_CHARS; i++)
{
if (current->child[i] != NULL)
{
ahocorasick_free_trei(current->child[i]);
}
}
free(current);
}
ac_node *ahocorasick_create_trie(const unsigned char **dictionary, int numWords)
{
ac_node *root_node = ahocorasick_create_node();
if (root_node == NULL)
{
return NULL;
}
int max_word_length = 0;
for (int i = 0; i < numWords; i++)
{
int word_length = strlen((const char *)dictionary[i]);
if (word_length > max_word_length)
{
max_word_length = word_length;
}
ahocorasick_insert(root_node, dictionary[i], i);
}
ahocorasick_build_failure_links(root_node);
return root_node;
}