-
Notifications
You must be signed in to change notification settings - Fork 1
/
ht.c
247 lines (209 loc) · 7.59 KB
/
ht.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <string.h>
#include <stdlib.h>
#include "ht.h"
#include "xmalloc.h"
/* **************************************************************** */
/* public domain code by Jerry Coffin, with improvements by HenkJan */
/* Wolthuias, Ken Murchison and Pierre Gentile (xmalloc cannot */
/* return NULL). */
/* **************************************************************** */
/* ================================================================= */
/* Initialize the hash table to the size asked for. Allocates space */
/* for the correct number of pointers and sets them to NULL. */
/* ================================================================= */
ht_t *
ht_new(ht_t *table, size_t size)
{
size_t i;
bucket **temp;
table->size = size;
table->table = (bucket **)xmalloc(sizeof(bucket *) * size);
temp = table->table;
for (i = 0; i < size; i++)
temp[i] = NULL;
return table;
}
/* ============================================================= */
/* Hashes a string to produce an unsigned short, which should be */
/* sufficient for most purposes. */
/* ============================================================= */
unsigned
hash(char *string)
{
unsigned ret_val = 0;
while (*string)
{
int i;
i = (int)*string;
ret_val ^= i;
ret_val <<= 1;
string++;
}
return ret_val;
}
/* =============================================================== */
/* Insert 'key' into hash table. */
/* Returns pointer to old data associated with the key, if any, or */
/* NULL if the key wasn't in the table previously. */
/* =============================================================== */
void *
ht_insert(ht_t *table, char *key, void *data)
{
unsigned val = hash(key) % table->size;
bucket *ptr;
/* NULL means this bucket hasn't been used yet. We'll simply */
/* allocate space for our new bucket and put our data there, with */
/* the table pointing at it. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (NULL == (table->table)[val])
{
(table->table)[val] = (bucket *)xmalloc(sizeof(bucket));
(table->table)[val]->key = xstrdup(key);
(table->table)[val]->next = NULL;
(table->table)[val]->data = data;
return (table->table)[val]->data;
}
/* This spot in the table is already in use. See if the current string */
/* has already been inserted, and if so, increment its count. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
for (ptr = (table->table)[val]; NULL != ptr; ptr = ptr->next)
if (0 == strcmp(key, ptr->key))
{
void *old_data;
old_data = ptr->data;
ptr->data = data;
return old_data;
}
/* This key must not be in the table yet. We'll add it to the head of */
/* the list at this spot in the hash table. Speed would be */
/* slightly improved if the list was kept sorted instead. In this case, */
/* this code would be moved into the loop above, and the insertion would */
/* take place as soon as it was determined that the present key in the */
/* list was larger than this one. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
ptr = (bucket *)xmalloc(sizeof(bucket));
ptr->key = xstrdup(key);
ptr->data = data;
ptr->next = (table->table)[val];
(table->table)[val] = ptr;
return data;
}
/* ============================================================== */
/* Look up a key and return the associated data. Returns NULL if */
/* the key is not in the table. */
/* ============================================================== */
void *
ht_search(ht_t *table, char *key)
{
unsigned val = hash(key) % table->size;
bucket *ptr;
if (NULL == (table->table)[val])
return NULL;
for (ptr = (table->table)[val]; NULL != ptr; ptr = ptr->next)
{
if (0 == strcmp(key, ptr->key))
return ptr->data;
}
return NULL;
}
/* ====================================================== */
/* Delete a key from the hash table and return associated */
/* data, or NULL if not present. */
/* ====================================================== */
void *
ht_delete(ht_t *table, char *key)
{
unsigned val = hash(key) % table->size;
void *data;
bucket *ptr, *last = NULL;
if (NULL == (table->table)[val])
return NULL;
/* Traverse the list, keeping track of the previous node in the list. */
/* When we find the node to delete, we set the previous node's next */
/* pointer to point to the node after ourself instead. We then delete */
/* the key from the present node, and return a pointer to the data it */
/* contains. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
for (last = NULL, ptr = (table->table)[val]; NULL != ptr;
last = ptr, ptr = ptr->next)
{
if (0 == strcmp(key, ptr->key))
{
if (last != NULL)
{
data = ptr->data;
last->next = ptr->next;
free(ptr->key);
free(ptr);
return data;
}
/* If 'last' still equals NULL, it means that we need to */
/* delete the first node in the list. This simply consists */
/* of putting our own 'next' pointer in the array holding */
/* the head of the list. We then dispose of the current */
/* node as above. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""" */
else
{
data = ptr->data;
(table->table)[val] = ptr->next;
free(ptr->key);
free(ptr);
return data;
}
}
}
/* If we get here, it means we didn't find the item in the table. */
/* Signal this by returning NULL. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
return NULL;
}
/* ===================================================================== */
/* Frees a complete table by iterating over it and freeing each node. */
/* the second parameter is the address of a function it will call with a */
/* pointer to the data associated with each node. This function is */
/* responsible for freeing the data, or doing whatever is needed with */
/* it. */
/* ===================================================================== */
void
ht_free(ht_t *table, void (*func)(void *))
{
unsigned i;
bucket *ptr, *temp;
for (i = 0; i < table->size; i++)
{
ptr = (table->table)[i];
while (ptr)
{
temp = ptr;
ptr = ptr->next;
free(temp->key);
if (func)
func(temp->data);
free(temp);
}
}
free(table->table);
table->table = NULL;
table->size = 0;
}
/* ================================================================== */
/* Simply invokes the function given as the second parameter for each */
/* node in the table, passing it the key and the associated data. */
/* ================================================================== */
void
ht_foreach(ht_t *table, void (*func)(char *, void *))
{
unsigned i;
bucket *temp;
for (i = 0; i < table->size; i++)
{
if ((table->table)[i] != NULL)
{
for (temp = (table->table)[i]; NULL != temp; temp = temp->next)
{
func(temp->key, temp->data);
}
}
}
}