-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.c
More file actions
231 lines (212 loc) · 4.43 KB
/
hash_map.c
File metadata and controls
231 lines (212 loc) · 4.43 KB
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "hash_map.h"
/**
* @Brief djb2 hash function by Dan Bernstein
* @Ref https://theartincode.stanis.me/008-djb2/
*
* @param key The string to hash
* @return The hash value
*/
unsigned int hash(const char *key)
{
unsigned long h = 5381;
int c;
while ((c = *key++))
{
h = ((h << 5) + h) + c; // h * 33 + c
}
return h % TABLE_SIZE;
}
/**
* @Brief Create a new HashMap
*
* @return Pointer to a newly created HashMap
*/
HashMap *hm_create(void)
{
HashMap *ht = malloc(sizeof(HashMap));
if (!ht)
{
perror("malloc");
exit(-1);
};
for (int i = 0; i < TABLE_SIZE; i++)
{
ht->buckets[i] = NULL;
}
return ht;
}
/**
* @Brief Insert or update key-value pair
*
* @param hm Pointer to the HashMap
* @param key The key string
* @param value The value string
*/
void hm_put(HashMap *hm, const char *key, const char *value)
{
unsigned int idx = hash(key);
Entry *e = hm->buckets[idx];
// Check if key already exists
while (e)
{
if (strcmp(e->key, key) == 0)
{
// Update value
free(e->value);
e->value = strdup(value);
return;
}
e = e->next;
}
// Insert new entry at head of list
Entry *new_entry = malloc(sizeof(Entry));
new_entry->key = strdup(key);
new_entry->value = strdup(value);
new_entry->next = hm->buckets[idx];
hm->buckets[idx] = new_entry;
}
/**
* @Brief Get value by key (NULL if not found)
*
* @param hm Pointer to the HashMap
* @param key The key string
*/
char *hm_get(const HashMap *hm, const char *key)
{
const unsigned int idx = hash(key);
const Entry *e = hm->buckets[idx];
while (e)
{
if (strcmp(e->key, key) == 0)
{
return e->value;
}
e = e->next;
}
return NULL;
}
/* Delete the entry with a given key from the hashmap */
void hm_delete(HashMap *hm, const char *key)
{
const unsigned int idx = hash(key);
Entry *e = hm->buckets[idx];
Entry *prev = NULL;
while (e)
{
if (strcmp(e->key, key) == 0)
{
if (prev)
{
prev->next = e->next;
}
else
{
hm->buckets[idx] = e->next;
}
free(e->key);
free(e->value);
free(e);
return;
}
prev = e;
e = e->next;
}
}
/* Print the entries in the hashmap, one in each line */
void hm_print(const HashMap *hm)
{
for (int i = 0; i < TABLE_SIZE; i++)
{
const Entry *e = hm->buckets[i];
while (e)
{
printf("%s = '%s'\n",e->key, e->value);
e = e->next;
}
}
}
/* Print the entries in the hashmap sorted by key */
int cmp_keys(const void *a, const void *b) {
const char *ka = *(const char **)a;
const char *kb = *(const char **)b;
return strcmp(ka, kb);
}
void hm_print_sorted(const HashMap *hm)
{
// Count total entries
int count = 0;
for (int i = 0; i < TABLE_SIZE; i++) {
Entry *e = hm->buckets[i];
while (e) {
count++;
e = e->next;
}
}
if (count == 0) return;
// Collect keys
char **keys = malloc(count * sizeof(char *));
int idx = 0;
for (int i = 0; i < TABLE_SIZE; i++) {
Entry *e = hm->buckets[i];
while (e) {
keys[idx++] = e->key;
e = e->next;
}
}
// Sort keys
qsort(keys, count, sizeof(char *), cmp_keys);
// Print key-value pairs
for (int i = 0; i < count; i++) {
char *val = hm_get(hm, keys[i]);
printf("%s = '%s'\n", keys[i], val);
}
free(keys);
}
/* Reinitialize the hashmap */
void hm_reset(HashMap *hm)
{
hm_free(hm);
hm = hm_create();
}
/* Free the memory used by the hashmap */
void hm_free(HashMap *hm)
{
for (int i = 0; i < TABLE_SIZE; i++)
{
Entry *e = hm->buckets[i];
while (e)
{
Entry *next = e->next;
free(e->key);
free(e->value);
free(e);
e = next;
}
}
free(hm);
}
/* (Unused) Use this an example to show how to use this hashmap implementation */
int hm_usage_example(void)
{
// Initialization
HashMap *hm = hm_create();
// Add Elements
hm_put(hm, "name", "Alice");
hm_put(hm, "bird", "flamingo");
hm_put(hm, "city", "Madison");
// Get Elements
printf("name = %s\n", hm_get(hm, "name"));
printf("city = %s\n", hm_get(hm, "city"));
// Update value for a Key
hm_put(hm, "city", "New York");
printf("city (updated) = %s\n", hm_get(hm, "city"));
// Delete Key & corresponding value
hm_delete(hm, "language");
printf("language = %s\n", hm_get(hm, "language"));
// Free memory once done
hm_free(hm);
return 0;
}