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