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