3 * Generic hash table routines. Uses integer keys to store integer values.
5 * (c) Copyright 1996 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
12 /* #include <moira.h> */
15 struct int_bucket *next;
21 struct int_bucket **data;
24 extern char *malloc();
27 #define int_hash_func(h, key) (key >= 0 ? (key % h->size) : (-key % h->size))
29 /* Create an int_hash table. The size is just a hint, not a maximum. */
31 struct int_hash *create_int_hash(size)
36 h = (struct int_hash *) malloc(sizeof(struct int_hash));
37 if (h == (struct int_hash *) NULL)
38 return((struct int_hash *) NULL);
40 h->data = (struct int_bucket **) malloc(size * sizeof(char *));
41 if (h->data == (struct int_bucket **) NULL) {
43 return((struct int_hash *) NULL);
45 bzero(h->data, size * sizeof(char *));
49 /* Lookup an object in the int_hash table. Returns the value associated with
50 * the key, or NULL (thus NULL is not a very good value to store...)
53 int int_hash_lookup(h, key)
57 register struct int_bucket *b;
59 b = h->data[int_hash_func(h, key)];
60 while (b && b->key != key)
62 if (b && b->key == key)
69 /* Update an existing object in the int_hash table. Returns 1 if the object
70 * existed, or 0 if not.
73 int int_hash_update(h, key, value)
78 register struct int_bucket *b;
80 b = h->data[int_hash_func(h, key)];
81 while (b && b->key != key)
83 if (b && b->key == key) {
91 /* Store an item in the int_hash table. Returns 0 if the key was not previously
92 * there, 1 if it was, or -1 if we ran out of memory.
95 int int_hash_store(h, key, value)
100 register struct int_bucket *b, **p;
102 p = &(h->data[int_hash_func(h, key)]);
104 b = *p = (struct int_bucket *) malloc(sizeof(struct int_bucket));
105 if (b == (struct int_bucket *) NULL)
113 for (b = *p; b && b->key != key; b = *p)
114 p = (struct int_bucket **) *p;
115 if (b && b->key == key) {
119 b = *p = (struct int_bucket *) malloc(sizeof(struct int_bucket));
120 if (b == (struct int_bucket *) NULL)
129 /* Search through the int_hash table for a given value. For each piece of
130 * data with that value, call the callback proc with the corresponding key.
133 int_hash_search(h, value, callback)
138 register struct int_bucket *b, **p;
140 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
141 for (b = *p; b; b = b->next) {
142 if (b->data == value)
149 /* Step through the int_hash table, calling the callback proc with each key.
152 int_hash_step(h, callback, hint)
157 register struct int_bucket *b, **p;
159 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
160 for (b = *p; b; b = b->next) {
161 (*callback)(b->key, b->data, hint);
167 /* Deallocate all of the memory associated with a table */
172 register struct int_bucket *b, **p, *b1;
174 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
175 for (b = *p; b; b = b1) {