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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/************************************************************************
Copyright 1988, 1991 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of Carnegie Mellon University not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission.
CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
************************************************************************/
#ifndef lint
static char rcsid[] = "$Id: hash.c,v 1.4 2003/02/18 13:14:43 jmc Exp $";
#endif
/*
* Generalized hash table ADT
*
* Provides multiple, dynamically-allocated, variable-sized hash tables on
* various data and keys.
*
* This package attempts to follow some of the coding conventions suggested
* by Bob Sidebotham and the AFS Clean Code Committee of the
* Information Technology Center at Carnegie Mellon.
*/
#include <sys/types.h>
#include <stdlib.h>
#ifndef USE_BFUNCS
#include <memory.h>
/* Yes, memcpy is OK here (no overlapped copies). */
#define bcopy(a,b,c) memcpy(b,a,c)
#define bzero(p,l) memset(p,0,l)
#define bcmp(a,b,c) memcmp(a,b,c)
#endif
#include "hash.h"
#define TRUE 1
#define FALSE 0
#ifndef NULL
#define NULL 0
#endif
/*
* This can be changed to make internal routines visible to debuggers, etc.
*/
#ifndef PRIVATE
#define PRIVATE static
#endif
#ifdef __STDC__
#define P(args) args
#else
#define P(args) ()
#endif
PRIVATE void hashi_FreeMembers P((hash_member *, hash_freefp));
#undef P
/*
* Hash table initialization routine.
*
* This routine creates and initializes a hash table of size "tablesize"
* entries. Successful calls return a pointer to the hash table (which must
* be passed to other hash routines to identify the hash table). Failed
* calls return NULL.
*/
hash_tbl *
hash_Init(tablesize)
unsigned int tablesize;
{
hash_tbl *hashtblptr;
unsigned int totalsize;
if (tablesize > 0) {
totalsize = sizeof(hash_tbl)
+ sizeof(hash_member *) * (tablesize - 1);
hashtblptr = (hash_tbl *) malloc(totalsize);
if (hashtblptr) {
bzero((char *) hashtblptr, totalsize);
hashtblptr->size = tablesize; /* Success! */
hashtblptr->bucketnum = 0;
hashtblptr->member = (hashtblptr->table)[0];
}
} else {
hashtblptr = NULL; /* Disallow zero-length tables */
}
return hashtblptr; /* NULL if failure */
}
/*
* Frees an entire linked list of bucket members (used in the open
* hashing scheme). Does nothing if the passed pointer is NULL.
*/
PRIVATE void
hashi_FreeMembers(bucketptr, free_data)
hash_member *bucketptr;
hash_freefp free_data;
{
hash_member *nextbucket;
while (bucketptr) {
nextbucket = bucketptr->next;
(*free_data) (bucketptr->data);
free((char *) bucketptr);
bucketptr = nextbucket;
}
}
/*
* This routine re-initializes the hash table. It frees all the allocated
* memory and resets all bucket pointers to NULL.
*/
void
hash_Reset(hashtable, free_data)
hash_tbl *hashtable;
hash_freefp free_data;
{
hash_member **bucketptr;
unsigned int i;
bucketptr = hashtable->table;
for (i = 0; i < hashtable->size; i++) {
hashi_FreeMembers(*bucketptr, free_data);
*bucketptr++ = NULL;
}
hashtable->bucketnum = 0;
hashtable->member = (hashtable->table)[0];
}
/*
* Generic hash function to calculate a hash code from the given string.
*
* For each byte of the string, this function left-shifts the value in an
* accumulator and then adds the byte into the accumulator. The contents of
* the accumulator is returned after the entire string has been processed.
* It is assumed that this result will be used as the "hashcode" parameter in
* calls to other functions in this package. These functions automatically
* adjust the hashcode for the size of each hashtable.
*
* This algorithm probably works best when the hash table size is a prime
* number.
*
* Hopefully, this function is better than the previous one which returned
* the sum of the squares of all the bytes. I'm still open to other
* suggestions for a default hash function. The programmer is more than
* welcome to supply his/her own hash function as that is one of the design
* features of this package.
*/
unsigned int
hash_HashFunction(string, len)
unsigned char *string;
unsigned int len;
{
unsigned int accum;
accum = 0;
for (; len > 0; len--) {
accum <<= 1;
accum += (unsigned) (*string++ & 0xFF);
}
return accum;
}
/*
* Returns TRUE if at least one entry for the given key exists; FALSE
* otherwise.
*/
int
hash_Exists(hashtable, hashcode, compare, key)
hash_tbl *hashtable;
unsigned int hashcode;
hash_cmpfp compare;
hash_datum *key;
{
hash_member *memberptr;
memberptr = (hashtable->table)[hashcode % (hashtable->size)];
while (memberptr) {
if ((*compare) (key, memberptr->data)) {
return TRUE; /* Entry does exist */
}
memberptr = memberptr->next;
}
return FALSE; /* Entry does not exist */
}
/*
* Insert the data item "element" into the hash table using "hashcode"
* to determine the bucket number, and "compare" and "key" to determine
* its uniqueness.
*
* If the insertion is successful 0 is returned. If a matching entry
* already exists in the given bucket of the hash table, or some other error
* occurs, -1 is returned and the insertion is not done.
*/
int
hash_Insert(hashtable, hashcode, compare, key, element)
hash_tbl *hashtable;
unsigned int hashcode;
hash_cmpfp compare;
hash_datum *key, *element;
{
hash_member *temp;
hashcode %= hashtable->size;
if (hash_Exists(hashtable, hashcode, compare, key)) {
return -1; /* At least one entry already exists */
}
temp = (hash_member *) malloc(sizeof(hash_member));
if (!temp)
return -1; /* malloc failed! */
temp->data = element;
temp->next = (hashtable->table)[hashcode];
(hashtable->table)[hashcode] = temp;
return 0; /* Success */
}
/*
* Delete all data elements which match the given key. If at least one
* element is found and the deletion is successful, 0 is returned.
* If no matching elements can be found in the hash table, -1 is returned.
*/
int
hash_Delete(hashtable, hashcode, compare, key, free_data)
hash_tbl *hashtable;
unsigned int hashcode;
hash_cmpfp compare;
hash_datum *key;
hash_freefp free_data;
{
hash_member *memberptr, *tempptr;
hash_member *previous = NULL;
int retval;
retval = -1;
hashcode %= hashtable->size;
/*
* Delete the first member of the list if it matches. Since this moves
* the second member into the first position we have to keep doing this
* over and over until it no longer matches.
*/
memberptr = (hashtable->table)[hashcode];
while (memberptr && (*compare) (key, memberptr->data)) {
(hashtable->table)[hashcode] = memberptr->next;
/*
* Stop hashi_FreeMembers() from deleting the whole list!
*/
memberptr->next = NULL;
hashi_FreeMembers(memberptr, free_data);
memberptr = (hashtable->table)[hashcode];
retval = 0;
}
/*
* Now traverse the rest of the list
*/
if (memberptr) {
previous = memberptr;
memberptr = memberptr->next;
}
while (memberptr) {
if ((*compare) (key, memberptr->data)) {
tempptr = memberptr;
previous->next = memberptr = memberptr->next;
/*
* Put the brakes on hashi_FreeMembers(). . . .
*/
tempptr->next = NULL;
hashi_FreeMembers(tempptr, free_data);
retval = 0;
} else {
previous = memberptr;
memberptr = memberptr->next;
}
}
return retval;
}
/*
* Locate and return the data entry associated with the given key.
*
* If the data entry is found, a pointer to it is returned. Otherwise,
* NULL is returned.
*/
hash_datum *
hash_Lookup(hashtable, hashcode, compare, key)
hash_tbl *hashtable;
unsigned int hashcode;
hash_cmpfp compare;
hash_datum *key;
{
hash_member *memberptr;
memberptr = (hashtable->table)[hashcode % (hashtable->size)];
while (memberptr) {
if ((*compare) (key, memberptr->data)) {
return (memberptr->data);
}
memberptr = memberptr->next;
}
return NULL;
}
/*
* Return the next available entry in the hashtable for a linear search
*/
hash_datum *
hash_NextEntry(hashtable)
hash_tbl *hashtable;
{
unsigned int bucket;
hash_member *memberptr;
/*
* First try to pick up where we left off.
*/
memberptr = hashtable->member;
if (memberptr) {
hashtable->member = memberptr->next; /* Set up for next call */
return memberptr->data; /* Return the data */
}
/*
* We hit the end of a chain, so look through the array of buckets
* until we find a new chain (non-empty bucket) or run out of buckets.
*/
bucket = hashtable->bucketnum + 1;
while ((bucket < hashtable->size) &&
!(memberptr = (hashtable->table)[bucket])) {
bucket++;
}
/*
* Check to see if we ran out of buckets.
*/
if (bucket >= hashtable->size) {
/*
* Reset to top of table for next call.
*/
hashtable->bucketnum = 0;
hashtable->member = (hashtable->table)[0];
/*
* But return end-of-table indication to the caller this time.
*/
return NULL;
}
/*
* Must have found a non-empty bucket.
*/
hashtable->bucketnum = bucket;
hashtable->member = memberptr->next; /* Set up for next call */
return memberptr->data; /* Return the data */
}
/*
* Return the first entry in a hash table for a linear search
*/
hash_datum *
hash_FirstEntry(hashtable)
hash_tbl *hashtable;
{
hashtable->bucketnum = 0;
hashtable->member = (hashtable->table)[0];
return hash_NextEntry(hashtable);
}
/*
* Local Variables:
* tab-width: 4
* c-indent-level: 4
* c-argdecl-indent: 4
* c-continued-statement-offset: 4
* c-continued-brace-offset: -4
* c-label-offset: -4
* c-brace-offset: 0
* End:
*/
|