4 * Terry Jones & Jordan Hubbard
6 * PCS Computer Systeme, GmbH.
10 * All rights reserved.
12 * This is unsupported software and is subject to change without notice.
13 * the author makes no representations about the suitability of this software
14 * for any purpose. It is supplied "as is" without express or implied
17 * Permission to use, copy, modify, and distribute this software and its
18 * documentation for any purpose and without fee is hereby granted, provided
19 * that the above copyright notice appear in all copies and that both that
20 * copyright notice and this permission notice appear in supporting
21 * documentation, and that the name of the author not be used in
22 * advertising or publicity pertaining to distribution of the software
23 * without specific, written prior permission.
25 * $FreeBSD: src/lib/libc/stdlib/strhash.c,v 1.8 1999/09/05 17:42:45 peter Exp $
26 * $DragonFly: src/lib/libc/stdlib/Attic/strhash.c,v 1.3 2003/09/06 08:10:46 asmodai Exp $
30 * This is a fairly simple open addressing hash scheme.
31 * Terry did all the code, I just did the spec.
32 * Thanks again, you crazy Aussie..
38 * Revision 2.0 90/03/26 01:44:26 jkh
41 * Revision 1.8 90/03/09 19:22:35 jkh
42 * Fixed bogus comment.
44 * Revision 1.7 90/03/09 19:01:08 jkh
45 * Added comments, GPL.
47 * Revision 1.6 90/03/08 17:55:58 terry
48 * Rearranged hash_purge to be a tiny bit more efficient.
49 * Added verbose option to hash_stats.
51 * Revision 1.5 90/03/08 17:19:54 terry
52 * Added hash_purge. Added arg to hash_traverse. Changed all
55 * Revision 1.4 90/03/08 12:02:35 terry
56 * Fixed problems with allocation that I screwed up last night.
57 * Changed bucket lists to be singly linked. Thanks to JKH, my hero.
59 * Revision 1.3 90/03/07 21:33:33 terry
60 * Cleaned up a few decls to keep gcc -Wall quiet.
62 * Revision 1.2 90/03/07 21:14:53 terry
63 * Comments. Added HASH_STATS define. Removed hash_find()
66 * Revision 1.1 90/03/07 20:49:45 terry
76 #include <sys/types.h>
79 #define HASH_NULL (hash_table *)0
80 #define NODE_NULL (hash_node *)0
81 #define GENERIC_NULL (void *)0
86 static int _hash(int size, char *key);
87 static hash_node *list_find(caddr_t key, hash_node *head);
93 * Malloc room for a new hash table and then room for its
94 * bucket pointers. Then set all the buckets to
95 * point to 0. Return the address of the new table.
101 hash_table *new = (hash_table *)malloc(sizeof(hash_table));
103 if (!new || size < 0){
111 if (!(new->buckets = (hash_node **)malloc(size * sizeof(hash_node *)))){
115 for (i = 0; i < size; i++){
116 new->buckets[i] = NODE_NULL;
127 * Find the key in the linked list pointed to by head.
130 list_find(caddr_t key, hash_node *head)
133 if (!strcmp(head->key, key)){
145 * Compute the hash value for the given key.
148 _hash(int size, char *key)
150 unsigned int h = 0x0;
153 h = (h << 1) ^ (h ^ (unsigned char) *key++);
163 * Find the key and (if it's there) remove it entirely.
164 * The function (*nukefunc)() is in charge of disposing
165 * of the storage help by the data associated with the node.
168 hash_destroy(hash_table *table, char *key, void (*nukefunc)())
170 int bucket = _hash(table->size, key);
171 hash_node *found = table->buckets[bucket];
172 hash_node *to_free = NODE_NULL;
178 if (!strcmp(found->key, key)) {
180 * It was the head of the list.
182 table->buckets[bucket] = found->next;
187 * Walk the list, looking one ahead.
189 while (found->next) {
190 if (!strcmp(found->next->key, key)) {
191 to_free = found->next;
192 found->next = found->next->next;
204 (*nukefunc)(to_free->key, to_free->data);
213 * Search the table for the given key. Then:
215 * 1) If you find it and there is no replacement function, just
216 * return what you found. (This is a simple search).
217 * 2) If you find it and there is a replacement function, run
218 * the function on the data you found, and replace the old
219 * data with whatever is passed in datum. Return 0.
220 * 3) If you don't find it and there is some datum, insert a
221 * new item into the table. Insertions go at the front of
222 * the bucket. Return 0.
223 * 4) Otherwise just return 0.
227 hash_search(hash_table *table, caddr_t key, void *datum,
228 void (*replace_func)())
230 int bucket = _hash(table->size, key);
231 hash_node *found = list_find(key, table->buckets[bucket]);
238 (*replace_func)(found->data);
245 static int assign_key();
247 hash_node *new = (hash_node *)malloc(sizeof(hash_node));
249 if (!new || !assign_key(key, new)){
253 new->next = table->buckets[bucket];
254 table->buckets[bucket] = new;
265 * Set the key value of a node to be 'key'. Get some space from
266 * malloc and copy it in etc. Return 1 if all is well, 0 otherwise.
269 assign_key(char *key, hash_node *node)
275 if (!(node->key = (char *)malloc(strlen(key) + 1))){
280 strcat(node->key, key);
287 * Traverse the hash table and run the function func on the
288 * data found at each node and the argument we're passed for it.
291 hash_traverse(hash_table *table, int (*func)(), void *arg)
294 int size = table->size;
299 for (i = 0; i < size; i++) {
300 hash_node *n = table->buckets[i];
302 if ((*func)(n->key, n->data, arg) == 0)
313 * Run through the entire hash table. Call purge_func
314 * on the data found at each node, and then free the node.
315 * Set all the bucket pointers to 0.
318 hash_purge(hash_table *table, void (*purge_func)(char *p1, void *p2))
321 int size = table->size;
323 for (i = 0; i < size; i++) {
324 hash_node *n = table->buckets[i];
327 hash_node *to_free = n;
329 (*purge_func)(n->key, n->data);
334 table->buckets[i] = NODE_NULL;
340 #define min(a, b) (a) < (b) ? (a) : (b)
345 * Print statistics about the current table allocation to stdout.
348 hash_stats(hash_table *table, int verbose)
351 int total_elements = 0;
352 int non_empty_buckets = 0;
356 int size = table->size;
358 if (!(counts = (int *)malloc(size * sizeof(int)))){
359 fprintf(stderr, "malloc returns 0\n");
363 for (i = 0; i < size; i++){
365 hash_node *n = table->buckets[i];
372 printf("bucket %2d: ", i);
376 printf(" %s", n->key);
382 total_elements += counts[i];
383 if (counts[i] > max_count){
384 max_count = counts[i];
387 else if (counts[i] == max_count){
391 if (counts[i] && verbose){
392 printf(" (%d)\n", counts[i]);
397 printf("%d element%s in storage.\n", total_elements, total_elements == 1 ? "" : "s");
400 printf("%d of %d (%.2f%%) buckets are in use\n", non_empty_buckets, size,
401 (double)100 * (double)non_empty_buckets / (double)(size));
402 printf("the maximum number of elements in a bucket is %d (%d times)\n", max_count, max_repeats);
403 printf("average per bucket is %f\n", (double)total_elements / (double)non_empty_buckets);
404 printf("optimal would be %f\n", (double)total_elements / (double)(min(size, total_elements)));