C开源hash代码uthash的用法总结(2)

33 篇文章 5 订阅
5.完整程序例子

5.1.key类型为int的完整的例子

#include <stdio.h>   /* gets */
#include <stdlib.h>  /* atoi, malloc */
#include <string.h>  /* strcpy */
#include "uthash.h"

struct my_struct {
    int ikey;                    /* key */
    char value[10];
    UT_hash_handle hh;         /* makes this structure hashable */
};

static struct my_struct *g_users = NULL;

void add_user(int mykey, char *value) {
    struct my_struct *s;

    HASH_FIND_INT(users, &mykey, s);  /* mykey already in the hash? */
    if (s==NULL) {
      s = (struct my_struct*)malloc(sizeof(struct my_struct));
      s->ikey = mykey;
      HASH_ADD_INT( users, ikey, s );  /* ikey: name of key field */
    }
    strcpy(s->value, value);
}

struct my_struct *find_user(int mykey) {
    struct my_struct *s;

    HASH_FIND_INT( users, &mykey, 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 ikey %d: value %s\n", s->ikey, s->value);
    }
}

int name_sort(struct my_struct *a, struct my_struct *b) {
    return strcmp(a->value,b->value);
}

int id_sort(struct my_struct *a, struct my_struct *b) {
    return (a->ikey - b->ikey);
}

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 ikey=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(ikey++, gets(in));
                break;
            case 2:
                printf("id?\n");
                gets(in); ikey = atoi(in);
                printf("name?\n");
                add_user(ikey, gets(in));
                break;
            case 3:
                printf("id?\n");
                s = find_user(atoi(gets(in)));
                printf("user: %s\n", s ? s->value : "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;
}

5.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 **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));
        strncpy(s->name, *n,10);
        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;
}

5.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;
}

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个使用 C 语言宏定义实现的哈希表(类似于 uthash)的示例代码: ```c #include <stdio.h> #include <stdlib.h> /* 定义哈希表结构体 */ #define HASH_TABLE(name, key_type, value_type) \ typedef struct name##_entry { \ key_type key; \ value_type value; \ struct name##_entry *next; \ } name##_entry; \ typedef struct { \ int size; \ name##_entry **table; \ } name /* 初始化哈希表 */ #define HASH_TABLE_INIT(table, size) \ do { \ (table)->size = size; \ (table)->table = calloc(size, sizeof(*(table)->table)); \ } while (0) /* 释放哈希表 */ #define HASH_TABLE_FREE(table) \ do { \ int i; \ for (i = 0; i < (table)->size; i++) { \ name##_entry *entry = (table)->table[i]; \ while (entry) { \ name##_entry *next = entry->next; \ free(entry); \ entry = next; \ } \ } \ free((table)->table); \ } while (0) /* 查找键对应的值 */ #define HASH_TABLE_GET(table, key, default) \ ({ \ int index = (key) % (table)->size; \ name##_entry *entry = (table)->table[index]; \ while (entry) { \ if ((entry)->key == (key)) { \ break; \ } \ entry = entry->next; \ } \ (entry) ? (entry)->value : (default); \ }) /* 设置键对应的值 */ #define HASH_TABLE_SET(table, key, value) \ do { \ int index = (key) % (table)->size; \ name##_entry *entry = (table)->table[index]; \ while (entry) { \ if ((entry)->key == (key)) { \ (entry)->value = (value); \ break; \ } \ entry = entry->next; \ } \ if (!entry) { \ entry = calloc(1, sizeof(*entry)); \ entry->key = (key); \ entry->value = (value); \ entry->next = (table)->table[index]; \ (table)->table[index] = entry; \ } \ } while (0) /* 删除键对应的值 */ #define HASH_TABLE_DEL(table, key) \ do { \ int index = (key) % (table)->size; \ name##_entry *entry = (table)->table[index]; \ name##_entry *prev = NULL; \ while (entry) { \ if ((entry)->key == (key)) { \ if (prev) { \ prev->next = entry->next; \ } else { \ (table)->table[index] = entry->next; \ } \ free(entry); \ break; \ } \ prev = entry; \ entry = entry->next; \ } \ } while (0) /* 示例用法 */ HASH_TABLE(my_hash_table, int, char *); int main() { my_hash_table table; HASH_TABLE_INIT(&table, 10); HASH_TABLE_SET(&table, 1, "one"); HASH_TABLE_SET(&table, 2, "two"); HASH_TABLE_SET(&table, 3, "three"); printf("%s\n", HASH_TABLE_GET(&table, 1, "not found")); printf("%s\n", HASH_TABLE_GET(&table, 2, "not found")); printf("%s\n", HASH_TABLE_GET(&table, 3, "not found")); HASH_TABLE_DEL(&table, 2); printf("%s\n", HASH_TABLE_GET(&table, 1, "not found")); printf("%s\n", HASH_TABLE_GET(&table, 2, "not found")); printf("%s\n", HASH_TABLE_GET(&table, 3, "not found")); HASH_TABLE_FREE(&table); return 0; } ``` 这个示例代码定义了一个名为 `my_hash_table` 的哈希表类型,其中键的类型为 `int`,值的类型为 `char *`。通过使用宏定义来实现,可以方便地在其他代码中使用这个哈希表类型,并调用宏定义中定义的函数来操作哈希表。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值