key为整型
1.引入库并初始化
代码如下(示例):
#include "uthash.h"
typedef struct {
int key; //可以就只有key不加value
int value;
UT_hash_handle hh;
} Hash;
Hash *hashtable = NULL;
2.增删改查hash数据
2.1 增加和修改 hash_insert(key,val)
/* 插入和修改hash */
void hash_insert(int ikey, int ival) {
struct Hash* it = find(ikey);
if (it == NULL) {
struct Hash* tmp = malloc(sizeof(struct Hash));
tmp->key = ikey, tmp->val = ival;
HASH_ADD_INT(hashtable, key, tmp);
} else {
it->val = ival;
}
}
2.2 查找 hash_find(key)
找到返回hash数据,找不到返回NULL
/* 查找hash */
struct hashTable* hash_find(int ikey) {
struct Hash* tmp;
HASH_FIND_INT(hashtable, &ikey, tmp);
return tmp;
}
2.3 删除 hash_delete(key)
传入key值,删除成功返回1,失败返回0
/* 删除hash */
int hash_delete(int ikey) {
struct Hash* it = find(ikey);
if (it != NULL) {
HASH_DEL(hash, it);
free(it);
it = NULL;
return 1;
}else{
return 0;
}
}
总结
最后在把代码整合在一起方便调用
#include "uthash.h"
typedef struct {
int key; //可以就只有key不加value
int value;
UT_hash_handle hh;
} Hash;
Hash *hashtable = NULL;
struct Hash* hash_find(int ikey) {
struct Hash* tmp;
HASH_FIND_INT(hashtable, &ikey, tmp);
return tmp;
}
void hash_insert(int ikey, int ival) {
struct Hash* it = find(ikey);
if (it == NULL) {
struct Hash* tmp = malloc(sizeof(struct Hash));
tmp->key = ikey, tmp->val = ival;
HASH_ADD_INT(hashtable, key, tmp);
} else {
it->val = ival;
}
}
int hash_delete(int ikey) {
struct Hash* it = find(ikey);
if (it != NULL) {
HASH_DEL(hashtable, it);
free(it);
it = NULL;
return 1;
}else{
return 0;
}
}
key为字符串string
那么使用的方法依赖于你定义key的方法:指针的方式(char *)还是数组的方式(char a[10])。这个差异是非常重要的。当你的结构体使用的是指针方式,那么你应当使用HASH_ADD_KEYPTR方法;当你使用的数组方式,则需要使用HASH_ADD_STR方法。
#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;
}
#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 *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);
s->name = names[i];
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;
}
指针key
你可以使用指针(地址)作为key,也就是说指针(地址)本身也可以作为key来使用。也对应HASH_ADD_KEYPTR相关方法。下面是一个实例:
#include <stdio.h>
#include <stdlib.h>
#include "uthash.h"
typedef struct {
void *key;
int i;
UT_hash_handle hh;
} el_t;
el_t *hash = NULL;
char *someaddr = NULL;
int main() {
el_t *d;
el_t *e = (el_t *)malloc(sizeof *e);
if (!e) return -1;
e->key = (void*)someaddr;
e->i = 1;
HASH_ADD_PTR(hash,key,e);
HASH_FIND_PTR(hash, &someaddr, d);
if (d) printf("found\n");
/* release memory */
HASH_DEL(hash,e);
free(e);
return 0;
}