关系:类似数据库,不过目前只限两个字段
元组:也类似数据库,不过只是关系返回的每条记录
2:结构
typedef struct {
guint len;
} GTuples;
3:原型
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);
4:实例
#include <stdio.h>
#include <glib.h>
#include <glib/gprintf.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);
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);
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, ...);
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 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);
// gpointer g_tuples_index(GTuples *tuples, gint index_, gint 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)
{
printf("BEGIN:\n************************************************************\n");
test_relation();
printf("\n************************************************************\nDONE\n");
return 0;
}
5:结果
[xied1@soho use]$ gcc `pkg-config --cflags --libs glib-2.0` -Wall -o relation relation.c
[xied1@soho use]$ ./relation
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
- 创建: g_relation_new()
- 插入: g_relation_insert()
- 统计: g_relation_count()
- 选择: g_relation_select()
- 删除: g_relation_delete()
- 销毁: g_relation_destroy()