~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2005 MySQL AB
2
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.
6
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.
11
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 */
15
16
#ifndef _trie_h
17
#define _trie_h
18
#ifdef	__cplusplus
19
extern "C" {
20
#endif
21
22
typedef struct st_trie_node
23
{
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 */
29
} TRIE_NODE;
30
31
typedef struct st_trie
32
{
33
  TRIE_NODE root;
34
  MEM_ROOT mem_root;
35
  CHARSET_INFO *charset;
36
  uint32 nnodes;
37
  uint32 nwords;
38
} TRIE;
39
40
typedef struct st_ac_trie_state
41
{
42
  TRIE *trie;
43
  TRIE_NODE *node;
44
} AC_TRIE_STATE;
45
46
extern TRIE *trie_init (TRIE *trie, CHARSET_INFO *charset);
47
extern void trie_free (TRIE *trie);
146 by Brian Aker
my_bool cleanup.
48
extern bool trie_insert (TRIE *trie, const uchar *key, uint keylen);
49
extern bool ac_trie_prepare (TRIE *trie);
1 by brian
clean slate
50
extern void ac_trie_init (TRIE *trie, AC_TRIE_STATE *state);
51
52
53
/* `trie_goto' is internal function and shouldn't be used. */
54
55
static inline TRIE_NODE *trie_goto (TRIE_NODE *root, TRIE_NODE *node, uchar c)
56
{
57
  TRIE_NODE *next;
58
  DBUG_ENTER("trie_goto");
59
  for (next= node->links; next; next= next->next)
60
    if (next->c == c)
61
      DBUG_RETURN(next);
62
  if (root == node)
63
    DBUG_RETURN(root);
64
  DBUG_RETURN(NULL);
65
}
66
67
68
/*
69
  SYNOPSIS
70
    int ac_trie_next (AC_TRIE_STATE *state, uchar *c);
71
    state - valid pointer to `AC_TRIE_STATE'
72
    c - character to lookup
73
74
  DESCRIPTION
75
    Implementation of search using Aho-Corasick automaton.
76
    Performs char-by-char search.
77
78
  RETURN VALUE
79
    `ac_trie_next' returns length of matched word or 0.
80
*/
81
82
static inline int ac_trie_next (AC_TRIE_STATE *state, uchar *c)
83
{
84
  TRIE_NODE *root, *node;
85
  DBUG_ENTER("ac_trie_next");
86
  DBUG_ASSERT(state && c);
87
  root= &state->trie->root;
88
  node= state->node;
89
  while (! (state->node= trie_goto(root, node, *c)))
90
    node= node->fail;
91
  DBUG_RETURN(state->node->leaf);
92
}
93
94
95
/*
96
  SYNOPSIS
146 by Brian Aker
my_bool cleanup.
97
    bool trie_search (TRIE *trie, const uchar *key, uint keylen);
1 by brian
clean slate
98
    trie - valid pointer to `TRIE'
99
    key - valid pointer to key to insert
100
    keylen - non-0 key length
101
102
  DESCRIPTION
103
    Performs key lookup in trie.
104
105
  RETURN VALUE
106
    `trie_search' returns `true' if key is in `trie'. Otherwise,
107
    `false' is returned.
108
109
  NOTES
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)
114
*/
115
146 by Brian Aker
my_bool cleanup.
116
static inline bool trie_search (TRIE *trie, const uchar *key, uint keylen)
1 by brian
clean slate
117
{
118
  TRIE_NODE *node;
119
  uint k;
120
  DBUG_ENTER("trie_search");
121
  DBUG_ASSERT(trie && key && keylen);
122
  node= &trie->root;
123
124
  for (k= 0; k < keylen; k++)
125
  {
126
    uchar p;
127
    if (! (node= node->links))
128
      DBUG_RETURN(FALSE);
129
    p= key[k];
130
    while (p != node->c)
131
      if (! (node= node->next))
132
        DBUG_RETURN(FALSE);
133
  }
134
135
  DBUG_RETURN(node->leaf > 0);
136
}
137
138
#ifdef	__cplusplus
139
}
140
#endif
141
#endif