Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4173858
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类:

2011-07-13 19:42:02

1:概述
   关系:类似数据库,不过目前只限两个字段
   元组:也类似数据库,不过只是关系返回的每条记录

2:结构

  1. typedef struct {
  2.   guint len;
  3. } GTuples;

3:原型

  1. GRelation* g_relation_new (gint fields);
  2. void g_relation_index (GRelation *relation,
  3.                                                          gint field,
  4.                                                          GHashFunc hash_func,
  5.                                                          GEqualFunc key_equal_func);
  6. void g_relation_insert (GRelation *relation,
  7.                                                          ...);
  8. gboolean g_relation_exists (GRelation *relation,
  9.                                                          ...);
  10. gint g_relation_count (GRelation *relation,
  11.                                                          gconstpointer key,
  12.                                                          gint field);
  13. GTuples* g_relation_select (GRelation *relation,
  14.                                                          gconstpointer key,
  15.                                                          gint field);
  16. gint g_relation_delete (GRelation *relation,
  17.                                                          gconstpointer key,
  18.                                                          gint field);
  19. void g_relation_destroy (GRelation *relation);

  20. void g_relation_print (GRelation *relation);

  21.                     GTuples;
  22. void g_tuples_destroy (GTuples *tuples);
  23. gpointer g_tuples_index (GTuples *tuples,
  24.                                                          gint index_,
  25.                                                          gint field);

4:实例

  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.     {0,"zero"},
  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. };

  19. typedef struct map map;

  20. #define NUMS    (sizeof(m)/sizeof(m[0]))

  21. static void
  22. test_relation(void)
  23. {
  24. // GRelation* g_relation_new(gint fields);
  25.     GRelation *relation = g_relation_new(2);

  26. // void g_relation_index(GRelation *relation, gint field, GHashFunc hash_func, GEqualFunc key_equal_func);
  27.     g_relation_index(relation, 0, g_int_hash, g_int_equal);
  28.     g_relation_index(relation, 1, g_str_hash, g_str_equal);

  29. // void g_relation_insert(GRelatioin *relation, ...);
  30.     gint i;
  31.     for (i = 0; i < NUMS; i++)
  32.         g_relation_insert(relation, &m[i].key, m[i].value);

  33. // gint g_relation_count(GRelation *relation, gconstpointer key, gint fields);
  34.     g_printf("The '%d' should be exist '%d' times now.\t\tResult: %d.\n",
  35.             m[1].key, 1, g_relation_count(relation, &m[1].key, 0));

  36. // gboolean g_relation_exists(GRelation *relation, ...);
  37.     gboolean b = g_relation_exists(relation, &m[1].key, m[1].value);
  38.     g_printf("The key: '%d' and value: '%s' should be %sfound now.\n",
  39.             m[1].key, m[1].value, b ? "" : "not ");

  40. // gint g_relation_delete(GRelation *relation, gconstpointer key, gint key);
  41.     g_printf("The key: '%d' has been found '%d' times and deleted now.\n",
  42.             m[1].key, g_relation_delete(relation, &m[1].key, 0));
  43.     
  44. // GTuples* g_relation_select(GRelation *relation, gconstpointer key, gint field);
  45. // gpointer g_tuples_index(GTuples *tuples, gint index_, gint field);
  46. // void g_tuples_destroy(GTuples *tuples);
  47.     g_printf("The all records now:\n");
  48.     for (i = 0; i < NUMS; i++) {
  49.         GTuples *tuples = g_relation_select(relation, m[i].value, 1);
  50.         gint j;
  51.         for (j = 0; j < tuples->len; j++)
  52.             g_printf("Key: %d\t\tValue: %s\n",
  53.                     *(gint *)g_tuples_index(tuples, j, 0),
  54.                     (gchar *)g_tuples_index(tuples, j, 1));
  55.         g_tuples_destroy(tuples);
  56.     }
  57.     g_printf("\n");
  58. // void g_relation_print(GRelation *relation);
  59. //    g_relation_print(relation);

  60. // void g_relation_destroy(GRelation *relation);
  61.     g_relation_destroy(relation);
  62. }

  63. int
  64. main(void)
  65. {
  66.     printf("BEGIN:\n************************************************************\n");
  67.     test_relation();
  68.     printf("\n************************************************************\nDONE\n");
  69.     
  70.     return 0;
  71. }

5:结果
  1. [xied1@soho use]$ gcc `pkg-config --cflags --libs glib-2.0` -Wall -o relation relation.c
  2. [xied1@soho use]$ ./relation
  3. BEGIN:
  4. ************************************************************
  5. The '1' should be exist '1' times now. Result: 1.
  6. The key: '1' and value: 'one' should be found now.
  7. The key: '1' has been found '1' times and deleted now.
  8. The all records now:
  9. Key: 0 Value: zero
  10. Key: 2 Value: two
  11. Key: 3 Value: three
  12. Key: 4 Value: four
  13. Key: 5 Value: five
  14. Key: 6 Value: six
  15. Key: 7 Value: seven
  16. Key: 8 Value: eight
  17. Key: 9 Value: nine


  18. ************************************************************
  19. DONE

6:小结
  • 创建: g_relation_new()
  • 插入: g_relation_insert()
  • 统计: g_relation_count()
  • 选择:  g_relation_select()
  • 删除: g_relation_delete()
  • 销毁: g_relation_destroy()

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