说明文档网址:http://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Relations-and-Tuples.html
关系:类似数据库,不过目前只限两个字段
元组:也类似数据库,不过只是关系返回的每条记录
结构体:
typedef struct { guint len; } GTuples;
功能函数:
#include <glib.h> GRelation; GRelation* g_relation_new (gint fields); void g_relation_index (GRelation *relation, gint field, GHashFunc hash_func, GEqualFunc key_equal_func); void g_relation_insert (GRelation *relation, ...); gboolean g_relation_exists (GRelation *relation, ...); gint g_relation_count (GRelation *relation, gconstpointer key, gint field); GTuples* g_relation_select (GRelation *relation, gconstpointer key, gint field); gint g_relation_delete (GRelation *relation, gconstpointer key, gint field); void g_relation_destroy (GRelation *relation); void g_relation_print (GRelation *relation); GTuples; void g_tuples_destroy (GTuples *tuples); gpointer g_tuples_index (GTuples *tuples, gint index_, gint field);
下面是一段例子代码:
#include <glib.h> struct map { int key; char *value; } m[10] = { {0,"zero"}, {1,"one"}, {2,"two"}, {3,"three"}, {4,"four"}, {5,"five"}, {6,"six"}, {7,"seven"}, {8,"eight"}, {9,"nine"} }; typedef struct map map; #define NUMS (sizeof(m)/sizeof(m[0])) static void test_relation(void) { // GRelation* g_relation_new(gint fields); 创建新的关系表,fields域的数量,现在这个值必须是2,也就是一个key域,一个value域 GRelation *relation = g_relation_new(2); // void g_relation_index(GRelation *relation, gint field, GHashFunc hash_func, GEqualFunc key_equal_func);创建一个域的索引 g_relation_index(relation, 0, g_int_hash, g_int_equal); g_relation_index(relation, 1, g_str_hash, g_str_equal); // void g_relation_insert(GRelatioin *relation, ...); 插入记录到关系表中,...的参数必须与域对应 gint i; for (i = 0; i < NUMS; i++) g_relation_insert(relation, &m[i].key, m[i].value); // gint g_relation_count(GRelation *relation, gconstpointer key, gint fields); 在域fields中key值对应的元组值 g_printf("The '%d' should be exist '%d' times now.\t\tResult: %d.\n", m[1].key, 1, g_relation_count(relation, &m[1].key, 0)); // gboolean g_relation_exists(GRelation *relation, ...); 被给的键值如果在关系表中存在则返回true gboolean b = g_relation_exists(relation, &m[1].key, m[1].value); g_printf("The key: '%d' and value: '%s' should be %sfound now.\n", m[1].key, m[1].value, b ? "" : "not "); // gint g_relation_delete(GRelation *relation, gconstpointer key, gint field); 在域field中删除所有的key,返回删除的个数 g_printf("The key: '%d' has been found '%d' times and deleted now.\n", m[1].key, g_relation_delete(relation, &m[1].key, 0)); // GTuples* g_relation_select(GRelation *relation, gconstpointer key, gint field); 返回所有的key对应的元组,配合g_tuples_index使用取出记录值 // gpointer g_tuples_index(GTuples *tuples, gint index_, gint field); 从g_relation_select的返回中取出field中的元组值 // void g_tuples_destroy(GTuples *tuples); 销毁元组 g_printf("The all records now:\n"); for (i = 0; i < NUMS; i++) { GTuples *tuples = g_relation_select(relation, m[i].value, 1); gint j; for (j = 0; j < tuples->len; j++) g_printf("Key: %d\t\tValue: %s\n", *(gint *)g_tuples_index(tuples, j, 0), (gchar *)g_tuples_index(tuples, j, 1)); g_tuples_destroy(tuples); } g_printf("\n"); // void g_relation_print(GRelation *relation); // g_relation_print(relation); // void g_relation_destroy(GRelation *relation); 销毁关系表 g_relation_destroy(relation); } int main(void) { g_printf("BEGIN:\n************************************************************\n"); test_relation(); g_printf("\n************************************************************\nDONE\n"); return 0; }
linux@ubuntu:~/16021/glibDemo$ gcc Relations_and_Tuples.c -o Relations_and_Tuples -lglib-2.0 linux@ubuntu:~/16021/glibDemo$ ./Relations_and_Tuples BEGIN: ************************************************************ The '1' should be exist '1' times now. Result: 1. The key: '1' and value: 'one' should be found now. The key: '1' has been found '1' times and deleted now. The all records now: Key: 0 Value: zero Key: 2 Value: two Key: 3 Value: three Key: 4 Value: four Key: 5 Value: five Key: 6 Value: six Key: 7 Value: seven Key: 8 Value: eight Key: 9 Value: nine ************************************************************ DONE linux@ubuntu:~/16021/glibDemo$
结语:
关于glib的基本数据类型就讲到这里,其实这里并不是所有的类型,还有几个没有讲到,但是那些没有特殊的要求是不会用到的。
glib其实还有很多的功能,它还带有核心应用支持,例如线程的实现也可以使用glib完成,那些就是后面有机会再去研究,目前为止可能对我最有用的就是strings和单链表。
几天的更新也算是对我的学习一个记录和分享,以后有什么心得体会也会记录在这里。
接下来要接触linux移植,希望能够将每天的学习分享在这里,可以和大家一起交流,争取做到中国的stackoverflow。