-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
306 lines (254 loc) · 6.42 KB
/
hashtable.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdlib.h>
#include <stdio.h>
#include "hashtable.h"
#define DEFAULT_SIZE 16
#define RESIZE_PERCENTAGE 70
/**
* Initialize hashtable
*/
Hashtable *initialize_hashtable()
{
Hashtable *map = malloc(sizeof(Hashtable));
printf("\nAddress of map value is %18p\n", &(map->val));
printf("Address of map key is %20p\n", &(map->key));
printf("Address of map string key is %13p\n", &(map->str_key));
printf("Address of map size is %19p\n", &(map->size));
printf("Address of map count is %18p\n\n", &(map->count));
map->val = (int *)calloc(DEFAULT_SIZE, sizeof(int));
// Using calloc here instead of malloc breaks a lot of stuff and I should figure out why
map->key = (unsigned long *)calloc(DEFAULT_SIZE, sizeof(unsigned long));
map->str_key = (char **)malloc(DEFAULT_SIZE * sizeof(char *));
printf("Address of first map value is %18p\n", map->val);
printf("Address of first map key is %20p\n", map->key);
printf("Address of first map string key is %13p\n\n", map->str_key);
printf("Pointer-to-pointer address is: %p\n", map->str_key);
printf("Pointer-to-pointer second address is: %p\n", (map->str_key + 1));
printf("Pointer-to-pointer second address is: %p\n", ((map->str_key) + 1));
// We need to initialize the pointers to string to NULL
for (int i = 0; i < DEFAULT_SIZE; i++)
{
*((map->str_key) + i) = NULL;
}
map->size = DEFAULT_SIZE;
map->count = 0;
if (map->val == NULL || map->key == NULL || map->str_key == NULL)
{
printf("Failed to initialize hashtable\n");
}
return map;
}
/**
* Free up allocated memory from hashtable
*/
void free_hashtable(Hashtable **map)
{
printf("Freeing memory!\n");
free((*map)->val);
(*map)->val = NULL;
free((*map)->key);
(*map)->key = NULL;
free((*map)->str_key);
(*map)->str_key = NULL;
if ((*map)->val != NULL || (*map)->key != NULL || (*map)->str_key != NULL)
{
printf("Failed to free hashtable memory\n");
}
free((*map));
(*map) = NULL;
if ((*map) != NULL)
{
printf("Failed to free hashtable\n");
}
}
/**
* djb2 hash function
*/
unsigned long hash(char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; // hash * 33 + c
return hash;
}
/**
* Return -1 if the current hash causes a collision
* Returns a correct index otherwize
*/
int collision(Hashtable *map, unsigned long h)
{
// Get index from hash of key
int i = (int)(h % (unsigned long)(map->size));
// Check if value at index i is 0
if (*((int *)(map->val) + i) == 0)
{
map->count += 1;
return i;
}
// Compare stored hash key to curernt hash at index i
else if (*((map->key) + i) == h)
{
return i;
}
else
{
printf("Collision detected for hash %lu\n", h);
return -1;
}
}
/**
* Return -1 if the current hash causes a collision
* Return 0 if the current index is empty
* Returns a correct index otherwize
*/
int search_collision(Hashtable *map, unsigned long h)
{
// Get index from hash of key
int i = (int)(h % (map->size));
// Compare stored hash key to curernt hash at index i
if (*((map->key) + i) == h)
{
return i;
}
else if (*((int *)(map->val) + i) == 0)
{
return 0;
}
else
{
printf("Collision detected for hash %lu. Returning -1\n", h);
return -1;
}
}
/**
* Helper function for hashtable resize
*/
Hashtable *initialize_hashtable_for_resize(int size)
{
Hashtable *map = malloc(sizeof(Hashtable));
map->val = (int *)calloc(size, sizeof(int));
map->key = (unsigned long *)calloc(size, sizeof(unsigned long));
map->str_key = (char **)malloc(size * sizeof(char *));
// We need to initialize the pointers to string to NULL
for (int i = 0; i < size; i++)
{
*((map->str_key) + i) = NULL;
}
map->size = size;
map->count = 0;
if (map->val == NULL || map->key == NULL || map->str_key == NULL)
{
printf("Failed to initialize hashtable\n");
}
return map;
}
/**
* Helper function for hashtable resize
*/
void add_to_hashtable_for_resize(Hashtable *map, char *key, int val)
{
// Calculate hash of key
unsigned long h = hash(key);
// Use linear probing to iterate until i does not create a collision
int i = 0;
while ((i = collision(map, h)) == -1)
{
h++;
}
// Copy previous value
*((map->val) + i) = val;
// Store key
*((map->key) + i) = h;
// Store string key
*((map->str_key) + i) = key;
}
/**
* Double size of hashtable
*/
void resize_hashtable(Hashtable **map)
{
Hashtable *new_map = initialize_hashtable_for_resize((*map)->size * 2);
for (int i = 0; i < (*map)->size; i++)
{
if ((*(((*map)->val) + i) != 0) && (*(((*map)->val) + i) != -1))
{
add_to_hashtable_for_resize(new_map, *(((*map)->str_key) + i), *(((*map)->val) + i));
}
}
// Free old memory
free_hashtable(map);
(*map) = new_map;
}
void add_to_hashtable(Hashtable **map, char *key)
{
// Calculate hash of key
unsigned long h = hash(key);
// Use linear probing to iterate until i does not create a collision
int i = 0;
while ((i = collision((*map), h)) == -1)
{
h++;
}
// Increment by 1
*(((*map)->val) + i) += 1;
// Store key
*(((*map)->key) + i) = h;
// Store string key
*(((*map)->str_key) + i) = key;
// Check for 33% capicity. If over, double the size of the hashtable
if ((((*map)->count * 100) / (*map)->size) > RESIZE_PERCENTAGE)
{
resize_hashtable(map);
printf("Size is now %d.\n", (*map)->size);
}
}
int search_hashtable(Hashtable *map, char *key)
{
// Calculate hash of key
unsigned long h = hash(key);
int i;
while ((i = search_collision(map, h)) == -1)
{
h++;
}
if (i == 0)
{
printf("Key \"%s\" was not found.\n", key);
return -1;
}
else
{
return *((map->val) + i);
}
}
void remove_from_hashtable(Hashtable *map, char *key)
{
// Calculate hash of key
unsigned long h = hash(key);
int i;
while ((i = search_collision(map, h)) == -1)
{
h++;
}
if (i == 0)
{
printf("Key \"%s\" was not found. Not removal needed.\n", key);
}
else
{
*((int *)(map->val) + i) = -1;
*((map->key) + i) = 0;
*((map->str_key) + i) = NULL;
map->count -= 1;
}
}
void print_hashtable(Hashtable *map)
{
for (int i = 0; i < map->size; i++)
{
// if ((*((int *)(map->val) + i) != 0) && (*((int *)(map->val) + i) != -1))
// {
printf("index: %4d || key: %20lu || str_key: %16s || value: %4d\n", i, *((map->key) + i), *((map->str_key) + i), *((map->val) + i));
// }
}
}