C语言hash总结
本文内容基本来自对官网的翻译,若有不准确的地方,望指正。
uthash 是C的比较优秀的开源代码,已经集成到最新的GCC。它实现了常见的hash操作函数,例如查找、插入、删除等待。该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用多个值作为key,无论是自定义的struct还是基本数据类型,需要注意的是不同类型的key其操作接口方式略有不同。使用uthash代码时只需要包含头文件"uthash.h"即可。由于该代码采用宏的方式实现,所有的实现代码都在uthash.h文件中,因此只需要在自己的代码中包含该头文件即可。
一、 uthash的使用
首先,需要定义一个结构体:
struct MyStruct {
int id; /* key */
char name[10];
UT_hash_handle hh; /* makes this structure hashable */
};
其中,
-
id是键(key),也可以是其他的数据结构,不同的数据结构对应hash操作可能不一样
-
name是值(value),其类型根据实际情况定义
-
hh是内部使用的hash处理句柄,在使用过程中,只需要在结构体中定义一个UT_hash_handle类型的变量即可,不需要为该句柄变量赋值,但必须在该结构体中定义该变量。
然后,你只需要愉快的调用uthash所定义的api就可以愉快的使用hash啦。然我们举个例子吧。
Key类型为int时
- 定义一个key为int类型的hash结构体
struct MyStruct {
int key; /* key */
char value[10]; /* value, 若无value也可以去掉该变量 */
UT_hash_handle hh;
};
struct MyStruct *g_users = NULL;
- 注意:定义一个hash结构的空指针
g_users
,用于指向保存数据的hash表,必须初始化为空,在后面的查、插等操作中,uthash内部会根据其是否为空而进行不同的操作。
2. 封装自己的查找接口
void AddUser(int iKey, char *iValue)
{
struct MyStruct *s;
HASH_FIND_INT(g_users, &iKey, is); /* 插入前先查看key值是否已经在hash表g_users里面了 */
if (s==NULL) {
s = (struct MyStruct *)malloc(sizeof(struct MyStruct));
s->key = iKey;
HASH_ADD_INT(g_users, key, s); /* 第二个参数为hash结构里面,hash key值的变量名 */
}
strcpy(s->value, iValue);
}
由于uthash要求键(key)必须唯一,而uthash内部未对key值得唯一性进行很好的处理,因此它要求外部在插入操作时要确保其key值不在当前的hash表中。若插入相同的key值到一个hash中,会被当做一种错误。
3. 封装实现删除接口
void DeleteUser(int ikey)
{
struct MyStruct *s = NULL;
HASH_FIND_INT(g_users, &ikey, s);
if (s==NULL) {
HASH_DEL(g_users, s); // HASH_DEL 只将结构体从hash表中移除,并未释放结构体内容
free(s);
}
}
4. 统计hash表中的元素个数
unsigned int numUsers;
numUsers = HASH_COUNT(g_users);
printf("there are %u items\n", numUsers);
5. 遍历元素
struct MyStruct *s, *tmp;
HASH_ITER(hh, g_users, s, tmp) {
printf("user ikey %d: value %s\n", s->key, s->value);
/* ... it is safe to delete and free s here */
}
还有另外一种删除方法(更加推荐前一种,不易出错)
struct MyStruct *s;
for(s = g_users; s != NULL; s = s->hh.next) {
printf("user ikey %d: value %s\n", s->key, s->value);
}
6. 清空hash表
void DeleteHash()
{
struct MyStruct *currentUser, *tmp;
HASH_ITER(hh, g_users, currentUser, tmp) {
HASH_DEL(g_users, currentUser);
free(currentUser);
}
}
使用注意事项总结
-
在定义hash结构体时不要忘记定义UT_hash_handle的变量
-
需确保key值唯一,如果插入key-value对时,key值已经存在,再插入的时候就会出错
-
不同的key值,其增加和查找调用的接口函数不一样。一般情况下,不通类型的key,其插入和查找接口函数是不一样的,删除、遍历、元素统计接口是通用的,特殊情况下,字符数组和字符串作为key值时,其插入接口函数不一样,但是查找接口是一样的
-
以上举的例子中,g_users是全局变量。但是如果函数如AddUser想将hash结构体的指针g_users作为参数传入。则有以下问题
/* bad */ void AddUser(struct Mytruct *users, int key, char *name) { ... HASH_ADD_INT(users, key, s); } /* good */ void AddUser(struct Mytruct **users, int key, char *name) { ... HASH_ADD_INT( *users, key, s ); }
官方给出的解释如下:The reason it’s necessary to deal with a pointer to the hash pointer is simple: the hash macros modify it (in other words, they modify the pointer itself not just what it points to). 就是这个指针的值会变化。
二、 完整的例子
2.1 key为int
#include <stdio.h> /* gets */
#include <stdlib.h> /* atoi, malloc */
#include <string.h> /* strcpy */
#include "uthash.h"
struct my_struct {
int id; /* key */
char name[10];
UT_hash_handle hh; /* makes this structure hashable */
};
struct my_struct *users = NULL;
void add_user(int user_id, char *name) {
struct my_struct *s;
HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */
if (s==NULL) {
s = (struct my_struct *)malloc(sizeof *s);
s->id = user_id;
HASH_ADD_INT( users, id, s ); /* id: name of key field */
}
strcpy(s->name, name);
}
struct my_struct *find_user(int user_id) {
struct my_struct *s;
HASH_FIND_INT( users, &user_id, s ); /* s: output pointer */
return s;
}
void delete_user(struct my_struct *user) {
HASH_DEL(users, user); /* user: pointer to deletee */
free(user);
}
void delete_all() {
struct my_struct *current_user, *tmp;
HASH_ITER(hh, users, current_user, tmp) {
HASH_DEL(users, current_user); /* delete it (users advances to next) */
free(current_user); /* free it */
}
}
void print_users() {
struct my_struct *s;
for(s=users; s != NULL; s=(struct my_struct*)(s->hh.next)) {
printf("user id %d: name %s\n", s->id, s->name);
}
}
int name_sort(struct my_struct *a, struct my_struct *b) {
return strcmp(a->name,b->name);
}
int id_sort(struct my_struct *a, struct my_struct *b) {
return (a->id - b->id);
}
void sort_by_name() {
HASH_SORT(users, name_sort);
}
void sort_by_id() {
HASH_SORT(users, id_sort);
}
int main(int argc, char *argv[]) {
char in[10];
int id=1, running=1;
struct my_struct *s;
unsigned num_users;
while (running) {
printf(" 1. add user\n");
printf(" 2. add/rename user by id\n");
printf(" 3. find user\n");
printf(" 4. delete user\n");
printf(" 5. delete all users\n");
printf(" 6. sort items by name\n");
printf(" 7. sort items by id\n");
printf(" 8. print users\n");
printf(" 9. count users\n");
printf("10. quit\n");
gets(in);
switch(atoi(in)) {
case 1:
printf("name?\n");
add_user(id++, gets(in));
break;
case 2:
printf("id?\n");
gets(in); id = atoi(in);
printf("name?\n");
add_user(id, gets(in));
break;
case 3:
printf("id?\n");
s = find_user(atoi(gets(in)));
printf("user: %s\n", s ? s->name : "unknown");
break;
case 4:
printf("id?\n");
s = find_user(atoi(gets(in)));
if (s) delete_user(s);
else printf("id unknown\n");
break;
case 5:
delete_all();
break;
case 6:
sort_by_name();
break;
case 7:
sort_by_id();
break;
case 8:
print_users();
break;
case 9:
num_users=HASH_COUNT(users);
printf("there are %u users\n", num_users);
break;
case 10:
running=0;
break;
}
}
delete_all(); /* free any structures */
return 0;
}
2.2 key为字符数组
#include <string.h> /* strcpy */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
#include "uthash.h"
struct my_struct {
char name[10]; /* key (string is WITHIN the structure) */
int id;
UT_hash_handle hh; /* makes this structure hashable */
};
int main(int argc, char *argv[]) {
const char *names[] = { "joe", "bob", "betty", NULL };
struct my_struct *s, *tmp, *users = NULL;
for (int i = 0; names[i]; ++i) {
s = (struct my_struct *)malloc(sizeof *s);
strcpy(s->name, names[i]);
s->id = i;
HASH_ADD_STR(users, name, s);
}
HASH_FIND_STR( users, "betty", s);
if (s) printf("betty's id is %d\n", s->id);
/* free the hash table contents */
HASH_ITER(hh, users, s, tmp) {
HASH_DEL(users, s);
free(s);
}
return 0;
}
2.3 key为字符
#include <string.h> /* strcpy */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
#include "uthash.h"
struct my_struct {
const char *name; /* key */
int id;
UT_hash_handle hh; /* makes this structure hashable */
};
int main(int argc, char *argv[]) {
const char **n, *names[] = { "joe", "bob", "betty", NULL };
struct my_struct *s, *tmp, *users = NULL;
int i=0;
for (n = names; *n != NULL; n++) {
s = (struct my_struct*)malloc(sizeof(struct my_struct));
s->name = *n;
s->id = i++;
HASH_ADD_KEYPTR(hh, users, s->name, strlen(s->name), s);
}
HASH_FIND_STR(users, "betty", s);
if (s) printf("betty's id is %d\n", s->id);
/* free the hash table contents */
HASH_ITER(hh, users, s, tmp) {
HASH_DEL(users, s);
free(s);
}
return 0;
}
参考链接:
https://github.com/troydhanson/uthash
http://troydhanson.github.io/uthash/
http://troydhanson.github.io/uthash/userguide.html