home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / ANTLR / HASH.C < prev    next >
C/C++ Source or Header  |  1993-09-02  |  5KB  |  212 lines

  1. /*
  2.  * hash.c
  3.  *
  4.  * $Id: hash.c,v 1.4 1993/08/24 14:44:32 pccts Exp pccts $
  5.  * $Revision: 1.4 $
  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.10
  37.  * Terence Parr
  38.  * Purdue University
  39.  * 1989-1993
  40.  */
  41.  
  42. #include <stdio.h>
  43. #ifdef __cplusplus
  44. #ifndef __STDC__
  45. #define __STDC__
  46. #endif
  47. #endif
  48. #include "hash.h"
  49. #ifdef __STDC__
  50. #include <stdlib.h>
  51. #else
  52. #ifdef VAXC
  53. #include <stdlib.h>
  54. #else
  55. #include <malloc.h>
  56. #endif
  57. #endif
  58. #include <string.h>
  59.  
  60. #define StrSame        0
  61. #define fatal(err)                                                            \
  62.             {fprintf(stderr, "%s(%d):", __FILE__, __LINE__);                \
  63.             fprintf(stderr, " %s\n", err); exit(1);}
  64. #define require(expr, err) {if ( !(expr) ) fatal(err);}
  65.  
  66. static unsigned size = HashTableSize;
  67. static char *strings = NULL;
  68. static char *strp;
  69. static unsigned strsize = StrTableSize;
  70.  
  71. /* create the hash table and string table for terminals (string table only once) */
  72. Entry **
  73. #ifdef __STDC__
  74. newHashTable( void )
  75. #else
  76. newHashTable( )
  77. #endif
  78. {
  79.     Entry **table;
  80.     
  81.     table = (Entry **) calloc(size, sizeof(Entry *));
  82.     require( table != NULL, "cannot allocate hash table");
  83.     if ( strings == NULL )
  84.     {
  85.         strings = (char *) calloc(strsize, sizeof(char));
  86.         require( strings != NULL, "cannot allocate string table");
  87.         strp = strings;
  88.     }
  89.     return table;
  90. }
  91.  
  92. /* Given a table, add 'rec' with key 'key' (add to front of list). return ptr to entry */
  93. Entry *
  94. #ifdef __STDC__
  95. hash_add( Entry **table, char *key, Entry *rec )
  96. #else
  97. hash_add( table, key, rec )
  98. Entry **table;
  99. char *key;
  100. Entry *rec;
  101. #endif
  102. {
  103.     unsigned h=0;
  104.     char *p=key;
  105.     extern Entry *Globals;
  106.     require(table!=NULL && key!=NULL && rec!=NULL, "add: invalid addition");
  107.     
  108.     Hash(p,h,size);
  109.     rec->next = table[h];            /* Add to singly-linked list */
  110.     table[h] = rec;
  111.     return rec;
  112. }
  113.  
  114. /* Return ptr to 1st entry found in table under key (return NULL if none found) */
  115. Entry *
  116. #ifdef __STDC__
  117. hash_get( Entry **table, char *key )
  118. #else
  119. hash_get( table, key )
  120. Entry **table;
  121. char *key;
  122. #endif
  123. {
  124.     unsigned h=0;
  125.     char *p=key;
  126.     Entry *q;
  127.     require(table!=NULL && key!=NULL, "get: invalid table and/or key");
  128.     
  129.     Hash(p,h,size);
  130.     for (q = table[h]; q != NULL; q = q->next)
  131.     {
  132.         if ( strcmp(key, q->str) == StrSame ) return( q );
  133.     }
  134.     return( NULL );
  135. }
  136.  
  137. void
  138. #ifdef __STDC__
  139. hashStat( Entry **table )
  140. #else
  141. hashStat( table )
  142. Entry **table;
  143. #endif
  144. {
  145.     static unsigned short count[20];
  146.     int i,n=0,low=0, hi=0;
  147.     Entry **p;
  148.     float avg=0.0;
  149.     
  150.     for (i=0; i<20; i++) count[i] = 0;
  151.     for (p=table; p<&(table[size]); p++)
  152.     {
  153.         Entry *q = *p;
  154.         int len;
  155.         
  156.         if ( q != NULL && low==0 ) low = p-table;
  157.         len = 0;
  158.         if ( q != NULL ) fprintf(stderr, "[%d]", p-table);
  159.         while ( q != NULL )
  160.         {
  161.             len++;
  162.             n++;
  163.             fprintf(stderr, " %s", q->str);
  164.             q = q->next;
  165.             if ( q == NULL ) fprintf(stderr, "\n");
  166.         }
  167.         count[len]++;
  168.         if ( *p != NULL ) hi = p-table;
  169.     }
  170.  
  171.     fprintf(stderr, "Storing %d recs used %d hash positions out of %d\n",
  172.                     n, size-count[0], size);
  173.     fprintf(stderr, "%f %% utilization\n",
  174.                     ((float)(size-count[0]))/((float)size));
  175.     for (i=0; i<20; i++)
  176.     {
  177.         if ( count[i] != 0 )
  178.         {
  179.             avg += (((float)(i*count[i]))/((float)n)) * i;
  180.             fprintf(stderr, "Bucket len %d == %d (%f %% of recs)\n",
  181.                             i, count[i], ((float)(i*count[i]))/((float)n));
  182.         }
  183.     }
  184.     fprintf(stderr, "Avg bucket length %f\n", avg);
  185.     fprintf(stderr, "Range of hash function: %d..%d\n", low, hi);
  186. }
  187.  
  188. /* Add a string to the string table and return a pointer to it.
  189.  * Bump the pointer into the string table to next avail position.
  190.  */
  191. char *
  192. #ifdef __STDC__
  193. mystrdup( char *s )
  194. #else
  195. mystrdup( s )
  196. char *s;
  197. #endif
  198. {
  199.     char *start=strp;
  200.     require(s!=NULL, "mystrdup: NULL string");
  201.  
  202.     while ( *s != '\0' )
  203.     {
  204.         require( strp <= &(strings[strsize-2]),
  205.                  "string table overflow\nIncrease StrTableSize in hash.h and recompile hash.c\n");
  206.         *strp++ = *s++;
  207.     }
  208.     *strp++ = '\0';
  209.  
  210.     return( start );
  211. }
  212.