1 #ifndef BGLIBS__GENERIC_HASH__H__ 2 #define BGLIBS__GENERIC_HASH__H__ 4 #include "adt_common.h" 55 #define ghash_entry_hash(P) (*(adt_hash_t*)(P)) 57 #define ghash_entry_keyptr(P) ((P)+sizeof(adt_hash_t)) 60 #define ghash_entry_dataptr(P,L) ((P)+sizeof(adt_hash_t)+(L)) 63 #define ghash_hashb adt_hashb 65 #define ghash_hashs adt_hashs 67 #define ghash_hashsp adt_hashsp 70 void*
ghash_add(
struct ghash* d,
const void* key,
const void* data);
71 void*
ghash_set(
struct ghash* d,
const void* key,
const void* data);
92 #define GHASH_STRUCT_ENTRY(PREFIX,KTYPE,DTYPE) \ 93 struct PREFIX##_entry { \ 100 #define GHASH_KEYOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->key) 102 #define GHASH_DATAOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->data) 104 #define GHASH_KEYSIZE(PREFIX) ( \ 105 GHASH_DATAOFFSET(PREFIX)-GHASH_KEYOFFSET(PREFIX) \ 110 #define GHASH_DECL(PREFIX,KTYPE,DTYPE) \ 111 GHASH_STRUCT_ENTRY(PREFIX,KTYPE,DTYPE); \ 112 extern void PREFIX##_init(struct ghash* d); \ 113 extern void PREFIX##_free(struct ghash* d); \ 114 extern struct PREFIX##_entry* PREFIX##_add(struct ghash* d, \ 116 DTYPE const* data); \ 117 extern struct PREFIX##_entry* PREFIX##_set(struct ghash* d, \ 119 DTYPE const* data); \ 120 extern struct PREFIX##_entry* PREFIX##_get(struct ghash* d, \ 122 extern int PREFIX##_rebuild(struct ghash* d); \ 123 extern int PREFIX##_rehash(struct ghash* d); \ 124 extern int PREFIX##_remove(struct ghash* d, KTYPE const* key); \ 125 extern void PREFIX##_foreach(struct ghash* d, \ 126 void (*fn)(struct PREFIX##_entry*)); \ 127 extern struct PREFIX##_entry* PREFIX##_search(struct ghash* d, \ 128 int (*fn)(const struct PREFIX##_entry*)); 131 #define GHASH_INIT_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMP,KCOPY,DCOPY,KFREE,DFREE)\ 132 void PREFIX##_init(struct ghash* d) { \ 134 GHASH_KEYSIZE(PREFIX), \ 135 sizeof(struct PREFIX##_entry), \ 136 (adt_hash_fn*)HASHFN, \ 138 (adt_copy_fn*)KCOPY, \ 139 (adt_copy_fn*)DCOPY, \ 140 (adt_free_fn*)KFREE, \ 141 (adt_free_fn*)DFREE); \ 145 #define GHASH_FREE_DEFN(PREFIX) \ 146 void PREFIX##_free(struct ghash* d) { \ 151 #define GHASH_ADD_DEFN(PREFIX,KTYPE,DTYPE) \ 152 struct PREFIX##_entry* PREFIX##_add(struct ghash* d, \ 153 KTYPE const* key, DTYPE const* data) { \ 154 return ghash_add(d, key, data); \ 158 #define GHASH_SET_DEFN(PREFIX,KTYPE,DTYPE) \ 159 struct PREFIX##_entry* PREFIX##_set(struct ghash* d, \ 160 KTYPE const* key, DTYPE const* data) { \ 161 return ghash_set(d, key, data); \ 165 #define GHASH_GET_DEFN(PREFIX,KTYPE) \ 166 struct PREFIX##_entry* PREFIX##_get(struct ghash* d, KTYPE const* key) { \ 167 return ghash_get(d, key); \ 171 #define GHASH_REBUILD_DEFN(PREFIX) \ 172 int PREFIX##_rebuild(struct ghash* d) { \ 173 return ghash_rebuild(d); \ 177 #define GHASH_REHASH_DEFN(PREFIX) \ 178 int PREFIX##_rehash(struct ghash* d) { \ 179 return ghash_rehash(d); \ 183 #define GHASH_REMOVE_DEFN(PREFIX,KTYPE) \ 184 extern int PREFIX##_remove(struct ghash* d, KTYPE const* key) { \ 185 return ghash_remove(d, (void*)key); \ 189 #define GHASH_FOREACH_DEFN(PREFIX) \ 190 void PREFIX##_foreach(struct ghash* d, void (*fn)(struct PREFIX##_entry*)) { \ 191 ghash_foreach(d, (void (*)(void*))fn); \ 195 #define GHASH_SEARCH_DEFN(PREFIX) \ 196 struct PREFIX##_entry* PREFIX##_search(struct ghash* d, int (*fn)(const struct PREFIX##_entry*)) { \ 197 return ghash_search(d, (int (*)(const void*))fn); \ 204 #define GHASH_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMPFN,KCOPY,DCOPY,KFREE,DFREE) \ 205 GHASH_INIT_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMPFN,KCOPY,DCOPY,KFREE,DFREE) \ 206 GHASH_FREE_DEFN(PREFIX) \ 207 GHASH_ADD_DEFN(PREFIX,KTYPE,DTYPE) \ 208 GHASH_SET_DEFN(PREFIX,KTYPE,DTYPE) \ 209 GHASH_GET_DEFN(PREFIX,KTYPE) \ 210 GHASH_REBUILD_DEFN(PREFIX) \ 211 GHASH_REHASH_DEFN(PREFIX) \ 212 GHASH_REMOVE_DEFN(PREFIX,KTYPE) \ 213 GHASH_FOREACH_DEFN(PREFIX) \ 214 GHASH_SEARCH_DEFN(PREFIX) 232 #define ghashiter_loop(I,G) \ 233 for(ghashiter_first(I,G);ghashiter_valid(I);ghashiter_next(I)) void ghash_insert(struct ghash *d, void *e)
Definition: ghash_insert.c:4
void adt_free_fn(void *)
Definition: adt_common.h:12
int ghash_rehash(struct ghash *d)
Definition: ghash_rehash.c:8
adt_copy_fn * keycopy
Definition: ghash.h:45
unsigned long keysize
Definition: ghash.h:36
adt_copy_fn * datacopy
Definition: ghash.h:47
void ** ghash_find(struct ghash *d, const void *key)
Definition: ghash_get.c:7
adt_free_fn * keyfree
Definition: ghash.h:49
int ghash_remove(struct ghash *d, const void *key)
Definition: ghash_remove.c:12
void ghash_foreach(struct ghash *d, void(*fn)(void *entry))
Definition: ghash_foreach.c:4
void ghashiter_first(struct ghashiter *, const struct ghash *)
Definition: ghash_iter.c:14
adt_free_fn * datafree
Definition: ghash.h:51
int ghash_rebuild(struct ghash *d)
Definition: ghash_rebuild.c:9
void ghash_free(struct ghash *d)
Definition: ghash_free.c:7
int adt_cmp_fn(const void *, const void *)
Definition: adt_common.h:20
unsigned size
Definition: ghash.h:33
adt_cmp_fn * keycmp
Definition: ghash.h:43
void ** table
Definition: ghash.h:29
void * ghash_search(struct ghash *d, int(*fn)(const void *entry))
Definition: ghash_search.c:5
int ghashiter_valid(const struct ghashiter *)
Definition: ghash_iter.c:21
void * ghash_set(struct ghash *d, const void *key, const void *data)
Definition: ghash_set.c:5
void ghashiter_next(struct ghashiter *)
Definition: ghash_iter.c:29
void * ghash_get(struct ghash *d, const void *key)
Definition: ghash_get.c:37
void ghash_init(struct ghash *d, unsigned long keysize, unsigned long entrysize, adt_hash_fn *hashfn, adt_cmp_fn *keycmp, adt_copy_fn *keycopy, adt_copy_fn *datacopy, adt_free_fn *keyfree, adt_free_fn *datafree)
Definition: ghash_init.c:4
void * entry
Definition: ghash.h:224
unsigned index
Definition: ghash.h:222
const struct ghash * ghashp
Definition: ghash.h:220
adt_hash_fn * hashfn
Definition: ghash.h:41
int adt_copy_fn(void *, const void *)
Definition: adt_common.h:16
unsigned count
Definition: ghash.h:31
unsigned long entrysize
Definition: ghash.h:38
void * ghash_add(struct ghash *d, const void *key, const void *data)
Definition: ghash_add.c:74
adt_hash_t adt_hash_fn(const void *)
Definition: adt_common.h:22