home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts.zip / pccts / antlr / hash.c < prev    next >
C/C++ Source or Header  |  1994-03-31  |  5KB  |  213 lines

  1. /*
  2.  * hash.c
  3.  *
  4.  * $Id: hash.c,v 1.2 1993/12/14 20:17:08 parrt Exp parrt $
  5.  * $Revision: 1.2 $
  6.  *
  7.  * Manage hash tables.
  8.  *
  9.  * The following functions are visible:
  10.  *
  11.  *        char    *mystrdup(char *);        Make space and copy string
  12.  *        Entry     **newHashTable();        Create and return initialized hash table
  13.  *        Entry    *hash_add(Entry **, char *, Entry *)
  14.  *        Entry    *hash_get(Entry **, char *)
  15.  *
  16.  * SOFTWARE RIGHTS
  17.  *
  18.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  19.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  20.  * company may do whatever they wish with source code distributed with
  21.  * PCCTS or the code generated by PCCTS, including the incorporation of
  22.  * PCCTS, or its output, into commerical software.
  23.  * 
  24.  * We encourage users to develop software with PCCTS.  However, we do ask
  25.  * that credit is given to us for developing PCCTS.  By "credit",
  26.  * we mean that if you incorporate our source code into one of your
  27.  * programs (commercial product, research project, or otherwise) that you
  28.  * acknowledge this fact somewhere in the documentation, research report,
  29.  * etc...  If you like PCCTS and have developed a nice tool with the
  30.  * output, please mention that you developed it using PCCTS.  In
  31.  * addition, we ask that this header remain intact in our source code.
  32.  * As long as these guidelines are kept, we expect to continue enhancing
  33.  * this system and expect to make other tools available as they are
  34.  * completed.
  35.  *
  36.  * ANTLR 1.20
  37.  * Terence Parr
  38.  * Purdue University
  39.  * With AHPCRC, University of Minnesota
  40.  * 1989-1994
  41.  */
  42.  
  43. #include <stdio.h>
  44. #ifdef __cplusplus
  45. #ifndef __STDC__
  46. #define __STDC__
  47. #endif
  48. #endif
  49. #include "hash.h"
  50. #ifdef __STDC__
  51. #include <stdlib.h>
  52. #else
  53. #ifdef VAXC
  54. #include <stdlib.h>
  55. #else
  56. #include <malloc.h>
  57. #endif
  58. #endif
  59. #include <string.h>
  60.  
  61. #define StrSame        0
  62. #define fatal(err)                                                            \
  63.             {fprintf(stderr, "%s(%d):", __FILE__, __LINE__);                \
  64.             fprintf(stderr, " %s\n", err); exit(1);}
  65. #define require(expr, err) {if ( !(expr) ) fatal(err);}
  66.  
  67. static unsigned size = HashTableSize;
  68. static char *strings = NULL;
  69. static char *strp;
  70. static unsigned strsize = StrTableSize;
  71.  
  72. /* create the hash table and string table for terminals (string table only once) */
  73. Entry **
  74. #ifdef __STDC__
  75. newHashTable( void )
  76. #else
  77. newHashTable( )
  78. #endif
  79. {
  80.     Entry **table;
  81.     
  82.     table = (Entry **) calloc(size, sizeof(Entry *));
  83.     require( table != NULL, "cannot allocate hash table");
  84.     if ( strings == NULL )
  85.     {
  86.         strings = (char *) calloc(strsize, sizeof(char));
  87.         require( strings != NULL, "cannot allocate string table");
  88.         strp = strings;
  89.     }
  90.     return table;
  91. }
  92.  
  93. /* Given a table, add 'rec' with key 'key' (add to front of list). return ptr to entry */
  94. Entry *
  95. #ifdef __STDC__
  96. hash_add( Entry **table, char *key, Entry *rec )
  97. #else
  98. hash_add( table, key, rec )
  99. Entry **table;
  100. char *key;
  101. Entry *rec;
  102. #endif
  103. {
  104.     unsigned h=0;
  105.     char *p=key;
  106.     extern Entry *Globals;
  107.     require(table!=NULL && key!=NULL && rec!=NULL, "add: invalid addition");
  108.     
  109.     Hash(p,h,size);
  110.     rec->next = table[h];            /* Add to singly-linked list */
  111.     table[h] = rec;
  112.     return rec;
  113. }
  114.  
  115. /* Return ptr to 1st entry found in table under key (return NULL if none found) */
  116. Entry *
  117. #ifdef __STDC__
  118. hash_get( Entry **table, char *key )
  119. #else
  120. hash_get( table, key )
  121. Entry **table;
  122. char *key;
  123. #endif
  124. {
  125.     unsigned h=0;
  126.     char *p=key;
  127.     Entry *q;
  128.     require(table!=NULL && key!=NULL, "get: invalid table and/or key");
  129.     
  130.     Hash(p,h,size);
  131.     for (q = table[h]; q != NULL; q = q->next)
  132.     {
  133.         if ( strcmp(key, q->str) == StrSame ) return( q );
  134.     }
  135.     return( NULL );
  136. }
  137.  
  138. void
  139. #ifdef __STDC__
  140. hashStat( Entry **table )
  141. #else
  142. hashStat( table )
  143. Entry **table;
  144. #endif
  145. {
  146.     static unsigned short count[20];
  147.     int i,n=0,low=0, hi=0;
  148.     Entry **p;
  149.     float avg=0.0;
  150.     
  151.     for (i=0; i<20; i++) count[i] = 0;
  152.     for (p=table; p<&(table[size]); p++)
  153.     {
  154.         Entry *q = *p;
  155.         int len;
  156.         
  157.         if ( q != NULL && low==0 ) low = p-table;
  158.         len = 0;
  159.         if ( q != NULL ) fprintf(stderr, "[%d]", p-table);
  160.         while ( q != NULL )
  161.         {
  162.             len++;
  163.             n++;
  164.             fprintf(stderr, " %s", q->str);
  165.             q = q->next;
  166.             if ( q == NULL ) fprintf(stderr, "\n");
  167.         }
  168.         count[len]++;
  169.         if ( *p != NULL ) hi = p-table;
  170.     }
  171.  
  172.     fprintf(stderr, "Storing %d recs used %d hash positions out of %d\n",
  173.                     n, size-count[0], size);
  174.     fprintf(stderr, "%f %% utilization\n",
  175.                     ((float)(size-count[0]))/((float)size));
  176.     for (i=0; i<20; i++)
  177.     {
  178.         if ( count[i] != 0 )
  179.         {
  180.             avg += (((float)(i*count[i]))/((float)n)) * i;
  181.             fprintf(stderr, "Bucket len %d == %d (%f %% of recs)\n",
  182.                             i, count[i], ((float)(i*count[i]))/((float)n));
  183.         }
  184.     }
  185.     fprintf(stderr, "Avg bucket length %f\n", avg);
  186.     fprintf(stderr, "Range of hash function: %d..%d\n", low, hi);
  187. }
  188.  
  189. /* Add a string to the string table and return a pointer to it.
  190.  * Bump the pointer into the string table to next avail position.
  191.  */
  192. char *
  193. #ifdef __STDC__
  194. mystrdup( char *s )
  195. #else
  196. mystrdup( s )
  197. char *s;
  198. #endif
  199. {
  200.     char *start=strp;
  201.     require(s!=NULL, "mystrdup: NULL string");
  202.  
  203.     while ( *s != '\0' )
  204.     {
  205.         require( strp <= &(strings[strsize-2]),
  206.                  "string table overflow\nIncrease StrTableSize in hash.h and recompile hash.c\n");
  207.         *strp++ = *s++;
  208.     }
  209.     *strp++ = '\0';
  210.  
  211.     return( start );
  212. }
  213.