• UT_hash实现增删改查


    key为整型

    1.引入库并初始化

    代码如下(示例):

    #include "uthash.h"
    typedef struct {
        int key; //可以就只有key不加value
        int value;
        UT_hash_handle hh;
    } Hash;
    Hash *hashtable = NULL;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    }

    总结
    最后在把代码整合在一起方便调用

    #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;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    key为字符串string
    那么使用的方法依赖于你定义key的方法:指针的方式(char *)还是数组的方式(char a[10])。这个差异是非常重要的。当你的结构体使用的是指针方式,那么你应当使用HASH_ADD_KEYPTR方法;当你使用的数组方式,则需要使用HASH_ADD_STR方法。

    #include   /* strcpy */
    #include   /* malloc */
    #include    /* 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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    #include   /* strcpy */
    #include   /* malloc */
    #include    /* 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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    指针key
    你可以使用指针(地址)作为key,也就是说指针(地址)本身也可以作为key来使用。也对应HASH_ADD_KEYPTR相关方法。下面是一个实例:

    #include 
    #include 
    #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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    1572.矩阵对角线元素的和
    数字藏品平台的企业需要准备哪些资质证书?
    通过数据模板自动生成表格table
    IDEA中pom文件出现灰色且有删除的线解决方案
    《HelloGitHub》第 88 期
    Linux进程概念
    Sql注入详解(原理篇)
    SSM框架介绍
    哈夫曼树的题
    独立开发的基于springboot + websocket IM网站聊天系统总结
  • 原文地址:https://blog.csdn.net/lsljiayou/article/details/132893871