要使用一個hash表首先必須創建它,glib庫裏有兩個函數可以用於創建hash表,分別是g_hash_table_new()和g_hash_table_new_full(),它們的原型如下:
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佔用的內存空間。
下面以一段代碼來演示glib庫中hash表的基本用法
file: g_hash.c
desc: 這個文件用於演示glib庫中hash表的用法
compile: gcc -o g_hash g_hash.c `pkg-config --cflags --libs glib-2.0`
***************************************************************************/
#include <glib.h>
void print_key_value(gpointer key, gpointer value, gpointer user_data);
void display_hash_table(GHashTable *table);
void free_key(gpointer data);
void free_value(gpointer value);
void print_key_value(gpointer key, gpointer value, gpointer user_data)
{
printf("%s ---> %s/n", key, value);
}
void display_hash_table(GHashTable *table)
{
g_hash_table_foreach(table, print_key_value, NULL);
}
void free_key(gpointer data)
{
printf("We free key: %s /n", data);
free(data);
}
void free_value(gpointer data)
{
printf("We free value: %s /n", data);
free(data);
}
int main(int argc, char *argv[])
{
GHashTable *table = NULL;
table = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(table, "1", "one");
g_hash_table_insert(table, "2", "two");
g_hash_table_insert(table, "3", "three");
g_hash_table_insert(table, "4", "four");
g_hash_table_insert(table, "5", "five");
display_hash_table(table);
printf("Size of hash table: %d /n", g_hash_table_size(table));
printf("Before replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
g_hash_table_replace(table, "3", "third");
printf("After replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
g_hash_table_remove(table, "2");
display_hash_table(table);
printf("Now size of hash table: %d /n", g_hash_table_size(table));
g_hash_table_destroy(table);
table = g_hash_table_new_full(g_str_hash, g_str_equal, free_key, free_value);
g_hash_table_insert(table, strdup("one"), strdup("first"));
g_hash_table_insert(table, strdup("two"), strdup("second"));
g_hash_table_insert(table, strdup("three"), strdup("third"));
printf("Remove an item from hash table: /n");
g_hash_table_remove(table, "two");
printf("Destroy hash table: /n");
g_hash_table_destroy(table);
return 0;
}
通過代碼我們看到glib庫爲hash表接供了非常簡單的接口。下面是代碼的輸出
[plusboy@localhost c]$ ./g_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動態分配內存。
對glib庫更多參考:
http://developer.gnome.org/doc/API/2.0/glib/index.html