e609b44053e5702d5d2f64c2b1f6cf5920129b02
[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 <structname>GPtrArray</structname></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 &percnt;p instead of &percnt;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 </para>
67
68 @pdata: points to the array of pointers, which may be moved when the array grows.
69 @len: number of pointers in the array.
70
71 <!-- ##### FUNCTION g_ptr_array_new ##### -->
72 <para>
73 Creates a new #GPtrArray.
74 </para>
75
76 @Returns: the new #GPtrArray.
77
78
79 <!-- ##### FUNCTION g_ptr_array_sized_new ##### -->
80 <para>
81 Creates a new #GPtrArray with @reserved_size pointers
82 preallocated. This avoids frequent reallocation, if you are going to
83 add many pointers to the array. Note however that the size of the
84 array is still 0.
85 </para>
86
87 @reserved_size: number of pointers preallocated.
88 @Returns: the new #GPtrArray.
89
90
91 <!-- ##### FUNCTION g_ptr_array_add ##### -->
92 <para>
93 Adds a pointer to the end of the pointer array.
94 The array will grow in size automatically if necessary.
95 </para>
96
97 @array: a #GPtrArray.
98 @data: the pointer to add.
99
100
101 <!-- ##### FUNCTION g_ptr_array_remove ##### -->
102 <para>
103 Removes the first occurrence of the given pointer from the pointer array.
104 The following elements are moved down one place.
105 </para>
106 <para>
107 It returns %TRUE if the pointer was removed, or %FALSE if the pointer
108 was not found.
109 </para>
110
111 @array: a #GPtrArray.
112 @data: the pointer to remove.
113 @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is not found
114 in the array.
115
116
117 <!-- ##### FUNCTION g_ptr_array_remove_index ##### -->
118 <para>
119 Removes the pointer at the given index from the pointer array.
120 The following elements are moved down one place.
121 </para>
122
123 @array: a #GPtrArray.
124 @index_: the index of the pointer to remove.
125 @Returns: the pointer which was removed.
126
127
128 <!-- ##### FUNCTION g_ptr_array_remove_fast ##### -->
129 <para>
130 Removes the first occurrence of the given pointer from the pointer array.
131 The last element in the array is used to fill in the space, so this function
132 does not preserve the order of the array. But it is faster than
133 g_ptr_array_remove().
134 </para>
135 <para>
136 It returns %TRUE if the pointer was removed, or %FALSE if the pointer
137 was not found.
138 </para>
139
140 @array: a #GPtrArray.
141 @data: the pointer to remove.
142 @Returns: %TRUE if the pointer was found in the array.
143
144
145 <!-- ##### FUNCTION g_ptr_array_remove_index_fast ##### -->
146 <para>
147 Removes the pointer at the given index from the pointer array.
148 The last element in the array is used to fill in the space, so this function
149 does not preserve the order of the array. But it is faster than
150 g_ptr_array_remove_index().
151 </para>
152
153 @array: a #GPtrArray.
154 @index_: the index of the pointer to remove.
155 @Returns: the pointer which was removed.
156
157
158 <!-- ##### FUNCTION g_ptr_array_sort ##### -->
159 <para>
160 Sorts the array, using @compare_func which should be a <function>qsort()</function>-style comparison
161 function (returns -1 for first arg is less than second arg, 0 for equal, 1 if
162 first arg is greater than second arg).
163 </para>
164 <note><para>
165 The comparison function for g_ptr_array_sort() doesn't take the pointers from the array as arguments, 
166 it takes pointers to the pointers in the array.
167 </para></note>
168
169 @array: a #GPtrArray.
170 @compare_func: comparison function.
171
172
173 <!-- ##### FUNCTION g_ptr_array_sort_with_data ##### -->
174 <para>
175 Like g_ptr_array_sort(), but the comparison function has a user data argument.
176 </para>
177 <note><para>
178 The comparison function for g_ptr_array_sort_with_data() doesn't take the pointers from the array as arguments, 
179 it takes pointers to the pointers in the array.
180 </para></note>
181
182 @array: a #GPtrArray.
183 @compare_func: comparison function.
184 @user_data: data to pass to @compare_func.
185
186
187 <!-- ##### FUNCTION g_ptr_array_set_size ##### -->
188 <para>
189 Sets the size of the array, expanding it if necessary.
190 New elements are set to %NULL.
191 </para>
192
193 @array: a #GPtrArray.
194 @length: the new length of the pointer array.
195
196
197 <!-- ##### MACRO g_ptr_array_index ##### -->
198 <para>
199 Returns the pointer at the given index of the pointer array.
200 </para>
201
202 @array: a #GPtrArray.
203 @index_: the index of the pointer to return.
204 @Returns: the pointer at the given index.
205
206
207 <!-- ##### FUNCTION g_ptr_array_free ##### -->
208 <para>
209 Frees all of the memory allocated for the pointer array.
210 </para>
211
212 @array: a #GPtrArray.
213 @free_seg: if %TRUE the actual element data is freed as well.
214 @Returns: 
215
216