Wild Life  2.30
 All Data Structures Files Functions Variables Typedefs Macros
hash_table.c
Go to the documentation of this file.
1 
12 #include "defs.h"
13 
15 
16 long rand_array[256];
17 
26 {
27  ptr_hash_table new;
28  int i;
29 
30  new=(ptr_hash_table)malloc(sizeof(struct wl_hash_table));
31  new->size=size;
32  new->used=0;
33  new->data=(ptr_keyword *)malloc(size*sizeof(ptr_keyword));
34  for(i=0;i<size;i++)
35  new->data[i]=NULL;
36  return new;
37 }
38 
47 void hash_expand(ptr_hash_table table,int new_size)
48 {
49  ptr_keyword *old_data;
50  int old_size;
51  int i;
52 
53 
54  old_data=table->data;
55  old_size=table->size;
56 
57  table->size=new_size; /* Must be power of 2 */
58  table->used=0;
59  table->data=(ptr_keyword *)malloc(new_size*sizeof(ptr_keyword));
60 
61  for(i=0;i<new_size;i++)
62  table->data[i]=NULL;
63 
64  for(i=0;i<old_size;i++)
65  if(old_data[i])
66  hash_insert(table,old_data[i]->symbol,old_data[i]);
67 
68  free(old_data);
69 }
70 
79 int hash_code(ptr_hash_table table,char *symbol)
80 {
81  int n=0;
82 
83  /* printf("code of %s ",symbol); */
84 
85  while(*symbol) {
86  n ^= rand_array[*symbol]+rand_array[n&255];
87  n++;
88  symbol++;
89  }
90 
91  n &= (table->size-1);
92 
93 
94  /* printf("=%d\n",n); */
95 
96  return n;
97 }
98 
106 int hash_find(ptr_hash_table table,char *symbol)
107 {
108  int n;
109  int i=1;
110 
111  n=hash_code(table,symbol);
112 
113  while(table->data[n] && strcmp(table->data[n]->symbol,symbol)) {
114  /* Not a direct hit... */
115  n+= i*i;
116  /* i++; */
117  n &= table->size-1;
118  }
119 
120  return n;
121 }
122 
132 {
133  int n;
134 
135  n=hash_find(table,symbol);
136 
137  /* printf("found %s at %d keyword %x\n",symbol,n,table->data[n]); */
138 
139  return table->data[n];
140 }
141 
151 void hash_insert(ptr_hash_table table,char *symbol,ptr_keyword keyword)
152 {
153  int n;
154 
155  n=hash_find(table,symbol);
156 
157  /* printf("inserting %s at %d keyword %x\n",symbol,n,keyword); */
158 
159  if(!table->data[n])
160  table->used++;
161  table->data[n]=keyword;
162 
163  if(table->used*2>table->size)
164  hash_expand(table,table->size*2);
165 }
166 
175 {
176  int i;
177  int n;
178  char *s;
179  int c=0;
180  int t=0;
181 
182  printf("*** Hash table %lx:\n",(long)table);
183  printf("Size: %d\n",table->size);
184  printf("Used: %d\n",table->used);
185 
186  for(i=0;i<table->size;i++)
187  if(table->data[i]) {
188  t++;
189  s=table->data[i]->symbol;
190  n=hash_code(table,s);
191 
192  printf("%4d %4d %s %s\n",
193  i,
194  n,
195  i==n?"ok ":"*bad*",
196  s);
197 
198  if(i!=n)
199  c++;
200  }
201 
202  printf("Really used: %d\n",t);
203  printf("Collisions: %d = %1.3f%%\n",
204  c,
205  100.0*c/(double)t);
206 }
ptr_keyword * data
Definition: def_struct.h:139
long rand_array[256]
Definition: hash_table.c:16
includes
#define NULL
Definition: def_const.h:533
int hash_find(ptr_hash_table table, char *symbol)
hash_find
Definition: hash_table.c:106
char * symbol
Definition: def_struct.h:118
struct wl_hash_table * ptr_hash_table
Definition: def_struct.h:100
void hash_insert(ptr_hash_table table, char *symbol, ptr_keyword keyword)
HASH_INSERT.
Definition: hash_table.c:151
void hash_display(ptr_hash_table table)
HASH_DISPLAY.
Definition: hash_table.c:174
int hash_code(ptr_hash_table table, char *symbol)
HASH_CODE.
Definition: hash_table.c:79
ptr_definition first_definition
Definition: hash_table.c:14
ptr_keyword hash_lookup(ptr_hash_table table, char *symbol)
HASH_LOOKUP.
Definition: hash_table.c:131
void hash_expand(ptr_hash_table table, int new_size)
HASH_EXPAND.
Definition: hash_table.c:47
ptr_hash_table hash_create(int size)
HASH_CREATE.
Definition: hash_table.c:25