Glib实例学习(3)哈希表

1:哈希表原型

 

 
 
  1. GHashTable* g_hash_table_new (GHashFunc hash_func,  
  2.                                                          GEqualFunc key_equal_func);  
  3. GHashTable* g_hash_table_new_full (GHashFunc hash_func,  
  4.                                                          GEqualFunc key_equal_func,  
  5.                                                          GDestroyNotify key_destroy_func,  
  6.                                                          GDestroyNotify value_destroy_func);  
  7. guint (*GHashFunc) (gconstpointer key);  
  8. gboolean (*GEqualFunc) (gconstpointer a,  
  9.                                                          gconstpointer b);  
  10. void g_hash_table_insert (GHashTable *hash_table,  
  11.                                                          gpointer key,  
  12.                                                          gpointer value);  
  13. void g_hash_table_replace (GHashTable *hash_table,  
  14.                                                          gpointer key,  
  15.                                                          gpointer value);  
  16. guint g_hash_table_size (GHashTable *hash_table);  
  17. gpointer g_hash_table_lookup (GHashTable *hash_table,  
  18.                                                          gconstpointer key);  
  19. gboolean g_hash_table_lookup_extended (GHashTable *hash_table,  
  20.                                                          gconstpointer lookup_key,  
  21.                                                          gpointer *orig_key,  
  22.                                                          gpointer *value);  
  23. void g_hash_table_foreach (GHashTable *hash_table,  
  24.                                                          GHFunc func,  
  25.                                                          gpointer user_data);  
  26. gpointer g_hash_table_find (GHashTable *hash_table,  
  27.                                                          GHRFunc predicate,  
  28.                                                          gpointer user_data);  
  29. void (*GHFunc) (gpointer key,  
  30.                                                          gpointer value,  
  31.                                                          gpointer user_data);  
  32. gboolean g_hash_table_remove (GHashTable *hash_table,  
  33.                                                          gconstpointer key);  
  34. gboolean g_hash_table_steal (GHashTable *hash_table,  
  35.                                                          gconstpointer key);  
  36. guint g_hash_table_foreach_remove (GHashTable *hash_table,  
  37.                                                          GHRFunc func,  
  38.                                                          gpointer user_data);  
  39. guint g_hash_table_foreach_steal (GHashTable *hash_table,  
  40.                                                          GHRFunc func,  
  41.                                                          gpointer user_data);  
  42. void g_hash_table_remove_all (GHashTable *hash_table);  
  43. void g_hash_table_steal_all (GHashTable *hash_table);  
  44. GList* g_hash_table_get_keys (GHashTable *hash_table);  
  45. GList* g_hash_table_get_values (GHashTable *hash_table);  
  46. gboolean (*GHRFunc) (gpointer key,  
  47.                                                          gpointer value,  
  48.                                                          gpointer user_data);  
  49. #define g_hash_table_freeze (hash_table)  
  50. #define g_hash_table_thaw (hash_table)  
  51. void g_hash_table_destroy (GHashTable *hash_table);  
  52. GHashTable* g_hash_table_ref (GHashTable *hash_table);  
  53. void g_hash_table_unref (GHashTable *hash_table);  
  54.                     GHashTableIter;  
  55. void g_hash_table_iter_init (GHashTableIter *iter,  
  56.                                                          GHashTable *hash_table);  
  57. gboolean g_hash_table_iter_next (GHashTableIter *iter,  
  58.                                                          gpointer *key,  
  59.                                                          gpointer *value);  
  60. GHashTable* g_hash_table_iter_get_hash_table (GHashTableIter *iter);  
  61. void g_hash_table_iter_remove (GHashTableIter *iter);  
  62. void g_hash_table_iter_steal (GHashTableIter *iter);  
  63.  
  64. gboolean g_direct_equal (gconstpointer v1,  
  65.                                                          gconstpointer v2);  
  66. guint g_direct_hash (gconstpointer v);  
  67. gboolean g_int_equal (gconstpointer v1,  
  68.                                                          gconstpointer v2);  
  69. guint g_int_hash (gconstpointer v);  
  70. gboolean g_str_equal (gconstpointer v1,  
  71.                                                          gconstpointer v2);  
  72. guint g_str_hash (gconstpointer v); 
2:哈希表实例
 
 
  1. #include <stdio.h>  
  2. #include <glib.h>  
  3. #include <glib/gprintf.h>  
  4.  
  5. struct map {  
  6.     int key;  
  7.     char *value;  
  8. } m[10] = {  
  9.     {1,"one"},  
  10.     {2,"two"},  
  11.     {3,"three"},  
  12.     {4,"four"},  
  13.     {5,"five"},  
  14.     {6,"six"},  
  15.     {7,"seven"},  
  16.     {8,"eight"},  
  17.     {9,"nine"},  
  18.     {10,"ten"}  
  19. };  
  20.  
  21. typedef struct map map;  
  22.  
  23. static gboolean  
  24. myHRFunc(gpointer key, gpointer value, gpointer user_data)  
  25. {  
  26.     gint a = *(gint *)key;  
  27.     gint b = *(gint *)user_data;  
  28.  
  29.     return a == b ? TRUE : FALSE;  
  30. }  
  31.  
  32. static void 
  33. myIterator(gpointer key, gpointer value, gpointer user_data)  
  34. {  
  35.     printf(user_data, *(gint*)key, value);  
  36. }  
  37.  
  38. /*  
  39. static void  
  40. printKey(gpointer p1, gpointer p2)  
  41. {  
  42.     printf(p2, *(gint*)p1);  
  43. }  
  44.  
  45. static void  
  46. printValue(gpointer p1, gpointer p2)  
  47. {  
  48.     printf(p2, p1);  
  49. }  
  50. */ 
  51.  
  52. static void 
  53. test_hash_1(void)  
  54. {  
  55. // GHashTable* g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func);  
  56.     GHashTable *hash = g_hash_table_new(g_int_hash, g_int_equal);  
  57.     gint i;  
  58.  
  59. // void g_hash_table_insert(GHashTable *hash_table, gpointer key, gpointer value);  
  60.     for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)  
  61.         g_hash_table_insert(hash, &m[i].key, m[i].value);  
  62.  
  63. // guint g_hash_table_size(GHashTable *hash_table);  
  64.     g_printf("It should has '%d' keys in the hash now.\t\tResult: %d.\n", 10, g_hash_table_size(hash));  
  65. // gpointer g_hash_table_lookup(GHashTable *hash_table, gconstpointer key);  
  66.     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));  
  67. // gboolean g_hash_table_remove(GHashTable *hash_table, gconstpointer key);  
  68.     gboolean found = g_hash_table_remove(hash, &m[8].key);  
  69.     g_printf("The key '%d' was %sfound and removed now.\n", m[8].key, found ? "" : "not ");  
  70.     found = g_hash_table_remove(hash, &m[8].key);  
  71.     g_printf("The key '%d' was %sfound and removed now.\n", m[8].key, found ? "" : "not ");  
  72.     g_hash_table_insert(hash, &m[8].key, m[8].value);  
  73. // gpointer g_hash_table_find(GHashTable *hash_table, GHRFunc predicate, gpointer user_data);  
  74.     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");  
  75.  
  76. // void g_hash_table_replace(GHashTable *hash_table, gpointer key, gpointer value);  
  77.     g_hash_table_replace(hash, &m[2].key, "2222");  
  78.     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));  
  79.     g_printf("The all items in hash table is :\n");  
  80. // void g_hash_table_foreach(GHashTable *hash_table, GHFunc func, gpointer user_data);  
  81.     g_hash_table_foreach(hash, myIterator, "Key:\t%d\t\tValue:\t%s\n");  
  82.  
  83. /*  
  84. // GList* g_hash_table_get_keys(GHashTable *hash_table);  
  85.     GList *lkey = g_hash_table_get_keys(hash);  
  86.     g_list_foreach(lkey, printKey, "%d\t");  
  87.     g_printf("\n");  
  88.  
  89. // GList* g_hash_table_get_values(GHashTable *hash_table);  
  90.     GList *lvalue = g_hash_table_get_values(hash);  
  91.     g_list_foreach(lvalue, printValue, "%s\t");  
  92.     g_printf("\n");  
  93.  
  94. // void g_hash_table_remove_all(GHashTable *hash_table);  
  95.     g_hash_table_remove_all(hash);  
  96.     g_printf("Now all items in hash table is :\n");  
  97.     g_hash_table_foreach(hash, myIterator, "Key:\t%d\t\tValue:\t%s\n");  
  98. */ 
  99.  
  100. // void g_hash_table_destroy(GHashTable *hash_table);  
  101.     g_hash_table_destroy(hash);  
  102. }  
  103.  
  104. int 
  105. main(void)  
  106. {  
  107.     printf("BEGIN:\n************************************************************\n");  
  108.     test_hash_1();  
  109.     printf("\n************************************************************\nDONE\n");  
  110.       
  111.     return 0;  
3:结果
 
 
  1. BEGIN:  
  2. ************************************************************  
  3. It should has '10' keys in the hash now. Result: 10.  
  4. The value of the second key should be 'two' now. Result: two.  
  5. The key '9' was found and removed now.  
  6. The key '9' was not found and removed now.  
  7. The key '9' should be there now. Result: YES.  
  8. The value of the third key should be '2222' now. Result: 2222.  
  9. The all items in hash table is :  
  10. Key: 1 Value: one  
  11. Key: 2 Value: two  
  12. Key: 3 Value: 2222  
  13. Key: 4 Value: four  
  14. Key: 5 Value: five  
  15. Key: 6 Value: six  
  16. Key: 7 Value: seven  
  17. Key: 8 Value: eight  
  18. Key: 9 Value: nine  
  19. Key: 10 Value: ten  
  20.  
  21. ************************************************************  
  22. DONE 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值