-
Notifications
You must be signed in to change notification settings - Fork 0
/
better_hashtable.c
231 lines (215 loc) · 4.79 KB
/
better_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
#include <stdio.h>
#include <string.h>
#include "better_hashtable.h"
typedef struct _entry
{
char *key;
void *obj;
struct _entry *next;
} entry;
struct _hash_table
{
uint32_t size; // number of elements
hashfunction *hash;
cleanupfunction *cleanup;
entry **elemnets;
};
/**
* @brief index of the key in the hash table
*
* @param ht : hash table
* @param key : key
* @return size_t : index of the key
*/
static size_t hash_table_index(hash_table *ht, const char *key)
{
return ht->hash(key, strlen(key)) % ht->size;
}
/**
* @brief Hash table constructor
*
* @param size : size of the hash table
* @param hf : hash function
* @param cf : cleanup function. If this value is NULL, then the default free function will be used.
* @return hash_table* : pointer to the hash table
*/
hash_table *hash_table_create(uint32_t size, hashfunction *hf, cleanupfunction *cf)
{
hash_table *ht = malloc(sizeof(hash_table));
ht->size = size;
ht->hash = hf;
if (cf)
{
ht->cleanup = cf;
}
else
{
ht->cleanup = free;
}
ht->elemnets = calloc(sizeof(entry *), ht->size); // It will be zero initialized
return ht;
}
/**
* @brief Hash table destructor
*
* @param ht : hash table
*/
void hash_table_destroy(hash_table *ht)
{
for (uint32_t i = 0; i < ht->size; ++i)
{
while (ht->elemnets[i])
{
entry *tmp = ht->elemnets[i];
ht->elemnets[i] = ht->elemnets[i]->next;
free(tmp->key);
ht->cleanup(tmp->obj);
free(tmp);
}
}
free(ht->elemnets);
free(ht);
}
/**
* @brief Print function for the hash table
*
* @param ht : hash table
*/
void hash_table_print(hash_table *ht)
{
printf("Hash Table:\n");
for (uint32_t i = 0; i < ht->size; ++i)
{
if (ht->elemnets[i] == NULL)
{
printf("\t%i\t---\n", i);
}
else
{
printf("\t%i", i);
entry *tmp = ht->elemnets[i];
while (tmp != NULL)
{
printf("\t\"%s\"(%p) - ", tmp->key, tmp->obj);
tmp = tmp->next;
}
printf("\n");
}
}
}
/**
* @brief Print function for the hash table without NULL entries
*
* @param ht : hash table
*/
void hash_table_print_wo_null(hash_table *ht)
{
printf("Hash Table:\n");
for (uint32_t i = 0; i < ht->size; ++i)
{
if (ht->elemnets[i] != NULL)
printf("\t%i", i);
entry *tmp = ht->elemnets[i];
while (tmp != NULL)
{
printf("\t\"%s\"(%p) - ", tmp->key, tmp->obj);
tmp = tmp->next;
}
printf("\n");
}
}
/**
* @brief Insert function for the hash table
*
* @param ht : hash table
* @param key : key
* @param obj : object
* @return true : if the insertion was successful
* @return false : if the insertion was unsuccessful
*/
bool hash_table_insert(hash_table *ht, const char *key, void *obj)
{
if (key == NULL || obj == NULL || ht == NULL)
{
return false;
}
size_t index = hash_table_index(ht, key);
if (hash_table_lookup(ht, key) != NULL)
{
return false;
}
// Create new entry
entry *e = malloc(sizeof(entry));
e->obj = obj;
e->key = strdup(key);
// e->key = malloc(strlen(key) + 1);
// strcpy(e->key, key);
// Insert entry
e->next = ht->elemnets[index];
ht->elemnets[index] = e;
return true;
}
/**
* @brief Lookup function for the hash table
*
* @param ht : hash table
* @param key : key for the lookup
* @return void* : pointer to the object
*/
void *hash_table_lookup(hash_table *ht, const char *key)
{
if (key == NULL || ht == NULL)
{
return false;
}
size_t index = hash_table_index(ht, key);
entry *tmp = ht->elemnets[index];
while (tmp != NULL && strcmp(tmp->key, key) != 0)
{
tmp = tmp->next;
}
if (tmp == NULL)
{
return NULL;
}
return tmp->obj;
}
/**
* @brief Delete function for the hash table
*
* @param ht : hash table
* @param key : key for the deletion
* @return void* : pointer to the object
*/
void *hash_table_delete(hash_table *ht, const char *key)
{
if (key == NULL || ht == NULL)
{
return false;
}
size_t index = hash_table_index(ht, key);
entry *tmp = ht->elemnets[index];
entry *prev = NULL;
while (tmp != NULL && strcmp(tmp->key, key) != 0)
{
prev = tmp;
tmp = tmp->next;
}
if (tmp == NULL)
{
return NULL;
}
if (prev == NULL)
{
// deleting the head of the list
ht->elemnets[index] = tmp->next;
}
else
{
// deleting from somewhere else
prev->next = tmp->next;
}
void *result = tmp->obj;
free(tmp);
return result;
}