汇总:
参考:http://troydhanson.github.io/uthash/userguide.html
一、键值是INT型
1、定义结构体,对应键值是整形int,需要有个全局指针比如users
typedef struct {
int id; /* key */
char name[10];
int cnt;
UT_hash_handle hh; /* makes this structure hashable */
}my_struct;
my_struct *users = NULL; /* important! initialize to NULL */
2、ADD&查找操作
先HASH_FIND_INT,如果不能找到,那么新加到HASH_ADD_INT中;如果能找到,就对cnt++;
注意HASH_FIND_INT的第二个入参是当前键值的名称
void add_user(int user_id, char *name, int cnt) {
my_struct *s;
HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */
if (s == NULL) {
s = (my_struct *)malloc(sizeof *s);
s->id = user_id;
s->cnt = cnt;
strcpy(s->name, name);
HASH_ADD_INT(users, id, s); /* id: name of key field */
} else {
s->cnt += cnt;
}
}
3、这个遍历操作比较新鲜;next是s->hh.next
void print_users() {
my_struct *s;
for (s = users; s != NULL; s = s->hh.next) {
printf("user id %d: name %s cnt %d\n", s->id, s->name, s->cnt);
}
}
4、排序操作
int id_sort(my_struct *a, my_struct *b) {
return (a->id - b->id);
}
void sort_by_id() {
HASH_SORT(users, id_sort);
}
5、计数操作
unsigned int num_users;
num_users = HASH_COUNT(users);
printf("\nthere are %u users\n", num_users)
6、删除操作
void delete_all() {
my_struct *current_user, *tmp;
HASH_ITER(hh, users, current_user, tmp) {
HASH_DEL(users, current_user); /* delete; users advances to next */
free(current_user); /* optional- if you want to free */
}
}
Main结果
int main(){
add_user(2002, "zhao", 10);
add_user(1003, "qian", 5);
add_user(1003, "qian", 2);
add_user(2002, "zhao", 3);
add_user(3001, "li", 9);
print_users();
sort_by_id();
printf("\nafter sort:\n");
print_users();
unsigned int num_users;
num_users = HASH_COUNT(users);
printf("\nthere are %u users\n", num_users);
delete_all();
return 0;
}
二、键值是string型
typedef struct {
int id;
char name[10]; /* key */
int cnt;
UT_hash_handle hh; /* makes this structure hashable */
}my_struct;
my_struct *users = NULL; /* important! initialize to NULL */
void add_user(int user_id, char *name, int cnt) {
my_struct *s;
HASH_FIND_STR(users, name, s); /* id already in the hash? */
if (s == NULL) {
s = (my_struct *)malloc(sizeof *s);
s->id = user_id;
s->cnt = cnt;
strcpy(s->name, name);
HASH_ADD_STR(users, name, s); /* id: name of key field */
}
else {
s->cnt += cnt;
}
}
main函数
三、键值是String指针型
typedef struct {
int id;
char *name; /* key */
int cnt;
UT_hash_handle hh; /* makes this structure hashable */
}my_struct;
my_struct *users = NULL; /* important! initialize to NULL */
void add_user(int user_id, char *name, int cnt) {
my_struct *s;
HASH_FIND_STR(users, name, s); /* id already in the hash? */
if (s == NULL) {
s = (my_struct *)malloc(sizeof *s);
s->id = user_id;
s->cnt = cnt;
s->name = name;
HASH_ADD_KEYPTR(hh, users, s->name, strlen(s->name), s); /* id: name of key field */
}
else {
s->cnt += cnt;
}
}
四、键值是结构体
typedef struct {
char *name;
int id;
}Person;
typedef struct {
Person per; /* key */
int cnt;
UT_hash_handle hh; /* makes this structure hashable */
}my_struct;
my_struct *users = NULL; /* important! initialize to NULL */
void add_user(int user_id, char *name, int cnt) {
my_struct *s = NULL;
Person input;
input.name = name;
input.id = user_id;
HASH_FIND(hh, users, &input, sizeof(Person), s); /* id already in the hash? */
if (s == NULL) {
s = (my_struct *)malloc(sizeof *s);
s->per.id = user_id;
s->cnt = cnt;
s->per.name = name;
HASH_ADD(hh, users, per, sizeof(Person), s);
}
else {
s->cnt += cnt;
}
}