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>
13 /* #include <moira.h> */
16 struct int_bucket *next;
22 struct int_bucket **data;
28 #define int_hash_func(h, key) (key >= 0 ? (key % h->size) : (-key % h->size))
30 /* Create an int_hash table. The size is just a hint, not a maximum. */
32 struct int_hash *create_int_hash(size)
37 h = (struct int_hash *) malloc(sizeof(struct int_hash));
38 if (h == (struct int_hash *) NULL)
39 return((struct int_hash *) NULL);
41 h->data = (struct int_bucket **) malloc(size * sizeof(char *));
42 if (h->data == (struct int_bucket **) NULL) {
44 return((struct int_hash *) NULL);
46 memset(h->data, 0, size * sizeof(char *));
50 /* Lookup an object in the int_hash table. Returns the value associated with
51 * the key, or NULL (thus NULL is not a very good value to store...)
54 int int_hash_lookup(h, key)
58 register struct int_bucket *b;
60 b = h->data[int_hash_func(h, key)];
61 while (b && b->key != key)
63 if (b && b->key == key)
70 /* Update an existing object in the int_hash table. Returns 1 if the object
71 * existed, or 0 if not.
74 int int_hash_update(h, key, value)
79 register struct int_bucket *b;
81 b = h->data[int_hash_func(h, key)];
82 while (b && b->key != key)
84 if (b && b->key == key) {
92 /* Store an item in the int_hash table. Returns 0 if the key was not previously
93 * there, 1 if it was, or -1 if we ran out of memory.
96 int int_hash_store(h, key, value)
101 register struct int_bucket *b, **p;
103 p = &(h->data[int_hash_func(h, key)]);
105 b = *p = (struct int_bucket *) malloc(sizeof(struct int_bucket));
106 if (b == (struct int_bucket *) NULL)
114 for (b = *p; b && b->key != key; b = *p)
115 p = (struct int_bucket **) *p;
116 if (b && b->key == key) {
120 b = *p = (struct int_bucket *) malloc(sizeof(struct int_bucket));
121 if (b == (struct int_bucket *) NULL)
130 /* Search through the int_hash table for a given value. For each piece of
131 * data with that value, call the callback proc with the corresponding key.
134 int_hash_search(h, value, callback)
139 register struct int_bucket *b, **p;
141 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
142 for (b = *p; b; b = b->next) {
143 if (b->data == value)
150 /* Step through the int_hash table, calling the callback proc with each key.
153 int_hash_step(h, callback, hint)
158 register struct int_bucket *b, **p;
160 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
161 for (b = *p; b; b = b->next) {
162 (*callback)(b->key, b->data, hint);
168 /* Deallocate all of the memory associated with a table */
173 register struct int_bucket *b, **p, *b1;
175 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
176 for (b = *p; b; b = b1) {