- 论坛徽章:
- 0
|
本帖最后由 peidright 于 2011-07-31 11:15 编辑
- typedef struct htable_s {
- int hsize;
- struct list_head *harray;
- int (*cmp)(void *d1, void *d2);
- int (*hash)(void *d);
- } htable_t;
- #define HTABLE_CREATE(htable, cmp_fn, hash_fn, size)\
- (htable)->cmp = cmp_fn;\
- (htable)->hash = hash_fn;\
- htable->harray = malloc(size * sizeof(struct list_head));\
- (htable)->hsize = size;\
- while(size) {\
- --size;\
- INIT_LIST_HEAD((&htable->harray[size]));\
- }
- #define HTABLE_TINSERT(htable, list_hook, entry)\
- list_add((&(entry)->list_hook),(&htable->harray[htable->hash(entry) % htable->hsize]));
- #define HTABLE_HINSERT(htable, list_hook, entry)\
- key = htable->hash(entry)%htable->hsize;\
- list_add_tail((&(entry)->list_hook),(&htable->harray[htable->hash(entry) % htable->hsize]));
- #define HTABLE_DELETE(htable, list_hook, entry)\
- list_del((&(entry)->list_hook));
- #define HTABLE_FIND(htable, list_hook, entry, retval) do{ \
- list_for_each((*((struct list_head **)retval)), (&htable->harray[htable->hash(entry) % htable->hsize])){\
- if (0==htable->cmp( list_entry(*retval,typeof(*entry), list_hook) , entry)) {\
- break;\
- }\
- }\
- if((* (struct list_head**)retval) == &htable->harray[htable->hash(entry) % htable->hsize]) {\
- (*retval) = NULL;\
- }else {\
- (*retval) = list_entry(*retval, typeof(*entry), list_hook);\
- }\
- }while(0);
- #define HTABLE_DESTORY(htable)\
- free(htable->harray);
- #endif
复制代码 只能说,是相当sb的代码. c不提供范型,大家有简结一点的hashtable的,对与 hashtable_entry 范形一点点的代妈么?
不接受下面这種.
struct {
struct list_head list;
void *
} |
|