Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120324
  • 博文数量: 19
  • 博客积分: 329
  • 博客等级: 一等列兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-10 09:58
文章分类

全部博文(19)

文章存档

2012年(3)

2011年(14)

2009年(2)

分类:

2011-07-01 20:52:57

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. gboolean g_direct_equal (gconstpointer v1,
  64.                                                          gconstpointer v2);
  65. guint g_direct_hash (gconstpointer v);
  66. gboolean g_int_equal (gconstpointer v1,
  67.                                                          gconstpointer v2);
  68. guint g_int_hash (gconstpointer v);
  69. gboolean g_str_equal (gconstpointer v1,
  70.                                                          gconstpointer v2);
  71. guint g_str_hash (gconstpointer v);
2:哈希表实例
  1. #include <stdio.h>
  2. #include <glib.h>
  3. #include <glib/gprintf.h>

  4. struct map {
  5.     int key;
  6.     char *value;
  7. } m[10] = {
  8.     {1,"one"},
  9.     {2,"two"},
  10.     {3,"three"},
  11.     {4,"four"},
  12.     {5,"five"},
  13.     {6,"six"},
  14.     {7,"seven"},
  15.     {8,"eight"},
  16.     {9,"nine"},
  17.     {10,"ten"}
  18. };

  19. typedef struct map map;

  20. static gboolean
  21. myHRFunc(gpointer key, gpointer value, gpointer user_data)
  22. {
  23.     gint a = *(gint *)key;
  24.     gint b = *(gint *)user_data;

  25.     return a == b ? TRUE : FALSE;
  26. }

  27. static void
  28. myIterator(gpointer key, gpointer value, gpointer user_data)
  29. {
  30.     printf(user_data, *(gint*)key, value);
  31. }

  32. /*
  33. static void
  34. printKey(gpointer p1, gpointer p2)
  35. {
  36.     printf(p2, *(gint*)p1);
  37. }

  38. static void
  39. printValue(gpointer p1, gpointer p2)
  40. {
  41.     printf(p2, p1);
  42. }
  43. */

  44. static void
  45. test_hash_1(void)
  46. {
  47. // GHashTable* g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func);
  48.     GHashTable *hash = g_hash_table_new(g_int_hash, g_int_equal);
  49.     gint i;

  50. // void g_hash_table_insert(GHashTable *hash_table, gpointer key, gpointer value);
  51.     for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
  52.         g_hash_table_insert(hash, &m[i].key, m[i].value);

  53. // guint g_hash_table_size(GHashTable *hash_table);
  54.     g_printf("It should has '%d' keys in the hash now.\t\tResult: %d.\n", 10, g_hash_table_size(hash));
  55. // gpointer g_hash_table_lookup(GHashTable *hash_table, gconstpointer key);
  56.     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));
  57. // gboolean g_hash_table_remove(GHashTable *hash_table, gconstpointer key);
  58.     gboolean found = g_hash_table_remove(hash, &m[8].key);
  59.     g_printf("The key '%d' was %sfound and removed now.\n", m[8].key, found ? "" : "not ");
  60.     found = g_hash_table_remove(hash, &m[8].key);
  61.     g_printf("The key '%d' was %sfound and removed now.\n", m[8].key, found ? "" : "not ");
  62.     g_hash_table_insert(hash, &m[8].key, m[8].value);
  63. // gpointer g_hash_table_find(GHashTable *hash_table, GHRFunc predicate, gpointer user_data);
  64.     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");

  65. // void g_hash_table_replace(GHashTable *hash_table, gpointer key, gpointer value);
  66.     g_hash_table_replace(hash, &m[2].key, "2222");
  67.     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));
  68.     g_printf("The all items in hash table is :\n");
  69. // void g_hash_table_foreach(GHashTable *hash_table, GHFunc func, gpointer user_data);
  70.     g_hash_table_foreach(hash, myIterator, "Key:\t%d\t\tValue:\t%s\n");

  71. /*
  72. // GList* g_hash_table_get_keys(GHashTable *hash_table);
  73.     GList *lkey = g_hash_table_get_keys(hash);
  74.     g_list_foreach(lkey, printKey, "%d\t");
  75.     g_printf("\n");

  76. // GList* g_hash_table_get_values(GHashTable *hash_table);
  77.     GList *lvalue = g_hash_table_get_values(hash);
  78.     g_list_foreach(lvalue, printValue, "%s\t");
  79.     g_printf("\n");

  80. // void g_hash_table_remove_all(GHashTable *hash_table);
  81.     g_hash_table_remove_all(hash);
  82.     g_printf("Now all items in hash table is :\n");
  83.     g_hash_table_foreach(hash, myIterator, "Key:\t%d\t\tValue:\t%s\n");
  84. */

  85. // void g_hash_table_destroy(GHashTable *hash_table);
  86.     g_hash_table_destroy(hash);
  87. }

  88. int
  89. main(void)
  90. {
  91.     printf("BEGIN:\n************************************************************\n");
  92.     test_hash_1();
  93.     printf("\n************************************************************\nDONE\n");
  94.     
  95.     return 0;
  96. }
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. DONE

阅读(1376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~