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 */
22
typedef struct st_trie_node
24
uint16 leaf; /* Depth from root node if match, 0 else */
25
uchar c; /* Label on this edge */
26
struct st_trie_node *next; /* Next label */
27
struct st_trie_node *links; /* Array of edges leaving this node */
28
struct st_trie_node *fail; /* AC failure function */
31
typedef struct st_trie
35
CHARSET_INFO *charset;
40
typedef struct st_ac_trie_state
46
extern TRIE *trie_init (TRIE *trie, CHARSET_INFO *charset);
47
extern void trie_free (TRIE *trie);
48
extern bool trie_insert (TRIE *trie, const uchar *key, uint keylen);
49
extern bool ac_trie_prepare (TRIE *trie);
50
extern void ac_trie_init (TRIE *trie, AC_TRIE_STATE *state);
53
/* `trie_goto' is internal function and shouldn't be used. */
55
static inline TRIE_NODE *trie_goto (TRIE_NODE *root, TRIE_NODE *node, uchar c)
58
for (next= node->links; next; next= next->next)
69
int ac_trie_next (AC_TRIE_STATE *state, uchar *c);
70
state - valid pointer to `AC_TRIE_STATE'
71
c - character to lookup
74
Implementation of search using Aho-Corasick automaton.
75
Performs char-by-char search.
78
`ac_trie_next' returns length of matched word or 0.
81
static inline int ac_trie_next (AC_TRIE_STATE *state, uchar *c)
83
TRIE_NODE *root, *node;
85
root= &state->trie->root;
87
while (! (state->node= trie_goto(root, node, *c)))
89
return(state->node->leaf);
95
bool trie_search (TRIE *trie, const uchar *key, uint keylen);
96
trie - valid pointer to `TRIE'
97
key - valid pointer to key to insert
98
keylen - non-0 key length
101
Performs key lookup in trie.
104
`trie_search' returns `true' if key is in `trie'. Otherwise,
108
Consecutive search here is "best by test". arrays are very short, so
109
binary search or hashing would add too much complexity that would
110
overweight speed gain. Especially because compiler can optimize simple
111
consecutive loop better (tested)
114
static inline bool trie_search (TRIE *trie, const uchar *key, uint keylen)
118
assert(trie && key && keylen);
121
for (k= 0; k < keylen; k++)
124
if (! (node= node->links))
128
if (! (node= node->next))
132
return(node->leaf > 0);