forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
516 lines (468 loc) · 13.6 KB
/
hash.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
* @file
* Hash Table data structure
*
* @authors
* Copyright (C) 1996-2009 Michael R. Elkins <[email protected]>
* Copyright (C) 2017-2020 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mutt_hash Hash Table data structure
*
* Hash Table data structure.
*/
#include "config.h"
#include <ctype.h>
#include <stdbool.h>
#include "hash.h"
#include "memory.h"
#include "string2.h"
#define SOME_PRIME 149711
/**
* gen_string_hash - Generate a hash from a string - Implements ::hash_gen_hash_t - @ingroup hash_gen_hash_api
*
* @note If the key is NULL or empty, the retval will be 0
*/
static size_t gen_string_hash(union HashKey key, size_t num_elems)
{
size_t hash = 0;
const unsigned char *s = (const unsigned char *) key.strkey;
if (!s)
return 0;
while (*s != '\0')
hash += ((hash << 7) + *s++);
hash = (hash * SOME_PRIME) % num_elems;
return hash;
}
/**
* cmp_string_key - Compare two string keys - Implements ::hash_cmp_key_t - @ingroup hash_cmp_key_api
*/
static int cmp_string_key(union HashKey a, union HashKey b)
{
return mutt_str_cmp(a.strkey, b.strkey);
}
/**
* gen_case_string_hash - Generate a hash from a string (ignore the case) - Implements ::hash_gen_hash_t - @ingroup hash_gen_hash_api
*
* @note If the key is NULL or empty, the retval will be 0
*/
static size_t gen_case_string_hash(union HashKey key, size_t num_elems)
{
size_t hash = 0;
const unsigned char *s = (const unsigned char *) key.strkey;
if (!s)
return 0;
while (*s != '\0')
hash += ((hash << 7) + tolower(*s++));
hash = (hash * SOME_PRIME) % num_elems;
return hash;
}
/**
* cmp_case_string_key - Compare two string keys (ignore case) - Implements ::hash_cmp_key_t - @ingroup hash_cmp_key_api
*/
static int cmp_case_string_key(union HashKey a, union HashKey b)
{
return mutt_istr_cmp(a.strkey, b.strkey);
}
/**
* gen_int_hash - Generate a hash from an integer - Implements ::hash_gen_hash_t - @ingroup hash_gen_hash_api
*/
static size_t gen_int_hash(union HashKey key, size_t num_elems)
{
return (key.intkey % num_elems);
}
/**
* cmp_int_key - Compare two integer keys - Implements ::hash_cmp_key_t - @ingroup hash_cmp_key_api
*/
static int cmp_int_key(union HashKey a, union HashKey b)
{
if (a.intkey == b.intkey)
return 0;
if (a.intkey < b.intkey)
return -1;
return 1;
}
/**
* hash_new - Create a new Hash Table
* @param num_elems Number of elements it should contain
* @retval ptr New Hash Table
*
* The Hash Table can contain more elements than num_elems, but they will be
* chained together.
*/
static struct HashTable *hash_new(size_t num_elems)
{
struct HashTable *table = mutt_mem_calloc(1, sizeof(struct HashTable));
if (num_elems == 0)
num_elems = 2;
table->num_elems = num_elems;
table->table = mutt_mem_calloc(num_elems, sizeof(struct HashElem *));
return table;
}
/**
* union_hash_insert - Insert into a hash table using a union as a key
* @param table Hash Table to update
* @param key Key to hash on
* @param type Data type
* @param data Data to associate with key
* @retval ptr Newly inserted HashElem
*/
static struct HashElem *union_hash_insert(struct HashTable *table,
union HashKey key, int type, void *data)
{
if (!table)
return NULL; // LCOV_EXCL_LINE
struct HashElem *he = mutt_mem_calloc(1, sizeof(struct HashElem));
size_t hash = table->gen_hash(key, table->num_elems);
he->key = key;
he->data = data;
he->type = type;
if (table->allow_dups)
{
he->next = table->table[hash];
table->table[hash] = he;
}
else
{
struct HashElem *tmp = NULL, *last = NULL;
for (tmp = table->table[hash], last = NULL; tmp; last = tmp, tmp = tmp->next)
{
const int rc = table->cmp_key(tmp->key, key);
if (rc == 0)
{
FREE(&he);
return NULL;
}
if (rc > 0)
break;
}
if (last)
last->next = he;
else
table->table[hash] = he;
he->next = tmp;
}
return he;
}
/**
* union_hash_find_elem - Find a HashElem in a Hash Table element using a key
* @param table Hash Table to search
* @param key Key (either string or integer)
* @retval ptr HashElem matching the key
*/
static struct HashElem *union_hash_find_elem(const struct HashTable *table, union HashKey key)
{
if (!table)
return NULL; // LCOV_EXCL_LINE
size_t hash = table->gen_hash(key, table->num_elems);
struct HashElem *he = table->table[hash];
for (; he; he = he->next)
{
if (table->cmp_key(key, he->key) == 0)
return he;
}
return NULL;
}
/**
* union_hash_find - Find the HashElem data in a Hash Table element using a key
* @param table Hash Table to search
* @param key Key (either string or integer)
* @retval ptr Data attached to the HashElem matching the key
*/
static void *union_hash_find(const struct HashTable *table, union HashKey key)
{
if (!table)
return NULL; // LCOV_EXCL_LINE
struct HashElem *he = union_hash_find_elem(table, key);
if (he)
return he->data;
return NULL;
}
/**
* union_hash_delete - Remove an element from a Hash Table
* @param table Hash Table to use
* @param key Key (either string or integer)
* @param data Private data to match (or NULL for any match)
*/
static void union_hash_delete(struct HashTable *table, union HashKey key, const void *data)
{
if (!table)
return; // LCOV_EXCL_LINE
size_t hash = table->gen_hash(key, table->num_elems);
struct HashElem *he = table->table[hash];
struct HashElem **he_last = &table->table[hash];
while (he)
{
if (((data == he->data) || !data) && (table->cmp_key(he->key, key) == 0))
{
*he_last = he->next;
if (table->hdata_free)
table->hdata_free(he->type, he->data, table->hdata);
if (table->strdup_keys)
FREE(&he->key.strkey);
FREE(&he);
he = *he_last;
}
else
{
he_last = &he->next;
he = he->next;
}
}
}
/**
* mutt_hash_new - Create a new Hash Table (with string keys)
* @param num_elems Number of elements it should contain
* @param flags Flags, see #HashFlags
* @retval ptr New Hash Table
*/
struct HashTable *mutt_hash_new(size_t num_elems, HashFlags flags)
{
struct HashTable *table = hash_new(num_elems);
if (flags & MUTT_HASH_STRCASECMP)
{
table->gen_hash = gen_case_string_hash;
table->cmp_key = cmp_case_string_key;
}
else
{
table->gen_hash = gen_string_hash;
table->cmp_key = cmp_string_key;
}
if (flags & MUTT_HASH_STRDUP_KEYS)
table->strdup_keys = true;
if (flags & MUTT_HASH_ALLOW_DUPS)
table->allow_dups = true;
return table;
}
/**
* mutt_hash_int_new - Create a new Hash Table (with integer keys)
* @param num_elems Number of elements it should contain
* @param flags Flags, see #HashFlags
* @retval ptr New Hash Table
*/
struct HashTable *mutt_hash_int_new(size_t num_elems, HashFlags flags)
{
struct HashTable *table = hash_new(num_elems);
table->gen_hash = gen_int_hash;
table->cmp_key = cmp_int_key;
if (flags & MUTT_HASH_ALLOW_DUPS)
table->allow_dups = true;
return table;
}
/**
* mutt_hash_set_destructor - Set the destructor for a Hash Table
* @param table Hash Table to use
* @param fn Callback function to free Hash Table's resources
* @param fn_data Data to pass to the callback function
*/
void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data)
{
if (!table)
return;
table->hdata_free = fn;
table->hdata = fn_data;
}
/**
* mutt_hash_typed_insert - Insert a string with type info into a Hash Table
* @param table Hash Table to use
* @param strkey String key
* @param type Type to associate with the key
* @param data Private data associated with the key
* @retval ptr Newly inserted HashElem
*/
struct HashElem *mutt_hash_typed_insert(struct HashTable *table,
const char *strkey, int type, void *data)
{
if (!table || !strkey)
return NULL;
union HashKey key;
key.strkey = table->strdup_keys ? mutt_str_dup(strkey) : strkey;
return union_hash_insert(table, key, type, data);
}
/**
* mutt_hash_insert - Add a new element to the Hash Table (with string keys)
* @param table Hash Table (with string keys)
* @param strkey String key
* @param data Private data associated with the key
* @retval ptr Newly inserted HashElem
*/
struct HashElem *mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
{
return mutt_hash_typed_insert(table, strkey, -1, data);
}
/**
* mutt_hash_int_insert - Add a new element to the Hash Table (with integer keys)
* @param table Hash Table (with integer keys)
* @param intkey Integer key
* @param data Private data associated with the key
* @retval ptr Newly inserted HashElem
*/
struct HashElem *mutt_hash_int_insert(struct HashTable *table, unsigned int intkey, void *data)
{
if (!table)
return NULL;
union HashKey key;
key.intkey = intkey;
return union_hash_insert(table, key, -1, data);
}
/**
* mutt_hash_find - Find the HashElem data in a Hash Table element using a key
* @param table Hash Table to search
* @param strkey String key to search for
* @retval ptr Data attached to the HashElem matching the key
*/
void *mutt_hash_find(const struct HashTable *table, const char *strkey)
{
if (!table || !strkey)
return NULL;
union HashKey key;
key.strkey = strkey;
return union_hash_find(table, key);
}
/**
* mutt_hash_find_elem - Find the HashElem in a Hash Table element using a key
* @param table Hash Table to search
* @param strkey String key to search for
* @retval ptr HashElem matching the key
*/
struct HashElem *mutt_hash_find_elem(const struct HashTable *table, const char *strkey)
{
if (!table || !strkey)
return NULL;
union HashKey key;
key.strkey = strkey;
return union_hash_find_elem(table, key);
}
/**
* mutt_hash_int_find - Find the HashElem data in a Hash Table element using a key
* @param table Hash Table to search
* @param intkey Integer key
* @retval ptr Data attached to the HashElem matching the key
*/
void *mutt_hash_int_find(const struct HashTable *table, unsigned int intkey)
{
if (!table)
return NULL;
union HashKey key;
key.intkey = intkey;
return union_hash_find(table, key);
}
/**
* mutt_hash_find_bucket - Find the HashElem in a Hash Table element using a key
* @param table Hash Table to search
* @param strkey String key to search for
* @retval ptr HashElem matching the key
*
* Unlike mutt_hash_find_elem(), this will return the first matching entry.
*/
struct HashElem *mutt_hash_find_bucket(const struct HashTable *table, const char *strkey)
{
if (!table || !strkey)
return NULL;
union HashKey key;
key.strkey = strkey;
size_t hash = table->gen_hash(key, table->num_elems);
return table->table[hash];
}
/**
* mutt_hash_delete - Remove an element from a Hash Table
* @param table Hash Table to use
* @param strkey String key to match
* @param data Private data to match (or NULL for any match)
*/
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
{
if (!table || !strkey || (strkey[0] == '\0'))
return;
union HashKey key;
// Copy the key because union_hash_delete() may use it after the HashElem is freed.
key.strkey = mutt_str_dup(strkey);
union_hash_delete(table, key, data);
FREE(&key.strkey);
}
/**
* mutt_hash_int_delete - Remove an element from a Hash Table
* @param table Hash Table to use
* @param intkey Integer key to match
* @param data Private data to match (or NULL for any match)
*/
void mutt_hash_int_delete(struct HashTable *table, unsigned int intkey, const void *data)
{
if (!table)
return;
union HashKey key;
key.intkey = intkey;
union_hash_delete(table, key, data);
}
/**
* mutt_hash_free - Free a hash table
* @param[out] ptr Hash Table to be freed
*/
void mutt_hash_free(struct HashTable **ptr)
{
if (!ptr || !*ptr)
return;
struct HashTable *table = *ptr;
struct HashElem *he = NULL, *tmp = NULL;
for (size_t i = 0; i < table->num_elems; i++)
{
for (he = table->table[i]; he;)
{
tmp = he;
he = he->next;
if (table->hdata_free && tmp->data)
table->hdata_free(tmp->type, tmp->data, table->hdata);
if (table->strdup_keys)
FREE(&tmp->key.strkey);
FREE(&tmp);
}
}
FREE(&table->table);
FREE(ptr);
}
/**
* mutt_hash_walk - Iterate through all the HashElem's in a Hash Table
* @param table Hash Table to search
* @param state Cursor to keep track
* @retval ptr Next HashElem in the Hash Table
* @retval NULL When the last HashElem has been seen
*/
struct HashElem *mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state)
{
if (!table || !state)
return NULL;
if (state->last && state->last->next)
{
state->last = state->last->next;
return state->last;
}
if (state->last)
state->index++;
while (state->index < table->num_elems)
{
if (table->table[state->index])
{
state->last = table->table[state->index];
return state->last;
}
state->index++;
}
state->index = 0;
state->last = NULL;
return NULL;
}