-
Notifications
You must be signed in to change notification settings - Fork 25
/
hash_table.h
306 lines (246 loc) · 6.9 KB
/
hash_table.h
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
#ifndef __HASH_TABLE__
#define __HASH_TABLE__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <stdint.h>
#include "hash_function.h"
#define TRUE 1
#define FALSE 0
typedef struct hash_entry_type
{
void *key;
void *data;
uint32_t key_len;
struct hash_entry_type *next;
struct hash_entry_type *prev;
} hash_entry_t;
typedef struct
{
hash_entry_t **row;
hash_entry_t **tail;
#ifdef __USE_HASH_LOCKS__
sem_t *row_lock;
#endif
uint32_t size;
} hash_table_t;
/*
hash table functions
copyright (c) 2005 dr. srinidhi varadarajan
*/
/*
creates a hash table and returns a pointer to it
parameters:
hash_table_size : size of the hash table to create
returns: pointer to created hash table or NULL on failure
*/
static inline hash_table_t *create_hash_table(uint32_t hash_table_size)
{
uint32_t t;
hash_table_t *hash_table;
hash_table = ( hash_table_t *) malloc(sizeof( hash_table_t));
if (hash_table == NULL) return(NULL);
hash_table->row = ( hash_entry_t **) malloc(sizeof( hash_entry_t *) * (hash_table_size));
if (hash_table->row == NULL) return(NULL);
hash_table->tail = ( hash_entry_t **) malloc(sizeof( hash_entry_t *) * (hash_table_size));
if (hash_table->tail == NULL) return(NULL);
#ifdef __USE_HASH_LOCKS__
hash_table->row_lock = (sem_t *) malloc(sizeof(sem_t) * (hash_table_size + 2));
if (hash_table->row_lock == NULL) return(NULL);
#endif
for (t=0; t<hash_table_size; t++)
{
hash_table->row[t] = NULL;
hash_table->tail[t] = NULL;
#ifdef __USE_HASH_LOCKS__
sem_init(&hash_table->row_lock[t], 0, 1);
#endif
}
hash_table->size = hash_table_size;
return(hash_table);
}
/*
inserts a structure into the hash table.
parameters :
hash_table : Hash table to use
key : key to index the hash table
key_len: length of the hash key in bytes
data : pointer to the data to insert. you should allocate and free the
data pointer within your application
returns:
TRUE if key was inserted into the table
FALSE if key could not be inserted into the table
note: a data element can be inserted more than once in this
hash structure. be careful when you use hash_insert to make sure
that if you insert multiple times, you also delete multiple times.
*/
static inline int32_t hash_insert( hash_table_t *hash_table, void *key, uint32_t key_len, void *data)
{
uint32_t hash_key, hash_table_size;
hash_entry_t *new_entry, *prev_ptr;
hash_table_size = hash_table->size;
hash_key = hash(key, key_len, 7) % hash_table_size;
#ifdef __USE_HASH_LOCKS__
sem_wait(&hash_table->row_lock[hash_key]);
#endif
new_entry = ( hash_entry_t *) malloc(sizeof( hash_entry_t));
if (new_entry == NULL)
{
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(FALSE);
}
new_entry->key = (char *) malloc(key_len);
if (new_entry->key == NULL)
{
printf("Warning: Unable to allocate memory for hash key. \n");
free(new_entry);
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(FALSE);
}
prev_ptr = hash_table->tail[hash_key];
new_entry->next = NULL;
new_entry->prev = hash_table->tail[hash_key];
if (prev_ptr == NULL)
hash_table->row[hash_key] = new_entry;
else
prev_ptr->next = new_entry;
hash_table->tail[hash_key] = new_entry;
memcpy(new_entry->key, key, key_len);
new_entry->data = data;
new_entry->key_len = key_len;
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(TRUE);
}
/*
deletes a hash table entry.
parameters :
hash_table : hash table to use
key : key to index the hash table
key_len: length of the key in bytes
returns:
TRUE: if key was successfully deleted
FALSE: if key could not be deleted (key was not found)
*/
static inline int32_t hash_delete( hash_table_t *hash_table, void *key, uint32_t key_len)
{
uint32_t hash_key, hash_table_size;
hash_entry_t *ptr, *prev_ptr;
hash_table_size = hash_table->size;
hash_key = hash(key, key_len, 7) % hash_table_size;
#ifdef __USE_HASH_LOCKS__
sem_wait(&(hash_table->row_lock[hash_key]));
#endif
ptr = hash_table->row[hash_key];
prev_ptr = NULL;
while (ptr != NULL)
{
if (memcmp(ptr->key, key, key_len) == 0)
{
if (prev_ptr == NULL) // First entry
hash_table->row[hash_key] = ptr->next;
else
prev_ptr->next = ptr->next;
if (ptr->next == NULL) hash_table->tail[hash_key] = prev_ptr;
free(ptr->key);
free(ptr);
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(TRUE);
}
prev_ptr = ptr;
ptr = ptr->next;
}
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(FALSE);
}
/*
finds the entry corresponding to key in the hash table
parameters:
hash_table : pointer to the hash table to use
key : key to index the hash table.
key_len: length of the key in bytes
returns:
pointer to the data field in the hash table on success
NULL on failure
*/
static inline void *hash_find( hash_table_t *hash_table, void *key, uint32_t key_len)
{
uint32_t hash_key, hash_table_size;
hash_entry_t *ptr;
hash_table_size = hash_table->size;
hash_key = hash(key, key_len, 7) % hash_table_size;
#ifdef __USE_HASH_LOCKS__
sem_wait(&hash_table->row_lock[hash_key]);
#endif
ptr = hash_table->row[hash_key];
while (ptr != NULL)
{
if ((key_len == ptr->key_len) && (memcmp(ptr->key, key, key_len) == 0))
{
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(ptr->data);
}
ptr = ptr->next;
}
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[hash_key]);
#endif
return(NULL);
}
/*
destroys the hash table and frees all allocated memory
parameters:
hash_table : pointer to the hash table to use
returns : nothing
*/
static inline void destroy_hash_table( hash_table_t *hash_table)
{
uint32_t t, count, max_count=0, tot_count=0, hash_table_size;
hash_entry_t *cur_ptr, *tmp_ptr;
hash_table_size = hash_table->size;
for (t=0; t<hash_table_size; t++)
{
#ifdef __USE_HASH_LOCKS__
sem_wait(&hash_table->row_lock[t]);
#endif
if (hash_table->row[t] != NULL)
{
cur_ptr = hash_table->row[t];
count = 1;
while (cur_ptr != NULL)
{
free(cur_ptr->key);
tmp_ptr = cur_ptr->next;
free(cur_ptr);
cur_ptr = tmp_ptr;
count++;
}
hash_table->row[t] = NULL;
tot_count += count;
if (count > max_count) max_count = count;
}
#ifdef __USE_HASH_LOCKS__
sem_post(&hash_table->row_lock[t]);
#endif
}
printf("Max collision list entries: %u. Total: %u\n", max_count, tot_count);
free(hash_table->row);
free(hash_table->tail);
#ifdef __USE_HASH_LOCKS__
free(hash_table->row_lock);
#endif
free(hash_table);
}
#endif