-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.c
271 lines (225 loc) · 5.53 KB
/
hashmap.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Generic map implementation.
*/
#include "hashmap.h"
#include <stdlib.h>
#define INITIAL_SIZE (256)
#define MAX_CHAIN_LENGTH (8)
#define M_NEW(type) (type *) malloc(sizeof(type))
#define C_NEW(count, type) (type *) calloc(count, sizeof(type))
/* We need to keep keys and values */
typedef struct {
map_key_t key;
int in_use;
void *data;
} map_elem_t;
/* A hashmap has some maximum size and current size,
* as well as the data to hold. */
struct map_s {
int table_size;
int size;
map_elem_t *data;
};
/*
* Return an empty hashmap, or NULL on failure.
*/
map_t *
hashmap_new(void)
{
map_t* m = M_NEW(map_t);
if (m != NULL) {
m->data = C_NEW(INITIAL_SIZE, map_elem_t);
if (m->data) {
m->table_size = INITIAL_SIZE;
m->size = 0;
} else {
hashmap_free(m);
return NULL;
}
}
return m;
}
/*
* Hashing function for a string
*/
static inline unsigned int
hashmap_hash_int(const map_t * const m, const map_key_t key)
{
return key % m->table_size;
}
/*
* Return the integer of the location in data
* to store the point to the item, or MAP_FULL.
*/
static int
hashmap_hash(const map_t * const m, const map_key_t key)
{
int curr;
int i;
/* If full, return immediately */
if (m->size >= (m->table_size / 2))
return MAP_FULL;
/* Find the best index */
curr = hashmap_hash_int(m, key);
/* Linear probing */
for (i = 0; i < MAX_CHAIN_LENGTH; i++){
if (m->data[curr].in_use == 0)
return curr;
if (m->data[curr].in_use == 1 && (m->data[curr].key == key))
return curr;
curr = (curr + 1) % m->table_size;
}
return MAP_FULL;
}
/*
* Doubles the size of the hashmap, and rehashes all the elements
*/
static int
hashmap_rehash(map_t * const m)
{
int i;
int old_size;
map_elem_t *curr;
/* Setup the new elements */
int new_size = 2 * m->table_size;
map_elem_t *new_elem = C_NEW(new_size, map_elem_t);
if (new_elem == NULL)
return MAP_OMEM;
/* Update the array */
curr = m->data;
m->data = new_elem;
/* Update the size */
old_size = m->table_size;
m->table_size = new_size;
m->size = 0;
/* Rehash the elements */
for (i = 0; i < old_size; i++) {
int status;
if (curr[i].in_use == 0)
continue;
status = hashmap_put(m, curr[i].key, curr[i].data);
if (status != MAP_OK)
return status;
}
free(curr);
return MAP_OK;
}
/*
* Add a pointer to the hashmap with some key
*/
int
hashmap_put(map_t * const in, const map_key_t key, void * const value)
{
int index;
map_t* m;
/* Cast the hashmap */
m = (map_t *) in;
/* Find a place to put our value */
index = hashmap_hash(in, key);
while (index == MAP_FULL) {
if (hashmap_rehash(in) == MAP_OMEM) {
return MAP_OMEM;
}
index = hashmap_hash(in, key);
}
/* Set the data */
m->data[index].data = value;
m->data[index].key = key;
m->data[index].in_use = 1;
m->size++;
return MAP_OK;
}
/*
* Get your pointer out of the hashmap with a key
*/
int
hashmap_get(const map_t * const m, const map_key_t key, void **arg)
{
int curr;
int i;
/* Find data location */
curr = hashmap_hash_int(m, key);
/* Linear probing, if necessary */
for (i = 0; i < MAX_CHAIN_LENGTH; i++) {
int in_use = m->data[curr].in_use;
if (in_use == 1) {
if (m->data[curr].key == key) {
*arg = (m->data[curr].data);
return MAP_OK;
}
}
curr = (curr + 1) % m->table_size;
}
*arg = NULL;
/* Not found */
return MAP_MISSING;
}
/*
* Iterate the function parameter over each element in the hashmap. The
* additional (void *) argument is passed to the function as its second
* argument and the hashmap element is the first.
*/
int
hashmap_iterate(const map_t * const m, map_visit_func_p f, void *f_arg)
{
int i;
/* On empty hashmap, return immediately */
if (hashmap_length(m) <= 0)
return MAP_MISSING;
/* Linear probing */
for (i = 0; i < m->table_size; i++) {
if (m->data[i].in_use != 0) {
void *data = m->data[i].data;
int status = f(data, f_arg);
if (status != MAP_OK) {
return status;
}
}
}
return MAP_OK;
}
/*
* Remove an element with that key from the map
*/
int
hashmap_remove(map_t * const m, const map_key_t key)
{
int i;
int curr;
/* Find key */
curr = hashmap_hash_int(m, key);
/* Linear probing, if necessary */
for (i = 0; i < MAX_CHAIN_LENGTH; i++) {
int in_use = m->data[curr].in_use;
if (in_use == 1) {
if (m->data[curr].key == key) {
/* Blank out the fields */
m->data[curr].in_use = 0;
m->data[curr].data = NULL;
m->data[curr].key = 0;
/* Reduce the size */
m->size--;
return MAP_OK;
}
}
curr = (curr + 1) % m->table_size;
}
/* Data not found */
return MAP_MISSING;
}
/* Deallocate the hashmap */
void
hashmap_free(map_t * const m)
{
free(m->data);
free(m);
}
/* Return the length of the hashmap */
int
hashmap_length(const map_t * const m)
{
if (m != NULL)
return m->size;
else
return 0;
}