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

  1. /***********************************************************
  2.  
  3. Copyright 1995 by Tom Lord
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of the copyright holder not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  19. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26. /*
  27.  * Tom Lord (lord@cygnus.com, lord@gnu.ai.mit.edu)
  28.  */
  29.  
  30.  
  31. #include "rxall.h"
  32. #include "rxhash.h"
  33.  
  34.  
  35.  
  36. #ifdef __STDC__
  37. static struct rx_hash *
  38. default_hash_alloc (struct rx_hash_rules * rules)
  39. #else
  40. static struct rx_hash *
  41. default_hash_alloc (rules)
  42.      struct rx_hash_rules * rules;
  43. #endif
  44. {
  45.   return (struct rx_hash *)malloc (sizeof (struct rx_hash));
  46. }
  47.  
  48.  
  49. #ifdef __STDC__
  50. static struct rx_hash_item *
  51. default_hash_item_alloc (struct rx_hash_rules * rules, void * value)
  52. #else
  53. static struct rx_hash_item *
  54. default_hash_item_alloc (rules, value)
  55.      struct rx_hash_rules * rules;
  56.      void * value;
  57. #endif
  58. {
  59.   struct rx_hash_item * it;
  60.   it = (struct rx_hash_item *)malloc (sizeof (*it));
  61.   if (it)
  62.     {
  63.       it->data = value;
  64.       it->binding = 0;
  65.     }
  66.   return it;
  67. }
  68.  
  69.  
  70. #ifdef __STDC__
  71. static void
  72. default_free_hash (struct rx_hash * tab,
  73.             struct rx_hash_rules * rules)
  74. #else
  75. static void
  76. default_free_hash (tab, rules)
  77.      struct rx_hash * tab;
  78.      struct rx_hash_rules * rules;
  79. #endif
  80. {
  81.   free ((char *)tab);
  82. }
  83.  
  84.  
  85. #ifdef __STDC__
  86. static void
  87. default_free_hash_item (struct rx_hash_item * item,
  88.              struct rx_hash_rules * rules)
  89. #else
  90. static void
  91. default_free_hash_item (item, rules)
  92.      struct rx_hash_item * item;
  93.      struct rx_hash_rules * rules;
  94. #endif
  95. {
  96.   free ((char *)item);
  97. }
  98.  
  99. #ifdef __STDC__
  100. static int 
  101. default_eq (void * va, void * vb)
  102. #else
  103. static int 
  104. default_eq (va, vb)
  105.      void * va;
  106.      void * vb;
  107. #endif
  108. {
  109.   return va == vb;
  110. }
  111.  
  112.  
  113.  
  114. #define EQ(rules) ((rules && rules->eq) ? rules->eq : default_eq)
  115. #define HASH_ALLOC(rules) ((rules && rules->hash_alloc) ? rules->hash_alloc : default_hash_alloc)
  116. #define FREE_HASH(rules) ((rules && rules->free_hash) ? rules->free_hash : default_free_hash)
  117. #define ITEM_ALLOC(rules) ((rules && rules->hash_item_alloc) ? rules->hash_item_alloc : default_hash_item_alloc)
  118. #define FREE_HASH_ITEM(rules) ((rules && rules->free_hash_item) ? rules->free_hash_item : default_free_hash_item)
  119.  
  120.  
  121. static unsigned long rx_hash_masks[4] =
  122. {
  123.   0x12488421,
  124.   0x96699669,
  125.   0xbe7dd7eb,
  126.   0xffffffff
  127. };
  128.  
  129. /* hash to bucket */
  130. #define JOIN_BYTE(H, B)  (((H) + ((H) << 3) + (B)) & 0xf)
  131.  
  132. #define H2B(X) JOIN_BYTE (JOIN_BYTE (JOIN_BYTE ((X & 0xf), ((X>>8) & 0xf)), ((X>>16) & 0xf)), ((X>>24) & 0xf))
  133.  
  134. #define BKTS 16
  135.  
  136. /* Hash tables */
  137. #ifdef __STDC__
  138. struct rx_hash_item * 
  139. rx_hash_find (struct rx_hash * table,
  140.           unsigned long hash,
  141.           void * value,
  142.           struct rx_hash_rules * rules)
  143. #else
  144. struct rx_hash_item * 
  145. rx_hash_find (table, hash, value, rules)
  146.      struct rx_hash * table;
  147.      unsigned long hash;
  148.      void * value;
  149.      struct rx_hash_rules * rules;
  150. #endif
  151. {
  152.   rx_hash_eq eq = EQ (rules);
  153.   int maskc = 0;
  154.   long mask = rx_hash_masks [0];
  155.   int bucket = H2B(hash & mask);
  156.  
  157.   while (RX_bitset_member (&table->nested_p, bucket))
  158.     {
  159.       table = (struct rx_hash *)(table->children [bucket]);
  160.       ++maskc;
  161.       mask = rx_hash_masks[maskc];
  162.       bucket = H2B (hash & mask);
  163.     }
  164.  
  165.   {
  166.     struct rx_hash_item * it;
  167.     it = (struct rx_hash_item *)(table->children[bucket]);
  168.     while (it)
  169.       if (eq (it->data, value))
  170.     return it;
  171.       else
  172.     it = it->next_same_hash;
  173.   }
  174.  
  175.   return 0;
  176. }
  177.  
  178.  
  179. #ifdef __STDC__
  180. static int 
  181. listlen (struct rx_hash_item * bucket)
  182. #else
  183. static int 
  184. listlen (bucket)
  185.      struct rx_hash_item * bucket;
  186. #endif
  187. {
  188.   int i;
  189.   for (i = 0; bucket; ++i, bucket = bucket->next_same_hash)
  190.     ;
  191.   return i;
  192. }
  193.  
  194. #ifdef __STDC__
  195. static int
  196. overflows (struct rx_hash_item * bucket)
  197. #else
  198. static int
  199. overflows (bucket)
  200.      struct rx_hash_item * bucket;
  201. #endif
  202. {
  203.   return !(   bucket
  204.        && bucket->next_same_hash
  205.        && bucket->next_same_hash->next_same_hash
  206.        && bucket->next_same_hash->next_same_hash->next_same_hash);
  207. }
  208.  
  209.  
  210. #ifdef __STDC__
  211. struct rx_hash_item *
  212. rx_hash_store (struct rx_hash * table,
  213.            unsigned long hash,
  214.            void * value,
  215.            struct rx_hash_rules * rules)
  216. #else
  217. struct rx_hash_item *
  218. rx_hash_store (table, hash, value, rules)
  219.      struct rx_hash * table;
  220.      unsigned long hash;
  221.      void * value;
  222.      struct rx_hash_rules * rules;
  223. #endif
  224. {
  225.   rx_hash_eq eq = EQ (rules);
  226.   int maskc = 0;
  227.   long mask = rx_hash_masks [0];
  228.   int bucket = H2B(hash & mask);
  229.   int depth = 0;
  230.   
  231.   while (RX_bitset_member (&table->nested_p, bucket))
  232.     {
  233.       table = (struct rx_hash *)(table->children [bucket]);
  234.       ++maskc;
  235.       mask = rx_hash_masks[maskc];
  236.       bucket = H2B(hash & mask);
  237.       ++depth;
  238.     }
  239.   
  240.   {
  241.     struct rx_hash_item * it;
  242.     it = (struct rx_hash_item *)(table->children[bucket]);
  243.     while (it)
  244.       if (eq (it->data, value))
  245.     return it;
  246.       else
  247.     it = it->next_same_hash;
  248.   }
  249.   
  250.   {
  251.     if (   (depth < 3)
  252.     && (overflows ((struct rx_hash_item *)table->children [bucket])))
  253.       {
  254.     struct rx_hash * newtab;
  255.     newtab = (struct rx_hash *) HASH_ALLOC(rules) (rules);
  256.     if (!newtab)
  257.       goto add_to_bucket;
  258.     bzero (newtab, sizeof (*newtab));
  259.     newtab->parent = table;
  260.     {
  261.       int old_bucket_size;
  262.       struct rx_hash_item * them;
  263.       unsigned long newmask;
  264.       them = (struct rx_hash_item *)table->children[bucket];
  265.       newmask = rx_hash_masks[maskc + 1];
  266.       while (them)
  267.         {
  268.           struct rx_hash_item * save = them->next_same_hash;
  269.           int new_buck = H2B(them->hash & newmask);
  270.           them->next_same_hash = ((struct rx_hash_item *)
  271.                       newtab->children[new_buck]);
  272.           ((struct rx_hash_item **)newtab->children)[new_buck] = them;
  273.           them->table = newtab;
  274.           them = save;
  275.           ++newtab->refs;
  276.           --table->refs;
  277.         }
  278.       ((struct rx_hash **)table->children)[bucket] = newtab;
  279.       RX_bitset_enjoin (&table->nested_p, bucket);
  280.       ++table->refs;
  281.       table = newtab;
  282.       bucket = H2B(hash & newmask);
  283.     }
  284.       }
  285.   }
  286.  add_to_bucket:
  287.   {
  288.     struct rx_hash_item  * it = ((struct rx_hash_item *)
  289.                  ITEM_ALLOC(rules) (rules, value));
  290.     if (!it)
  291.       return 0;
  292.     it->hash = hash;
  293.     it->table = table;
  294.     /* DATA and BINDING are to be set in hash_item_alloc */
  295.     it->next_same_hash = (struct rx_hash_item *)table->children [bucket];
  296.     ((struct rx_hash_item **)table->children)[bucket] = it;
  297.     ++table->refs;
  298.     return it;
  299.   }
  300. }
  301.  
  302.  
  303. #ifdef __STDC__
  304. void
  305. rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules)
  306. #else
  307. void
  308. rx_hash_free (it, rules)
  309.      struct rx_hash_item * it;
  310.      struct rx_hash_rules * rules;
  311. #endif
  312. {
  313.   if (it)
  314.     {
  315.       struct rx_hash * table = it->table;
  316.       unsigned long hash = it->hash;
  317.       int depth = (table->parent
  318.            ? (table->parent->parent
  319.               ? (table->parent->parent->parent
  320.              ? 3
  321.              : 2)
  322.               : 1)
  323.            : 0);
  324.       int bucket = H2B (hash & rx_hash_masks [depth]);
  325.       struct rx_hash_item ** pos
  326.     = (struct rx_hash_item **)&table->children [bucket];
  327.       
  328.       while (*pos != it)
  329.     pos = &(*pos)->next_same_hash;
  330.       *pos = it->next_same_hash;
  331.       FREE_HASH_ITEM(rules) (it, rules);
  332.       --table->refs;
  333.       while (!table->refs && depth)
  334.     {
  335.       struct rx_hash * save = table;
  336.       table = table->parent;
  337.       --depth;
  338.       bucket = H2B(hash & rx_hash_masks [depth]);
  339.       --table->refs;
  340.       table->children[bucket] = 0;
  341.       RX_bitset_remove (&table->nested_p, bucket);
  342.       FREE_HASH (rules) (save, rules);
  343.     }
  344.     }
  345. }
  346.  
  347. #ifdef __STDC__
  348. void
  349. rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
  350.             struct rx_hash_rules * rules)
  351. #else
  352. void
  353. rx_free_hash_table (tab, freefn, rules)
  354.      struct rx_hash * tab;
  355.      rx_hash_freefn freefn;
  356.      struct rx_hash_rules * rules;
  357. #endif
  358. {
  359.   int x;
  360.  
  361.   for (x = 0; x < BKTS; ++x)
  362.     if (RX_bitset_member (&tab->nested_p, x))
  363.       {
  364.     rx_free_hash_table ((struct rx_hash *)tab->children[x],
  365.                 freefn, rules);
  366.     FREE_HASH (rules) ((struct rx_hash *)tab->children[x], rules);
  367.       }
  368.     else
  369.       {
  370.     struct rx_hash_item * them = (struct rx_hash_item *)tab->children[x];
  371.     while (them)
  372.       {
  373.         struct rx_hash_item * that = them;
  374.         them = that->next_same_hash;
  375.         freefn (that);
  376.         FREE_HASH_ITEM (rules) (that, rules);
  377.       }
  378.       }
  379. }
  380.  
  381.  
  382.  
  383. #ifdef __STDC__
  384. int 
  385. rx_count_hash_nodes (struct rx_hash * st)
  386. #else
  387. int 
  388. rx_count_hash_nodes (st)
  389.      struct rx_hash * st;
  390. #endif
  391. {
  392.   int x;
  393.   int count = 0;
  394.   for (x = 0; x < BKTS; ++x)
  395.     count += ((RX_bitset_member (&st->nested_p, x))
  396.           ? rx_count_hash_nodes ((struct rx_hash *)st->children[x])
  397.           : listlen ((struct rx_hash_item *)(st->children[x])));
  398.   
  399.   return count;
  400. }
  401.  
  402.