htable_insert() returns HT_REPLACED if key already existed
[eleutheria.git] / genstructs / htable / htable.h
blob1975986ef59b808db0834b3f601ebef347ddf3ac
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 const void *hn_key;
9 void *hn_data;
10 TAILQ_ENTRY(hnode) hn_next;
11 } hnode_t;
13 typedef struct htable {
14 size_t ht_size; /* size must be a power of 2 */
15 unsigned int ht_used;
16 unsigned int ht_factor;
17 unsigned int ht_limit;
18 unsigned int (*ht_hashf)(const void *key);
19 int (*ht_cmpf)(const void *arg1, const void *arg2);
20 void (*ht_printf)(const void *key, const void *data);
21 TAILQ_HEAD(htablehead, hnode) *ht_table;
22 } htable_t;
24 typedef struct htablehead hhead_t;
26 typedef enum {
27 HT_OK,
28 HT_NOMEM,
29 HT_NOTFOUND,
30 HT_REPLACED
31 } htret_t;
33 /* Function prototypes */
34 htret_t htable_init(htable_t *htable, size_t size, unsigned int factor,
35 unsigned int (*hashf)(const void *key),
36 int (*cmpf)(const void *arg1, const void *arg2),
37 void (*printf)(const void *key, const void *data));
38 void htable_free(htable_t *htable);
39 void htable_free_objects(htable_t *htable);
40 htret_t htable_grow(htable_t *htable);
41 htret_t htable_insert(htable_t *htable, const void *key, void *data);
42 htret_t htable_remove(htable_t *htable, const void *key);
43 void *htable_search(const htable_t *htable, const void *key);
44 void htable_print(const htable_t *htable);
46 #endif /* HTABLE_H */