hash链表自动生成宏

来源:互联网 发布:dsa数据 编辑:程序博客网 时间:2024/06/06 03:52
#include <stdio.h>#include <stdlib.h>#include <memory.h>struct StdListLinker {    struct StdListLinker * next;    struct StdListLinker * prev;};typedef struct ListHead_struct {    struct StdListLinker link_mbr; /* must be the first member of ListHeadType */    int total_elements;} ListHeadType;typedef struct HashListTestStruct {    struct StdListLinker link_mbr; /* the first member must be "link_mbr" */    int index;    int data;} HashListTestType;static inline void __util_list_add(struct StdListLinker * the_new,                               struct StdListLinker * prev, struct StdListLinker * next){    next->prev = the_new;    the_new->next = next;    the_new->prev = prev;    prev->next = the_new;}static inline void util_list_add(struct StdListLinker * the_new, struct StdListLinker * head){    __util_list_add(the_new, head, head->next);}static inline void __util_list_del(struct StdListLinker * prev, struct StdListLinker * next){    next->prev = prev;    prev->next = next;}static inline void util_list_del(struct StdListLinker * entry){    __util_list_del(entry->prev, entry->next);    entry->next = (void *) 0;    entry->prev = (void *) 0;}int std_init_hash_list(ListHeadType * hash_list, int hash_size){    int i = 0;    if(hash_list == NULL || hash_size < 1) return -1;    for(i = 0; i < hash_size; ++i) {        hash_list[i].total_elements = 0;        hash_list[i].link_mbr.next = &hash_list[i].link_mbr;         hash_list[i].link_mbr.prev = &hash_list[i].link_mbr;     }    return 0;}ListHeadType g_hashlist_test_table[10] = {{0}};#define DEFINE_HASH_LIST_INSERT_FUN(the_func_name,data_type,hash_fun_ptr)  \int the_func_name(ListHeadType * the_buckets, data_type * the_ele)      \{                                                                       \    printf("%s \n",__FUNCTION__);                                       \    int hash_code = 0;                                                  \    struct StdListLinker * pos = NULL;                                  \    struct StdListLinker * the_head = NULL;                             \    data_type * new_ele_ptr = NULL;                                     \    if(the_buckets == NULL  || the_ele == NULL) return -4;              \    new_ele_ptr = (data_type *)malloc(sizeof(data_type));               \    if(new_ele_ptr == NULL) return -3;                                  \    memset(new_ele_ptr, 0, sizeof(data_type));                          \    memcpy(new_ele_ptr, the_ele, sizeof(data_type));                    \    hash_code = hash_fun_ptr(new_ele_ptr);                              \    printf("%s hash_code = %d \n",__FUNCTION__, hash_code);             \    the_head = (struct StdListLinker *) &the_buckets[hash_code];        \    pos = (struct StdListLinker *)new_ele_ptr;                          \    util_list_add(pos,the_head);                                        \    the_buckets[hash_code].total_elements += 1;                         \    return 0;                                                       \}#define DEFINE_HASH_LIST_LOOKUP_FUN(the_func_name, data_type, hash_fun_name, cmp_fun_name)  \struct StdListLinker * the_func_name(ListHeadType * list_header, data_type * the_ele)  \{                                                                       \    printf("%s \n",__FUNCTION__);                                       \    int hash_code = 0;                                                  \    struct StdListLinker * pos = NULL;                                  \    struct StdListLinker * the_head = NULL;                             \    if(list_header == NULL  || the_ele == NULL) return NULL;            \    hash_code = hash_fun_name(the_ele);                                 \    printf("%s hash_code = %d \n",__FUNCTION__, hash_code);             \    the_head = (struct StdListLinker *) &list_header[hash_code];        \    for(pos = the_head->next; pos != the_head; pos = pos->next) {       \        if(!cmp_fun_name((data_type *)pos, the_ele)) {                  \            return pos;                                                 \        }                                                               \    }                                                                   \    return NULL;                                                        \}#define DEFINE_HASH_LIST_DELETE_FUN(the_func_name,data_type, hash_fun_name)  \int the_func_name(ListHeadType * the_buckets, data_type * the_ele)      \{                                                                       \    printf("%s \n",__FUNCTION__);                                       \    int hash_code = 0;                                                  \    struct StdListLinker * pos = NULL;                                  \    if(the_buckets == NULL  || the_ele == NULL) return -4;              \    pos = (struct StdListLinker *) the_ele;                             \    hash_code = hash_fun_name(the_ele);                                 \    printf("%s hash_code = %d \n",__FUNCTION__, hash_code);             \    util_list_del(pos);                                                 \    free((char*)the_ele);                                               \    the_ele = NULL;                                                     \    the_buckets[hash_code].total_elements -= 1;                         \    return 0;                                                           \}int inline hash_code_creator(HashListTestType * the_ele){                                       return the_ele->index % 10;}int inline list_node_cmpfunc(HashListTestType * ele_in_list, HashListTestType * the_ele){                                       if(ele_in_list->index == the_ele->index) {        return 0;    }    return -1;}DEFINE_HASH_LIST_INSERT_FUN(hash_list_insert, HashListTestType, hash_code_creator);DEFINE_HASH_LIST_DELETE_FUN(hash_list_delete, HashListTestType, hash_code_creator);DEFINE_HASH_LIST_LOOKUP_FUN(hash_list_lookup, HashListTestType, hash_code_creator,list_node_cmpfunc);int dump_hash_test(){    HashListTestType * TestInfo_p = NULL;    struct StdListLinker * the_listhead  = NULL;    struct StdListLinker * pos = NULL;    int i = 0;    int total = 0;    for(i=0; i < 10; i++) {        the_listhead = (struct StdListLinker *)&g_hashlist_test_table[i];        total += g_hashlist_test_table[i].total_elements;        printf("~~~~~~~~~bucket[%d].total_elements = %d \n", i,g_hashlist_test_table[i].total_elements);        for(pos = the_listhead->next; pos != the_listhead; pos = pos->next)     {            TestInfo_p = (HashListTestType *)pos;             printf("ros_index = %d \n", TestInfo_p->index);        }    }    printf("~~~~~~~~~total_teifs = %d \n ", total);    return 0;}int main(){    int i = 0;    int rv = 0;    std_init_hash_list(g_hashlist_test_table,10);    HashListTestType test_date[33] = {{{0}}};    HashListTestType * test_ptr  = NULL;    for(i = 0; i < 10 ;i++){        test_date[i].index = i;        rv = hash_list_insert(g_hashlist_test_table,&test_date[i]);    }    test_ptr = (HashListTestType *)hash_list_lookup(g_hashlist_test_table,&test_date[3]);    if(!test_ptr){        printf("notfound \n");    }else{        printf("test_ptr.index = %d \n",test_ptr->index);    }    dump_hash_test();    hash_list_delete(g_hashlist_test_table,test_ptr);    dump_hash_test();    return 0;}
0 0
原创粉丝点击