Patch from Havoc Pennington to add functions for setting and getting a
[platform/upstream/glib.git] / docs / reference / glib / tmpl / linked_lists_double.sgml
1 <!-- ##### SECTION Title ##### -->
2 Doubly-Linked Lists
3
4 <!-- ##### SECTION Short_Description ##### -->
5 linked lists containing integer values or pointers to data, with the ability
6 to iterate over the list in both directions.
7
8 <!-- ##### SECTION Long_Description ##### -->
9 <para>
10 The #GList structure and its associated functions provide a standard
11 doubly-linked list data structure.
12 </para>
13 <para>
14 Each element in the list contains a piece of data, together with pointers
15 which link to the previous and next elements in the list.
16 Using these pointers it is possible to move through the list in both
17 directions (unlike the
18 <link linkend="glib-Singly-Linked-lists">Singly-Linked Lists</link>
19 which only allows movement through the list in the forward direction).
20 </para>
21 <para>
22 The data contained in each element can be either integer values, by using one
23 of the
24 <link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
25 or simply pointers to any type of data.
26 </para>
27 <para>
28 List elements are allocated in blocks using a #GAllocator, which is
29 more efficient than allocating elements individually.
30 </para>
31 <para>
32 Note that most of the #GList functions expect to be passed a pointer to
33 the first element in the list. The functions which insert elements return
34 the new start of the list, which may have changed.
35 </para>
36 <para>
37 There is no function to create a #GList. %NULL is considered to be the empty
38 list so you simply set a #GList* to %NULL.
39 </para>
40 <para>
41 To add elements, use g_list_append(), g_list_prepend(), g_list_insert()
42 and g_list_insert_sorted().
43 </para>
44 <para>
45 To remove elements, use g_list_remove().
46 </para>
47 <para>
48 To find elements in the list use g_list_first(), g_list_last(), g_list_next(),
49 g_list_previous(), g_list_nth(), g_list_nth_data(), g_list_find() and
50 g_list_find_custom().
51 </para>
52 <para>
53 To find the index of an element use g_list_position() and g_list_index().
54 </para>
55 <para>
56 To call a function for each element in the list use g_list_foreach().
57 </para>
58 <para>
59 To free the entire list, use g_list_free().
60 </para>
61
62 <!-- ##### SECTION See_Also ##### -->
63 <para>
64
65 </para>
66
67 <!-- ##### STRUCT GList ##### -->
68 <para>
69 The #GList struct is used for each element in a doubly-linked list.
70 The <structfield>data</structfield> field holds the element's data, which can
71 be a pointer to any kind of data, or any integer value using the
72 <link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>.
73 The <structfield>next</structfield> and <structfield>prev</structfield>
74 pointers are the links to the next and previous elements in the list.
75 </para>
76
77 @data: 
78 @next: 
79 @prev: 
80
81 <!-- ##### FUNCTION g_list_append ##### -->
82 <para>
83 Adds a new element on to the end of the list.
84 </para>
85 <note>
86 <para>
87 The return value is the new start of the list, which may have changed, so
88 make sure you store the new value.
89 </para>
90 </note>
91 <informalexample><programlisting>
92   /* Notice that these are initialized to the empty list. */
93   GList *list = NULL, *number_list = NULL;
94
95   /* This is a list of strings. */
96   list = g_list_append (list, "first");
97   list = g_list_append (list, "second");
98
99   /* This is a list of integers. */
100   number_list = g_list_append (number_list, GINT_TO_POINTER (27));
101   number_list = g_list_append (number_list, GINT_TO_POINTER (14));
102 </programlisting></informalexample>
103
104 @list: a pointer to a #GList.
105 @data: the data for the new element.
106 @Returns: the new start of the #GList.
107
108
109 <!-- ##### FUNCTION g_list_prepend ##### -->
110 <para>
111 Adds a new element on to the start of the list.
112 </para>
113 <note>
114 <para>
115 The return value is the new start of the list, which may have changed, so
116 make sure you store the new value.
117 </para>
118 </note>
119 <informalexample><programlisting>
120   /* Notice that it is initialized to the empty list. */
121   GList *list = NULL;
122   list = g_list_prepend (list, "last");
123   list = g_list_prepend (list, "first");
124 </programlisting></informalexample>
125
126 @list: a pointer to a #GList.
127 @data: the data for the new element.
128 @Returns: the new start of the #GList.
129
130
131 <!-- ##### FUNCTION g_list_insert ##### -->
132 <para>
133 Inserts a new element into the list at the given position.
134 </para>
135
136 @list: a pointer to a #GList.
137 @data: the data for the new element.
138 @position: the position to insert the element. If this is negative, or is
139 larger than the number of elements in the list, the new element is added on
140 to the end of the list.
141 @Returns: the new start of the #GList.
142
143
144 <!-- ##### FUNCTION g_list_insert_before ##### -->
145 <para>
146 Inserts a new element into the list before the given position.
147 </para>
148
149 @list: a pointer to a #GList.
150 @sibling: the list element before which the new element is inserted
151   or %NULL to insert at the end of the list.
152 @data: the data for the new element.
153 @Returns: the new start of the #GList.
154
155
156 <!-- ##### FUNCTION g_list_insert_sorted ##### -->
157 <para>
158 Inserts a new element into the list, using the given comparison function
159 to determine its position.
160 </para>
161
162 @list: a pointer to a #GList.
163 @data: the data for the new element.
164 @func: the function to compare elements in the list. It should return a
165 number > 0 if the first parameter comes after the second parameter in
166 the sort order.
167 @Returns: the new start of the #GList.
168
169
170 <!-- ##### FUNCTION g_list_remove ##### -->
171 <para>
172 Removes an element from a #GList.
173 If two elements contain the same data, only the first is removed.
174 If none of the elements contain the data, the #GList is unchanged.
175 </para>
176
177 @list: a #GList.
178 @data: the data of the element to remove.
179 @Returns: the new start of the #GList.
180
181
182 <!-- ##### FUNCTION g_list_remove_link ##### -->
183 <para>
184 Removes an element from a #GList, without freeing the element.
185 The removed element's prev and next links are set to %NULL, so that it becomes a
186 self-contained list with one element.
187 </para>
188
189 @list: a #GList.
190 @llink: an element in the #GList.
191 @Returns: the new start of the #GList, without the element.
192
193
194 <!-- ##### FUNCTION g_list_delete_link ##### -->
195 <para>
196 Deletes the node @link from @list.
197 </para>
198
199 @list: a #GList.
200 @link: node to delete from @list.
201 @Returns: the new head of @list.
202
203
204 <!-- ##### FUNCTION g_list_remove_all ##### -->
205 <para>
206 Removes all list nodes with data equal to @data. Returns the new
207 head of the list. Contrast with g_list_remove() which removes only 
208 the first node matching the given data.
209 </para>
210
211 @list: a #GList.
212 @data: data to remove.
213 @Returns: new head of @list.
214
215
216 <!-- ##### FUNCTION g_list_free ##### -->
217 <para>
218 Frees all of the memory used by a #GList.
219 The freed elements are added to the #GAllocator free list.
220 </para>
221 <note>
222 <para>
223 If list elements contain dynamically-allocated memory, they should be freed
224 first.
225 </para>
226 </note>
227
228 @list: a #GList.
229
230
231 <!-- ##### FUNCTION g_list_alloc ##### -->
232 <para>
233 Allocates space for one #GList element.
234 It is called by g_list_append(), g_list_prepend(), g_list_insert() and
235 g_list_insert_sorted() and so is rarely used on its own.
236 </para>
237
238 @Returns: a pointer to the newly-allocated #GList element.
239
240
241 <!-- ##### FUNCTION g_list_free_1 ##### -->
242 <para>
243 Frees one #GList element.
244 It is usually used after g_list_remove_link().
245 </para>
246
247 @list: a #GList element.
248
249
250 <!-- ##### FUNCTION g_list_length ##### -->
251 <para>
252 Gets the number of elements in a #GList.
253 </para>
254
255 @list: a #GList.
256 @Returns: the number of elements in the #GList.
257
258
259 <!-- ##### FUNCTION g_list_copy ##### -->
260 <para>
261 Copies a #GList.
262 </para>
263 <para>
264 Note that this is a "shallow" copy. If the list elements consist of pointers
265 to data, the pointers are copied but the actual data isn't.
266 </para>
267
268 @list: a #GList.
269 @Returns: a copy of @list.
270
271
272 <!-- ##### FUNCTION g_list_reverse ##### -->
273 <para>
274 Reverses a #GList.
275 It simply switches the next and prev pointers of each element.
276 </para>
277
278 @list: a #GList.
279 @Returns: the start of the reversed #GList.
280
281
282 <!-- ##### FUNCTION g_list_sort ##### -->
283 <para>
284 Sorts a #GList using the given comparison function.
285 </para>
286
287 @list: a #GList.
288 @compare_func: the comparison function used to sort the #GList. This function
289 is passed 2 elements of the #GList and should return 0 if they are equal,
290 a negative value if the first element comes before the second, or a positive
291 value if the first element comes after the second.
292 @Returns: the start of the sorted #GList.
293
294
295 <!-- ##### USER_FUNCTION GCompareFunc ##### -->
296 <para>
297 Specifies the type of a comparison function used to compare two
298 values.  The function should return a negative integer if the first
299 value comes before the second, 0 if they are equal, or a positive
300 integer if the first value comes after the second.
301 </para>
302
303 @a: a value.
304 @b: a value to compare with.
305 @Returns: negative value if @a &lt; @b; zero if @a = @b; positive value
306 if @a > @b.
307
308
309 <!-- ##### FUNCTION g_list_sort_with_data ##### -->
310 <para>
311 Like g_list_sort(), but the comparison function accepts a user data argument.
312 </para>
313
314 @list: a #GList.
315 @compare_func: comparison function.
316 @user_data: user data to pass to comparison function.
317 @Returns: the new head of @list.
318
319
320 <!-- ##### USER_FUNCTION GCompareDataFunc ##### -->
321 <para>
322 Specifies the type of a comparison function used to compare two
323 values.  The function should return a negative integer if the first
324 value comes before the second, 0 if they are equal, or a positive
325 integer if the first value comes after the second.
326 </para>
327
328 @a: a value.
329 @b: a value to compare with.
330 @user_data: user data to pass to comparison function.
331 @Returns: negative value if @a &lt; @b; zero if @a = @b; positive value
332 if @a > @b.
333
334
335 <!-- ##### FUNCTION g_list_concat ##### -->
336 <para>
337 Adds the second #GList onto the end of the first #GList.
338 Note that the elements of the second #GList are not copied.
339 They are used directly.
340 </para>
341
342 @list1: a #GList.
343 @list2: the #GList to add to the end of the first #GList.
344 @Returns: the start of the new #GList.
345
346
347 <!-- ##### FUNCTION g_list_foreach ##### -->
348 <para>
349 Calls a function for each element of a #GList.
350 </para>
351
352 @list: a #GList.
353 @func: the function to call with each element's data.
354 @user_data: user data to pass to the function.
355
356
357 <!-- ##### USER_FUNCTION GFunc ##### -->
358 <para>
359 Specifies the type of functions passed to g_list_foreach() and
360 g_slist_foreach().
361 </para>
362
363 @data: the element's data.
364 @user_data: user data passed to g_list_foreach() or g_slist_foreach().
365
366
367 <!-- ##### FUNCTION g_list_first ##### -->
368 <para>
369 Gets the first element in a #GList.
370 </para>
371
372 @list: a #GList.
373 @Returns: the first element in a #GList, or %NULL if the #GList has no elements.
374
375
376 <!-- ##### FUNCTION g_list_last ##### -->
377 <para>
378 Gets the last element in a #GList.
379 </para>
380
381 @list: a #GList.
382 @Returns: the last element in the #GList, or %NULL if the #GList has no
383 elements.
384
385
386 <!-- ##### MACRO g_list_previous ##### -->
387 <para>
388 A convenience macro to gets the previous element in a #GList.
389 </para>
390
391 @list: an element in a #GList.
392 @Returns: the previous element, or %NULL if there are no previous elements.
393
394
395 <!-- ##### MACRO g_list_next ##### -->
396 <para>
397 A convenience macro to gets the next element in a #GList.
398 </para>
399
400 @list: an element in a #GList.
401 @Returns: the next element, or %NULL if there are no more elements.
402
403
404 <!-- ##### FUNCTION g_list_nth ##### -->
405 <para>
406 Gets the element at the given position in a #GList.
407 </para>
408
409 @list: a #GList.
410 @n: the position of the element, counting from 0.
411 @Returns: the element, or %NULL if the position is off the end of the #GList.
412
413
414 <!-- ##### FUNCTION g_list_nth_data ##### -->
415 <para>
416 Gets the data of the element at the given position.
417 </para>
418
419 @list: a #GList.
420 @n: the position of the element.
421 @Returns: the element's data, or %NULL if the position is off the end of the
422 #GList.
423
424
425 <!-- ##### FUNCTION g_list_nth_prev ##### -->
426 <para>
427 Gets the element @n places before @list.
428 </para>
429
430 @list: a #GList.
431 @n: the position of the element, counting from 0.
432 @Returns: the element, or %NULL if the position is off the end of the #GList.
433
434
435 <!-- ##### FUNCTION g_list_find ##### -->
436 <para>
437 Finds the element in a #GList which contains the given data.
438 </para>
439
440 @list: a #GList.
441 @data: the element data to find.
442 @Returns: the found #GList element, or %NULL if it is not found.
443
444
445 <!-- ##### FUNCTION g_list_find_custom ##### -->
446 <para>
447 Finds an element in a #GList, using a supplied function to find the desired
448 element.
449 It iterates over the list, calling the given function which should return 0
450 when the desired element is found.
451 The function takes two #gconstpointer arguments, the #GList element's data
452 and the given user data.
453 </para>
454
455 @list: a #GList.
456 @data: user data passed to the function.
457 @func: the function to call for each element. It should return 0 when the
458 desired element is found.
459 @Returns: the found #GList element, or %NULL if it is not found.
460
461
462 <!-- ##### FUNCTION g_list_position ##### -->
463 <para>
464 Gets the position of the given element in the #GList (starting from 0).
465 </para>
466
467 @list: a #GList.
468 @llink: an element in the #GList.
469 @Returns: the position of the element in the #GList, or -1 if the element is
470 not found.
471
472
473 <!-- ##### FUNCTION g_list_index ##### -->
474 <para>
475 Gets the position of the element containing the given data (starting from 0).
476 </para>
477
478 @list: a #GList.
479 @data: the data to find.
480 @Returns: the index of the element containing the data, or -1 if the data
481 is not found.
482
483
484 <!-- ##### FUNCTION g_list_push_allocator ##### -->
485 <para>
486 Sets the allocator to use to allocate #GList elements.
487 Use g_list_pop_allocator() to restore the previous allocator.
488 </para>
489
490 @allocator: the #GAllocator to use when allocating #GList elements.
491
492
493 <!-- ##### FUNCTION g_list_pop_allocator ##### -->
494 <para>
495 Restores the previous #GAllocator, used when allocating #GList elements.
496 </para>
497
498
499