ghash.h
#ifndef BGLIBS__GENERIC_HASH__H__
#define BGLIBS__GENERIC_HASH__H__
#include "adt_common.h"
struct ghash
{
void** table;
unsigned count;
unsigned size;
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;
};
#define ghash_entry_hash(P) (*(adt_hash_t*)(P))
#define ghash_entry_keyptr(P) ((P)+sizeof(adt_hash_t))
#define ghash_entry_dataptr(P,L) ((P)+sizeof(adt_hash_t)+(L))
#define ghash_hashb adt_hashb
#define ghash_hashs adt_hashs
#define ghash_hashsp adt_hashsp
void ghash_insert(struct ghash* d, void* e);
void* ghash_add(struct ghash* d, const void* key, const void* data);
void* ghash_set(struct ghash* d, const void* key, const void* data);
void ghash_free(struct ghash* d);
void** ghash_find(struct ghash* d, const void* key);
void* ghash_get(struct ghash* d, const void* key);
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);
int ghash_rebuild(struct ghash* d);
int ghash_rehash(struct ghash* d);
int ghash_remove(struct ghash* d, const void* key);
void ghash_foreach(struct ghash* d, void (*fn)(void* entry));
void* ghash_search(struct ghash* d, int (*fn)(const void* entry));
#define GHASH_STRUCT_ENTRY(PREFIX,KTYPE,DTYPE) \
struct PREFIX##_entry { \
adt_hash_t hash; \
KTYPE key; \
DTYPE data; \
}
#define GHASH_KEYOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->key)
#define GHASH_DATAOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->data)
#define GHASH_KEYSIZE(PREFIX) ( \
GHASH_DATAOFFSET(PREFIX)-GHASH_KEYOFFSET(PREFIX) \
)
#define GHASH_DECL(PREFIX,KTYPE,DTYPE) \
GHASH_STRUCT_ENTRY(PREFIX,KTYPE,DTYPE); \
extern void PREFIX##_init(struct ghash* d); \
extern void PREFIX##_free(struct ghash* d); \
extern struct PREFIX##_entry* PREFIX##_add(struct ghash* d, \
KTYPE const* key, \
DTYPE const* data); \
extern struct PREFIX##_entry* PREFIX##_set(struct ghash* d, \
KTYPE const* key, \
DTYPE const* data); \
extern struct PREFIX##_entry* PREFIX##_get(struct ghash* d, \
KTYPE const* key); \
extern int PREFIX##_rebuild(struct ghash* d); \
extern int PREFIX##_rehash(struct ghash* d); \
extern int PREFIX##_remove(struct ghash* d, KTYPE const* key); \
extern void PREFIX##_foreach(struct ghash* d, \
void (*fn)(struct PREFIX##_entry*)); \
extern struct PREFIX##_entry* PREFIX##_search(struct ghash* d, \
int (*fn)(const struct PREFIX##_entry*));
#define GHASH_INIT_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMP,KCOPY,DCOPY,KFREE,DFREE)\
void PREFIX##_init(struct ghash* d) { \
ghash_init(d, \
GHASH_KEYSIZE(PREFIX), \
sizeof(struct PREFIX##_entry), \
(adt_hash_fn*)HASHFN, \
(adt_cmp_fn*)CMP, \
(adt_copy_fn*)KCOPY, \
(adt_copy_fn*)DCOPY, \
(adt_free_fn*)KFREE, \
(adt_free_fn*)DFREE); \
}
#define GHASH_FREE_DEFN(PREFIX) \
void PREFIX##_free(struct ghash* d) { \
ghash_free(d); \
}
#define GHASH_ADD_DEFN(PREFIX,KTYPE,DTYPE) \
struct PREFIX##_entry* PREFIX##_add(struct ghash* d, \
KTYPE const* key, DTYPE const* data) { \
return ghash_add(d, key, data); \
}
#define GHASH_SET_DEFN(PREFIX,KTYPE,DTYPE) \
struct PREFIX##_entry* PREFIX##_set(struct ghash* d, \
KTYPE const* key, DTYPE const* data) { \
return ghash_set(d, key, data); \
}
#define GHASH_GET_DEFN(PREFIX,KTYPE) \
struct PREFIX##_entry* PREFIX##_get(struct ghash* d, KTYPE const* key) { \
return ghash_get(d, key); \
}
#define GHASH_REBUILD_DEFN(PREFIX) \
int PREFIX##_rebuild(struct ghash* d) { \
return ghash_rebuild(d); \
}
#define GHASH_REHASH_DEFN(PREFIX) \
int PREFIX##_rehash(struct ghash* d) { \
return ghash_rehash(d); \
}
#define GHASH_REMOVE_DEFN(PREFIX,KTYPE) \
extern int PREFIX##_remove(struct ghash* d, KTYPE const* key) { \
return ghash_remove(d, (void*)key); \
}
#define GHASH_FOREACH_DEFN(PREFIX) \
void PREFIX##_foreach(struct ghash* d, void (*fn)(struct PREFIX##_entry*)) { \
ghash_foreach(d, (void (*)(void*))fn); \
}
#define GHASH_SEARCH_DEFN(PREFIX) \
struct PREFIX##_entry* PREFIX##_search(struct ghash* d, int (*fn)(const struct PREFIX##_entry*)) { \
return ghash_search(d, (int (*)(const void*))fn); \
}
#define GHASH_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMPFN,KCOPY,DCOPY,KFREE,DFREE) \
GHASH_INIT_DEFN(PREFIX,KTYPE,DTYPE,HASHFN,CMPFN,KCOPY,DCOPY,KFREE,DFREE) \
GHASH_FREE_DEFN(PREFIX) \
GHASH_ADD_DEFN(PREFIX,KTYPE,DTYPE) \
GHASH_SET_DEFN(PREFIX,KTYPE,DTYPE) \
GHASH_GET_DEFN(PREFIX,KTYPE) \
GHASH_REBUILD_DEFN(PREFIX) \
GHASH_REHASH_DEFN(PREFIX) \
GHASH_REMOVE_DEFN(PREFIX,KTYPE) \
GHASH_FOREACH_DEFN(PREFIX) \
GHASH_SEARCH_DEFN(PREFIX)
struct ghashiter
{
const struct ghash* ghashp;
unsigned index;
void* entry;
};
void ghashiter_first(struct ghashiter*, const struct ghash*);
int ghashiter_valid(const struct ghashiter*);
void ghashiter_next(struct ghashiter*);
#define ghashiter_loop(I,G) \
for(ghashiter_first(I,G);ghashiter_valid(I);ghashiter_next(I))
#endif
ghash_insert
void ghash_insert(struct ghash *d, void *e)
Definition: ghash_insert.c:4
adt_free_fn
void adt_free_fn(void *)
Definition: adt_common.h:12
ghash_rehash
int ghash_rehash(struct ghash *d)
Definition: ghash_rehash.c:8
ghash::keycopy
adt_copy_fn * keycopy
Definition: ghash.h:45
ghash::keysize
unsigned long keysize
Definition: ghash.h:36
ghash::datacopy
adt_copy_fn * datacopy
Definition: ghash.h:47
ghash_find
void ** ghash_find(struct ghash *d, const void *key)
Definition: ghash_get.c:7
ghash::keyfree
adt_free_fn * keyfree
Definition: ghash.h:49
ghash_remove
int ghash_remove(struct ghash *d, const void *key)
Definition: ghash_remove.c:12
ghash_foreach
void ghash_foreach(struct ghash *d, void(*fn)(void *entry))
Definition: ghash_foreach.c:4
ghashiter_first
void ghashiter_first(struct ghashiter *, const struct ghash *)
Definition: ghash_iter.c:14
ghash::datafree
adt_free_fn * datafree
Definition: ghash.h:51
ghash_rebuild
int ghash_rebuild(struct ghash *d)
Definition: ghash_rebuild.c:9
ghash_free
void ghash_free(struct ghash *d)
Definition: ghash_free.c:7
adt_cmp_fn
int adt_cmp_fn(const void *, const void *)
Definition: adt_common.h:20
ghash
Definition: ghash.h:25
ghash::size
unsigned size
Definition: ghash.h:33
ghash::keycmp
adt_cmp_fn * keycmp
Definition: ghash.h:43
ghash::table
void ** table
Definition: ghash.h:29
ghash_search
void * ghash_search(struct ghash *d, int(*fn)(const void *entry))
Definition: ghash_search.c:5
ghashiter_valid
int ghashiter_valid(const struct ghashiter *)
Definition: ghash_iter.c:21
ghashiter
Definition: ghash.h:217
ghash_set
void * ghash_set(struct ghash *d, const void *key, const void *data)
Definition: ghash_set.c:5
ghashiter_next
void ghashiter_next(struct ghashiter *)
Definition: ghash_iter.c:29
ghash_get
void * ghash_get(struct ghash *d, const void *key)
Definition: ghash_get.c:37
ghash_init
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
ghashiter::entry
void * entry
Definition: ghash.h:224
ghashiter::index
unsigned index
Definition: ghash.h:222
ghashiter::ghashp
const struct ghash * ghashp
Definition: ghash.h:220
ghash::hashfn
adt_hash_fn * hashfn
Definition: ghash.h:41
adt_copy_fn
int adt_copy_fn(void *, const void *)
Definition: adt_common.h:16
ghash::count
unsigned count
Definition: ghash.h:31
ghash::entrysize
unsigned long entrysize
Definition: ghash.h:38
ghash_add
void * ghash_add(struct ghash *d, const void *key, const void *data)
Definition: ghash_add.c:74
adt_hash_fn
adt_hash_t adt_hash_fn(const void *)
Definition: adt_common.h:22