Wild Life: hash_table.c Source File

Wild Life 2.29

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros
hash_table.c
Go to the documentation of this file.
1 /******************************* KEYWORDS *************************************
2 RM: Feb 3 1993
3
4 New version of the keyword table and related routines.
5
6 The keyword table will not NOT be sorted, however, access will be hashed.
7
8 Each module has its own hash table of symbols.
9
10 All definition are stores in a linked list starting at first_definition.
11 */
12 /* $Id: hash_table.c,v 1.2 1994/12/08 23:24:09 duchier Exp $ */
13
14 #include "defs.h"
15
16 ptr_definition first_definition=NULL;
17
18 long rand_array[256];
19
20
21
22 /******** HASH_CREATE(size)
23 Create a hash-table for max size keywords.
24 */
25
26 ptr_hash_table hash_create(size)
27
28 int 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 }
41
42
43
44 /******** HASH_EXPAND(table,new_size)
45 Allocate a bigger hash table.
46 */
47
48 void hash_expand(table,new_size)
49
50 ptr_hash_table table;
51 int new_size;
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 }
74
75
76
77 /******** HASH_CODE(table,symbol)
78 Return the hash code for a symbol
79 */
80
81 int hash_code(table,symbol)
82
83 ptr_hash_table table;
84 char *symbol;
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 }
103
104
105
106 int hash_find(table,symbol)
107
108 ptr_hash_table table;
109 char *symbol;
110
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 }
126
127
128
129 /******** HASH_LOOKUP(table,symbol)
130 Look up a symbol in the symbol table.
131 */
132
133 ptr_keyword hash_lookup(table,symbol)
134
135 ptr_hash_table table;
136 char *symbol;
137
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 }
148
149
150
151 /******** HASH_INSERT(table,symbol,keyword)
152 Add a symbol and data to a table. Overwrite previous data.
153 */
154
155 void hash_insert(table,symbol,keyword)
156
157 ptr_hash_table table;
158 char *symbol;
159 ptr_keyword keyword;
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 }
175
176
177
178 /******** HASH_DISPLAY(table)
179 Display a symbol table (for debugging).
180 */
181
182 void hash_display(table)
183
184 ptr_hash_table table;
185
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 }
wl_hash_table
Definition: def_struct.h:111
rand_array
long rand_array[256]
Definition: hash_table.c:18
wl_hash_table::size
int size
Definition: def_struct.h:112
defs.h
NULL
#define NULL
Definition: def_const.h:203
hash_find
int hash_find(ptr_hash_table table, char *symbol)
Definition: hash_table.c:106
ptr_hash_table
struct wl_hash_table * ptr_hash_table
Definition: def_struct.h:68
hash_insert
void hash_insert(ptr_hash_table table, char *symbol, ptr_keyword keyword)
Definition: hash_table.c:155
wl_keyword
Definition: def_struct.h:89
wl_definition
Definition: def_struct.h:121
hash_display
void hash_display(ptr_hash_table table)
Definition: hash_table.c:182
hash_code
int hash_code(ptr_hash_table table, char *symbol)
Definition: hash_table.c:81
first_definition
ptr_definition first_definition
Definition: hash_table.c:16
hash_lookup
ptr_keyword hash_lookup(ptr_hash_table table, char *symbol)
Definition: hash_table.c:133
hash_expand
void hash_expand(ptr_hash_table table, int new_size)
Definition: hash_table.c:48
hash_create
ptr_hash_table hash_create(int size)
Definition: hash_table.c:26