C开源hash代码uthash的用法总结

来源:互联网 发布:jquery ajax 返回json 编辑:程序博客网 时间:2024/06/05 10:16

uthash 是C的比较优秀的开源代码,它实现了常见的hash操作函数,例如查找、插入、删除等待。该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用多个值作为key,无论是自定义的struct还是基本数据类型,需要注意的是不同类型的key其操作接口方式略有不通。

      使用uthash代码时只需要包含头文件"uthash.h"即可。由于该代码采用宏的方式实现,所有的实现代码都在uthash.h文件中,因此只需要在自己的代码中包含该头文件即可。可以通过下面两种方式获取源代码:

  • 通过官方下载链接:

https://github.com/troydhanson/uthash

  • 国外网络无法访问的时候,可以从下面网址获取:

http://download.csdn.net/detail/hjx_1000/6539789

  • 另外,uthash的英文使用文档介绍可从下面网址获得:

http://troydhanson.github.io/uthash/userguide.html#_add_item

1.uthash的效率

      uthash的插入、查找、删除的操作时间都是常量,当然这个常量的值收到key以及所选择的hash函数的影响,uthash共提供了7中函数函数,一般情况下选择默认的即可。如果对效率要求特别高时,可以再根据自己的需求选择适合自己的hash函数。

2、uthash的使用

      在hash操作中,都是按照“键-值“对的方式进行插、查等操作,在uthash中,其基本数据结构就是一个包含“键-值“对的结构体,另外,该结构体中还包含一个uthash内部使用的hash处理句柄,如下代码所示:

[cpp] view plaincopy
  1. #include"uthash.h"  
  2.    
  3. struct my_struct {  
  4.     int id;                    /* key */  
  5.     char name[10];  
  6.     UT_hash_handle hh;         /* makes this structure hashable */  
  7. };  
其中:

  • id是键(key);
  • name是值,即自己要保存的数据域,这里可以根据自己的需要让它变成结构体指针或者其他类型都可以;
  • hh是内部使用的hash处理句柄,在使用过程中,只需要在结构体中定义一个UT_hash_handle类型的变量即可,不需要为该句柄变量赋值,但必须在该结构体中定义该变量
  •       Uthash所实现的hash表中可以提供类似于双向链表的操作,可以通过结构体成员hh的 hh.prevhh.next获取当前节点的上一个节点或者下一个节点。

3.Key类型为int的简单示例

1)定义一个键为int类型的hash结构体:
[cpp] view plaincopy
  1. #include "uthash.h"  
  2.    
  3. struct my_struct {  
  4.     int ikey;                    /* key */  
  5.     char value[10];  
  6. UT_hash_handle hh;           
  7.      };  
  8.      struct my_struct *g_users = NULL;  
这里需要注意:
  • key的类型为int,key的类型不一样,后面的插入、查找调用的接口函数就不一样,因此要求确保key的类型与uthash的接口函数一致。
  • 必须提供UT_hash_handle变量hh,无需为其初始化。
  • 定义一个hash结构的空指针users,用于指向保存数据的hash表,必须初始化为空,在后面的查、插等操作中,uthash内部会根据其是否为空而进行不同的操作。

2)实现自己的查找接口函数:

[cpp] view plaincopy
  1. struct my_struct *find_user(int ikey) {  
  2.     struct my_struct *s;  
  3. HASH_FIND_INT(g_users, &ikey, s );  
  4. return s;  
  5. }  
其实现过程就是先定义一个hash结构体指针变量,然后通过HASH_FIND_INT接口找到该key所对应的hash结构体。这里需要注意:
  • Uthash为整型key提供的查找接口为HASH_FIND_INT
  • 传给接口HASH_FIND_INT的第一个参数就是在1)中定义的指向hash表的指针,传入的第二个参数是整型变量ikey的地址。

3)实现自己的插入接口函数:

[cpp] view plaincopy
  1. void add_user(int ikey, char *value_buf) {  
  2.     struct my_struct *s;  
  3.     HASH_FIND_INT(g_users, &ikey, s);  /* 插入前先查看key值是否已经在hash表g_users里面了 */  
  4.     if (s==NULL) {  
  5.       s = (struct my_struct*)malloc(sizeof(struct my_struct));  
  6.       s->ikey = ikey;  
  7.       HASH_ADD_INT(g_users, ikey, s );  /* 这里必须明确告诉插入函数,自己定义的hash结构体中键变量的名字 */  
  8.     }  
  9.     strcpy(s-> value, value_buf);  
  10. }  
由于uthash要求键(key)必须唯一,而uthash内部未对key值得唯一性进行很好的处理,因此它要求外部在插入操作时要确保其key值不在当前的hash表中,这就需要,在插入操作时,先查找hash表看其值是否已经存在,不存在在时再进行插入操作,在这里需要特别注意以下两点:
  • 插入时,先查找,当键不在当前的hash表中时再进行插入,以确保键的唯一性。
  • 需调用插入接口函数时需要明确告诉接口函数,自己定义的键变量的名字是什么。

4)实现删除接口

[cpp] view plaincopy
  1. void delete_user(int ikey) {  
  2.     struct my_struct *s = NULL;  
  3.     HASH_FIND_INT(g_users, &ikey, s);  
  4.     if (s==NULL) {  
  5.       HASH_DEL(g_users, s);   
  6.       free(s);              
  7.     }  
  8. }  

      删除操作的接口函数为HASH_DEL,只需要告诉该接口要释放哪个hash表(这里是g_users)里的哪个节点(这里是s),需要注意:释放申请的hash结构体变量,uthash函数只将结构体从hash表中移除,并未释放该结构体所占据的内存。

5)清空hash表

[cpp] view plaincopy
  1. void delete_all() {  
  2.   struct my_struct *current_user, *tmp;  
  3.    
  4.   HASH_ITER(hh, users, current_user, tmp) {  
  5.     HASH_DEL(g_users,current_user);    
  6. free(current_user);              
  7.   }  
  8. }  
这里需要注意:uthash内部提供了另外一个清空函数:
HASH_CLEAR(hh, g_users);

函数,但它不释放各节点的内存,因此尽量不要使用它,

6)统计hash表中的已经存在的元素数

该操作使用函数HASH_COUNT即可获取到当前hash表中的元素数,其用法为:
[cpp] view plaincopy
  1. unsigned int num_users;  
  2. num_users = HASH_COUNT(g_users);  
  3. printf("there are %u items\n", num_users);  
7、遍历元素
      在开发过程中,可能需要对整个hash表进行遍历,这里可以通过hh.next获取当前元素的下一个元素。具体遍历方法为:
[cpp] view plaincopy
  1. struct my_struct *s, *tmp;  
  2. HASH_ITER(hh, g_users, s, tmp) {  
  3.     printf("user ikey %d: value %s\n", s->ikey, s->value);  
  4.     /* ... it is safe to delete and free s here */  
  5. }  
另外还有一种不安全的删除方法,尽量避免使用它:
[cpp] view plaincopy
  1. void print_users() {  
  2.     struct my_struct *s;  
  3.    
  4.     for(s=g_users; s != NULL; s=s->hh.next) {  
  5.         printf("user ikey %d: value %s\n", s->ikey, s->value);  
  6.     }  
  7. }  
4. 其他类型key的使用
       本节主要关于key值类型为其他任意类型,例如整型、字符串、指针、结构体等时的用法。
         注意:在使用key值为浮点类型时,由于浮点类型的比较受到精度的影响,例如:1.0000000002被认为与1相等,这些问题在uthash中也存在。
4.1. int类型key
       前面就是以int类型的key作为示例,总结int类型key使用方法,可以看到其查找和插入分别使用专用接口:HASH_FIND_INTASH_ADD_INT
4.2. 字符指针char*类型key与字符数组char key[100]类型key
       特别注意在Strting类型中,uthash对指针char*和字符数组(例如char key[100])做了区分,这两种情况下使用的接口函数时不一样的。在添加的时候,key的类型为指针时使用接口函数HASH_ADD_KEYPTR,key的类型为字符数组时,使用接口函数HASH_ADD_STR,除了添加的接口不一样外,其他的查找、删除、变量等接口函数都是一样的。
4.3.使用地址作为key
          在uthash中也可使用地址做key进行hash操作,使用地址作为key值时,其类型为void*,这样它就可以支持任意类型的地址了。在使用地址作为key时,插入和查找的专用接口函数为HASH_ADD_PTRHASH_FIND_PTR,其余接口是一样的。
4.3.其他非常用类型key
       在uthash中还可使用结构体作为key,甚至可以采用组合的方式让多个值作为key,这些在其官方的网站张均有较详细的使用示例。在使用uthash需要注意以下几点:

  •  在定义hash结构体时不要忘记定义UT_hash_handle的变量
  •  需确保key值唯一,如果插入key-value对时,key值已经存在,再插入的时候就会出错。

  • 不同的key值,其增加和查找调用的接口函数不一样,具体可见第4节。一般情况下,不通类型的key,其插入和查找接口函数是不一样的,删除、遍历、元素统计接口是通用的,特殊情况下,字符数组和字符串作为key值时,其插入接口函数不一样,但是查找接口是一样的。

5.完整程序例子

5.1.key类型为int的完整的例子

[cpp] view plaincopy
  1. #include <stdio.h>   /* gets */  
  2. #include <stdlib.h>  /* atoi, malloc */  
  3. #include <string.h>  /* strcpy */  
  4. #include "uthash.h"  
  5.   
  6. struct my_struct {  
  7.     int ikey;                    /* key */  
  8.     char value[10];  
  9.     UT_hash_handle hh;         /* makes this structure hashable */  
  10. };  
  11.   
  12. static struct my_struct *g_users = NULL;  
  13.   
  14. void add_user(int mykey, char *value) {  
  15.     struct my_struct *s;  
  16.   
  17.     HASH_FIND_INT(users, &mykey, s);  /* mykey already in the hash? */  
  18.     if (s==NULL) {  
  19.       s = (struct my_struct*)malloc(sizeof(struct my_struct));  
  20.       s->ikey = mykey;  
  21.       HASH_ADD_INT( users, ikey, s );  /* ikey: name of key field */  
  22.     }  
  23.     strcpy(s->value, value);  
  24. }  
  25.   
  26. struct my_struct *find_user(int mykey) {  
  27.     struct my_struct *s;  
  28.   
  29.     HASH_FIND_INT( users, &mykey, s );  /* s: output pointer */  
  30.     return s;  
  31. }  
  32.   
  33. void delete_user(struct my_struct *user) {  
  34.     HASH_DEL( users, user);  /* user: pointer to deletee */  
  35.     free(user);  
  36. }  
  37.   
  38. void delete_all() {  
  39.   struct my_struct *current_user, *tmp;  
  40.   
  41.   HASH_ITER(hh, users, current_user, tmp) {  
  42.     HASH_DEL(users,current_user);  /* delete it (users advances to next) */  
  43.     free(current_user);            /* free it */  
  44.   }  
  45. }  
  46.   
  47. void print_users() {  
  48.     struct my_struct *s;  
  49.   
  50.     for(s=users; s != NULL; s=(struct my_struct*)(s->hh.next)) {  
  51.         printf("user ikey %d: value %s\n", s->ikey, s->value);  
  52.     }  
  53. }  
  54.   
  55. int name_sort(struct my_struct *a, struct my_struct *b) {  
  56.     return strcmp(a->value,b->value);  
  57. }  
  58.   
  59. int id_sort(struct my_struct *a, struct my_struct *b) {  
  60.     return (a->ikey - b->ikey);  
  61. }  
  62.   
  63. void sort_by_name() {  
  64.     HASH_SORT(users, name_sort);  
  65. }  
  66.   
  67. void sort_by_id() {  
  68.     HASH_SORT(users, id_sort);  
  69. }  
  70.   
  71. int main(int argc, char *argv[]) {  
  72.     char in[10];  
  73.     int ikey=1, running=1;  
  74.     struct my_struct *s;  
  75.     unsigned num_users;  
  76.   
  77.     while (running) {  
  78.         printf(" 1. add user\n");  
  79.         printf(" 2. add/rename user by id\n");  
  80.         printf(" 3. find user\n");  
  81.         printf(" 4. delete user\n");  
  82.         printf(" 5. delete all users\n");  
  83.         printf(" 6. sort items by name\n");  
  84.         printf(" 7. sort items by id\n");  
  85.         printf(" 8. print users\n");  
  86.         printf(" 9. count users\n");  
  87.         printf("10. quit\n");  
  88.         gets(in);  
  89.         switch(atoi(in)) {  
  90.             case 1:  
  91.                 printf("name?\n");  
  92.                 add_user(ikey++, gets(in));  
  93.                 break;  
  94.             case 2:  
  95.                 printf("id?\n");  
  96.                 gets(in); ikey = atoi(in);  
  97.                 printf("name?\n");  
  98.                 add_user(ikey, gets(in));  
  99.                 break;  
  100.             case 3:  
  101.                 printf("id?\n");  
  102.                 s = find_user(atoi(gets(in)));  
  103.                 printf("user: %s\n", s ? s->value : "unknown");  
  104.                 break;  
  105.             case 4:  
  106.                 printf("id?\n");  
  107.                 s = find_user(atoi(gets(in)));  
  108.                 if (s) delete_user(s);  
  109.                 else printf("id unknown\n");  
  110.                 break;  
  111.             case 5:  
  112.                 delete_all();  
  113.                 break;  
  114.             case 6:  
  115.                 sort_by_name();  
  116.                 break;  
  117.             case 7:  
  118.                 sort_by_id();  
  119.                 break;  
  120.             case 8:  
  121.                 print_users();  
  122.                 break;  
  123.             case 9:  
  124.                 num_users=HASH_COUNT(users);  
  125.                 printf("there are %u users\n", num_users);  
  126.                 break;  
  127.             case 10:  
  128.                 running=0;  
  129.                 break;  
  130.         }  
  131.     }  
  132.   
  133.     delete_all();  /* free any structures */  
  134.     return 0;  
  135. }  

5.2.key类型为字符数组的完整的例子

[cpp] view plaincopy
  1. #include <string.h>  /* strcpy */  
  2. #include <stdlib.h>  /* malloc */  
  3. #include <stdio.h>   /* printf */  
  4. #include "uthash.h"  
  5.   
  6. struct my_struct {  
  7.     char name[10];             /* key (string is WITHIN the structure) */  
  8.     int id;  
  9.     UT_hash_handle hh;         /* makes this structure hashable */  
  10. };  
  11.   
  12.   
  13. int main(int argc, char *argv[]) {  
  14.     const char **n, *names[] = { "joe""bob""betty", NULL };  
  15.     struct my_struct *s, *tmp, *users = NULL;  
  16.     int i=0;  
  17.   
  18.     for (n = names; *n != NULL; n++) {  
  19.         s = (struct my_struct*)malloc(sizeof(struct my_struct));  
  20.         strncpy(s->name, *n,10);  
  21.         s->id = i++;  
  22.         HASH_ADD_STR( users, name, s );  
  23.     }  
  24.   
  25.     HASH_FIND_STR( users, "betty", s);  
  26.     if (s) printf("betty's id is %d\n", s->id);  
  27.   
  28.     /* free the hash table contents */  
  29.     HASH_ITER(hh, users, s, tmp) {  
  30.       HASH_DEL(users, s);  
  31.       free(s);  
  32.     }  
  33.     return 0;  
  34. }  

5.3.key类型为字符指针的完整的例子

[cpp] view plaincopy
  1. #include <string.h>  /* strcpy */  
  2. #include <stdlib.h>  /* malloc */  
  3. #include <stdio.h>   /* printf */  
  4. #include "uthash.h"  
  5.   
  6. struct my_struct {  
  7.     const char *name;          /* key */  
  8.     int id;  
  9.     UT_hash_handle hh;         /* makes this structure hashable */  
  10. };  
  11.   
  12.   
  13. int main(int argc, char *argv[]) {  
  14.     const char **n, *names[] = { "joe""bob""betty", NULL };  
  15.     struct my_struct *s, *tmp, *users = NULL;  
  16.     int i=0;  
  17.   
  18.     for (n = names; *n != NULL; n++) {  
  19.         s = (struct my_struct*)malloc(sizeof(struct my_struct));  
  20.         s->name = *n;  
  21.         s->id = i++;  
  22.         HASH_ADD_KEYPTR( hh, users, s->name, strlen(s->name), s );  
  23.     }  
  24.   
  25.     HASH_FIND_STR( users, "betty", s);  
  26.     if (s) printf("betty's id is %d\n", s->id);  
  27.   
  28.     /* free the hash table contents */  
  29.     HASH_ITER(hh, users, s, tmp) {  
  30.       HASH_DEL(users, s);  
  31.       free(s);  
  32.     }  
  33.     return 0;  
  34. }  

0 0
原创粉丝点击