2 * Knuth-Morris-Pratt's Substring Search for N given strings
4 * (c) 1999--2005, Robert Spalek <robert@ucw.cz>
5 * (c) 2006, Pavel Charvat <pchar@ucw.cz>
7 * (In fact, the algorithm is usually referred to as Aho-McCorasick,
8 * but that's just an extension of KMP to multiple strings.)
12 * This is not a normal header file, it's a generator of KMP algorithm.
13 * Each time you include it with parameters set in the corresponding
14 * preprocessor macros, it generates KMP structures and functions
15 * with the parameters given.
19 * KMP_PREFIX(x) macro to add a name prefix (used on all global names
20 * defined by the KMP generator); mandatory
22 * KMP_CHAR alphabet type, the default is u16
24 * KMP_SOURCE user-defined text source; KMP_GET_CHAR must
25 * KMP_GET_CHAR(ctx,src,c) return next character from the input or zero at the end;
26 * if not defined, zero-terminated array of bytes is used as the input
28 * KMP_NODE user-defined data in each state of the automaton
29 * KMP_CONTEXT user-defined data in struct context (a structure describing
30 * the whole automaton)
32 * Parameters which select how the input is interpreted (if KMP_SOURCE is unset):
33 * KMP_USE_ASCII reads single bytes from the input (default)
34 * KMP_USE_UTF8 reads UTF-8 characters from the input (valid UTF-8 needed)
35 * KMP_TOLOWER converts all to lowercase
36 * KMP_UNACCENT removes accents
37 * KMP_ONLYALPHA converts non-alphas to KMP_CONTROL_CHAR
38 * KMP_CONTROL_CHAR special control character (default is ':')
40 * Parameters controlling add():
41 * KMP_ADD_EXTRA_ARGS extra arguments
42 * KMP_ADD_EXTRA_VAR structure with extra local variables
43 * KMP_ADD_INIT(ctx,src,v)
44 * KMP_ADD_NEW(ctx,src,v,s)
45 * KMP_ADD_DUP(ctx,src,v,s)
46 * KMP_NO_DUPS no support for duplicates
48 * Parameters to build():
49 * KMP_BUILD_STATE(ctx,s) called for all states (including null) in order of non-decreasing tree depth
52 * KMP_WANT_CLEANUP define cleanup()
53 * KMP_WANT_SEARCH includes lib/kmp-search.h with the same prefix;
54 * there can be multiple search variants for a single KMP structure
56 * KMP_USE_POOL allocates in a given pool
60 #error Missing KMP_PREFIX
63 #include "lib/mempool.h"
67 #define P(x) KMP_PREFIX(x)
70 typedef KMP_CHAR P(char_t);
72 typedef u16 P(char_t);
78 typedef KMP_NODE P(node_t);
80 typedef struct {} P(node_t);
84 struct P(state) *from; /* state with previous character */
85 struct P(state) *back; /* backwards edge to the largest shorter state */
86 struct P(state) *next; /* largest shorter match */
87 P(len_t) len; /* largest match, zero otherwise */
88 P(char_t) c; /* last character */
89 P(node_t) n; /* user-defined data */
93 static inline P(char_t)
94 P(control_char) (void)
96 #ifdef KMP_CONTROL_CHAR
97 return KMP_CONTROL_CHAR;
103 /* User-defined source */
104 struct P(hash_table);
107 P(hash_hash) (struct P(hash_table) *t UNUSED, struct P(state) *f, P(char_t) c)
109 return (((uns)c) << 16) + (uns)(addr_int_t)f;
113 P(hash_eq) (struct P(hash_table) *t UNUSED, struct P(state) *f1, P(char_t) c1, struct P(state) *f2, P(char_t) c2)
115 return f1 == f2 && c1 == c2;
119 P(hash_init_key) (struct P(hash_table) *t UNUSED, struct P(state) *s, struct P(state) *f, P(char_t) c)
121 bzero(s, sizeof(*s));
124 s->next = f->back; /* the pointers hold the link-list of sons... changed in build() */
129 #define HASH_PREFIX(x) KMP_PREFIX(GLUE(hash_,x))
130 #define HASH_NODE struct KMP_PREFIX(state)
131 #define HASH_KEY_COMPLEX(x) x from, x c
132 #define HASH_KEY_DECL struct KMP_PREFIX(state) *from, KMP_PREFIX(char_t) c
133 #define HASH_WANT_NEW
134 #define HASH_WANT_FIND
135 #ifdef KMP_WANT_CLEANUP
136 #define HASH_WANT_CLEANUP
138 #define HASH_GIVE_HASHFN
140 #define HASH_GIVE_INIT_KEY
141 #if defined(KMP_USE_POOL)
142 #define HASH_USE_POOL KMP_USE_POOL
143 #elif defined(KMP_PARAM_POOL)
144 #define HASH_PARAM_POOL
146 #define HASH_AUTO_POOL 4096
148 #define HASH_CONSERVE_SPACE
149 #define HASH_TABLE_DYNAMIC
150 #include "lib/hashtable.h"
151 #define P(x) KMP_PREFIX(x)
154 struct P(hash_table) hash; /* hash table of state transitions */
155 struct P(state) null; /* null state */
157 KMP_CONTEXT v; /* user defined data */
162 typedef KMP_SOURCE P(source_t);
164 typedef byte *P(source_t);
169 P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src UNUSED, P(char_t) *c UNUSED)
171 return KMP_GET_CHAR(ctx, (*src), (*c));
174 # if defined(KMP_USE_UTF8)
175 # include "lib/unicode.h"
176 # if defined(KMP_ONLYALPHA) || defined(KMP_TOLOWER) || defined(KMP_UNACCENT)
177 # include "charset/unicat.h"
179 # elif defined(KMP_USE_ASCII)
180 # if defined(KMP_ONLYALPHA) || defined(KMP_TOLOWER)
181 # include "lib/chartype.h"
185 P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src, P(char_t) *c)
189 *src = (byte *)utf8_get(*src, &cc);
190 # ifdef KMP_ONLYALPHA
192 else if (!Ualpha(cc))
193 cc = P(control_char)();
206 # ifdef KMP_ONLYALPHA
208 else if (!Calpha(cc))
209 cc = P(control_char)();
216 # error Do not know how to unaccent ASCII characters
224 static struct P(state) *
225 P(add) (struct P(context) *ctx, P(source_t) src
226 # ifdef KMP_ADD_EXTRA_ARGS
231 # ifdef KMP_ADD_EXTRA_VAR
235 { KMP_ADD_INIT(ctx, src, v); }
239 if (!P(get_char)(ctx, &src, &c))
241 struct P(state) *p = &ctx->null, *s;
245 s = P(hash_find)(&ctx->hash, p, c);
249 s = P(hash_new)(&ctx->hash, p, c);
251 if (!(P(get_char)(ctx, &src, &c)))
258 while (P(get_char)(ctx, &src, &c));
265 { KMP_ADD_DUP(ctx, src, v, s); }
273 { KMP_ADD_NEW(ctx, src, v, s); }
279 P(init) (struct P(context) *ctx
280 # ifdef KMP_PARAM_POOL
281 , struct mempool *pool
285 bzero(ctx, sizeof(*ctx));
286 P(hash_init)(&ctx->hash
287 # ifdef KMP_PARAM_POOL
293 #ifdef KMP_WANT_CLEANUP
295 P(cleanup) (struct P(context) *ctx)
297 P(hash_cleanup)(&ctx->hash);
302 P(empty) (struct P(context) *ctx)
304 return !ctx->hash.hash_count;
307 static inline struct P(state) *
308 P(chain_start) (struct P(state) *s)
310 return s->len ? s : s->next;
314 P(build) (struct P(context) *ctx)
318 uns read = 0, write = 0;
319 struct P(state) *fifo[ctx->hash.hash_count], *null = &ctx->null;
320 for (struct P(state) *s = null->back; s; s = s->next)
323 # ifdef KMP_BUILD_STATE
324 { KMP_BUILD_STATE(ctx, null); }
326 while (read != write)
328 struct P(state) *s = fifo[read++], *t;
329 for (t = s->back; t; t = t->next)
331 for (t = s->from->back; 1; t = t->back)
339 s->back = P(hash_find)(&ctx->hash, t, s->c);
342 s->next = s->back->len ? s->back : s->back->next;
346 # ifdef KMP_BUILD_STATE
347 { KMP_BUILD_STATE(ctx, s); }
363 #undef KMP_CONTROL_CHAR
364 #undef KMP_ADD_EXTRA_ARGS
365 #undef KMP_ADD_EXTRA_VAR
370 #undef KMP_BUILD_STATE
372 #undef KMP_PARAM_POOL
374 #ifdef KMP_WANT_SEARCH
375 # undef KMP_WANT_SEARCH
376 # define KMPS_PREFIX(x) KMP_PREFIX(x)
377 # define KMPS_KMP_PREFIX(x) KMP_PREFIX(x)
378 # include "lib/kmp-search.h"