f44d90f9e4c5d48c2e33da370b3052d699922ef3
[platform/upstream/glib.git] / docs / reference / glib / tmpl / arrays_pointer.sgml
1 <!-- ##### SECTION Title ##### -->
2 Pointer Arrays
3
4 <!-- ##### SECTION Short_Description ##### -->
5 arrays of pointers to any type of data, which grow automatically as new
6 elements are added.
7
8 <!-- ##### SECTION Long_Description ##### -->
9 <para>
10 Pointer Arrays are similar to Arrays but are used only for storing pointers.
11 </para>
12 <note>
13 <para>
14 If you remove elements from the array, elements at the end of the array
15 are moved into the space previously occupied by the removed element.
16 This means that you should not rely on the index of particular elements
17 remaining the same. You should also be careful when deleting elements while
18 iterating over the array.
19 </para>
20 </note>
21 <para>
22 To create a pointer array, use g_ptr_array_new().
23 </para>
24 <para>
25 To add elements to a pointer array, use g_ptr_array_add().
26 </para>
27 <para>
28 To remove elements from a pointer array, use g_ptr_array_remove(),
29 g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
30 </para>
31 <para>
32 To access an element of a pointer array, use g_ptr_array_index().
33 </para>
34 <para>
35 To set the size of a pointer array, use g_ptr_array_set_size().
36 </para>
37 <para>
38 To free a pointer array, use g_ptr_array_free().
39 </para>
40 <example>
41 <title>Using a GPtrArray.</title>
42 <programlisting>
43   GPtrArray *gparray;
44   gchar *string1 = "one", *string2 = "two", *string3 = "three";
45
46   gparray = g_ptr_array_new ();
47   g_ptr_array_add (gparray, (gpointer) string1);
48   g_ptr_array_add (gparray, (gpointer) string2);
49   g_ptr_array_add (gparray, (gpointer) string3);
50
51   if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
52     g_print ("ERROR: got %p instead of %p\n",
53              g_ptr_array_index (gparray, 0), string1);
54
55   g_ptr_array_free (gparray, TRUE);
56 </programlisting></example>
57
58 <!-- ##### SECTION See_Also ##### -->
59 <para>
60
61 </para>
62
63 <!-- ##### STRUCT GPtrArray ##### -->
64 <para>
65 Contains the public fields of a pointer array.
66 The <structfield>pdata</structfield> field points to the array of pointers,
67 which may as when the array grows.
68 The <structfield>len</structfield> field is the number of pointers in the
69 array.
70 </para>
71
72 @pdata: 
73 @len: 
74
75 <!-- ##### FUNCTION g_ptr_array_new ##### -->
76 <para>
77 Creates a new #GPtrArray.
78 </para>
79
80 @Returns: the new #GPtrArray.
81
82
83 <!-- ##### FUNCTION g_ptr_array_sized_new ##### -->
84 <para>
85
86 </para>
87
88 @reserved_size: 
89 @Returns: 
90
91
92 <!-- ##### FUNCTION g_ptr_array_add ##### -->
93 <para>
94 Adds a pointer to the end of the pointer array.
95 The array will grow in size automatically if necessary.
96 </para>
97
98 @array: a #GPtrArray.
99 @data: the pointer to add.
100
101
102 <!-- ##### FUNCTION g_ptr_array_remove ##### -->
103 <para>
104 Removes the first occurrence of the given pointer from the pointer array.
105 The following elements are moved down one place.
106 </para>
107 <para>
108 It returns TRUE if the pointer was removed, or FALSE if the pointer
109 was not found.
110 </para>
111
112 @array: a #GPtrArray.
113 @data: the pointer to remove.
114 @Returns: TRUE if the pointer is removed. FALSE if the pointer is not found
115 in the array.
116
117
118 <!-- ##### FUNCTION g_ptr_array_remove_index ##### -->
119 <para>
120 Removes the pointer at the given index from the pointer array.
121 The following elements are moved down one place.
122 </para>
123
124 @array: a #GPtrArray.
125 @index: the index of the pointer to remove.
126 @Returns: the pointer which was removed.
127
128
129 <!-- ##### FUNCTION g_ptr_array_remove_fast ##### -->
130 <para>
131 Removes the first occurrence of the given pointer from the pointer array.
132 The last element in the array is used to fill in the space, so this function
133 does not preserve the order of the array. But it is faster than
134 g_ptr_array_remove().
135 </para>
136 <para>
137 It returns TRUE if the pointer was removed, or FALSE if the pointer
138 was not found.
139 </para>
140
141 @array: a #GPtrArray.
142 @data: the pointer to remove.
143 @Returns: TRUE if the pointer was found in the array.
144
145
146 <!-- ##### FUNCTION g_ptr_array_remove_index_fast ##### -->
147 <para>
148 Removes the pointer at the given index from the pointer array.
149 The last element in the array is used to fill in the space, so this function
150 does not preserve the order of the array. But it is faster than
151 g_ptr_array_remove_index().
152 </para>
153
154 @array: a #GPtrArray.
155 @index: the index of the pointer to remove.
156 @Returns: the pointer which was removed.
157
158
159 <!-- ##### FUNCTION g_ptr_array_sort ##### -->
160 <para>
161
162 </para>
163
164 @array: 
165 @compare_func: 
166
167
168 <!-- ##### FUNCTION g_ptr_array_sort_with_data ##### -->
169 <para>
170
171 </para>
172
173 @array: 
174 @compare_func: 
175 @user_data: 
176
177
178 <!-- ##### FUNCTION g_ptr_array_set_size ##### -->
179 <para>
180 Sets the size of the array, expanding it if necessary.
181 New elements are set to NULL.
182 </para>
183
184 @array: a #GPtrArray.
185 @length: the new length of the pointer array.
186
187
188 <!-- ##### MACRO g_ptr_array_index ##### -->
189 <para>
190 Returns the pointer at the given index of the pointer array.
191 </para>
192
193 @array: a #GPtrArray.
194 @index: the index of the pointer to return.
195 @Returns: the pointer at the given index.
196
197
198 <!-- ##### FUNCTION g_ptr_array_free ##### -->
199 <para>
200 Frees all of the memory allocated for the pointer array.
201 </para>
202
203 @array: a #GPtrArray.
204 @free_seg: if TRUE the actual element data is freed as well.
205 @Returns: 
206
207