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 my_bool trie_insert (TRIE *trie, const uchar *key, uint keylen);
49
extern my_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
DBUG_ENTER("trie_goto");
59
for (next= node->links; next; next= next->next)
70
int ac_trie_next (AC_TRIE_STATE *state, uchar *c);
71
state - valid pointer to `AC_TRIE_STATE'
72
c - character to lookup
75
Implementation of search using Aho-Corasick automaton.
76
Performs char-by-char search.
79
`ac_trie_next' returns length of matched word or 0.
82
static inline int ac_trie_next (AC_TRIE_STATE *state, uchar *c)
84
TRIE_NODE *root, *node;
85
DBUG_ENTER("ac_trie_next");
86
DBUG_ASSERT(state && c);
87
root= &state->trie->root;
89
while (! (state->node= trie_goto(root, node, *c)))
91
DBUG_RETURN(state->node->leaf);
97
my_bool trie_search (TRIE *trie, const uchar *key, uint keylen);
98
trie - valid pointer to `TRIE'
99
key - valid pointer to key to insert
100
keylen - non-0 key length
103
Performs key lookup in trie.
106
`trie_search' returns `true' if key is in `trie'. Otherwise,
110
Consecutive search here is "best by test". arrays are very short, so
111
binary search or hashing would add too much complexity that would
112
overweight speed gain. Especially because compiler can optimize simple
113
consecutive loop better (tested)
116
static inline my_bool trie_search (TRIE *trie, const uchar *key, uint keylen)
120
DBUG_ENTER("trie_search");
121
DBUG_ASSERT(trie && key && keylen);
124
for (k= 0; k < keylen; k++)
127
if (! (node= node->links))
131
if (! (node= node->next))
135
DBUG_RETURN(node->leaf > 0);