Add some comments
[eleutheria.git] / genstructs / htable / htable.h
blobf9abf39be67dcf8b0ac733f6965ef8ebf6557cb8
1 #ifndef HTABLE_H
2 #define HTABLE_H
4 #include <sys/queue.h>
5 #include <stddef.h> /* for size_t type */
7 typedef struct hnode {
8 void *hn_key;
9 void *hn_data;
10 TAILQ_ENTRY(hnode) hn_next;
11 } hnode_t;
13 /* Type definitions for use in callback functions */
14 typedef size_t hashf_t(const void *key);
15 typedef int cmpf_t(const void *arg1, const void *arg2);
16 typedef void printf_t(const void *key, const void *data);
18 typedef struct htable {
19 size_t ht_size; /* size must be a power of 2 */
20 size_t ht_used; /* number of hash table entries */
21 size_t ht_factor;
22 size_t ht_limit; /* limit = factor * size */
23 hashf_t *ht_hashf; /* pointer to hash function */
24 cmpf_t *ht_cmpf; /* pointer to compare function */
25 printf_t *ht_printf; /* pointer to printf function */
26 TAILQ_HEAD(htablehead, hnode) *ht_table;
27 } htable_t;
29 typedef struct htablehead hhead_t;
31 typedef enum {
32 HT_OK,
33 HT_EXISTS,
34 HT_NOMEM,
35 HT_NOTFOUND
36 } htret_t;
38 typedef enum {
39 HT_FREEKEY = 1,
40 HT_FREEDATA = 2
41 } htfree_t;
43 /* Function prototypes */
44 htret_t htable_init(htable_t *htable, size_t size, size_t factor,
45 hashf_t *myhashf,
46 cmpf_t *mycmpf,
47 printf_t *myprintf);
48 void htable_free(htable_t *htable);
49 htret_t htable_free_obj(htable_t *htable, void *key, htfree_t htfree);
50 void htable_free_all_obj(htable_t *htable, htfree_t htfree);
51 htret_t htable_grow(htable_t *htable);
52 htret_t htable_insert(htable_t *htable, void *key, void *data);
53 htret_t htable_remove(htable_t *htable, const void *key);
54 void *htable_search(const htable_t *htable, const void *key);
55 void htable_print(const htable_t *htable);
56 size_t htable_get_size(const htable_t *htable);
57 size_t htable_get_used(const htable_t *htable);
58 void htable_traverse(const htable_t *htable, void (*pfunc)(void *data));
59 const hnode_t *htable_get_next_elm(const htable_t *htable, size_t *pos, const hnode_t *pnode);
61 #endif /* HTABLE_H */