Dummy commit to test new ssh key
[eleutheria.git] / genstructs / htable / htable.h
blob3fae473c371f882247277f924e481e8284b98d26
1 #ifndef HTABLE_H
2 #define HTABLE_H
4 #include <sys/queue.h>
5 #include <stddef.h> /* for size_t type */
6 #include <stdio.h> /* for FILE */
8 #define HTABLE_STATS
10 typedef struct hnode {
11 void *hn_key;
12 void *hn_data;
13 TAILQ_ENTRY(hnode) hn_next;
14 } hnode_t;
16 /* Type definitions for use in callback functions */
17 typedef size_t hashf_t(const void *key);
18 typedef int cmpf_t(const void *arg1, const void *arg2);
19 typedef void printf_t(const void *key, const void *data);
21 typedef struct htable {
22 #ifdef HTABLE_STATS
23 size_t ht_grows; /* number of automatic resizes */
24 #endif
25 size_t ht_size; /* size must be a power of 2 */
26 size_t ht_used; /* number of hash table entries */
27 size_t ht_factor;
28 size_t ht_limit; /* limit = factor * size */
29 hashf_t *ht_hashf; /* pointer to hash function */
30 cmpf_t *ht_cmpf; /* pointer to compare function */
31 printf_t *ht_printf; /* pointer to printf function */
32 TAILQ_HEAD(htablehead, hnode) *ht_table;
33 } htable_t;
35 typedef struct htablehead hhead_t;
37 typedef struct htable_iterator {
38 size_t pos;
39 const hnode_t *pnode;
40 } htable_iterator_t;
42 typedef enum {
43 HT_OK,
44 HT_EXISTS,
45 HT_NOMEM,
46 HT_NOTFOUND
47 } htret_t;
49 typedef enum {
50 HT_FREEKEY = 1,
51 HT_FREEDATA = 2
52 } htfree_t;
54 /* Function prototypes */
55 htret_t htable_init(htable_t *htable, size_t size, size_t factor,
56 hashf_t *myhashf,
57 cmpf_t *mycmpf,
58 printf_t *myprintf);
59 void htable_free(htable_t *htable);
60 htret_t htable_free_obj(htable_t *htable, void *key, htfree_t htfree);
61 void htable_free_all_obj(htable_t *htable, htfree_t htfree);
62 htret_t htable_grow(htable_t *htable);
63 htret_t htable_insert(htable_t *htable, void *key, void *data);
64 htret_t htable_remove(htable_t *htable, const void *key);
65 void *htable_search(const htable_t *htable, const void *key);
66 void htable_print(const htable_t *htable, FILE *fp);
67 size_t htable_get_size(const htable_t *htable);
68 size_t htable_get_used(const htable_t *htable);
69 void htable_traverse(const htable_t *htable, void (*pfunc)(void *data));
70 void htable_iterator_init(htable_iterator_t *it);
71 void *htable_iterator_get_data(const htable_iterator_t it);
72 void *htable_iterator_get_key(const htable_iterator_t it);
73 const hnode_t *htable_get_next_elm(const htable_t *htable, htable_iterator_t *it);
75 #ifdef HTABLE_STATS
76 size_t htable_stat_get_grows(const htable_t *htable);
77 size_t htable_stat_get_chain_len(const htable_t *htable, size_t pos);
78 #endif
80 #endif /* HTABLE_H */