3 * Generic hash table routines. Uses integer keys to store char * values.
11 /* Create a hash table. The size is just a hint, not a maximum. */
13 struct hash *create_hash(size)
18 h = (struct hash *) malloc(sizeof(struct hash));
20 h->data = (struct bucket **) malloc(size * sizeof(char *));
21 bzero(h->data, size * sizeof(char *));
25 /* Lookup an object in the hash table. Returns the value associated with
26 * the key, or NULL (thus NULL is not a very good value to store...)
29 char *hash_lookup(h, key)
33 register struct bucket *b;
35 b = h->data[key % h->size];
36 while (b && b->key != key)
38 if (b && b->key == key)
45 /* Update an existing object in the hash table. Returns 1 if the object
46 * existed, or 0 if not.
49 int hash_update(h, key, value)
54 register struct bucket *b;
56 b = h->data[key % h->size];
57 while (b && b->key != key)
59 if (b && b->key == key) {
67 /* Store an item in the hash table. Returns 0 if the key was not previously
68 * there, or 1 if it was.
71 int hash_store(h, key, value)
76 register struct bucket *b, **p;
78 p = &(h->data[key % h->size]);
80 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
87 for (b = *p; b && b->key != key; b = *p)
88 p = (struct bucket **) *p;
89 if (b && b->key == key) {
93 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
101 /* Search through the hash table for a given value. For each piece of
102 * data with that value, call the callback proc with the corresponding key.
105 hash_search(h, value, callback)
107 register char *value;
110 register struct bucket *b, **p;
112 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
113 for (b = *p; b; b = b->next) {
114 if (b->data == value)
121 /* Step through the hash table, calling the callback proc with each key.
124 hash_step(h, callback, hint)
129 register struct bucket *b, **p;
131 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
132 for (b = *p; b; b = b->next) {
133 (*callback)(b->key, b->data, hint);
139 /* Deallocate all of the memory associated with a table */
144 register struct bucket *b, **p, *b1;
146 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
147 for (b = *p; b; b = b1) {