http://blog.chinaunix.net/uid-25696269-id-752935.html
1:哈希表原型
GHashTable* g_hash_table_new (GHashFunc hash_func,
GEqualFunc key_equal_func);
GHashTable* g_hash_table_new_full (GHashFunc hash_func,
GEqualFunc key_equal_func,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func);
guint (*GHashFunc) (gconstpointer key);
gboolean (*GEqualFunc) (gconstpointer a,
gconstpointer b);
void g_hash_table_insert (GHashTable *hash_table,
gpointer key,
gpointer value);
void g_hash_table_replace (GHashTable *hash_table,
gpointer key,
gpointer value);
guint g_hash_table_size (GHashTable *hash_table);
gpointer g_hash_table_lookup (GHashTable *hash_table,
gconstpointer key);
gboolean g_hash_table_lookup_extended (GHashTable *hash_table,
gconstpointer lookup_key,
gpointer *orig_key,
gpointer *value);
void g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data);
gpointer g_hash_table_find (GHashTable *hash_table,
GHRFunc predicate,
gpointer user_data);
void (*GHFunc) (gpointer key,
gpointer value,
gpointer user_data);
gboolean g_hash_table_remove (GHashTable *hash_table,
gconstpointer key);
gboolean g_hash_table_steal (GHashTable *hash_table,
gconstpointer key);
guint g_hash_table_foreach_remove (GHashTable *hash_table,
GHRFunc func,
gpointer user_data);
guint g_hash_table_foreach_steal (GHashTable *hash_table,
GHRFunc func,
gpointer user_data);
void g_hash_table_remove_all (GHashTable *hash_table);
void g_hash_table_steal_all (GHashTable *hash_table);
GList* g_hash_table_get_keys (GHashTable *hash_table);
GList* g_hash_table_get_values (GHashTable *hash_table);
gboolean (*GHRFunc) (gpointer key,
gpointer value,
gpointer user_data);
#define g_hash_table_freeze (hash_table)
#define g_hash_table_thaw (hash_table)
void g_hash_table_destroy (GHashTable *hash_table);
GHashTable* g_hash_table_ref (GHashTable *hash_table);
void g_hash_table_unref (GHashTable *hash_table);
GHashTableIter;
void g_hash_table_iter_init (GHashTableIter *iter,
GHashTable *hash_table);
gboolean g_hash_table_iter_next (GHashTableIter *iter,
gpointer *key,
gpointer *value);
GHashTable* g_hash_table_iter_get_hash_table (GHashTableIter *iter);
void g_hash_table_iter_remove (GHashTableIter *iter);
void g_hash_table_iter_steal (GHashTableIter *iter);
gboolean g_direct_equal (gconstpointer v1,
gconstpointer v2);
guint g_direct_hash (gconstpointer v);
gboolean g_int_equal (gconstpointer v1,
gconstpointer v2);
guint g_int_hash (gconstpointer v);
gboolean g_str_equal (gconstpointer v1,
gconstpointer v2);
guint g_str_hash (gconstpointer v);
2:哈希表实例
#include <stdio.h>
#include <glib.h>
#include <glib/gprintf.h>
struct map {
int key;
char *value;
} m[10] = {
{1,"one"},
{2,"two"},
{3,"three"},
{4,"four"},
{5,"five"},
{6,"six"},
{7,"seven"},
{8,"eight"},
{9,"nine"},
{10,"ten"}
};
typedef struct map map;
static gboolean
myHRFunc(gpointer key, gpointer value, gpointer user_data)
{
gint a = *(gint *)key;
gint b = *(gint *)user_data;
return a == b ? TRUE : FALSE;
}
static void
myIterator(gpointer key, gpointer value, gpointer user_data)
{
printf(user_data, *(gint*)key, value);
}
/*
static void
printKey(gpointer p1, gpointer p2)
{
printf(p2, *(gint*)p1);
}
static void
printValue(gpointer p1, gpointer p2)
{
printf(p2, p1);
}
*/
static void
test_hash_1(void)
{
// GHashTable* g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func);
GHashTable *hash = g_hash_table_new(g_int_hash, g_int_equal);
gint i;
// void g_hash_table_insert(GHashTable *hash_table, gpointer key, gpointer value);
for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
g_hash_table_insert(hash, &m[i].key, m[i].value);
// guint g_hash_table_size(GHashTable *hash_table);
g_printf("It should has '%d' keys in the hash now.\t\tResult: %d.\n", 10, g_hash_table_size(hash));
// gpointer g_hash_table_lookup(GHashTable *hash_table, gconstpointer key);
g_printf("The value of the second key should be '%s' now.\t\tResult: %s.\n", m[1].value, (gchar *)g_hash_table_lookup(hash, &m[1].key));
// gboolean g_hash_table_remove(GHashTable *hash_table, gconstpointer key);
gboolean found = g_hash_table_remove(hash, &m[8].key);
g_printf("The key '%d' was %sfound and removed now.\n", m[8].key, found ? "" : "not ");
found = g_hash_table_remove(hash, &m[8].key);
g_printf("The key '%d' was %sfound and removed now.\n", m[8].key, found ? "" : "not ");
g_hash_table_insert(hash, &m[8].key, m[8].value);
// gpointer g_hash_table_find(GHashTable *hash_table, GHRFunc predicate, gpointer user_data);
g_printf("The key '%d' should be there now.\t\tResult: %s.\n", m[8].key, g_hash_table_find(hash, myHRFunc, &m[8].key) == NULL ? "NO" : "YES");
// void g_hash_table_replace(GHashTable *hash_table, gpointer key, gpointer value);
g_hash_table_replace(hash, &m[2].key, "2222");
g_printf("The value of the third key should be '%s' now.\t\tResult: %s.\n", "2222", (gchar *)g_hash_table_lookup(hash, &m[2].key));
g_printf("The all items in hash table is :\n");
// void g_hash_table_foreach(GHashTable *hash_table, GHFunc func, gpointer user_data);
g_hash_table_foreach(hash, myIterator, "Key:\t%d\t\tValue:\t%s\n");
/*
// GList* g_hash_table_get_keys(GHashTable *hash_table);
GList *lkey = g_hash_table_get_keys(hash);
g_list_foreach(lkey, printKey, "%d\t");
g_printf("\n");
// GList* g_hash_table_get_values(GHashTable *hash_table);
GList *lvalue = g_hash_table_get_values(hash);
g_list_foreach(lvalue, printValue, "%s\t");
g_printf("\n");
// void g_hash_table_remove_all(GHashTable *hash_table);
g_hash_table_remove_all(hash);
g_printf("Now all items in hash table is :\n");
g_hash_table_foreach(hash, myIterator, "Key:\t%d\t\tValue:\t%s\n");
*/
// void g_hash_table_destroy(GHashTable *hash_table);
g_hash_table_destroy(hash);
}
int
main(void)
{
printf("BEGIN:\n************************************************************\n");
test_hash_1();
printf("\n************************************************************\nDONE\n");
return 0;
}
3:结果
BEGIN:
************************************************************
It should has '10' keys in the hash now. Result: 10.
The value of the second key should be 'two' now. Result: two.
The key '9' was found and removed now.
The key '9' was not found and removed now.
The key '9' should be there now. Result: YES.
The value of the third key should be '2222' now. Result: 2222.
The all items in hash table is :
Key: 1 Value: one
Key: 2 Value: two
Key: 3 Value: 2222
Key: 4 Value: four
Key: 5 Value: five
Key: 6 Value: six
Key: 7 Value: seven
Key: 8 Value: eight
Key: 9 Value: nine
Key: 10 Value: ten
************************************************************
DONE