~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to include/my_trie.h

Merge/fix in FAQ.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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);
 
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);
 
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
  for (next= node->links; next; next= next->next)
 
59
    if (next->c == c)
 
60
      return(next);
 
61
  if (root == node)
 
62
    return(root);
 
63
  return(NULL);
 
64
}
 
65
 
 
66
 
 
67
/*
 
68
  SYNOPSIS
 
69
    int ac_trie_next (AC_TRIE_STATE *state, uchar *c);
 
70
    state - valid pointer to `AC_TRIE_STATE'
 
71
    c - character to lookup
 
72
 
 
73
  DESCRIPTION
 
74
    Implementation of search using Aho-Corasick automaton.
 
75
    Performs char-by-char search.
 
76
 
 
77
  RETURN VALUE
 
78
    `ac_trie_next' returns length of matched word or 0.
 
79
*/
 
80
 
 
81
static inline int ac_trie_next (AC_TRIE_STATE *state, uchar *c)
 
82
{
 
83
  TRIE_NODE *root, *node;
 
84
  assert(state && c);
 
85
  root= &state->trie->root;
 
86
  node= state->node;
 
87
  while (! (state->node= trie_goto(root, node, *c)))
 
88
    node= node->fail;
 
89
  return(state->node->leaf);
 
90
}
 
91
 
 
92
 
 
93
/*
 
94
  SYNOPSIS
 
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
 
99
 
 
100
  DESCRIPTION
 
101
    Performs key lookup in trie.
 
102
 
 
103
  RETURN VALUE
 
104
    `trie_search' returns `true' if key is in `trie'. Otherwise,
 
105
    `false' is returned.
 
106
 
 
107
  NOTES
 
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)
 
112
*/
 
113
 
 
114
static inline bool trie_search (TRIE *trie, const uchar *key, uint keylen)
 
115
{
 
116
  TRIE_NODE *node;
 
117
  uint k;
 
118
  assert(trie && key && keylen);
 
119
  node= &trie->root;
 
120
 
 
121
  for (k= 0; k < keylen; k++)
 
122
  {
 
123
    uchar p;
 
124
    if (! (node= node->links))
 
125
      return(false);
 
126
    p= key[k];
 
127
    while (p != node->c)
 
128
      if (! (node= node->next))
 
129
        return(false);
 
130
  }
 
131
 
 
132
  return(node->leaf > 0);
 
133
}
 
134
 
 
135
#ifdef  __cplusplus
 
136
}
 
137
#endif
 
138
#endif