#include "uthash.h"
// hash结构体
struct hashtable {
int key;
char val[10];
UT_hash_handle hh;
};
// hash初始化
struct hashtable *my_hash = NULL;
// hash查找
HASH_FIND_INT(my_hash, &my_key, hash2ret); // 参数二是个指针
// hash添加
HASH_ADD_INT(my_hash, key, hash2add); // 参数二是key名
// hash删除
HASH_DEL(my_hash, hash2del);
free(hash2del);
// hash清除
HASH_CLEAR(hh, my_hash); // 没有free
// hash计数
int count = HASH_COUNT(my_hash);
// hash遍历
struct hashtable *iter;
for (iter = my_hash; iter != NULL; iter = iter->hh.next) {
printf("key=%d, val=%s \n", iter->key, iter->val);
}
// hash排序
int name_sort(struct my_struct *a, struct my_struct *b) {
return strcmp(a->name, b->name);
}
HASH_SORT(users, name_sort);
int id_sort(struct my_struct *a, struct my_struct *b) {
return (a->id - b->id);
}
HASH_SORT(users, id_sort);