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

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: C/C++

2011-07-13 19:41:36

1:概述
GArray 可以存放任意类型的元素,并且大小随着元素的增加可以自动增长。

2:GArray结构

  1. typedef struct {
  2.   gchar *data;
  3.   guint len;
  4. } GArray;

3:GArray原型
  1. GArray* g_array_new (gboolean zero_terminated,
  2.                                                          gboolean clear_,
  3.                                                          guint element_size);
  4. GArray* g_array_sized_new (gboolean zero_terminated,
  5.                                                          gboolean clear_,
  6.                                                          guint element_size,
  7.                                                          guint reserved_size);
  8. #define g_array_append_val (a,v)
  9. GArray* g_array_append_vals (GArray *array,
  10.                                                          gconstpointer data,
  11.                                                          guint len);
  12. #define g_array_prepend_val (a,v)
  13. GArray* g_array_prepend_vals (GArray *array,
  14.                                                          gconstpointer data,
  15.                                                          guint len);
  16. #define g_array_insert_val (a,i,v)
  17. GArray* g_array_insert_vals (GArray *array,
  18.                                                          guint index_,
  19.                                                          gconstpointer data,
  20.                                                          guint len);
  21. GArray* g_array_remove_index (GArray *array,
  22.                                                          guint index_);
  23. GArray* g_array_remove_index_fast (GArray *array,
  24.                                                          guint index_);
  25. GArray* g_array_remove_range (GArray *array,
  26.                                                          guint index_,
  27.                                                          guint length);
  28. void g_array_sort (GArray *array,
  29.                                                          GCompareFunc compare_func);
  30. void g_array_sort_with_data (GArray *array,
  31.                                                          GCompareDataFunc compare_func,
  32.                                                          gpointer user_data);
  33. #define g_array_index (a,t,i)
  34. GArray* g_array_set_size (GArray *array,
  35.                                                          guint length);
  36. gchar* g_array_free (GArray *array,
  37.                                                          gboolean free_segment);

4:GArray实例
  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.     {1,"one"},
  9.     {2,"two"},
  10.     {3,"three"},
  11.     {4,"four"},
  12.     {5,"five"},
  13.     {6,"six"},
  14.     {7,"seven"},
  15.     {8,"eight"},
  16.     {9,"nine"},
  17.     {10,"ten"}
  18. };
  19. typedef struct map map;

  20. static gint
  21. sort(gconstpointer p1, gconstpointer p2)
  22. {
  23.     gint32 a, b;
  24.     
  25.     a = *(gint*)(p1);
  26.     b = *(gint*)(p2);

  27.     return (a > b ? +1 : a == b ? 0 : -1);
  28. }

  29. static gint
  30. sort_r(gconstpointer p1, gconstpointer p2, gpointer user_data)
  31. {
  32.     gint32 a, b;
  33.     
  34.     a = *(gint*)(p1);
  35.     b = *(gint*)(p2);

  36.     return (a < b ? +1 : a == b ? 0 : -1);
  37. }

  38. static void
  39. print(GArray *array)
  40. {
  41.     gint i;
  42.     for (i = 0; i < array->len; i++)
  43.         g_printf("%d,", g_array_index(array, int, i));
  44.     g_printf("\n");
  45. }

  46. static void
  47. test_array(void)
  48. {
  49.     GArray *array = NULL;
  50.     gint i;

  51. // GArray* g_array_new(gboolean zero_terminated, gboolean clear_, guint element_size);
  52.     array = g_array_new(FALSE, FALSE, sizeof(int));
  53. // #define g_array_append_val(a,v)
  54.     for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
  55.         g_array_append_val(array, m[i].key);

  56.     g_printf("There should be '%d' items now.\t\tResult: %d.\n", 10, array->len);
  57.     g_printf("All of items:\n");
  58. // #define g_array_index(a, t, i)
  59.     for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
  60.         g_printf("%d,", g_array_index(array, int, i));
  61.     g_printf("\n");

  62. // GArray* g_array_remove_index(GArray *array, guint index_);
  63.     array = g_array_remove_index(array, 1);
  64.     g_printf("All of items[exclude the second item]:\n");
  65.     for (i = 0; i < sizeof(m)/sizeof(m[0])-1; i++)
  66.         g_printf("%d,", g_array_index(array, int, i));
  67.     g_printf("\n");

  68. // GArray* g_array_remove_index_fast(GArray *array, guint index_);
  69.     array = g_array_remove_index_fast(array, 1);
  70.     g_printf("All of items[exclude the second item]:\n");
  71.     for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1; i++)
  72.         g_printf("%d,", g_array_index(array, int, i));
  73.     g_printf("\n");

  74. // GArray* g_array_remove_range(GArray *array, guint index_, guint length);
  75.     array = g_array_remove_range(array, 2, 2);
  76.     g_printf("All of items[after remove 2 items from the third item]:\n");
  77.     for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
  78.         g_printf("%d,", g_array_index(array, int, i));
  79.     g_printf("\n");

  80. // void g_array_sort(GArray *array, GCompareFunc compare_func);
  81.     g_array_sort(array, sort);
  82.     g_printf("All of items[sorted]:\n");
  83.     for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
  84.         g_printf("%d,", g_array_index(array, int, i));
  85.     g_printf("\n");

  86. // void g_array_sort_with_data(GArray *array, GCompareDataFunc compare_func, gpointer user_data);
  87.     g_array_sort_with_data(array, sort_r, NULL);
  88.     g_printf("All of items[sorted reversed]:\n");
  89.     for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
  90.         g_printf("%d,", g_array_index(array, int, i));
  91.     g_printf("\n");

  92.     int x[4] = {2,3,4,5};
  93. // GArray* g_array_append_vals(GArray *array, gconstpointer data, guint len);
  94.     array = g_array_append_vals(array, x, 4);
  95.     g_printf("All of items[after append all 2,3,4,5]:\n");
  96.     for (i = 0; i < array->len; i++)
  97.         g_printf("%d,", g_array_index(array, int, i));
  98.     g_printf("\n");

  99.     g_printf("All of items[after prepend one by one 2,3,4,5]:\n");
  100. // #define g_array_prepend_val(a,v)
  101.     for (i = 0; i < 4; i++)
  102.         g_array_prepend_val(array, x[i]);
  103.     print(array);

  104.     g_printf("All of items[after prepend all 2,3,4,5]:\n");
  105. // GArray* g_array_prepend_vals(GArray *array, gconstpointer data, guint len);
  106.     array = g_array_prepend_vals(array, x, 4);
  107.     print(array);

  108.     int t = 0;
  109. // #define g_array_insert_val(a, i, v)
  110.     g_array_insert_val(array, 0, t);
  111.     g_printf("All of items[after insert 0 at the first index]:\n");
  112.     print(array);

  113.     g_array_sort(array, sort);
  114.     g_printf("All of items[sorted]:\n");
  115.     print(array);

  116. // gchar* g_array_free(GArray *array, gboolean free_segment);
  117.     g_array_free(array, TRUE);
  118. }

  119. int
  120. main(void)
  121. {
  122.     printf("BEGIN:\n************************************************************\n");
  123.     test_array();
  124.     printf("\n************************************************************\nDONE\n");

  125.     return 0;
  126. }

5:结果
  1. BEGIN:
  2. ************************************************************
  3. There should be '10' items now. Result: 10.
  4. All of items:
  5. 1,2,3,4,5,6,7,8,9,10,
  6. All of items[exclude the second item]:
  7. 1,3,4,5,6,7,8,9,10,
  8. All of items[exclude the second item]:
  9. 1,10,4,5,6,7,8,9,
  10. All of items[after remove 2 items from the third item]:
  11. 1,10,6,7,8,9,
  12. All of items[sorted]:
  13. 1,6,7,8,9,10,
  14. All of items[sorted reversed]:
  15. 10,9,8,7,6,1,
  16. All of items[after append all 2,3,4,5]:
  17. 10,9,8,7,6,1,2,3,4,5,
  18. All of items[after prepend one by one 2,3,4,5]:
  19. 5,4,3,2,10,9,8,7,6,1,2,3,4,5,
  20. All of items[after prepend all 2,3,4,5]:
  21. 2,3,4,5,5,4,3,2,10,9,8,7,6,1,2,3,4,5,
  22. All of items[after insert 0 at the first index]:
  23. 0,2,3,4,5,5,4,3,2,10,9,8,7,6,1,2,3,4,5,
  24. All of items[sorted]:
  25. 0,1,2,2,2,3,3,3,4,4,4,5,5,5,6,7,8,9,10,

  26. ************************************************************
  27. DONE


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