Wild Life  2.29
 All Data Structures Files Functions Variables Typedefs Macros
Functions | Variables
hash_table.c File Reference

Go to the source code of this file.

Functions

ptr_hash_table hash_create (int size)
 
void hash_expand (ptr_hash_table table, int new_size)
 
int hash_code (ptr_hash_table table, char *symbol)
 
int hash_find (ptr_hash_table table, char *symbol)
 
ptr_keyword hash_lookup (ptr_hash_table table, char *symbol)
 
void hash_insert (ptr_hash_table table, char *symbol, ptr_keyword keyword)
 
void hash_display (ptr_hash_table table)
 

Variables

ptr_definition first_definition =NULL
 
long rand_array [256]
 

Function Documentation

int hash_code ( ptr_hash_table  table,
char *  symbol 
)

Definition at line 81 of file hash_table.c.

References rand_array.

85 {
86  int n=0;
87 
88  /* printf("code of %s ",symbol); */
89 
90  while(*symbol) {
91  n ^= rand_array[*symbol]+rand_array[n&255];
92  n++;
93  symbol++;
94  }
95 
96  n &= (table->size-1);
97 
98 
99  /* printf("=%d\n",n); */
100 
101  return n;
102 }
long rand_array[256]
Definition: hash_table.c:18
ptr_hash_table hash_create ( int  size)

Definition at line 26 of file hash_table.c.

References NULL, and wl_hash_table::size.

29 {
30  ptr_hash_table new;
31  int i;
32 
33  new=(ptr_hash_table)malloc(sizeof(struct wl_hash_table));
34  new->size=size;
35  new->used=0;
36  new->data=(ptr_keyword *)malloc(size*sizeof(ptr_keyword));
37  for(i=0;i<size;i++)
38  new->data[i]=NULL;
39  return new;
40 }
#define NULL
Definition: def_const.h:203
struct wl_hash_table * ptr_hash_table
Definition: def_struct.h:68
void hash_display ( ptr_hash_table  table)

Definition at line 182 of file hash_table.c.

References hash_code().

186 {
187  int i;
188  int n;
189  char *s;
190  int c=0;
191  int t=0;
192 
193  printf("*** Hash table %lx:\n",(long)table);
194  printf("Size: %d\n",table->size);
195  printf("Used: %d\n",table->used);
196 
197  for(i=0;i<table->size;i++)
198  if(table->data[i]) {
199  t++;
200  s=table->data[i]->symbol;
201  n=hash_code(table,s);
202 
203  printf("%4d %4d %s %s\n",
204  i,
205  n,
206  i==n?"ok ":"*bad*",
207  s);
208 
209  if(i!=n)
210  c++;
211  }
212 
213  printf("Really used: %d\n",t);
214  printf("Collisions: %d = %1.3f%%\n",
215  c,
216  100.0*c/(double)t);
217 }
ptr_keyword * data
Definition: def_struct.h:114
char * symbol
Definition: def_struct.h:91
int hash_code(ptr_hash_table table, char *symbol)
Definition: hash_table.c:81
void hash_expand ( ptr_hash_table  table,
int  new_size 
)

Definition at line 48 of file hash_table.c.

References hash_insert(), and NULL.

52 {
53  ptr_keyword *old_data;
54  int old_size;
55  int i;
56 
57 
58  old_data=table->data;
59  old_size=table->size;
60 
61  table->size=new_size; /* Must be power of 2 */
62  table->used=0;
63  table->data=(ptr_keyword *)malloc(new_size*sizeof(ptr_keyword));
64 
65  for(i=0;i<new_size;i++)
66  table->data[i]=NULL;
67 
68  for(i=0;i<old_size;i++)
69  if(old_data[i])
70  hash_insert(table,old_data[i]->symbol,old_data[i]);
71 
72  free(old_data);
73 }
ptr_keyword * data
Definition: def_struct.h:114
#define NULL
Definition: def_const.h:203
void hash_insert(ptr_hash_table table, char *symbol, ptr_keyword keyword)
Definition: hash_table.c:155
int hash_find ( ptr_hash_table  table,
char *  symbol 
)

Definition at line 106 of file hash_table.c.

References hash_code().

111 {
112  int n;
113  int i=1;
114 
115  n=hash_code(table,symbol);
116 
117  while(table->data[n] && strcmp(table->data[n]->symbol,symbol)) {
118  /* Not a direct hit... */
119  n+= i*i;
120  /* i++; */
121  n &= table->size-1;
122  }
123 
124  return n;
125 }
ptr_keyword * data
Definition: def_struct.h:114
char * symbol
Definition: def_struct.h:91
int hash_code(ptr_hash_table table, char *symbol)
Definition: hash_table.c:81
void hash_insert ( ptr_hash_table  table,
char *  symbol,
ptr_keyword  keyword 
)

Definition at line 155 of file hash_table.c.

References hash_expand(), and hash_find().

160 {
161  int n;
162 
163 
164  n=hash_find(table,symbol);
165 
166  /* printf("inserting %s at %d keyword %x\n",symbol,n,keyword); */
167 
168  if(!table->data[n])
169  table->used++;
170  table->data[n]=keyword;
171 
172  if(table->used*2>table->size)
173  hash_expand(table,table->size*2);
174 }
ptr_keyword * data
Definition: def_struct.h:114
int hash_find(ptr_hash_table table, char *symbol)
Definition: hash_table.c:106
void hash_expand(ptr_hash_table table, int new_size)
Definition: hash_table.c:48
ptr_keyword hash_lookup ( ptr_hash_table  table,
char *  symbol 
)

Definition at line 133 of file hash_table.c.

References hash_find().

138 {
139  int n;
140 
141 
142  n=hash_find(table,symbol);
143 
144  /* printf("found %s at %d keyword %x\n",symbol,n,table->data[n]); */
145 
146  return table->data[n];
147 }
ptr_keyword * data
Definition: def_struct.h:114
int hash_find(ptr_hash_table table, char *symbol)
Definition: hash_table.c:106

Variable Documentation

ptr_definition first_definition =NULL

Definition at line 16 of file hash_table.c.

long rand_array[256]

Definition at line 18 of file hash_table.c.