]> andersk Git - moira.git/blame - lib/hash.c
declare whoami
[moira.git] / lib / hash.c
CommitLineData
b93ad422 1/* $Header$
2 *
3 * Generic hash table routines. Uses integer keys to store char * values.
4 */
5
6#include <ctype.h>
7#include "sms_app.h"
8#define NULL 0
9
10
11/* Create a hash table. The size is just a hint, not a maximum. */
12
13struct hash *create_hash(size)
14int size;
15{
16 struct hash *h;
17
18 h = (struct hash *) malloc(sizeof(struct hash));
19 h->size = size;
20 h->data = (struct bucket **) malloc(size * sizeof(char *));
21 bzero(h->data, size * sizeof(char *));
22 return(h);
23}
24
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...)
27 */
28
29char *hash_lookup(h, key)
30struct hash *h;
31register int key;
32{
33 register struct bucket *b;
34
35 b = h->data[key % h->size];
36 while (b && b->key != key)
37 b = b->next;
38 if (b && b->key == key)
39 return(b->data);
40 else
41 return(NULL);
42}
43
44
45/* Update an existing object in the hash table. Returns 1 if the object
46 * existed, or 0 if not.
47 */
48
49int hash_update(h, key, value)
50struct hash *h;
51register int key;
52char *value;
53{
54 register struct bucket *b;
55
56 b = h->data[key % h->size];
57 while (b && b->key != key)
58 b = b->next;
59 if (b && b->key == key) {
60 b->data = value;
61 return(1);
62 } else
63 return(0);
64}
65
66
67/* Store an item in the hash table. Returns 0 if the key was not previously
68 * there, or 1 if it was.
69 */
70
71int hash_store(h, key, value)
72struct hash *h;
73register int key;
74char *value;
75{
76 register struct bucket *b, **p;
77
78 p = &(h->data[key % h->size]);
79 if (*p == NULL) {
80 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
81 b->next = NULL;
82 b->key = key;
83 b->data = value;
84 return(0);
85 }
86
87 for (b = *p; b && b->key != key; b = *p)
88 p = (struct bucket **) *p;
89 if (b && b->key == key) {
90 b->data = value;
91 return(1);
92 }
93 b = *p = (struct bucket *) malloc(sizeof(struct bucket));
94 b->next = NULL;
95 b->key = key;
96 b->data = value;
97 return(0);
98}
99
100
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.
103 */
104
105hash_search(h, value, callback)
106struct hash *h;
107register char *value;
108void (*callback)();
109{
110 register struct bucket *b, **p;
111
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)
115 (*callback)(b->key);
116 }
117 }
118}
119
120
0efcee1f 121/* Step through the hash table, calling the callback proc with each key.
122 */
123
07d9123c 124hash_step(h, callback, hint)
0efcee1f 125struct hash *h;
126void (*callback)();
07d9123c 127char *hint;
0efcee1f 128{
129 register struct bucket *b, **p;
130
131 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
132 for (b = *p; b; b = b->next) {
07d9123c 133 (*callback)(b->key, b->data, hint);
0efcee1f 134 }
135 }
136}
137
138
b93ad422 139/* Deallocate all of the memory associated with a table */
140
141hash_destroy(h)
142struct hash *h;
143{
144 register struct bucket *b, **p, *b1;
145
146 for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
147 for (b = *p; b; b = b1) {
148 b1 = b->next;
149 free(b);
150 }
151 }
152}
This page took 0.071317 seconds and 5 git commands to generate.