home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / FGRE11AS.ZIP / KWSET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  18.5 KB  |  661 lines

  1. /* kwset.c - search for any of a set of keywords.
  2.    Copyright 1989 Free Software Foundation
  3.           Written August 1989 by Mike Haertel.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 1, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    The author may be reached (Email) at the address mike@ai.mit.edu,
  20.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  21.  
  22. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  23.    This port is also distributed under the terms of the
  24.    GNU General Public License as published by the
  25.    Free Software Foundation.
  26.  
  27.    Please note that this file is not identical to the
  28.    original GNU release, you should have received this
  29.    code as patch to the official release.
  30.  
  31.    $Header: e:/gnu/fgrep/RCS/kwset.c 1.1.0.3 90/09/24 00:37:52 tho Exp $
  32.  */
  33.  
  34. #include "std.h"
  35.  
  36. /* The algorithm implemented by these routines bears a startling resemblence
  37.    to one discovered by Beate Commentz-Walter, although it is not identical.
  38.    See "A String Matching Algorithm Fast on the Average," Technical Report,
  39.    IBM-Germany, Scientific Center Heidelberg, Tiergartenstrasse 15, D-6900
  40.    Heidelberg, Germany.  See also Aho, A.V., and M. Corasick, "Efficient
  41.    String Matching:  An Aid to Bibliographic Search," CACM June 1975,
  42.    Vol. 18, No. 6, which describes the failure function used below. */
  43.  
  44. #include "kwset.h"
  45.  
  46. #ifdef MSDOS
  47.  
  48. #ifdef USE_OBSTACKS
  49. #if defined(M_I86CM) || defined(M_I86LM)
  50. #error The GNU `obstack' macros don't work with far pointers yet!
  51. #else /* M_I86SM or M_I86MM */
  52. #include "obstack.h"
  53. #endif /* M_I86SM or M_I86MM */
  54. #else /* not USE_OBSTACKS */
  55. #undef obstack_alloc
  56. #undef obstack_free
  57. #undef obstack_init
  58. #define obstack_alloc(dummy, size)    xmalloc (size)
  59. #define obstack_free(dummy1, dummy2)
  60. #define obstack_init(dummy)
  61. #endif /* not USE_OBSTACKS */
  62.  
  63. #else /* not MSDOS */
  64.  
  65. #include "obstack.h"
  66.  
  67. #endif /* not MSDOS */
  68.  
  69.  
  70. #define NCHAR (UCHAR_MAX + 1)
  71. #ifdef MSDOS            /* walk on the save side */
  72. #define obstack_chunk_alloc xmalloc
  73. #else
  74. #define obstack_chunk_alloc malloc
  75. #endif
  76. #define obstack_chunk_free free
  77.  
  78. /* Balanced tree of edges and labels leaving a given trie node. */
  79. struct tree
  80. {
  81.   struct tree *llink;        /* Left link; MUST be first field. */
  82.   struct tree *rlink;        /* Right link (to larger labels). */
  83.   struct trie *trie;        /* Trie node pointed to by this edge. */
  84.   unsigned char label;        /* Label on this edge. */
  85.   char balance;            /* Difference in depths of subtrees. */
  86. };
  87.  
  88. /* Node of a trie representing a set of reversed keywords. */
  89. struct trie
  90. {
  91.   unsigned int accepting;    /* Word index of accepted word, or zero. */
  92.   struct tree *links;        /* Tree of edges leaving this node. */
  93.   struct trie *parent;        /* Parent of this node. */
  94.   struct trie *next;        /* List of all trie nodes in level order. */
  95.   struct trie *fail;        /* Aho-Corasick failure function. */
  96.   int depth;            /* Depth of this node from the root. */
  97.   int shift;            /* Shift function for search failures. */
  98.   int maxshift;            /* Max shift of self and descendents. */
  99. };
  100.  
  101. /* Structure returned opaquely to the caller, containing everything. */
  102. struct kwset
  103. {
  104. #if !defined (MSDOS) || defined (USE_OBSTACKS)
  105.   struct obstack obstack;    /* Obstack for node allocation. */
  106. #endif
  107.   int words;            /* Number of words in the trie. */
  108.   struct trie *trie;        /* The trie itself. */
  109.   int mind;            /* Minimum depth of an accepting node. */
  110.   int maxd;            /* Maximum depth of any node. */
  111.   int delta[NCHAR];        /* Delta table for rapid search. */
  112.   struct trie *next[NCHAR];    /* Table of children of the root. */
  113.   const char *trans;        /* Character translation table. */
  114. };
  115.  
  116. /* Allocate and initialize a keyword set object, returning an opaque
  117.    pointer to it.  Return NULL if memory is not available. */
  118. kwset_t
  119. DEFUN(kwsalloc, (trans), const char *trans)
  120. {
  121.   struct kwset *kwset;
  122.  
  123.   kwset = (struct kwset *) malloc(sizeof (struct kwset));
  124.   if (!kwset)
  125.     return NULL;
  126.  
  127.   obstack_init(&kwset->obstack);
  128.   kwset->words = 0;
  129.   kwset->trie
  130.     = (struct trie *) obstack_alloc(&kwset->obstack, sizeof (struct trie));
  131.   if (!kwset->trie)
  132.     {
  133.       kwsfree((kwset_t) kwset);
  134.       return NULL;
  135.     }
  136.   kwset->trie->accepting = 0;
  137.   kwset->trie->links = NULL;
  138.   kwset->trie->parent = NULL;
  139.   kwset->trie->next = NULL;
  140.   kwset->trie->fail = NULL;
  141.   kwset->trie->depth = 0;
  142.   kwset->trie->shift = 0;
  143.   kwset->mind = INT_MAX;
  144.   kwset->maxd = -1;
  145.   kwset->trans = trans;
  146.  
  147.   return (kwset_t) kwset;
  148. }
  149.  
  150. /* Add the given string to the contents of the keyword set.  Return NULL
  151.    for success, an error message otherwise. */
  152. const char *
  153. DEFUN(kwsincr, (kws, text, len),
  154.       kwset_t kws AND const char *text AND size_t len)
  155. {
  156.   struct kwset *kwset;
  157.   register struct trie *trie;
  158.   register unsigned char label;
  159.   register struct tree *link;
  160.   register int depth;
  161.   struct tree *links[12];
  162.   enum { L, R } dirs[12];
  163.   struct tree *t, *r, *l, *rl, *lr;
  164.  
  165.   kwset = (struct kwset *) kws;
  166.   trie = kwset->trie;
  167.   text += len;
  168.  
  169.   /* Descend the trie (built of reversed keywords) character-by-character,
  170.      installing new nodes when necessary. */
  171.   while (len--)
  172.     {
  173.       label = kwset->trans ? kwset->trans[(unsigned char) *--text] : *--text;
  174.  
  175.       /* Descend the tree of outgoing links for this trie node,
  176.      looking for the current character and keeping track
  177.      of the path followed. */
  178.       link = trie->links;
  179.       links[0] = (struct tree *) &trie->links;
  180.       dirs[0] = L;
  181.       depth = 1;
  182.  
  183.       while (link && label != link->label)
  184.     {
  185.       links[depth] = link;
  186.       if (label < link->label)
  187.         dirs[depth++] = L, link = link->llink;
  188.       else
  189.         dirs[depth++] = R, link = link->rlink;
  190.     }
  191.  
  192.       /* The current character doesn't have an outgoing link at
  193.      this trie node, so build a new trie node and install
  194.      a link in the current trie node's tree. */
  195.       if (!link)
  196.     {
  197.       link = (struct tree *) obstack_alloc(&kwset->obstack,
  198.                            sizeof (struct tree));
  199.       if (!link)
  200.         return "memory exhausted";
  201.       link->llink = NULL;
  202.       link->rlink = NULL;
  203.       link->trie = (struct trie *) obstack_alloc(&kwset->obstack,
  204.                              sizeof (struct trie));
  205.       if (!link->trie)
  206.         return "memory exhausted";
  207.       link->trie->accepting = 0;
  208.       link->trie->links = NULL;
  209.       link->trie->parent = trie;
  210.       link->trie->next = NULL;
  211.       link->trie->fail = NULL;
  212.       link->trie->depth = trie->depth + 1;
  213.       link->trie->shift = 0;
  214.       link->label = label;
  215.       link->balance = 0;
  216.  
  217.       /* Install the new tree node in its parent. */
  218.       if (dirs[--depth] == L)
  219.         links[depth]->llink = link;
  220.       else
  221.         links[depth]->rlink = link;
  222.  
  223.       /* Back up the tree fixing the balance flags. */
  224.       while (depth && !links[depth]->balance)
  225.         {
  226.           if (dirs[depth] == L)
  227.         --links[depth]->balance;
  228.           else
  229.         ++links[depth]->balance;
  230.           --depth;
  231.         }
  232.  
  233.       /* Rebalance the tree by pointer rotations if necessary. */
  234.       if (depth && (dirs[depth] == L && --links[depth]->balance
  235.             || dirs[depth] == R && ++links[depth]->balance))
  236.         {
  237.           switch (links[depth]->balance)
  238.         {
  239.         case (char) -2:
  240.           switch (dirs[depth + 1])
  241.             {
  242.             case L:
  243.               r = links[depth], t = r->llink, rl = t->rlink;
  244.               t->rlink = r, r->llink = rl;
  245.               t->balance = r->balance = 0;
  246.               break;
  247.             case R:
  248.               r = links[depth], l = r->llink, t = l->rlink;
  249.               rl = t->rlink, lr = t->llink;
  250.               t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
  251. #ifdef MSDOS
  252.               l->balance = (char) (t->balance != 1 ? 0 : -1);
  253.               r->balance = (char) (t->balance != (char) -1 ? 0 : 1);
  254. #else
  255.               l->balance = t->balance != 1 ? 0 : -1;
  256.               r->balance = t->balance != (char) -1 ? 0 : 1;
  257. #endif
  258.               t->balance = 0;
  259.               break;
  260.             }
  261.           break;
  262.         case 2:
  263.           switch (dirs[depth + 1])
  264.             {
  265.             case R:
  266.               l = links[depth], t = l->rlink, lr = t->llink;
  267.               t->llink = l, l->rlink = lr;
  268.               t->balance = l->balance = 0;
  269.               break;
  270.             case L:
  271.               l = links[depth], r = l->rlink, t = r->llink;
  272.               lr = t->llink, rl = t->rlink;
  273.               t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
  274. #ifdef MSDOS
  275.               l->balance = (char) (t->balance != 1 ? 0 : -1);
  276.               r->balance = (char) (t->balance != (char) -1 ? 0 : 1);
  277. #else
  278.               l->balance = t->balance != 1 ? 0 : -1;
  279.               r->balance = t->balance != (char) -1 ? 0 : 1;
  280. #endif
  281.               t->balance = 0;
  282.               break;
  283.             }
  284.           break;
  285.         }
  286.  
  287.           if (dirs[depth - 1] == L)
  288.         links[depth - 1]->llink = t;
  289.           else
  290.         links[depth - 1]->rlink = t;
  291.         }
  292.     }
  293.  
  294.       trie = link->trie;
  295.     }
  296.  
  297.   /* Mark the node we finally reached as accepting, encoding the
  298.      index number of this word in the keyword set so far. */
  299.   if (!trie->accepting)
  300.     trie->accepting = 1 + 2 * kwset->words;
  301.   ++kwset->words;
  302.  
  303.   /* Keep track of the longest and shortest string of the keyword set. */
  304.   if (trie->depth < kwset->mind)
  305.     kwset->mind = trie->depth;
  306.   if (trie->depth > kwset->maxd)
  307.     kwset->maxd = trie->depth;
  308.  
  309.   return NULL;
  310. }
  311.  
  312. /* Enqueue the trie nodes referenced from the given tree in the
  313.    given queue. */
  314. static void
  315. DEFUN(enqueue, (tree, last), struct tree *tree AND struct trie **last)
  316. {
  317.   if (!tree)
  318.     return;
  319.   enqueue(tree->llink, last);
  320.   enqueue(tree->rlink, last);
  321.   (*last) = (*last)->next = tree->trie;
  322. }
  323.  
  324. /* Compute the Aho-Corasick failure function for the trie nodes referenced
  325.    from the given tree, given the failure function for their parent as
  326.    well as a last resort failure node. */
  327. static void
  328. DEFUN(treefails, (tree, fail, recourse),
  329.       register struct tree *tree
  330.       AND struct trie *fail AND struct trie *recourse)
  331. {
  332.   register struct tree *link;
  333.  
  334.   if (!tree)
  335.     return;
  336.  
  337.   treefails(tree->llink, fail, recourse);
  338.   treefails(tree->rlink, fail, recourse);
  339.  
  340.   /* Find, in the chain of fails going back to the root, the first
  341.      node that has a descendent on the current label. */
  342.   while (fail)
  343.     {
  344.       link = fail->links;
  345.       while (link && tree->label != link->label)
  346.     if (tree->label < link->label)
  347.       link = link->llink;
  348.     else
  349.       link = link->rlink;
  350.       if (link)
  351.     {
  352.       tree->trie->fail = link->trie;
  353.       return;
  354.     }
  355.       fail = fail->fail;
  356.     }
  357.  
  358.   tree->trie->fail = recourse;
  359. }
  360.  
  361. /* Set delta entries for the links of the given tree such that
  362.    the preexisting delta value is larger than the current depth. */
  363. static void
  364. DEFUN(treedelta, (tree, depth, delta),
  365.       register struct tree *tree AND register int depth AND int delta[])
  366. {
  367.   if (!tree)
  368.     return;
  369.   treedelta(tree->llink, depth, delta);
  370.   treedelta(tree->rlink, depth, delta);
  371.   if (depth < delta[tree->label])
  372.     delta[tree->label] = depth;
  373. }
  374.  
  375. /* Return true if A has every label in B. */
  376. static int
  377. DEFUN(hasevery, (a, b), register struct tree *a AND register struct tree *b)
  378. {
  379.   if (!b)
  380.     return 1;
  381.   if (!hasevery(a, b->llink))
  382.     return 0;
  383.   if (!hasevery(a, b->rlink))
  384.     return 0;
  385.   while (a && b->label != a->label)
  386.     if (b->label < a->label)
  387.       a = a->llink;
  388.     else
  389.       a = a->rlink;
  390.   return !!a;
  391. }
  392.  
  393. /* Compute a vector, indexed by character code, of the trie nodes
  394.    referenced from the given tree. */
  395. static void
  396. DEFUN(treenext, (tree, next), struct tree *tree AND struct trie *next[])
  397. {
  398.   if (!tree)
  399.     return;
  400.   treenext(tree->llink, next);
  401.   treenext(tree->rlink, next);
  402.   next[tree->label] = tree->trie;
  403. }
  404.  
  405. /* Compute the shift for each trie node, as well as the delta
  406.    table and next cache for the given keyword set. */
  407. const char *
  408. DEFUN(kwsprep, (kws), kwset_t kws)
  409. {
  410.   register struct kwset *kwset;
  411.   register int i;
  412.   register struct trie *curr, *fail;
  413.   register const char *trans;
  414.   int delta[NCHAR];
  415.   struct trie *last, *next[NCHAR];
  416.  
  417.   kwset = (struct kwset *) kws;
  418.  
  419.   /* Initial values for the delta table; will be changed later.  The
  420.      delta entry for a given character is the smallest depth of any
  421.      node at which an outgoing edge is labeled by that character. */
  422.   for (i = 0; i < NCHAR; ++i)
  423.     delta[i] = kwset->mind;
  424.  
  425.   /* Traverse the nodes of the trie in level order, simultaneously
  426.      computing the delta table, failure function, and shift function. */
  427.   for (curr = last = kwset->trie; curr; curr = curr->next)
  428.     {
  429.       /* Enqueue the immediate descendents in the level order queue. */
  430.       enqueue(curr->links, &last);
  431.  
  432.       curr->shift = kwset->mind;
  433.       curr->maxshift = kwset->mind;
  434.  
  435.       /* Update the delta table for the descendents of this node. */
  436.       treedelta(curr->links, curr->depth, delta);
  437.  
  438.       /* Compute the failure function for the decendents of this node. */
  439.       treefails(curr->links, curr->fail, kwset->trie);
  440.  
  441.       /* Update the shifts at each node in the current node's chain
  442.      of fails back to the root. */
  443.       for (fail = curr->fail; fail; fail = fail->fail)
  444.     {
  445.       /* If the current node has some outgoing edge that the fail
  446.          doesn't, then the shift at the fail should be no larger
  447.          than the difference of their depths. */
  448.       if (!hasevery(fail->links, curr->links))
  449.         if (curr->depth - fail->depth < fail->shift)
  450.           fail->shift = curr->depth - fail->depth;
  451.  
  452.       /* If the current node is accepting then the shift at the
  453.          fail and its descendents should be no larger than the
  454.          difference of their depths. */
  455.       if (curr->accepting && fail->maxshift > curr->depth - fail->depth)
  456.         fail->maxshift = curr->depth - fail->depth;
  457.     }
  458.     }
  459.  
  460.   /* Traverse the trie in level order again, fixing up all nodes whose
  461.      shift exceeds their inherited maxshift. */
  462.   for (curr = kwset->trie->next; curr; curr = curr->next)
  463.     {
  464.       if (curr->maxshift > curr->parent->maxshift)
  465.     curr->maxshift = curr->parent->maxshift;
  466.       if (curr->shift > curr->maxshift)
  467.     curr->shift = curr->maxshift;
  468.     }
  469.  
  470.   /* Create a vector, indexed by character code, of the outgoing links
  471.      from the root node. */
  472.   for (i = 0; i < NCHAR; ++i)
  473.     next[i] = NULL;
  474.   treenext(kwset->trie->links, next);
  475.  
  476.   /* Fix things up for any translation table. */
  477.   if (trans = kwset->trans)
  478.     for (i = 0; i < NCHAR; ++i)
  479.       {
  480.     kwset->delta[i] = delta[(unsigned char) trans[i]];
  481.     kwset->next[i] = next[(unsigned char) trans[i]];
  482.       }
  483.   else
  484.     for (i = 0; i < NCHAR; ++i)
  485.       {
  486.     kwset->delta[i] = delta[i];
  487.     kwset->next[i] = next[i];
  488.       }
  489.  
  490.   return NULL;
  491. }
  492.  
  493. /* Search through the given text for a match of any member of the
  494.    given keyword set.  Return a pointer to the first character of
  495.    the matching substring, or NULL if no match is found.  If FOUNDLEN
  496.    is non-NULL store in the referenced location the length of the
  497.    matching substring.  Similarly, if FOUNDIDX is non-NULL, store
  498.    in the referenced location the index number of the particular
  499.    keyword matched. */
  500. char *
  501. DEFUN(kwsexec, (kws, text, len, kwsmatch),
  502.       kwset_t kws AND char *text AND size_t len AND struct kwsmatch *kwsmatch)
  503. {
  504.   struct kwset *kwset;
  505.   struct trie **next, *trie, *accept;
  506.   char *beg, *lim, *mch, *lmch;
  507.   register unsigned char c;
  508.   register int *delta, d;
  509.   register char *end, *qlim;
  510.   register struct tree *tree;
  511.   register const char *trans;
  512.  
  513.   /* Initialize register copies and look for easy ways out. */
  514.   kwset = (struct kwset *) kws;
  515.   if (len < kwset->mind)
  516.     return NULL;
  517.   next = kwset->next;
  518.   delta = kwset->delta;
  519.   trans = kwset->trans;
  520.   lim = text + len;
  521.   end = text;
  522.   if (d = kwset->mind)
  523.     mch = NULL;
  524.   else
  525.     {
  526.       mch = text, accept = kwset->trie;
  527.       goto match;
  528.     }
  529.  
  530.   if (len >= 4 * kwset->mind)
  531.     qlim = lim - 4 * kwset->mind;
  532.   else
  533.     qlim = NULL;
  534.  
  535.   while (lim - end >= d)
  536.     {
  537.       if (qlim && end <= qlim)
  538.     {
  539.       end += d - 1;
  540.       while ((d = delta[c = *end]) && end < qlim)
  541.         {
  542.           end += d;
  543.           end += delta[(unsigned char) *end];
  544.           end += delta[(unsigned char) *end];
  545.         }
  546.       ++end;
  547.     }
  548.       else
  549.     d = delta[c = (end += d)[-1]];
  550.       if (d)
  551.     continue;
  552.       beg = end - 1;
  553.       trie = next[c];
  554.       if (trie->accepting)
  555.     {
  556.       mch = beg;
  557.       accept = trie;
  558.     }
  559.       d = trie->shift;
  560.       while (beg > text)
  561.     {
  562.       c = trans ? trans[(unsigned char) *--beg] : *--beg;
  563.       tree = trie->links;
  564.       while (tree && c != tree->label)
  565.         if (c < tree->label)
  566.           tree = tree->llink;
  567.         else
  568.           tree = tree->rlink;
  569.       if (tree)
  570.         {
  571.           trie = tree->trie;
  572.           if (trie->accepting)
  573.         {
  574.           mch = beg;
  575.           accept = trie;
  576.         }
  577.         }
  578.       else
  579.         break;
  580.       d = trie->shift;
  581.     }
  582.       if (mch)
  583.     goto match;
  584.     }
  585.   return NULL;
  586.  
  587.  match:
  588.   /* Given a known match, find the longest possible match anchored
  589.      at or before its starting point.  This is nearly a verbatim
  590.      copy of the preceding main search loops. */
  591.   if (lim - mch > kwset->maxd)
  592.     lim = mch + kwset->maxd;
  593.   lmch = NULL;
  594.   d = 1;
  595.   while (lim - end >= d)
  596.     {
  597.       if (d = delta[c = (end += d)[-1]])
  598.     continue;
  599.       beg = end - 1;
  600.       if (!(trie = next[c]))
  601.     {
  602.       d = 1;
  603.       continue;
  604.     }
  605.       if (trie->accepting && beg <= mch)
  606.     {
  607.       lmch = beg;
  608.       accept = trie;
  609.     }
  610.       d = trie->shift;
  611.       while (beg > text)
  612.     {
  613.       c = trans ? trans[(unsigned char) *--beg] : *--beg;
  614.       tree = trie->links;
  615.       while (tree && c != tree->label)
  616.         if (c < tree->label)
  617.           tree = tree->llink;
  618.         else
  619.           tree = tree->rlink;
  620.       if (tree)
  621.         {
  622.           trie = tree->trie;
  623.           if (trie->accepting && beg <= mch)
  624.         {
  625.           lmch = beg;
  626.           accept = trie;
  627.         }
  628.         }
  629.       else
  630.         break;
  631.       d = trie->shift;
  632.     }
  633.       if (lmch)
  634.     {
  635.       mch = lmch;
  636.       goto match;
  637.     }
  638.       if (!d)
  639.     d = 1;
  640.     }
  641.  
  642.   if (kwsmatch)
  643.     {
  644.       kwsmatch->index = accept->accepting / 2;
  645.       kwsmatch->beg[0] = mch;
  646.       kwsmatch->size[0] = accept->depth;
  647.     }
  648.   return mch;
  649. }
  650.   
  651. /* Free the components of the given keyword set. */
  652. void
  653. DEFUN(kwsfree, (kws), kwset_t kws)
  654. {
  655.   struct kwset *kwset;
  656.  
  657.   kwset = (struct kwset *) kws;
  658.   obstack_free(&kwset->obstack, (PTR) NULL);
  659.   free((PTR) kws);
  660. }
  661.