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
- hash链表自动生成宏
- Hibernate自动生成表
- hibernate 自动生成表
- Hibernate自动生成表
- 数据库表自动生成
- Hibernate表自动生成
- hibernate自动生成表
- facebook key hash生成
- 加盐hash生成密码
- 生成key hash
- 自动为表生成序列号
- hibernate配置文件 自动生成表
- hibernate自动生成数据库表
- Hibernate自动生成数据库表
- hibernate 自动生成数据库表
- 由Hibernate自动生成表
- js自动生成颜色表
- SSH自动生成表结构
- gaussian mixture model
- unity3d 5.5以上场景物体不能被移动不能编辑问题
- SAP根据配置表名和配置信息查找传输请求号及时间
- udp与tcp协议介绍
- Uiautomator 2.0 VS 1.0
- hash链表自动生成宏
- 使用机顶盒录码流方法
- 技术 svn 使用简介
- C/C++ 取整函数ceil(),floor()
- ansible-playbook
- scikit-learn:0.3. 从文本文件中提取特征(tf、tf-idf)、训练一个分类器
- JDK目录结构和文件作用介绍
- Java操作excel-兼容office 2007版本之后
- Android SELinux 调试