3 * Generic hash table routines. Uses integer keys to store char * values.
5 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
16 /* Create a hash table. The size is just a hint, not a maximum. */
18 struct hash *create_hash(size)
23 h = (struct hash *) malloc(sizeof(struct hash));
25 h->data = (struct bucket **) malloc(size * sizeof(char *));
26 bzero(h->data, size * sizeof(char *));
30 /* Lookup an object in the hash table. Returns the value associated with
31 * the key, or NULL (thus NULL is not a very good value to store...)
34 char *hash_lookup(h, key)
38 register struct bucket *b;
40 b = h->data[key % h->size];
41 while (b && b->key != key)
43 if (b && b->key == key)
50 /* Update an existing object in the hash table. Returns 1 if the object
51 * existed, or 0 if not.
54 int hash_update(h, key, value)
59 register struct bucket *b;
61 b = h->data[key % h->size];
62 while (b && b->key != key)
64 if (b && b->key == key) {
72 /* Store an item in the hash table. Returns 0 if the key was not previously
73 * there, or 1 if it was.
76 int hash_store(h, key, value)
81 register struct bucket *b, **p;
83 p = &(h->data[key % h->size]);
85 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
92 for (b = *p; b && b->key != key; b = *p)
93 p = (struct bucket **) *p;
94 if (b && b->key == key) {
98 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
106 /* Search through the hash table for a given value. For each piece of
107 * data with that value, call the callback proc with the corresponding key.
110 hash_search(h, value, callback)
112 register char *value;
115 register struct bucket *b, **p;
117 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
118 for (b = *p; b; b = b->next) {
119 if (b->data == value)
126 /* Step through the hash table, calling the callback proc with each key.
129 hash_step(h, callback, hint)
134 register struct bucket *b, **p;
136 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
137 for (b = *p; b; b = b->next) {
138 (*callback)(b->key, b->data, hint);
144 /* Deallocate all of the memory associated with a table */
149 register struct bucket *b, **p, *b1;
151 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
152 for (b = *p; b; b = b1) {