bglibs
ghash.h
1 #ifndef BGLIBS__GENERIC_HASH__H__
2 #define BGLIBS__GENERIC_HASH__H__
3 
4 #include "adt_common.h"
5 
25 struct ghash
26 {
29  void** table;
31  unsigned count;
33  unsigned size;
34 
36  unsigned long keysize;
38  unsigned long entrysize;
39 
52 };
53 
55 #define ghash_entry_hash(P) (*(adt_hash_t*)(P))
56 
57 #define ghash_entry_keyptr(P) ((P)+sizeof(adt_hash_t))
58 
60 #define ghash_entry_dataptr(P,L) ((P)+sizeof(adt_hash_t)+(L))
61 
63 #define ghash_hashb adt_hashb
64 
65 #define ghash_hashs adt_hashs
66 
67 #define ghash_hashsp adt_hashsp
68 
69 void ghash_insert(struct ghash* d, void* e);
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);
72 void ghash_free(struct ghash* d);
73 void** ghash_find(struct ghash* d, const void* key);
74 void* ghash_get(struct ghash* d, const void* key);
75 void ghash_init(struct ghash* d,
76  unsigned long keysize,
77  unsigned long entrysize,
84 int ghash_rebuild(struct ghash* d);
85 int ghash_rehash(struct ghash* d);
86 int ghash_remove(struct ghash* d, const void* key);
87 void ghash_foreach(struct ghash* d, void (*fn)(void* entry));
88 void* ghash_search(struct ghash* d, int (*fn)(const void* entry));
89 
92 #define GHASH_STRUCT_ENTRY(PREFIX,KTYPE,DTYPE) \
93 struct PREFIX##_entry { \
94  adt_hash_t hash; \
95  KTYPE key; \
96  DTYPE data; \
97 }
98 
100 #define GHASH_KEYOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->key)
101 
102 #define GHASH_DATAOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->data)
103 
104 #define GHASH_KEYSIZE(PREFIX) ( \
105  GHASH_DATAOFFSET(PREFIX)-GHASH_KEYOFFSET(PREFIX) \
106 )
107 
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, \
115  KTYPE const* key, \
116  DTYPE const* data); \
117 extern struct PREFIX##_entry* PREFIX##_set(struct ghash* d, \
118  KTYPE const* key, \
119  DTYPE const* data); \
120 extern struct PREFIX##_entry* PREFIX##_get(struct ghash* d, \
121  KTYPE const* key); \
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*));
129 
131 #define GHASH_INIT_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMP,KCOPY,DCOPY,KFREE,DFREE)\
132 void PREFIX##_init(struct ghash* d) { \
133  ghash_init(d, \
134  GHASH_KEYSIZE(PREFIX), \
135  sizeof(struct PREFIX##_entry), \
136  (adt_hash_fn*)HASHFN, \
137  (adt_cmp_fn*)CMP, \
138  (adt_copy_fn*)KCOPY, \
139  (adt_copy_fn*)DCOPY, \
140  (adt_free_fn*)KFREE, \
141  (adt_free_fn*)DFREE); \
142 }
143 
145 #define GHASH_FREE_DEFN(PREFIX) \
146 void PREFIX##_free(struct ghash* d) { \
147  ghash_free(d); \
148 }
149 
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); \
155 }
156 
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); \
162 }
163 
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); \
168 }
169 
171 #define GHASH_REBUILD_DEFN(PREFIX) \
172 int PREFIX##_rebuild(struct ghash* d) { \
173  return ghash_rebuild(d); \
174 }
175 
177 #define GHASH_REHASH_DEFN(PREFIX) \
178 int PREFIX##_rehash(struct ghash* d) { \
179  return ghash_rehash(d); \
180 }
181 
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); \
186 }
187 
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); \
192 }
193 
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); \
198 }
199 
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)
215 
217 struct ghashiter
218 {
220  const struct ghash* ghashp;
222  unsigned index;
224  void* entry;
225 };
226 
227 void ghashiter_first(struct ghashiter*, const struct ghash*);
228 int ghashiter_valid(const struct ghashiter*);
229 void ghashiter_next(struct ghashiter*);
232 #define ghashiter_loop(I,G) \
233  for(ghashiter_first(I,G);ghashiter_valid(I);ghashiter_next(I))
234 
237 #endif
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
Definition: ghash.h:25
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
Definition: ghash.h:217
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