/External/Mysql-5.0/include/my_trie.h

http://awoe.googlecode.com/ · C++ Header · 141 lines · 76 code · 17 blank · 48 comment · 14 complexity · 05ba1fa605bf9d7d63d6d4135b1ea41a MD5 · raw file

  1. /* Copyright (C) 2005 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #ifndef _trie_h
  13. #define _trie_h
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef struct st_trie_node
  18. {
  19. uint16 leaf; /* Depth from root node if match, 0 else */
  20. uchar c; /* Label on this edge */
  21. struct st_trie_node *next; /* Next label */
  22. struct st_trie_node *links; /* Array of edges leaving this node */
  23. struct st_trie_node *fail; /* AC failure function */
  24. } TRIE_NODE;
  25. typedef struct st_trie
  26. {
  27. TRIE_NODE root;
  28. MEM_ROOT mem_root;
  29. CHARSET_INFO *charset;
  30. uint32 nnodes;
  31. uint32 nwords;
  32. } TRIE;
  33. typedef struct st_ac_trie_state
  34. {
  35. TRIE *trie;
  36. TRIE_NODE *node;
  37. } AC_TRIE_STATE;
  38. extern TRIE *trie_init (TRIE *trie, CHARSET_INFO *charset);
  39. extern void trie_free (TRIE *trie);
  40. extern my_bool trie_insert (TRIE *trie, const uchar *key, uint keylen);
  41. extern my_bool ac_trie_prepare (TRIE *trie);
  42. extern void ac_trie_init (TRIE *trie, AC_TRIE_STATE *state);
  43. /* `trie_goto' is internal function and shouldn't be used. */
  44. static inline TRIE_NODE *trie_goto (TRIE_NODE *root, TRIE_NODE *node, uchar c)
  45. {
  46. TRIE_NODE *next;
  47. DBUG_ENTER("trie_goto");
  48. for (next= node->links; next; next= next->next)
  49. if (next->c == c)
  50. DBUG_RETURN(next);
  51. if (root == node)
  52. DBUG_RETURN(root);
  53. DBUG_RETURN(NULL);
  54. }
  55. /*
  56. SYNOPSIS
  57. int ac_trie_next (AC_TRIE_STATE *state, uchar *c);
  58. state - valid pointer to `AC_TRIE_STATE'
  59. c - character to lookup
  60. DESCRIPTION
  61. Implementation of search using Aho-Corasick automaton.
  62. Performs char-by-char search.
  63. RETURN VALUE
  64. `ac_trie_next' returns length of matched word or 0.
  65. */
  66. static inline int ac_trie_next (AC_TRIE_STATE *state, uchar *c)
  67. {
  68. TRIE_NODE *root, *node;
  69. DBUG_ENTER("ac_trie_next");
  70. DBUG_ASSERT(state && c);
  71. root= &state->trie->root;
  72. node= state->node;
  73. while (! (state->node= trie_goto(root, node, *c)))
  74. node= node->fail;
  75. DBUG_RETURN(state->node->leaf);
  76. }
  77. /*
  78. SYNOPSIS
  79. my_bool trie_search (TRIE *trie, const uchar *key, uint keylen);
  80. trie - valid pointer to `TRIE'
  81. key - valid pointer to key to insert
  82. keylen - non-0 key length
  83. DESCRIPTION
  84. Performs key lookup in trie.
  85. RETURN VALUE
  86. `trie_search' returns `true' if key is in `trie'. Otherwise,
  87. `false' is returned.
  88. NOTES
  89. Consecutive search here is "best by test". arrays are very short, so
  90. binary search or hashing would add too much complexity that would
  91. overweight speed gain. Especially because compiler can optimize simple
  92. consecutive loop better (tested)
  93. */
  94. static inline my_bool trie_search (TRIE *trie, const uchar *key, uint keylen)
  95. {
  96. TRIE_NODE *node;
  97. uint k;
  98. DBUG_ENTER("trie_search");
  99. DBUG_ASSERT(trie && key && keylen);
  100. node= &trie->root;
  101. for (k= 0; k < keylen; k++)
  102. {
  103. uchar p;
  104. if (! (node= node->links))
  105. DBUG_RETURN(FALSE);
  106. p= key[k];
  107. while (p != node->c)
  108. if (! (node= node->next))
  109. DBUG_RETURN(FALSE);
  110. }
  111. DBUG_RETURN(node->leaf > 0);
  112. }
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif