home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnurx.zip / rx / rxnode.c < prev    next >
C/C++ Source or Header  |  1995-12-31  |  6KB  |  303 lines

  1. /* classes: src_files */
  2.  
  3. /***********************************************************
  4.  
  5. Copyright 1995 by Tom Lord
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the name of the copyright holder not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  19. EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  23. PERFORMANCE OF THIS SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27.  
  28.  
  29. #include "rxall.h"
  30. #include "rxnode.h"
  31.  
  32.  
  33. #ifdef __STDC__
  34. struct rexp_node *
  35. rexp_node (int type)
  36. #else
  37. struct rexp_node *
  38. rexp_node (type)
  39.      int type;
  40. #endif
  41. {
  42.   struct rexp_node *n;
  43.  
  44.   n = (struct rexp_node *)malloc (sizeof (*n));
  45.   bzero (n, sizeof (*n));
  46.   if (n)
  47.     {
  48.       n->type = type;
  49.       n->id = -1;
  50.       n->refs = 1;
  51.     }
  52.   return n;
  53. }
  54.  
  55.  
  56. /* free_rexp_node assumes that the bitset passed to rx_mk_r_cset
  57.  * can be freed using rx_free_cset.
  58.  */
  59.  
  60. #ifdef __STDC__
  61. struct rexp_node *
  62. rx_mk_r_cset (int type, int size, rx_Bitset b)
  63. #else
  64. struct rexp_node *
  65. rx_mk_r_cset (type, int size, b)
  66.      int type;
  67.      int size;
  68.      rx_Bitset b;
  69. #endif
  70. {
  71.   struct rexp_node * n;
  72.   n = rexp_node (type);
  73.   if (n)
  74.     {
  75.       n->params.cset = b;
  76.       n->params.cset_size = size;
  77.     }
  78.   return n;
  79. }
  80.  
  81.  
  82. #ifdef __STDC__
  83. struct rexp_node *
  84. rx_mk_r_int (int type, int intval)
  85. #else
  86. struct rexp_node *
  87. rx_mk_r_int (type, intval)
  88.      int type;
  89.      int intval;
  90. #endif
  91. {
  92.   struct rexp_node * n;
  93.   n = rexp_node (type);
  94.   if (n)
  95.     n->params.intval = intval;
  96.   return n;
  97. }
  98.  
  99.  
  100. #ifdef __STDC__
  101. struct rexp_node *
  102. rx_mk_r_binop (int type, struct rexp_node * a, struct rexp_node * b)
  103. #else
  104. struct rexp_node *
  105. rx_mk_r_binop (type, a, b)
  106.      int type;
  107.      struct rexp_node * a;
  108.      struct rexp_node * b;
  109. #endif
  110. {
  111.   struct rexp_node * n = rexp_node (type);
  112.   if (n)
  113.     {
  114.       n->params.pair.left = a;
  115.       n->params.pair.right = b;
  116.     }
  117.   return n;
  118. }
  119.  
  120.  
  121. #ifdef __STDC__
  122. struct rexp_node *
  123. rx_mk_r_monop (int type, struct rexp_node * a)
  124. #else
  125. struct rexp_node *
  126. rx_mk_r_monop (type, a)
  127.      int type;
  128.      struct rexp_node * a;
  129. #endif
  130. {
  131.   return rx_mk_r_binop (type, a, 0);
  132. }
  133.  
  134.  
  135. #ifdef __STDC__
  136. void
  137. rx_free_rexp (struct rexp_node * node)
  138. #else
  139. void
  140. rx_free_rexp (node)
  141.      struct rexp_node * node;
  142. #endif
  143. {
  144.   if (node && !--node->refs)
  145.     {
  146.       if (node->params.cset)
  147.     rx_free_cset (node->params.cset);
  148.       rx_free_rexp (node->params.pair.left);
  149.       rx_free_rexp (node->params.pair.right);
  150.       rx_free_rexp (node->simplified);
  151.       free ((char *)node);
  152.     }
  153. }
  154.  
  155. #ifdef __STDC__
  156. void
  157. rx_save_rexp (struct rexp_node * node)
  158. #else
  159. void
  160. rx_save_rexp (node)
  161.      struct rexp_node * node;
  162. #endif
  163. {
  164.   if (node)
  165.     ++node->refs;
  166. }
  167.  
  168.  
  169. #ifdef __STDC__
  170. struct rexp_node * 
  171. rx_copy_rexp (int cset_size, struct rexp_node *node)
  172. #else
  173. struct rexp_node * 
  174. rx_copy_rexp (cset_size, node)
  175.      int cset_size;
  176.      struct rexp_node *node;
  177. #endif
  178. {
  179.   if (!node)
  180.     return 0;
  181.   else
  182.     {
  183.       struct rexp_node *n;
  184.       n = rexp_node (node->type);
  185.       if (!n)
  186.     return 0;
  187.  
  188.       if (node->params.cset)
  189.     {
  190.       n->params.cset = rx_copy_cset (cset_size,
  191.                      node->params.cset);
  192.       if (!n->params.cset)
  193.         {
  194.           rx_free_rexp (n);
  195.           return 0;
  196.         }
  197.     }
  198.  
  199.       n->params.intval = node->params.intval;
  200.       n->params.pair.left = rx_copy_rexp (cset_size, node->params.pair.left);
  201.       n->params.pair.right = rx_copy_rexp (cset_size, node->params.pair.right);
  202.       if (   (node->params.pair.left && !n->params.pair.left)
  203.       || (node->params.pair.right && !n->params.pair.right))
  204.     {
  205.       rx_free_rexp  (n);
  206.       return 0;
  207.     }
  208.       return n;
  209.     }
  210. }
  211.  
  212.  
  213.  
  214. int re_abs = 0;
  215. int re_ans = 0;
  216.  
  217. #ifdef __STDC__
  218. int
  219. rx_rexp_equal (struct rexp_node * a, struct rexp_node * b)
  220. #else
  221. int
  222. rx_rexp_equal (a, b)
  223.      struct rexp_node * a;
  224.      struct rexp_node * b;
  225. #endif
  226. {
  227.   int ret;
  228.  
  229.   if (a == b)
  230.     return 1;
  231.  
  232.   if ((a == 0) || (b == 0))
  233.     return 0;
  234.  
  235.   if (a->type != b->type)
  236.     return 0;
  237.  
  238.   switch (a->type)
  239.     {
  240.     case r_cset:
  241.       ret = (   (a->params.cset_size == b->params.cset_size)
  242.          && rx_bitset_is_equal (a->params.cset_size,
  243.                     a->params.cset,
  244.                     b->params.cset));
  245.       break;
  246.     case r_concat:
  247.     case r_alternate:
  248.       ret = (   rx_rexp_equal (a->params.pair.left, b->params.pair.left)
  249.          && rx_rexp_equal (a->params.pair.right, b->params.pair.right));
  250.       break;
  251.     case r_opt:
  252.     case r_star:
  253.     case r_plus:
  254.       ret = rx_rexp_equal (a->params.pair.left, b->params.pair.left);
  255.       break;
  256.     case r_interval:
  257.       ret = (   (a->params.intval == b->params.intval)
  258.          && (a->params.intval2 == b->params.intval2));
  259.       break;
  260.     case r_parens:
  261.     case r_context:
  262.       ret = (a->params.intval == b->params.intval);
  263.       break;
  264.     default:
  265.       return 0;
  266.     }
  267.  
  268.   /**/
  269.   ++re_abs;
  270.   re_ans += (ret*2 - 1);
  271.  
  272.   return ret;
  273. }
  274.  
  275.  
  276.  
  277.  
  278.  
  279. #ifdef __STDC__
  280. unsigned long
  281. rx_rexp_hash (struct rexp_node * node, unsigned long seed)
  282. #else
  283.      unsigned long
  284.      rx_rexp_hash (node, seed)
  285.      struct rexp_node * node;
  286.      unsigned long seed;
  287. #endif
  288. {
  289.   if (!node)
  290.     return 0;
  291.  
  292.   seed += (seed << 3) + rx_rexp_hash (node->params.pair.left, seed);
  293.   seed += (seed << 3) + rx_rexp_hash (node->params.pair.right, seed);
  294.   seed += (seed << 3) + rx_bitset_hash (node->params.cset_size, node->params.cset);
  295.   seed += (seed << 3) + node->params.intval;
  296.   seed += (seed << 3) + node->params.intval2;
  297.   seed += (seed << 3) + node->type;
  298.   seed += (seed << 3) + node->id;
  299.   seed += (seed << 3) + node->len;
  300.   seed += (seed << 3) + node->observed;
  301.   return seed;
  302. }
  303.