-
Notifications
You must be signed in to change notification settings - Fork 1
/
obhash.c
214 lines (187 loc) · 4.93 KB
/
obhash.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
/* obhash.c
*
* Implementation of generic string hashtable.
*/
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include "obhash.h"
#include "util.h"
#define TABLESIZE 7
typedef struct _hnode hnode_t;
struct _obhash {
void (*obfree)(void*);
hnode_t** chains;
int size;
int load;
};
/* hnode
*
* Node that holds an object, a key, and a pointer to another node.
*/
struct _hnode {
hnode_t* next;
char* key;
void* obj;
};
/**************************static functions**************************/
/** free_chains
*
* Deallocates all of the nodes in array of nodes, including the
* objects and keys, but doesn't free the array itself.
*/
static void free_chains(hnode_t** chains,
int size, void (*obfree)(void*)) {
for (int i = 0; i < size; ++i) {
hnode_t* node = chains[i];
while (node) {
hnode_t* next = node->next;
if (obfree)
obfree(node->obj);
free(node->key);
free(node);
node = next;
}
chains[i] = NULL;
}
}
/** hnode_new
*
* Make a new hnode and tree the next field to NULL. Returns the
* new node.
*/
static hnode_t* hnode_new(char* key, void* obj) {
hnode_t* newnode = malloc(sizeof(hnode_t));
assert(newnode);
newnode->key = key;
newnode->obj = obj;
newnode->next = NULL;
return newnode;
}
/** get_node
*
* For a given string return NULL if the bucket is empty. If a node is
* returned, then if the bool is set to true, it is the node
* containing the key. If false, then the node is the last in the
* chain, and doesn't contain the correct key.
*/
static hnode_t* get_node(hnode_t** chains, int index,
char* key, bool* found) {
hnode_t* node = chains[index];
if (!node)
return NULL;
for (;; node = node->next) {
if (strcmp(key, node->key) == 0) {
*found = true;
return node;
}
if (!node->next)
break;
}
*found = false;
return node;
}
/** add_to_chains
*
* Add an object to the hnode array of chains. Return true if a new
* node is created.
*/
static bool add_to_chains(hnode_t** chains, int size,
char* key, void* obj, void (*obfree)(void*)) {
bool found;
int i = strhash(key) % size;
hnode_t* node = get_node(chains, i, key, &found);
if (!node) {
chains[i] = hnode_new(key, obj);
return true;
} else if (found) {
if (obfree)
obfree(node->obj);
node->obj = obj;
return false;
} else {
node->next = hnode_new(key, obj);
return true;
}
}
/** expand_table
*
* Double the internal array of the hash table.
*/
static void expand_table(obhash_t* table) {
int newsize = table->size * 2 + 1;
hnode_t** newchains = calloc(newsize, sizeof(hnode_t*));
assert(newchains);
for (int i = 0; i < table->size; ++i) {
hnode_t* node = table->chains[i];
while (node) {
hnode_t* next = node->next;
(void) add_to_chains(newchains, newsize,
node->key, node->obj, table->obfree);
free(node);
node = next;
}
}
free(table->chains);
table->chains = newchains;
table->size = newsize;
}
/**************************public functions**************************/
void* obhash_find(obhash_t* table, char* key) {
assert(table && key);
bool found = false;
int index = strhash(key) % table->size;
hnode_t* node = get_node(table->chains, index, key, &found);
if (found)
return node->obj;
return NULL;
}
void obhash_add(obhash_t* table, char* key, void* obj) {
assert(table && key);
if (table->load * 2 > table->size)
expand_table(table);
if (add_to_chains(table->chains, table->size,
key, obj, table->obfree))
++table->load;
}
int obhash_size(obhash_t* table) {
assert(table);
return table->load;
}
void obhash_clear(obhash_t* table) {
assert(table);
free_chains(table->chains, table->size, table->obfree);
table->load = 0;
}
obhash_t* obhash_new(void (*obfree)(void*)) {
obhash_t* table = malloc(sizeof(obhash_t));
assert(table);
table->obfree = obfree;
table->size = TABLESIZE;
table->load = 0;
table->chains = calloc(table->size, sizeof(hnode_t*));
assert(table->chains);
return table;
}
void obhash_free(obhash_t* table) {
if (table) {
free_chains(table->chains, table->size, table->obfree);
free(table->chains);
free(table);
}
}
/****************************obhash_hook*****************************/
#ifdef OBHASH_HOOK
void obhash_hook(obhash_t* table) {
int count = 0;
for (int i = 0; i < table->size; ++i) {
hnode_t* curr = table->chains[i];
while (curr) {
printf("%6d\t%s\n", ++count, curr->key);
curr = curr->next;
}
}
}
#endif /* ifdef OBHASH_HOOK */
/********************************************************************/