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
64 #error Missing KMP_PREFIX
67 #include "lib/mempool.h"
71 #define P(x) KMP_PREFIX(x)
74 typedef KMP_CHAR P(char_t);
76 typedef u16 P(char_t);
82 typedef KMP_NODE P(node_t);
84 typedef struct {} P(node_t);
90 struct P(state) *from; /* state with previous character */
91 struct P(state) *back; /* backwards edge to the largest shorter state */
92 struct P(state) *next; /* largest shorter match */
93 P(len_t) len; /* largest match, zero otherwise */
94 P(char_t) c; /* last character */
95 P(node_t) n; /* user-defined data */
99 static inline P(char_t)
102 #ifdef KMP_CONTROL_CHAR
103 return KMP_CONTROL_CHAR;
109 /* User-defined source */
110 struct P(hash_table);
112 #define HASH_GIVE_HASHFN
113 #ifdef KMP_GIVE_HASHFN
115 P(hash_hash) (struct P(hash_table) *t, struct P(state) *f, P(char_t) c)
117 return P(hash) ((struct P(context) *) t, f, c);
121 P(hash_hash) (struct P(hash_table) *t UNUSED, struct P(state) *f, P(char_t) c)
123 return (((uns)c) << 16) + (uns)(addr_int_t)f;
129 P(eq) (struct P(context) *ctx UNUSED, P(char_t) c1, P(char_t) c2)
136 P(is_control) (struct P(context) *ctx, P(char_t) c)
138 return P(eq) (ctx, c, P(control)());
143 P(hash_eq) (struct P(hash_table) *t, struct P(state) *f1, P(char_t) c1, struct P(state) *f2, P(char_t) c2)
145 return f1 == f2 && P(eq)((struct P(context) *) t, c1, c2);
148 #ifdef KMP_GIVE_ALLOC
149 #define HASH_GIVE_ALLOC
151 P(hash_alloc) (struct P(hash_table) *t, uns size)
153 return P(alloc) ((struct P(context) *) t, size);
157 P(hash_free) (struct P(hash_table) *t, void *ptr)
159 P(free) ((struct P(context) *) t, ptr);
163 #define HASH_GIVE_INIT_KEY
165 P(hash_init_key) (struct P(hash_table) *t UNUSED, struct P(state) *s, struct P(state) *f, P(char_t) c)
167 bzero(s, sizeof(*s));
170 s->next = f->back; /* the pointers hold the link-list of sons... changed in build() */
175 #define HASH_PREFIX(x) KMP_PREFIX(GLUE(hash_,x))
176 #define HASH_NODE struct KMP_PREFIX(state)
177 #define HASH_KEY_COMPLEX(x) x from, x c
178 #define HASH_KEY_DECL struct KMP_PREFIX(state) *from, KMP_PREFIX(char_t) c
179 #define HASH_WANT_NEW
180 #define HASH_WANT_FIND
181 #ifdef KMP_WANT_CLEANUP
182 #define HASH_WANT_CLEANUP
184 #if defined(KMP_USE_POOL)
185 #define HASH_USE_POOL KMP_USE_POOL
187 #define HASH_AUTO_POOL 4096
189 #define HASH_CONSERVE_SPACE
190 #define HASH_TABLE_DYNAMIC
191 #include "lib/hashtable.h"
192 #define P(x) KMP_PREFIX(x)
195 struct P(hash_table) hash; /* hash table of state transitions */
196 struct P(state) null; /* null state */
198 KMP_CONTEXT v; /* user defined data */
203 typedef KMP_SOURCE P(source_t);
205 typedef byte *P(source_t);
210 P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src UNUSED, P(char_t) *c UNUSED)
212 return KMP_GET_CHAR(ctx, (*src), (*c));
215 # if defined(KMP_USE_UTF8)
216 # include "lib/unicode.h"
217 # if defined(KMP_ONLYALPHA) || defined(KMP_TOLOWER) || defined(KMP_UNACCENT)
218 # include "charset/unicat.h"
220 # elif defined(KMP_USE_ASCII)
221 # if defined(KMP_ONLYALPHA) || defined(KMP_TOLOWER)
222 # include "lib/chartype.h"
226 P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src, P(char_t) *c)
230 *src = (byte *)utf8_get(*src, &cc);
231 # ifdef KMP_ONLYALPHA
233 else if (!Ualpha(cc))
247 # ifdef KMP_ONLYALPHA
249 else if (!Calpha(cc))
257 # error Do not know how to unaccent ASCII characters
265 static struct P(state) *
266 P(add) (struct P(context) *ctx, P(source_t) src
267 # ifdef KMP_ADD_EXTRA_ARGS
272 # ifdef KMP_ADD_EXTRA_VAR
276 { KMP_ADD_INIT(ctx, src, v); }
280 if (!P(get_char)(ctx, &src, &c))
282 struct P(state) *p = &ctx->null, *s;
286 s = P(hash_find)(&ctx->hash, p, c);
290 s = P(hash_new)(&ctx->hash, p, c);
292 if (!(P(get_char)(ctx, &src, &c)))
299 while (P(get_char)(ctx, &src, &c));
306 { KMP_ADD_DUP(ctx, src, v, s); }
314 { KMP_ADD_NEW(ctx, src, v, s); }
320 P(init) (struct P(context) *ctx)
322 bzero(&ctx->null, sizeof(struct P(state)));
323 P(hash_init)(&ctx->hash);
326 #ifdef KMP_WANT_CLEANUP
328 P(cleanup) (struct P(context) *ctx)
330 P(hash_cleanup)(&ctx->hash);
335 P(empty) (struct P(context) *ctx)
337 return !ctx->hash.hash_count;
340 static inline struct P(state) *
341 P(chain_start) (struct P(state) *s)
343 return s->len ? s : s->next;
347 P(build) (struct P(context) *ctx)
351 uns read = 0, write = 0;
352 struct P(state) *fifo[ctx->hash.hash_count], *null = &ctx->null;
353 for (struct P(state) *s = null->back; s; s = s->next)
356 # ifdef KMP_BUILD_STATE
357 { KMP_BUILD_STATE(ctx, null); }
359 while (read != write)
361 struct P(state) *s = fifo[read++], *t;
362 for (t = s->back; t; t = t->next)
364 for (t = s->from->back; 1; t = t->back)
372 s->back = P(hash_find)(&ctx->hash, t, s->c);
375 s->next = s->back->len ? s->back : s->back->next;
379 # ifdef KMP_BUILD_STATE
380 { KMP_BUILD_STATE(ctx, s); }
396 #undef KMP_CONTROL_CHAR
397 #undef KMP_ADD_EXTRA_ARGS
398 #undef KMP_ADD_EXTRA_VAR
403 #undef KMP_BUILD_STATE
405 #undef KMP_GIVE_ALLOC
406 #undef KMP_GIVE_HASHFN
409 #ifdef KMP_WANT_SEARCH
410 # undef KMP_WANT_SEARCH
411 # define KMPS_PREFIX(x) KMP_PREFIX(x)
412 # define KMPS_KMP_PREFIX(x) KMP_PREFIX(x)
413 # include "lib/kmp-search.h"