說明文檔網址:http://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Relations-and-Tuples.html
關系:類似資料庫,不過目前隻限兩個字段
元組:也類似資料庫,不過隻是關系傳回的每條記錄
結構體:
GTuples
typedef struct {
guint len;
} GTuples;
功能函數:
Synopsis
#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;
}
運作結果:
[email protected]:~/16021/glibDemo$ gcc Relations_and_Tuples.c -o Relations_and_Tuples -lglib-2.0
[email protected]:~/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
[email protected]:~/16021/glibDemo$
結語:
關于glib的基本資料類型就講到這裡,其實這裡并不是所有的類型,還有幾個沒有講到,但是那些沒有特殊的要求是不會用到的。
glib其實還有很多的功能,它還帶有核心應用支援,例如線程的實作也可以使用glib完成,那些就是後面有機會再去研究,目前為止可能對我最有用的就是strings和單連結清單。
幾天的更新也算是對我的學習一個記錄和分享,以後有什麼心得體會也會記錄在這裡。
接下來要接觸linux移植,希望能夠将每天的學習分享在這裡,可以和大家一起交流,争取做到中國的stackoverflow。