-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashMap.c
427 lines (373 loc) · 8.55 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//#pragma warning(disable : 4996)
/*
* CS 261 Data Structures
* Assignment 5
* Name: Rowan Simmons
* Date: 3/9/20
*/
#include "hashMap.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
int hashFunction1(const char* key)
{
int r = 0;
for (int i = 0; key[i] != '\0'; i++)
{
r += key[i];
}
return r;
}
int hashFunction2(const char* key)
{
int r = 0;
for (int i = 0; key[i] != '\0'; i++)
{
r += (i + 1) * key[i];
}
return r;
}
/**
* Creates a new hash table link with a copy of the key string.
* @param key Key string to copy in the link.
* @param value Value to set in the link.
* @param next Pointer to set as the link's next.
* @return Hash table link allocated on the heap.
*/
HashLink* hashLinkNew(const char* key, int value, HashLink* next)
{
HashLink* link = malloc(sizeof(HashLink));
link->key = malloc(sizeof(char) * (strlen(key) + 1));
strcpy(link->key, key);
link->value = value;
link->next = next;
return link;
}
/**
* Free the allocated memory for a hash table link created with hashLinkNew.
* @param link
*/
static void hashLinkDelete(HashLink* link)
{
free(link->key);
free(link);
}
/**
* Initializes a hash table map, allocating memory for a link pointer table with
* the given number of buckets.
* @param map
* @param capacity The number of table buckets.
*/
void hashMapInit(HashMap* map, int capacity)
{
map->capacity = capacity;
map->size = 0;
map->table = malloc(sizeof(HashLink*) * capacity);
for (int i = 0; i < capacity; i++)
{
map->table[i] = NULL;
}
}
/**
* Removes all links in the map and frees all allocated memory. You can use
* hashLinkDelete to free the links.
* @param map
*/
void hashMapCleanUp(HashMap* map)
{
// FIXME: implement
assert(map != NULL);
for (int i = 0; i < map->capacity; i++)
{
HashLink* link = map->table[i];
//if (map->size == 0)
//{
// break;
//}
while (link != NULL)
{
HashLink* keepGoing = link;
link = link->next;
//link->key = 0;
//link->value = 0;
hashLinkDelete(keepGoing);
map->size--;
}
free(link);
}
free(map->table);
}
/**
* Creates a hash table map, allocating memory for a link pointer table with
* the given number of buckets.
* @param capacity The number of buckets.
* @return The allocated map.
*/
HashMap* hashMapNew(int capacity)
{
HashMap* map = malloc(sizeof(HashMap));
hashMapInit(map, capacity);
return map;
}
/**
* Removes all links in the map and frees all allocated memory, including the
* map itself.
* @param map
*/
void hashMapDelete(HashMap* map)
{
hashMapCleanUp(map);
free(map);
}
/**
* Returns a pointer to the value of the link with the given key and skip traversing as well. Returns NULL
* if no link with that key is in the table.
*
* Use HASH_FUNCTION(key) and the map's capacity to find the index of the
* correct linked list bucket. Also make sure to search the entire list.
*
* @param map
* @param key
* @return Link value or NULL if no matching link.
*/
int* hashMapGet(HashMap* map, const char* key)
{
// FIXME: implement
int index = HASH_FUNCTION(key) % map->capacity;
HashLink *link = map->table[index];
// Returns the pointer to the value of the key by comparing to passed argument key.
while (link != NULL) {
if (strcmp(link->key, key) == 0) {
return &(link->value);
}
link = link->next;
}
return NULL;
}
/**
* Resizes the hash table to have a number of buckets equal to the given
* capacity (double of the old capacity). After allocating the new table,
* all of the links need to rehashed into it because the capacity has changed.
*
* Remember to free the old table and any old links if you use hashMapPut to
* rehash them.
*
* @param map
* @param capacity The new number of buckets.
*/
void resizeTable(HashMap* map, int capacity)
{
// FIXME: implement
assert(map != NULL);
assert(capacity == map->capacity * 2);
int oldCap = hashMapCapacity(map);
struct HashMap* temp = hashMapNew(capacity);
for (int i = 0; i < oldCap; i++)
{
HashLink* link = map->table[i];
while (link != 0)
{
hashMapPut(temp, link->key, link->value);
link = link->next;
}
}
hashMapCleanUp(map);
map->size = temp->size;
map->table = temp->table;
map->capacity = temp->capacity;
temp->table = NULL;
free(temp);
temp = 0;
}
/**
* Updates the given key-value pair in the hash table. If a link with the given
* key already exists, this will just update the value and skip traversing. Otherwise, it will
* create a new link with the given key and value and add it to the table
* bucket's linked list. You can use hashLinkNew to create the link.
*
* Use HASH_FUNCTION(key) and the map's capacity to find the index of the
* correct linked list bucket.
*
* @param map
* @param key
* @param value
*/
void hashMapPut(HashMap* map, const char* key, int value)
{
// FIXME: implement
assert(map != 0);
if (hashMapTableLoad(map) >= MAX_TABLE_LOAD)
{
resizeTable(map, 2 * map->capacity);
}
int index = HASH_FUNCTION(key) % map->capacity;
if (index < 0)
{
index += map->capacity;
}
struct HashLink *link = map->table[index];
while (link != 0)
{
if (strcmp(link->key, key) == 0)
{
link->value = value; //already exists
return;
}
link = link->next;
}
struct HashLink *node;
node = hashLinkNew(key, value, map->table[index]);
map->table[index] = node;
map->size++;
}
/**
* Removes and frees the link with the given key from the table. If no such link
* exists, this does nothing. Remember to search the entire linked list at the
* bucket. You can use hashLinkDelete to free the link.
* @param map
* @param key
*/
void hashMapRemove(HashMap* map, const char* key)
{
// FIXME: implement
assert(map != NULL);
assert(key != NULL);
int index = HASH_FUNCTION(key) % hashMapCapacity(map);
if (index < 0)
{
index += map->capacity;
}
HashLink* link = map->table[index];
HashLink* prev = NULL;
while (link != NULL)
{
//first link
if (strcmp(link->key, key) == 0)
{
if (prev == 0)
{
map->table[index] = link->next;
}
else
{
prev->next = link->next;
}
hashLinkDelete(link);
map->size--;
link = 0;
return;
}
prev = link;
link = link->next;
}
}
/**
* Returns 1 if a link with the given key is in the table and 0 otherwise.
*
* Use HASH_FUNCTION(key) and the map's capacity to find the index of the
* correct linked list bucket. Also make sure to search the entire list.
*
* @param map
* @param key
* @return 1 if the key is found, 0 otherwise.
*/
int hashMapContainsKey(HashMap* map, const char* key)
{
// FIXME: implement
assert(map != NULL);
assert(key != NULL);
int cap = hashMapCapacity(map);
int index = HASH_FUNCTION(key) % cap;
if (index < 0)
{
index += cap;
}
HashLink *link = map->table[index];
// Iterate current until key is found.
while (link != NULL)
{
if (strcmp(link->key, key) == 0)
{
return 1;
}
link = link->next;
}
return 0;
}
/**
* Returns the number of links in the table.
* @param map
* @return Number of links in the table.
*/
int hashMapSize(HashMap* map)
{
// FIXME: implement
assert(map != NULL);
return map->size;
}
/**
* Returns the number of buckets in the table.
* @param map
* @return Number of buckets in the table.
*/
int hashMapCapacity(HashMap* map)
{
// FIXME: implement
assert(map != NULL);
return map->capacity;
}
/**
* Returns the number of table buckets without any links.
* @param map
* @return Number of empty buckets.
*/
int hashMapEmptyBuckets(HashMap* map)
{
// FIXME: implement
assert(map != NULL);
int counter = 0;
int capacity = hashMapCapacity(map);
for (int i = 0; i < capacity; i++)
{
if (map->table[i] == 0)
{
counter++;
}
}
return counter;
}
/**
* Returns the ratio of (number of links) / (number of buckets) in the table.
* Remember that the buckets are linked lists, so this ratio tells you nothing
* about the number of empty buckets. Remember also that the load is a floating
* point number, so don't do integer division.
* @param map
* @return Table load.
*/
float hashMapTableLoad(HashMap* map)
{
// FIXME: implement
return (float)map->size / (float)map->capacity;
}
/**
* Prints all the links in each of the buckets in the table.
* @param map
*/
void hashMapPrint(HashMap* map)
{
// FIXME: implement
printf("\n");
for (int i = 0; i < map->capacity; i++)
{
HashLink *link = map->table[i];
printf("Bucket %i: ", i);
while (link != 0)
{
printf("(%s, %d) ", link->key, link->value);
link = link->next;
}
printf("\n");
}
}