File: camel-trie.c

package info (click to toggle)
evolution-data-server 3.22.7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,116 kB
  • ctags: 41,630
  • sloc: ansic: 302,318; makefile: 4,890; sh: 4,182; cpp: 472; xml: 462; perl: 368; python: 71
file content (395 lines) | stat: -rw-r--r-- 7,718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "camel-memchunk.h"
#include "camel-trie.h"

#define d(x)

struct _trie_state {
	struct _trie_state *next;
	struct _trie_state *fail;
	struct _trie_match *match;
	guint final;
	gint id;
};

struct _trie_match {
	struct _trie_match *next;
	struct _trie_state *state;
	gunichar c;
};

/**
 * CamelTrie:
 *
 * A trie data structure.
 *
 * Since: 2.24
 **/
struct _CamelTrie {
	struct _trie_state root;
	GPtrArray *fail_states;
	gboolean icase;

	CamelMemChunk *match_chunks;
	CamelMemChunk *state_chunks;
};

static inline gunichar
trie_utf8_getc (const guchar **in,
                gsize inlen)
{
	register const guchar *inptr = *in;
	const guchar *inend = inptr + inlen;
	register guchar c, r;
	register gunichar u, m;

	if (inlen == 0)
		return 0;

	r = *inptr++;
	if (r < 0x80) {
		*in = inptr;
		u = r;
	} else if (r < 0xfe) { /* valid start char? */
		u = r;
		m = 0x7f80;	/* used to mask out the length bits */
		do {
			if (inptr >= inend)
				return 0;

			c = *inptr++;
			if ((c & 0xc0) != 0x80)
				goto error;

			u = (u << 6) | (c & 0x3f);
			r <<= 1;
			m <<= 5;
		} while (r & 0x40);

		*in = inptr;

		u &= ~m;
	} else {
	error:
		*in = (*in)+1;
		u = 0xfffe;
	}

	return u;
}

/**
 * camel_trie_new:
 * @icase: Case sensitivity for the #CamelTrie.
 *
 * Creates a new #CamelTrie. If @icase is %TRUE, then pattern matching
 * done by the CamelTrie will be case insensitive.
 *
 * Returns: The newly-created #CamelTrie.
 *
 * Since: 2.24
 **/
CamelTrie *
camel_trie_new (gboolean icase)
{
	CamelTrie *trie;

	trie = g_new (CamelTrie, 1);
	trie->root.next = NULL;
	trie->root.fail = NULL;
	trie->root.match = NULL;
	trie->root.final = 0;

	trie->fail_states = g_ptr_array_sized_new (8);
	trie->icase = icase;

	trie->match_chunks = camel_memchunk_new (8, sizeof (struct _trie_match));
	trie->state_chunks = camel_memchunk_new (8, sizeof (struct _trie_state));

	return trie;
}

/**
 * camel_trie_free:
 * @trie: The #CamelTrie to free.
 *
 * Frees the memory associated with the #CamelTrie @trie.
 *
 * Since: 2.24
 **/
void
camel_trie_free (CamelTrie *trie)
{
	g_ptr_array_free (trie->fail_states, TRUE);
	camel_memchunk_destroy (trie->match_chunks);
	camel_memchunk_destroy (trie->state_chunks);
	g_free (trie);
}

static struct _trie_match *
g (struct _trie_state *s,
   gunichar c)
{
	struct _trie_match *m = s->match;

	while (m && m->c != c)
		m = m->next;

	return m;
}

static struct _trie_state *
trie_insert (CamelTrie *trie,
             gint depth,
             struct _trie_state *q,
             gunichar c)
{
	struct _trie_match *m;

	m = camel_memchunk_alloc (trie->match_chunks);
	m->next = q->match;
	m->c = c;

	q->match = m;
	q = m->state = camel_memchunk_alloc (trie->state_chunks);
	q->match = NULL;
	q->fail = &trie->root;
	q->final = 0;
	q->id = -1;

	if (trie->fail_states->len < depth + 1) {
		guint size = trie->fail_states->len;

		size = MAX (size + 64, depth + 1);
		g_ptr_array_set_size (trie->fail_states, size);
	}

	q->next = trie->fail_states->pdata[depth];
	trie->fail_states->pdata[depth] = q;

	return q;
}

#if d(!)0
static void
dump_trie (struct _trie_state *s,
           gint depth)
{
	gchar *p = g_alloca ((depth * 2) + 1);
	struct _trie_match *m;

	memset (p, ' ', depth * 2);
	p[depth * 2] = '\0';

	fprintf (
		stderr, "%s[state] %p: final=%d; pattern-id=%d; fail=%p\n",
		p, s, s->final, s->id, s->fail);
	m = s->match;
	while (m) {
		fprintf (stderr, " %s'%c' -> %p\n", p, m->c, m->state);
		if (m->state)
			dump_trie (m->state, depth + 1);

		m = m->next;
	}
}
#endif

/*
 * final = empty set
 * FOR p = 1 TO #pat
 *   q = root
 *   FOR j = 1 TO m[p]
 *     IF g(q, pat[p][j]) == null
 *       insert(q, pat[p][j])
 *     ENDIF
 *     q = g(q, pat[p][j])
 *   ENDFOR
 *   final = union(final, q)
 * ENDFOR
*/

/**
 * camel_trie_add:
 * @trie: The #CamelTrie to add a pattern to.
 * @pattern: The pattern to add.
 * @pattern_id: The id to use for the pattern.
 *
 * Add a new pattern to the #CamelTrie @trie.
 *
 * Since: 2.24
 **/
void
camel_trie_add (CamelTrie *trie,
                const gchar *pattern,
                gint pattern_id)
{
	const guchar *inptr = (const guchar *) pattern;
	struct _trie_state *q, *q1, *r;
	struct _trie_match *m, *n;
	gint i, depth = 0;
	gunichar c;

	/* Step 1: add the pattern to the trie */

	q = &trie->root;

	while ((c = trie_utf8_getc (&inptr, -1))) {
		if (trie->icase)
			c = g_unichar_tolower (c);

		m = g (q, c);
		if (m == NULL) {
			q = trie_insert (trie, depth, q, c);
		} else {
			q = m->state;
		}

		depth++;
	}

	q->final = depth;
	q->id = pattern_id;

	/* Step 2: compute failure graph */

	for (i = 0; i < trie->fail_states->len; i++) {
		q = trie->fail_states->pdata[i];
		while (q) {
			m = q->match;
			while (m) {
				c = m->c;
				q1 = m->state;
				r = q->fail;
				while (r && (n = g (r, c)) == NULL)
					r = r->fail;

				if (r != NULL) {
					q1->fail = n->state;
					if (q1->fail->final > q1->final)
						q1->final = q1->fail->final;
				} else {
					if ((n = g (&trie->root, c)))
						q1->fail = n->state;
					else
						q1->fail = &trie->root;
				}

				m = m->next;
			}

			q = q->next;
		}
	}

	d (fprintf (stderr, "\nafter adding pattern '%s' to trie %p:\n", pattern, trie));
	d (dump_trie (&trie->root, 0));
}

/*
 * Aho-Corasick
 *
 * q = root
 * FOR i = 1 TO n
 *   WHILE q != fail AND g(q, text[i]) == fail
 *     q = h(q)
 *   ENDWHILE
 *   IF q == fail
 *     q = root
 *   ELSE
 *     q = g(q, text[i])
 *   ENDIF
 *   IF isElement(q, final)
 *     RETURN TRUE
 *   ENDIF
 * ENDFOR
 * RETURN FALSE
 */

/**
 * camel_trie_search:
 * @trie: The #CamelTrie to search in.
 * @buffer: The string to match against a pattern in @trie.
 * @buflen: The length of @buffer.
 * @matched_id: An integer address to store the matched pattern id in.
 *
 * Try to match the string @buffer with a pattern in @trie.
 *
 * Returns: The matched pattern, or %NULL if no pattern is matched.
 *
 * Since: 2.24
 **/
const gchar *
camel_trie_search (CamelTrie *trie,
                   const gchar *buffer,
                   gsize buflen,
                   gint *matched_id)
{
	const guchar *inptr, *inend, *prev, *pat;
	register gsize inlen = buflen;
	struct _trie_state *q;
	struct _trie_match *m = NULL; /* init to please gcc */
	gunichar c;

	inptr = (const guchar *) buffer;
	inend = inptr + buflen;

	q = &trie->root;
	pat = prev = inptr;
	while ((c = trie_utf8_getc (&inptr, inlen))) {
		inlen = (inend - inptr);

		if (c != 0xfffe) {
			if (trie->icase)
				c = g_unichar_tolower (c);

			while (q != NULL && (m = g (q, c)) == NULL)
				q = q->fail;

			if (q == &trie->root)
				pat = prev;

			if (q == NULL) {
				q = &trie->root;
				pat = inptr;
			} else if (m != NULL) {
				q = m->state;

				if (q->final) {
					if (matched_id)
						*matched_id = q->id;

					return (const gchar *) pat;
				}
			}
		}

		prev = inptr;
	}

	return NULL;
}