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>
17 /* Create a hash table. The size is just a hint, not a maximum. */
19 struct hash *create_hash(size)
24 h = (struct hash *) malloc(sizeof(struct hash));
26 h->data = (struct bucket **) malloc(size * sizeof(char *));
27 bzero(h->data, size * sizeof(char *));
31 /* Lookup an object in the hash table. Returns the value associated with
32 * the key, or NULL (thus NULL is not a very good value to store...)
35 char *hash_lookup(h, key)
39 register struct bucket *b;
41 b = h->data[key % h->size];
42 while (b && b->key != key)
44 if (b && b->key == key)
51 /* Update an existing object in the hash table. Returns 1 if the object
52 * existed, or 0 if not.
55 int hash_update(h, key, value)
60 register struct bucket *b;
62 b = h->data[key % h->size];
63 while (b && b->key != key)
65 if (b && b->key == key) {
73 /* Store an item in the hash table. Returns 0 if the key was not previously
74 * there, or 1 if it was.
77 int hash_store(h, key, value)
82 register struct bucket *b, **p;
84 p = &(h->data[key % h->size]);
86 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
93 for (b = *p; b && b->key != key; b = *p)
94 p = (struct bucket **) *p;
95 if (b && b->key == key) {
99 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
107 /* Search through the hash table for a given value. For each piece of
108 * data with that value, call the callback proc with the corresponding key.
111 hash_search(h, value, callback)
113 register char *value;
116 register struct bucket *b, **p;
118 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
119 for (b = *p; b; b = b->next) {
120 if (b->data == value)
127 /* Step through the hash table, calling the callback proc with each key.
130 hash_step(h, callback, hint)
135 register struct bucket *b, **p;
137 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
138 for (b = *p; b; b = b->next) {
139 (*callback)(b->key, b->data, hint);
145 /* Deallocate all of the memory associated with a table */
150 register struct bucket *b, **p, *b1;
152 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
153 for (b = *p; b; b = b1) {