1:概述
GArray 可以存放任意类型的元素,并且大小随着元素的增加可以自动增长。
2:GArray结构
- typedef struct {
-
gchar *data;
-
guint len;
-
} GArray;
3:GArray原型
- GArray* g_array_new (gboolean zero_terminated,
-
gboolean clear_,
-
guint element_size);
-
GArray* g_array_sized_new (gboolean zero_terminated,
-
gboolean clear_,
-
guint element_size,
-
guint reserved_size);
-
#define g_array_append_val (a,v)
-
GArray* g_array_append_vals (GArray *array,
-
gconstpointer data,
-
guint len);
-
#define g_array_prepend_val (a,v)
-
GArray* g_array_prepend_vals (GArray *array,
-
gconstpointer data,
-
guint len);
-
#define g_array_insert_val (a,i,v)
-
GArray* g_array_insert_vals (GArray *array,
-
guint index_,
-
gconstpointer data,
-
guint len);
-
GArray* g_array_remove_index (GArray *array,
-
guint index_);
-
GArray* g_array_remove_index_fast (GArray *array,
-
guint index_);
-
GArray* g_array_remove_range (GArray *array,
-
guint index_,
-
guint length);
-
void g_array_sort (GArray *array,
-
GCompareFunc compare_func);
-
void g_array_sort_with_data (GArray *array,
-
GCompareDataFunc compare_func,
-
gpointer user_data);
-
#define g_array_index (a,t,i)
-
GArray* g_array_set_size (GArray *array,
-
guint length);
-
gchar* g_array_free (GArray *array,
-
gboolean free_segment);
4:GArray实例
- #include <stdio.h>
-
-
#include <glib.h>
-
#include <glib/gprintf.h>
-
-
struct map {
-
int key;
-
char *value;
-
} m[10] = {
-
{1,"one"},
-
{2,"two"},
-
{3,"three"},
-
{4,"four"},
-
{5,"five"},
-
{6,"six"},
-
{7,"seven"},
-
{8,"eight"},
-
{9,"nine"},
-
{10,"ten"}
-
};
-
typedef struct map map;
-
-
static gint
-
sort(gconstpointer p1, gconstpointer p2)
-
{
-
gint32 a, b;
-
-
a = *(gint*)(p1);
-
b = *(gint*)(p2);
-
-
return (a > b ? +1 : a == b ? 0 : -1);
-
}
-
-
static gint
-
sort_r(gconstpointer p1, gconstpointer p2, gpointer user_data)
-
{
-
gint32 a, b;
-
-
a = *(gint*)(p1);
-
b = *(gint*)(p2);
-
-
return (a < b ? +1 : a == b ? 0 : -1);
-
}
-
-
static void
-
print(GArray *array)
-
{
-
gint i;
-
for (i = 0; i < array->len; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
}
-
-
static void
-
test_array(void)
-
{
-
GArray *array = NULL;
-
gint i;
-
-
// GArray* g_array_new(gboolean zero_terminated, gboolean clear_, guint element_size);
-
array = g_array_new(FALSE, FALSE, sizeof(int));
-
// #define g_array_append_val(a,v)
-
for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
-
g_array_append_val(array, m[i].key);
-
-
g_printf("There should be '%d' items now.\t\tResult: %d.\n", 10, array->len);
-
g_printf("All of items:\n");
-
// #define g_array_index(a, t, i)
-
for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
// GArray* g_array_remove_index(GArray *array, guint index_);
-
array = g_array_remove_index(array, 1);
-
g_printf("All of items[exclude the second item]:\n");
-
for (i = 0; i < sizeof(m)/sizeof(m[0])-1; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
// GArray* g_array_remove_index_fast(GArray *array, guint index_);
-
array = g_array_remove_index_fast(array, 1);
-
g_printf("All of items[exclude the second item]:\n");
-
for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
// GArray* g_array_remove_range(GArray *array, guint index_, guint length);
-
array = g_array_remove_range(array, 2, 2);
-
g_printf("All of items[after remove 2 items from the third item]:\n");
-
for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
// void g_array_sort(GArray *array, GCompareFunc compare_func);
-
g_array_sort(array, sort);
-
g_printf("All of items[sorted]:\n");
-
for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
// void g_array_sort_with_data(GArray *array, GCompareDataFunc compare_func, gpointer user_data);
-
g_array_sort_with_data(array, sort_r, NULL);
-
g_printf("All of items[sorted reversed]:\n");
-
for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
int x[4] = {2,3,4,5};
-
// GArray* g_array_append_vals(GArray *array, gconstpointer data, guint len);
-
array = g_array_append_vals(array, x, 4);
-
g_printf("All of items[after append all 2,3,4,5]:\n");
-
for (i = 0; i < array->len; i++)
-
g_printf("%d,", g_array_index(array, int, i));
-
g_printf("\n");
-
-
g_printf("All of items[after prepend one by one 2,3,4,5]:\n");
-
// #define g_array_prepend_val(a,v)
-
for (i = 0; i < 4; i++)
-
g_array_prepend_val(array, x[i]);
-
print(array);
-
-
g_printf("All of items[after prepend all 2,3,4,5]:\n");
-
// GArray* g_array_prepend_vals(GArray *array, gconstpointer data, guint len);
-
array = g_array_prepend_vals(array, x, 4);
-
print(array);
-
-
int t = 0;
-
// #define g_array_insert_val(a, i, v)
-
g_array_insert_val(array, 0, t);
-
g_printf("All of items[after insert 0 at the first index]:\n");
-
print(array);
-
-
g_array_sort(array, sort);
-
g_printf("All of items[sorted]:\n");
-
print(array);
-
-
// gchar* g_array_free(GArray *array, gboolean free_segment);
-
g_array_free(array, TRUE);
-
}
-
-
int
-
main(void)
-
{
-
printf("BEGIN:\n************************************************************\n");
-
test_array();
-
printf("\n************************************************************\nDONE\n");
-
-
return 0;
-
}
5:结果
- BEGIN:
-
************************************************************
-
There should be '10' items now. Result: 10.
-
All of items:
-
1,2,3,4,5,6,7,8,9,10,
-
All of items[exclude the second item]:
-
1,3,4,5,6,7,8,9,10,
-
All of items[exclude the second item]:
-
1,10,4,5,6,7,8,9,
-
All of items[after remove 2 items from the third item]:
-
1,10,6,7,8,9,
-
All of items[sorted]:
-
1,6,7,8,9,10,
-
All of items[sorted reversed]:
-
10,9,8,7,6,1,
-
All of items[after append all 2,3,4,5]:
-
10,9,8,7,6,1,2,3,4,5,
-
All of items[after prepend one by one 2,3,4,5]:
-
5,4,3,2,10,9,8,7,6,1,2,3,4,5,
-
All of items[after prepend all 2,3,4,5]:
-
2,3,4,5,5,4,3,2,10,9,8,7,6,1,2,3,4,5,
-
All of items[after insert 0 at the first index]:
-
0,2,3,4,5,5,4,3,2,10,9,8,7,6,1,2,3,4,5,
-
All of items[sorted]:
-
0,1,2,2,2,3,3,3,4,4,4,5,5,5,6,7,8,9,10,
-
-
************************************************************
-
DONE
阅读(3150) | 评论(0) | 转发(0) |