1
/* Copyright (C) 2005 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Implementation of trie and Aho-Corasick automaton.
18
Supports only charsets that can be compared byte-wise.
21
Add character frequencies. Can increase lookup speed
23
Implement character-wise comparision.
27
#include "mysys_priv.h"
35
TRIE *trie_init (TRIE *trie, CHARSET_INFO *charset);
38
Allocates or initializes a `TRIE' object. If `trie' is a `NULL'
39
pointer, the function allocates, initializes, and returns a new
40
object. Otherwise, the object is initialized and the address of
41
the object is returned. If `trie_init()' allocates a new object,
42
it will be freed when `trie_free()' is called.
45
An initialized `TRIE*' object. `NULL' if there was insufficient
46
memory to allocate a new object.
49
TRIE *trie_init (TRIE *trie, CHARSET_INFO *charset)
52
DBUG_ENTER("trie_init");
54
init_alloc_root(&mem_root,
55
(sizeof(TRIE_NODE) * 128) + ALLOC_ROOT_MIN_BLOCK_SIZE,
56
sizeof(TRIE_NODE) * 128);
59
if (! (trie= (TRIE *)alloc_root(&mem_root, sizeof(TRIE))))
61
free_root(&mem_root, MYF(0));
66
memcpy(&trie->mem_root, &mem_root, sizeof(MEM_ROOT));
69
trie->root.next= NULL;
70
trie->root.links= NULL;
71
trie->root.fail= NULL;
72
trie->charset= charset;
81
void trie_free (TRIE *trie);
82
trie - valid pointer to `TRIE'
85
Frees the memory allocated for a `trie'.
91
void trie_free (TRIE *trie)
94
DBUG_ENTER("trie_free");
96
memcpy(&mem_root, &trie->mem_root, sizeof(MEM_ROOT));
97
free_root(&mem_root, MYF(0));
104
my_bool trie_insert (TRIE *trie, const uchar *key, uint keylen);
105
trie - valid pointer to `TRIE'
106
key - valid pointer to key to insert
107
keylen - non-0 key length
110
Inserts new key into trie.
113
Upon successful completion, `trie_insert' returns `FALSE'. Otherwise
117
If this function fails you must assume `trie' is broken.
118
However it can be freed with trie_free().
121
my_bool trie_insert (TRIE *trie, const uchar *key, uint keylen)
127
DBUG_ENTER("trie_insert");
128
DBUG_ASSERT(trie && key && keylen);
130
trie->root.fail= NULL;
131
for (k= 0; k < keylen; k++)
134
for (next= node->links; next; next= next->next)
140
TRIE_NODE *tmp= (TRIE_NODE *)alloc_root(&trie->mem_root,
146
tmp->links= tmp->fail= tmp->next= NULL;
154
for (next= node->links; next->next; next= next->next) /* no-op */;
172
my_bool trie_prepare (TRIE *trie);
173
trie - valid pointer to `TRIE'
176
Constructs Aho-Corasick automaton.
179
Upon successful completion, `trie_prepare' returns `FALSE'. Otherwise
183
my_bool ac_trie_prepare (TRIE *trie)
185
TRIE_NODE **tmp_nodes;
189
DBUG_ENTER("trie_prepare");
192
tmp_nodes= (TRIE_NODE **)my_malloc(trie->nnodes * sizeof(TRIE_NODE *), MYF(0));
196
trie->root.fail= &trie->root;
197
for (node= trie->root.links; node; node= node->next)
199
node->fail= &trie->root;
200
tmp_nodes[lnode++]= node;
203
while (fnode < lnode)
205
TRIE_NODE *current= (TRIE_NODE *)tmp_nodes[fnode++];
206
for (node= current->links; node; node= node->next)
208
TRIE_NODE *fail= current->fail;
209
tmp_nodes[lnode++]= node;
210
while (! (node->fail= trie_goto(&trie->root, fail, node->c)))
214
my_free((uchar*)tmp_nodes, MYF(0));
221
void ac_trie_init (TRIE *trie, AC_TRIE_STATE *state);
222
trie - valid pointer to `TRIE'
223
state - value pointer to `AC_TRIE_STATE'
226
Initializes `AC_TRIE_STATE' object.
229
void ac_trie_init (TRIE *trie, AC_TRIE_STATE *state)
231
DBUG_ENTER("ac_trie_init");
232
DBUG_ASSERT(trie && state);
234
state->node= &trie->root;