-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
274 lines (223 loc) · 5.86 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* hash.c - A simple hash table
*
* Uses null terminated strings as the keys for the table.
* Stores an integer value with the string key. It would be
* easy to change to use void *'s instead of ints. Maybe rewrite
* as a C++ template??
*
* Donated by John Stone
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#define HASH_LIMIT 0.5
/*
* Local types
*/
typedef struct hash_node_t {
int data; /* data in hash node */
const char * key; /* key for hash lookup */
struct hash_node_t *next; /* next node in hash chain */
} hash_node_t;
/*
* hash() - Hash function returns a hash number for a given key.
*
* tptr: Pointer to a hash table
* key: The key to create a hash number for
*/
static int hash(hash_t *tptr, const char *key) {
int i=0;
int hashvalue;
while (*key != '\0')
i=(i<<3)+(*key++ - '0');
hashvalue = (((i*1103515249)>>tptr->downshift) & tptr->mask);
if (hashvalue < 0) {
hashvalue = 0;
}
return hashvalue;
}
/*
* rebuild_table() - Create new hash table when old one fills up.
*
* tptr: Pointer to a hash table
*/
static void rebuild_table(hash_t *tptr) {
hash_node_t **old_bucket, *old_hash, *tmp;
int old_size, h, i;
old_bucket=tptr->bucket;
old_size=tptr->size;
/* create a new table and rehash old buckets */
hash_init(tptr, old_size<<1);
for (i=0; i<old_size; i++) {
old_hash=old_bucket[i];
while(old_hash) {
tmp=old_hash;
old_hash=old_hash->next;
h=hash(tptr, tmp->key);
tmp->next=tptr->bucket[h];
tptr->bucket[h]=tmp;
tptr->entries++;
} /* while */
} /* for */
/* free memory used by old table */
free(old_bucket);
return;
}
/*
* hash_init() - Initialize a new hash table.
*
* tptr: Pointer to the hash table to initialize
* buckets: The number of initial buckets to create
*/
void hash_init(hash_t *tptr, int buckets) {
/* make sure we allocate something */
if (buckets==0)
buckets=16;
/* initialize the table */
tptr->entries=0;
tptr->size=2;
tptr->mask=1;
tptr->downshift=29;
/* ensure buckets is a power of 2 */
while (tptr->size<buckets) {
tptr->size<<=1;
tptr->mask=(tptr->mask<<1)+1;
tptr->downshift--;
} /* while */
/* allocate memory for table */
tptr->bucket=(hash_node_t **) calloc(tptr->size, sizeof(hash_node_t *));
return;
}
/*
* hash_lookup() - Lookup an entry in the hash table and return a pointer to
* it or HASH_FAIL if it wasn't found.
*
* tptr: Pointer to the hash table
* key: The key to lookup
*/
int hash_lookup(hash_t *tptr, const char *key) {
int h;
hash_node_t *node;
/* find the entry in the hash table */
h=hash(tptr, key);
for (node=tptr->bucket[h]; node!=NULL; node=node->next) {
if (!strcmp(node->key, key))
break;
}
/* return the entry if it exists, or HASH_FAIL */
return(node ? node->data : HASH_FAIL);
}
/*
* hash_insert() - Insert an entry into the hash table. If the entry already
* exists return a pointer to it, otherwise return HASH_FAIL.
*
* tptr: A pointer to the hash table
* key: The key to insert into the hash table
* data: A pointer to the data to insert into the hash table
*/
int hash_insert(hash_t *tptr, const char *key, int data) {
int tmp;
hash_node_t *node;
int h;
/* check to see if the entry exists */
if ((tmp=hash_lookup(tptr, key)) != HASH_FAIL)
return(tmp);
/* expand the table if needed */
while (tptr->entries>=HASH_LIMIT*tptr->size)
rebuild_table(tptr);
/* insert the new entry */
h=hash(tptr, key);
node=(struct hash_node_t *) malloc(sizeof(hash_node_t));
node->data=data;
node->key=key;
node->next=tptr->bucket[h];
tptr->bucket[h]=node;
tptr->entries++;
return HASH_FAIL;
}
/*
* hash_delete() - Remove an entry from a hash table and return a pointer
* to its data or HASH_FAIL if it wasn't found.
*
* tptr: A pointer to the hash table
* key: The key to remove from the hash table
*/
int hash_delete(hash_t *tptr, const char *key) {
hash_node_t *node, *last;
int data;
int h;
/* find the node to remove */
h=hash(tptr, key);
for (node=tptr->bucket[h]; node; node=node->next) {
if (!strcmp(node->key, key))
break;
}
/* Didn't find anything, return HASH_FAIL */
if (node==NULL)
return HASH_FAIL;
/* if node is at head of bucket, we have it easy */
if (node==tptr->bucket[h])
tptr->bucket[h]=node->next;
else {
/* find the node before the node we want to remove */
for (last=tptr->bucket[h]; last && last->next; last=last->next) {
if (last->next==node)
break;
}
last->next=node->next;
}
/* free memory and return the data */
data=node->data;
free(node);
return(data);
}
/*
* hash_destroy() - Delete the entire table, and all remaining entries.
*
*/
void hash_destroy(hash_t *tptr) {
hash_node_t *node, *last;
int i;
for (i=0; i<tptr->size; i++) {
node = tptr->bucket[i];
while (node != NULL) {
last = node;
node = node->next;
free(last);
}
}
/* free the entire array of buckets */
if (tptr->bucket != NULL) {
free(tptr->bucket);
memset(tptr, 0, sizeof(hash_t));
}
}
/*
* alos() - Find the average length of search.
*
* tptr: Pointer to a hash table
*/
static float alos(hash_t *tptr) {
int i,j;
float alos=0;
hash_node_t *node;
for (i=0; i<tptr->size; i++) {
for (node=tptr->bucket[i], j=0; node!=NULL; node=node->next, j++);
if (j)
alos+=((j*(j+1))>>1);
} /* for */
return(tptr->entries ? alos/tptr->entries : 0);
}
/*
* hash_stats() - Return a string with stats about a hash table.
*
* tptr: A pointer to the hash table
*/
char * hash_stats(hash_t *tptr) {
static char buf[1024];
sprintf(buf, "%u slots, %u entries, and %1.2f ALOS",
(int)tptr->size, (int)tptr->entries, alos(tptr));
return(buf);
}