/hash.h
C Header | 37 lines | 22 code | 6 blank | 9 comment | 0 complexity | 30d6efa3bccdb7d999c7eda99b4ad656 MD5 | raw file
1#ifndef INC_HASH_H
2#define INC_HASH_H
3
4/* simple hashtable
5 * David Kaplan <david[at]2of1.org>, 2011
6 * Modified since by Erik Shadwick.
7 *
8 * I found this simple yet functional hash table on 2of1.org in 2011. However,
9 * the site appears to be down for now; but either way, as stated above, it
10 * was written by David Kaplan originally.
11 * I've modified it by adding ht_init, ht_end, and ht_iterate.
12 */
13
14#define HT_KEY_LEN 30
15
16struct ht_node {
17 void *val;
18 char key[HT_KEY_LEN + 1];
19 struct ht_node *nxt;
20};
21
22typedef struct ht {
23 struct ht_node **tbl;
24 int size;
25} HT;
26
27HT *ht_create(int size); /* allocate hashtable mem */
28void ht_init(HT *ht, int size); /* initialize pre-alloc'd HT */
29void ht_destroy(HT *ht); /* free hashtable mem */
30void ht_end(HT *ht); /* deinitialize pre-alloc'd HT */
31void *ht_get(HT *ht, const char *key); /* retrieve entry */
32void ht_put(HT *ht, const char *key, void *val); /* store entry */
33void ht_remove(HT *ht, const char *key); /* remove entry */
34void ht_clear(HT *ht);
35struct ht_node *ht_iterate(HT *ht, struct ht_node *prev);
36
37#endif