前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >glib hash(1)

glib hash(1)

作者头像
随心助手
发布2021-11-12 15:04:19
7970
发布2021-11-12 15:04:19
举报
文章被收录于专栏:nginx遇上redisnginx遇上redis

hash表是一种提供key-value访问的数据结构,通过指定的key值可以快速的访问到与它相关联的value值。hash表的一种典型用法就是字典,通过单词的首字母能够快速的找到单词。关于hash表的详细介绍请查阅数据结构的相关书籍,我这里只介绍glib库中hash表的基本用法。

要使用一个hash表首先必须创建它,glib库里有两个函数可以用于创建hash表,分别是g_hash_table_new()和g_hash_table_new_full(),它们的原型如下:

代码语言:javascript
复制
GHashTable *g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func);
代码语言:javascript
复制
GHashTable* g_hash_table_new_full(GHashFunc hash_func,
                                    GEqualFunc key_equal_func,
                                    GDestroyNotify key_destroy_func,
                                    GDestroyNotify value_destroy_func);

其中hash_func是一个函数,它为key创建一个hash值;key_equal_func用于比较两个key是否相等;key_destroy_func当你从hash表里删除、销毁一个条目时,glib库会自动调用它释放key所占用的内存空间,这对于key是动态分配内存的hash表来说非常有用;value_destroy_func的作用与key_destroy_func相似,只是它释放的是value占用的内存空间。

代码语言:javascript
复制
1 /***************************************************************************
 2    file: g_hash.c
 3    desc: 这个文件用于演示glib库中hash表的用法
 4    compile: gcc -o g_hash g_hash.c `pkg-config --cflags --libs glib-2.0`
 5  ***************************************************************************/
 6 
 7 #include <glib.h>
 8 
 9 void print_key_value(gpointer key, gpointer value, gpointer user_data);
10 void display_hash_table(GHashTable *table);
11 void free_key(gpointer data);
12 void free_value(gpointer value);
13 
14 void print_key_value(gpointer key, gpointer value, gpointer user_data)
15 {
16     printf("%s ---> %s/n", key, value);
17 }
18 
19 void display_hash_table(GHashTable *table)
20 {
21     g_hash_table_foreach(table, print_key_value, NULL);
22 }
23 
24 void free_key(gpointer data)
25 {
26     printf("We free key: %s /n", data);
27     free(data);
28 }
29 
30 void free_value(gpointer data)
31 {
32     printf("We free value: %s /n", data);
33     free(data);
34 }
35 
36 int main(int argc, char *argv[])
37 {
38     GHashTable *table = NULL;
39 
40     table = g_hash_table_new(g_str_hash, g_str_equal);
41 
42     g_hash_table_insert(table, "1", "one");
43     g_hash_table_insert(table, "2", "two");
44     g_hash_table_insert(table, "3", "three");
45     g_hash_table_insert(table, "4", "four");
46     g_hash_table_insert(table, "5", "five");
47     display_hash_table(table);
48     printf("Size of hash table: %d /n", g_hash_table_size(table));
49 
50     printf("Before replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
51     g_hash_table_replace(table, "3", "third");
52     printf("After replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
53 
54     g_hash_table_remove(table, "2");
55     display_hash_table(table);
56     printf("Now size of hash table: %d /n", g_hash_table_size(table));
57 
58     g_hash_table_destroy(table);
59 
60     table = g_hash_table_new_full(g_str_hash, g_str_equal, free_key, free_value);
61     g_hash_table_insert(table, strdup("one"), strdup("first"));
62     g_hash_table_insert(table, strdup("two"), strdup("second"));
63     g_hash_table_insert(table, strdup("three"), strdup("third"));
64     
65     printf("Remove an item from hash table: /n");
66     g_hash_table_remove(table, "two");
67 
68     printf("Destroy hash table: /n");
69     g_hash_table_destroy(table);
70 
71     return 0;
72 }

通过代码我们看到glib库为hash表供了非常简单的接口。下面是代码的输出:

1 ---> one 2 ---> two 3 ---> three 4 ---> four 5 ---> five Size of hash table: 5 Before replace: 3 ---> three After replace: 3 ---> third 1 ---> one 3 ---> third 4 ---> four 5 ---> five Now size of hash table: 4 Remove an item from hash table: We free key: two We free value: second Destroy hash table: We free key: three We free value: third We free key: one We free value: first

对代码的说明: 1、首先我们调用了g_hash_table_new()来创建一个hash表,然后用g_hash_table_insert()向hash表里插入条目,插入的条目必须是一个key-value对,要查看hash表里有多少个条目可以用g_hash_table_size()。 2、用g_hash_table_lookup()通过key可以查找到与它相对应的value,g_hash_table_replace()可以替换掉一个key对应的value。 3、用g_hash_table_foreach()可以遍历hash表里的每一个条目,并对它们进行相应的操作。 4、用g_hash_table_remove()把一个条目从hash表里删除。 5、用g_hash_table_destroy()销毁一个hash表。 6、对于用g_hash_table_new_full()创建并提供了key_destroy_func和value_destroy_func的hash表,删除hash表中的条目或者销毁hash表的时候,库自动调用这两个函数释放内存,在销毁hash表的时候,销毁条目的顺序与插入条目的顺序并不总是相同的。代码中我们用strdup()来为hash表的key和value动态分配内。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-11-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 nginx遇上redis 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档