C:/Users/Dennis/src/lang/Life_start/Life/life-1.02/source/hash_tab.c

Go to the documentation of this file.
00001 /******************************* KEYWORDS *************************************
00002   RM: Feb 3 1993
00003   
00004   New version of the keyword table and related routines.
00005 
00006   The keyword table will not NOT be sorted, however, access will be hashed.
00007 
00008   Each module has its own hash table of symbols.
00009 
00010   All definition are stores in a linked list starting at first_definition.
00011   */
00012 /*      $Id: hash_table.c,v 1.2 1994/12/08 23:24:09 duchier Exp $        */
00013 
00014 #ifndef lint
00015 static char vcid[] = "$Id: hash_table.c,v 1.2 1994/12/08 23:24:09 duchier Exp $";
00016 #endif /* lint */
00017 
00018 
00019 #include "extern.h"
00020 
00021 
00022 ptr_definition first_definition=NULL;
00023 
00024 int rand_array[256];
00025 
00026 
00027 
00028 /******** HASH_CREATE(size)
00029   Create a hash-table for max size keywords.
00030   */
00031   
00032 ptr_hash_table hash_create(size)
00033 
00034      int size;
00035 {
00036   ptr_hash_table new;
00037   int i;
00038   
00039   new=(ptr_hash_table)malloc(sizeof(struct wl_hash_table));
00040   new->size=size;
00041   new->used=0;
00042   new->data=(ptr_keyword *)malloc(size*sizeof(ptr_keyword));
00043   for(i=0;i<size;i++)
00044     new->data[i]=NULL;
00045   return new;
00046 }
00047 
00048 
00049 
00050 /******** HASH_EXPAND(table,new_size)
00051   Allocate a bigger hash table.
00052   */
00053 
00054 void hash_expand(table,new_size)
00055 
00056      ptr_hash_table table;
00057      int new_size;
00058 {
00059   ptr_keyword *old_data;
00060   int old_size;
00061   int i;
00062 
00063   
00064   old_data=table->data;
00065   old_size=table->size;
00066 
00067   table->size=new_size; /* Must be power of 2 */
00068   table->used=0;
00069   table->data=(ptr_keyword *)malloc(new_size*sizeof(ptr_keyword));
00070   
00071   for(i=0;i<new_size;i++)
00072     table->data[i]=NULL;
00073   
00074   for(i=0;i<old_size;i++)
00075     if(old_data[i])
00076       hash_insert(table,old_data[i]->symbol,old_data[i]);
00077 
00078   free(old_data);
00079 }
00080 
00081 
00082 
00083 /******** HASH_CODE(table,symbol)
00084   Return the hash code for a symbol
00085   */
00086 
00087 int hash_code(table,symbol)
00088      
00089      ptr_hash_table table;
00090      char *symbol;
00091 {
00092   int n=0;
00093   
00094   /* printf("code of %s ",symbol); */
00095   
00096   while(*symbol) {
00097     n ^= rand_array[*symbol]+rand_array[n&255];
00098     n++;
00099     symbol++;
00100   }
00101   
00102   n &= (table->size-1);
00103   
00104   
00105   /* printf("=%d\n",n); */
00106   
00107   return n;
00108 }
00109 
00110 
00111 
00112 int hash_find(table,symbol)
00113 
00114      ptr_hash_table table;
00115      char *symbol;
00116 
00117 {
00118   int n;
00119   int i=1;
00120   
00121   n=hash_code(table,symbol);
00122   
00123   while(table->data[n] && strcmp(table->data[n]->symbol,symbol)) {
00124     /* Not a direct hit... */
00125     n+= i*i;
00126     /* i++; */
00127     n &= table->size-1;
00128   }
00129   
00130   return n;
00131 }
00132 
00133 
00134 
00135 /******** HASH_LOOKUP(table,symbol)
00136   Look up a symbol in the symbol table.
00137   */
00138 
00139 ptr_keyword hash_lookup(table,symbol)
00140      
00141      ptr_hash_table table;
00142      char *symbol;
00143 
00144 {
00145   int n;
00146 
00147   
00148   n=hash_find(table,symbol);
00149   
00150   /* printf("found %s at %d keyword %x\n",symbol,n,table->data[n]); */
00151   
00152   return table->data[n];
00153 }
00154 
00155 
00156 
00157 /******** HASH_INSERT(table,symbol,keyword)
00158   Add a symbol and data to a table. Overwrite previous data.
00159   */
00160 
00161 void hash_insert(table,symbol,keyword)
00162      
00163      ptr_hash_table table;
00164      char *symbol;
00165      ptr_keyword keyword;
00166 {
00167   int n;
00168 
00169 
00170   n=hash_find(table,symbol);
00171 
00172   /* printf("inserting %s at %d keyword %x\n",symbol,n,keyword); */
00173   
00174   if(!table->data[n])
00175     table->used++;
00176   table->data[n]=keyword;
00177   
00178   if(table->used*2>table->size)
00179     hash_expand(table,table->size*2);
00180 }
00181 
00182 
00183 
00184 /******** HASH_DISPLAY(table)
00185   Display a symbol table (for debugging).
00186   */
00187 
00188 void hash_display(table)
00189      
00190      ptr_hash_table table;
00191      
00192 {
00193   int i;
00194   int n;
00195   char *s;
00196   int c=0;
00197   int t=0;
00198   
00199   printf("*** Hash table %x:\n",table);
00200   printf("Size: %d\n",table->size);
00201   printf("Used: %d\n",table->used);
00202   
00203   for(i=0;i<table->size;i++)
00204     if(table->data[i]) {
00205       t++;
00206       s=table->data[i]->symbol;
00207       n=hash_code(table,s);
00208       
00209       printf("%4d %4d %s %s\n",
00210              i,
00211              n,
00212              i==n?"ok   ":"*bad*",
00213              s);
00214       
00215       if(i!=n)
00216         c++;
00217     }
00218   
00219   printf("Really used: %d\n",t);
00220   printf("Collisions: %d = %1.3f%%\n",
00221          c,
00222          100.0*c/(double)t);
00223 }

Generated on Sat Jan 26 08:48:06 2008 for WildLife by  doxygen 1.5.4