📄 kwset.c
字号:
/* kwset.c - search for any of a set of keywords. Copyright 1989, 1998, 2000 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Written August 1989 by Mike Haertel. The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. *//* The algorithm implemented by these routines bears a startling resemblence to one discovered by Beate Commentz-Walter, although it is not identical. See "A String Matching Algorithm Fast on the Average," Technical Report, IBM-Germany, Scientific Center Heidelberg, Tiergartenstrasse 15, D-6900 Heidelberg, Germany. See also Aho, A.V., and M. Corasick, "Efficient String Matching: An Aid to Bibliographic Search," CACM June 1975, Vol. 18, No. 6, which describes the failure function used below. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <sys/types.h>#include "system.h"#include "kwset.h"#include "obstack.h"#ifdef GREPextern char *xmalloc();# undef malloc# define malloc xmalloc#endif#define NCHAR (UCHAR_MAX + 1)#define obstack_chunk_alloc malloc#define obstack_chunk_free free/* Balanced tree of edges and labels leaving a given trie node. */struct tree{ struct tree *llink; /* Left link; MUST be first field. */ struct tree *rlink; /* Right link (to larger labels). */ struct trie *trie; /* Trie node pointed to by this edge. */ unsigned char label; /* Label on this edge. */ char balance; /* Difference in depths of subtrees. */};/* Node of a trie representing a set of reversed keywords. */struct trie{ unsigned int accepting; /* Word index of accepted word, or zero. */ struct tree *links; /* Tree of edges leaving this node. */ struct trie *parent; /* Parent of this node. */ struct trie *next; /* List of all trie nodes in level order. */ struct trie *fail; /* Aho-Corasick failure function. */ int depth; /* Depth of this node from the root. */ int shift; /* Shift function for search failures. */ int maxshift; /* Max shift of self and descendents. */};/* Structure returned opaquely to the caller, containing everything. */struct kwset{ struct obstack obstack; /* Obstack for node allocation. */ int words; /* Number of words in the trie. */ struct trie *trie; /* The trie itself. */ int mind; /* Minimum depth of an accepting node. */ int maxd; /* Maximum depth of any node. */ unsigned char delta[NCHAR]; /* Delta table for rapid search. */ struct trie *next[NCHAR]; /* Table of children of the root. */ char *target; /* Target string if there's only one. */ int mind2; /* Used in Boyer-Moore search for one string. */ char const *trans; /* Character translation table. */};/* Allocate and initialize a keyword set object, returning an opaque pointer to it. Return NULL if memory is not available. */kwset_tkwsalloc (char const *trans){ struct kwset *kwset; kwset = (struct kwset *) malloc(sizeof (struct kwset)); if (!kwset) return 0; obstack_init(&kwset->obstack); kwset->words = 0; kwset->trie = (struct trie *) obstack_alloc(&kwset->obstack, sizeof (struct trie)); if (!kwset->trie) { kwsfree((kwset_t) kwset); return 0; } kwset->trie->accepting = 0; kwset->trie->links = 0; kwset->trie->parent = 0; kwset->trie->next = 0; kwset->trie->fail = 0; kwset->trie->depth = 0; kwset->trie->shift = 0; kwset->mind = INT_MAX; kwset->maxd = -1; kwset->target = 0; kwset->trans = trans; return (kwset_t) kwset;}/* Add the given string to the contents of the keyword set. Return NULL for success, an error message otherwise. */char *kwsincr (kwset_t kws, char const *text, size_t len){ struct kwset *kwset; register struct trie *trie; register unsigned char label; register struct tree *link; register int depth; struct tree *links[12]; enum { L, R } dirs[12]; struct tree *t, *r, *l, *rl, *lr; kwset = (struct kwset *) kws; trie = kwset->trie; text += len; /* Descend the trie (built of reversed keywords) character-by-character, installing new nodes when necessary. */ while (len--) { label = kwset->trans ? kwset->trans[(unsigned char) *--text] : *--text; /* Descend the tree of outgoing links for this trie node, looking for the current character and keeping track of the path followed. */ link = trie->links; links[0] = (struct tree *) &trie->links; dirs[0] = L; depth = 1; while (link && label != link->label) { links[depth] = link; if (label < link->label) dirs[depth++] = L, link = link->llink; else dirs[depth++] = R, link = link->rlink; } /* The current character doesn't have an outgoing link at this trie node, so build a new trie node and install a link in the current trie node's tree. */ if (!link) { link = (struct tree *) obstack_alloc(&kwset->obstack, sizeof (struct tree)); if (!link) return _("memory exhausted"); link->llink = 0; link->rlink = 0; link->trie = (struct trie *) obstack_alloc(&kwset->obstack, sizeof (struct trie)); if (!link->trie) return _("memory exhausted"); link->trie->accepting = 0; link->trie->links = 0; link->trie->parent = trie; link->trie->next = 0; link->trie->fail = 0; link->trie->depth = trie->depth + 1; link->trie->shift = 0; link->label = label; link->balance = 0; /* Install the new tree node in its parent. */ if (dirs[--depth] == L) links[depth]->llink = link; else links[depth]->rlink = link; /* Back up the tree fixing the balance flags. */ while (depth && !links[depth]->balance) { if (dirs[depth] == L) --links[depth]->balance; else ++links[depth]->balance; --depth; } /* Rebalance the tree by pointer rotations if necessary. */ if (depth && ((dirs[depth] == L && --links[depth]->balance) || (dirs[depth] == R && ++links[depth]->balance))) { switch (links[depth]->balance) { case (char) -2: switch (dirs[depth + 1]) { case L: r = links[depth], t = r->llink, rl = t->rlink; t->rlink = r, r->llink = rl; t->balance = r->balance = 0; break; case R: r = links[depth], l = r->llink, t = l->rlink; rl = t->rlink, lr = t->llink; t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; l->balance = t->balance != 1 ? 0 : -1; r->balance = t->balance != (char) -1 ? 0 : 1; t->balance = 0; break; default: abort (); } break; case 2: switch (dirs[depth + 1]) { case R: l = links[depth], t = l->rlink, lr = t->llink; t->llink = l, l->rlink = lr; t->balance = l->balance = 0; break; case L: l = links[depth], r = l->rlink, t = r->llink; lr = t->llink, rl = t->rlink; t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; l->balance = t->balance != 1 ? 0 : -1; r->balance = t->balance != (char) -1 ? 0 : 1; t->balance = 0; break; default: abort (); } break; default: abort (); } if (dirs[depth - 1] == L) links[depth - 1]->llink = t; else links[depth - 1]->rlink = t; } } trie = link->trie; } /* Mark the node we finally reached as accepting, encoding the index number of this word in the keyword set so far. */ if (!trie->accepting) trie->accepting = 1 + 2 * kwset->words; ++kwset->words; /* Keep track of the longest and shortest string of the keyword set. */ if (trie->depth < kwset->mind) kwset->mind = trie->depth; if (trie->depth > kwset->maxd) kwset->maxd = trie->depth; return 0;}/* Enqueue the trie nodes referenced from the given tree in the given queue. */static voidenqueue (struct tree *tree, struct trie **last){ if (!tree) return; enqueue(tree->llink, last); enqueue(tree->rlink, last); (*last) = (*last)->next = tree->trie;}/* Compute the Aho-Corasick failure function for the trie nodes referenced from the given tree, given the failure function for their parent as well as a last resort failure node. */static voidtreefails (register struct tree const *tree, struct trie const *fail, struct trie *recourse){ register struct tree *link; if (!tree) return; treefails(tree->llink, fail, recourse); treefails(tree->rlink, fail, recourse); /* Find, in the chain of fails going back to the root, the first node that has a descendent on the current label. */ while (fail) { link = fail->links; while (link && tree->label != link->label) if (tree->label < link->label) link = link->llink; else link = link->rlink; if (link) { tree->trie->fail = link->trie; return; } fail = fail->fail; } tree->trie->fail = recourse;}/* Set delta entries for the links of the given tree such that the preexisting delta value is larger than the current depth. */static voidtreedelta (register struct tree const *tree, register unsigned int depth, unsigned char delta[]){ if (!tree) return; treedelta(tree->llink, depth, delta); treedelta(tree->rlink, depth, delta); if (depth < delta[tree->label]) delta[tree->label] = depth;}/* Return true if A has every label in B. */static inthasevery (register struct tree const *a, register struct tree const *b){ if (!b) return 1; if (!hasevery(a, b->llink)) return 0; if (!hasevery(a, b->rlink)) return 0; while (a && b->label != a->label) if (b->label < a->label) a = a->llink; else a = a->rlink; return !!a;}/* Compute a vector, indexed by character code, of the trie nodes referenced from the given tree. */static voidtreenext (struct tree const *tree, struct trie *next[]){ if (!tree) return; treenext(tree->llink, next); treenext(tree->rlink, next); next[tree->label] = tree->trie;}/* Compute the shift for each trie node, as well as the delta table and next cache for the given keyword set. */char *kwsprep (kwset_t kws){ register struct kwset *kwset; register int i; register struct trie *curr, *fail; register char const *trans; unsigned char delta[NCHAR]; struct trie *last, *next[NCHAR]; kwset = (struct kwset *) kws; /* Initial values for the delta table; will be changed later. The
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -