1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 * SECTION: linked_lists_double
37 * @title: Doubly-Linked Lists
38 * @short_description: linked lists containing integer values or
39 * pointers to data, with the ability to iterate
40 * over the list in both directions
42 * The #GList structure and its associated functions provide a standard
43 * doubly-linked list data structure.
45 * Each element in the list contains a piece of data, together with
46 * pointers which link to the previous and next elements in the list.
47 * Using these pointers it is possible to move through the list in both
48 * directions (unlike the <link
49 * linkend="glib-Singly-Linked-lists">Singly-Linked Lists</link> which
50 * only allows movement through the list in the forward direction).
52 * The data contained in each element can be either integer values, by
53 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
54 * Conversion Macros</link>, or simply pointers to any type of data.
56 * List elements are allocated from the <link
57 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
58 * efficient than allocating elements individually.
60 * Note that most of the #GList functions expect to be passed a pointer
61 * to the first element in the list. The functions which insert
62 * elements return the new start of the list, which may have changed.
64 * There is no function to create a #GList. %NULL is considered to be
65 * the empty list so you simply set a #GList* to %NULL.
67 * To add elements, use g_list_append(), g_list_prepend(),
68 * g_list_insert() and g_list_insert_sorted().
70 * To remove elements, use g_list_remove().
72 * To find elements in the list use g_list_first(), g_list_last(),
73 * g_list_next(), g_list_previous(), g_list_nth(), g_list_nth_data(),
74 * g_list_find() and g_list_find_custom().
76 * To find the index of an element use g_list_position() and
79 * To call a function for each element in the list use g_list_foreach().
81 * To free the entire list, use g_list_free().
86 * @data: holds the element's data, which can be a pointer to any kind
87 * of data, or any integer value using the <link
88 * linkend="glib-Type-Conversion-Macros">Type Conversion
90 * @next: contains the link to the next element in the list.
91 * @prev: contains the link to the previous element in the list.
93 * The #GList struct is used for each element in a doubly-linked list.
98 * @list: an element in a #GList.
99 * @Returns: the previous element, or %NULL if there are no previous
102 * A convenience macro to get the previous element in a #GList.
107 * @list: an element in a #GList.
108 * @Returns: the next element, or %NULL if there are no more elements.
110 * A convenience macro to get the next element in a #GList.
116 * g_list_push_allocator:
117 * @allocator: the #GAllocator to use when allocating #GList elements.
119 * Sets the allocator to use to allocate #GList elements. Use
120 * g_list_pop_allocator() to restore the previous allocator.
122 * Note that this function is not available if GLib has been compiled
123 * with <option>--disable-mem-pools</option>
125 * Deprecated:2.10: It does nothing, since #GList has been converted
126 * to the <link linkend="glib-Memory-Slices">slice
129 void g_list_push_allocator (gpointer dummy) { /* present for binary compat only */ }
132 * g_list_pop_allocator:
134 * Restores the previous #GAllocator, used when allocating #GList
137 * Note that this function is not available if GLib has been compiled
138 * with <option>--disable-mem-pools</option>
140 * Deprecated:2.10: It does nothing, since #GList has been converted
141 * to the <link linkend="glib-Memory-Slices">slice
144 void g_list_pop_allocator (void) { /* present for binary compat only */ }
146 #define _g_list_alloc() g_slice_new (GList)
147 #define _g_list_alloc0() g_slice_new0 (GList)
148 #define _g_list_free1(list) g_slice_free (GList, list)
152 * @Returns: a pointer to the newly-allocated #GList element.
154 * Allocates space for one #GList element. It is called by
155 * g_list_append(), g_list_prepend(), g_list_insert() and
156 * g_list_insert_sorted() and so is rarely used on its own.
161 return _g_list_alloc0 ();
168 * Frees all of the memory used by a #GList.
169 * The freed elements are returned to the slice allocator.
172 * If list elements contain dynamically-allocated memory,
173 * they should be freed first.
177 g_list_free (GList *list)
179 g_slice_free_chain (GList, list, next);
184 * @list: a #GList element
186 * Frees one #GList element.
187 * It is usually used after g_list_remove_link().
192 * Another name for g_list_free_1().
195 g_list_free_1 (GList *list)
197 _g_list_free1 (list);
202 * @list: a pointer to a #GList
203 * @data: the data for the new element
205 * Adds a new element on to the end of the list.
208 * The return value is the new start of the list, which
209 * may have changed, so make sure you store the new value.
213 * Note that g_list_append() has to traverse the entire list
214 * to find the end, which is inefficient when adding multiple
215 * elements. A common idiom to avoid the inefficiency is to prepend
216 * the elements and reverse the list when all elements have been added.
220 * /* Notice that these are initialized to the empty list. */
221 * GList *list = NULL, *number_list = NULL;
223 * /* This is a list of strings. */
224 * list = g_list_append (list, "first");
225 * list = g_list_append (list, "second");
227 * /* This is a list of integers. */
228 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
229 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
232 * Returns: the new start of the #GList
235 g_list_append (GList *list,
241 new_list = _g_list_alloc ();
242 new_list->data = data;
243 new_list->next = NULL;
247 last = g_list_last (list);
248 /* g_assert (last != NULL); */
249 last->next = new_list;
250 new_list->prev = last;
256 new_list->prev = NULL;
263 * @list: a pointer to a #GList
264 * @data: the data for the new element
266 * Adds a new element on to the start of the list.
269 * The return value is the new start of the list, which
270 * may have changed, so make sure you store the new value.
274 * /* Notice that it is initialized to the empty list. */
275 * GList *list = NULL;
276 * list = g_list_prepend (list, "last");
277 * list = g_list_prepend (list, "first");
280 * Returns: the new start of the #GList
283 g_list_prepend (GList *list,
288 new_list = _g_list_alloc ();
289 new_list->data = data;
290 new_list->next = list;
294 new_list->prev = list->prev;
296 list->prev->next = new_list;
297 list->prev = new_list;
300 new_list->prev = NULL;
307 * @list: a pointer to a #GList
308 * @data: the data for the new element
309 * @position: the position to insert the element. If this is
310 * negative, or is larger than the number of elements in the
311 * list, the new element is added on to the end of the list.
313 * Inserts a new element into the list at the given position.
315 * Returns: the new start of the #GList
318 g_list_insert (GList *list,
326 return g_list_append (list, data);
327 else if (position == 0)
328 return g_list_prepend (list, data);
330 tmp_list = g_list_nth (list, position);
332 return g_list_append (list, data);
334 new_list = _g_list_alloc ();
335 new_list->data = data;
336 new_list->prev = tmp_list->prev;
338 tmp_list->prev->next = new_list;
339 new_list->next = tmp_list;
340 tmp_list->prev = new_list;
342 if (tmp_list == list)
349 * g_list_insert_before:
350 * @list: a pointer to a #GList
351 * @sibling: the list element before which the new element
352 * is inserted or %NULL to insert at the end of the list
353 * @data: the data for the new element
355 * Inserts a new element into the list before the given position.
357 * Returns: the new start of the #GList
360 g_list_insert_before (GList *list,
366 list = g_list_alloc ();
368 g_return_val_if_fail (sibling == NULL, list);
375 node = _g_list_alloc ();
377 node->prev = sibling->prev;
378 node->next = sibling;
379 sibling->prev = node;
382 node->prev->next = node;
387 g_return_val_if_fail (sibling == list, node);
399 last->next = _g_list_alloc ();
400 last->next->data = data;
401 last->next->prev = last;
402 last->next->next = NULL;
411 * @list2: the #GList to add to the end of the first #GList
413 * Adds the second #GList onto the end of the first #GList.
414 * Note that the elements of the second #GList are not copied.
415 * They are used directly.
417 * Returns: the start of the new #GList
420 g_list_concat (GList *list1, GList *list2)
426 tmp_list = g_list_last (list1);
428 tmp_list->next = list2;
431 list2->prev = tmp_list;
440 * @data: the data of the element to remove
442 * Removes an element from a #GList.
443 * If two elements contain the same data, only the first is removed.
444 * If none of the elements contain the data, the #GList is unchanged.
446 * Returns: the new start of the #GList
449 g_list_remove (GList *list,
457 if (tmp->data != data)
462 tmp->prev->next = tmp->next;
464 tmp->next->prev = tmp->prev;
480 * @data: data to remove
482 * Removes all list nodes with data equal to @data.
483 * Returns the new head of the list. Contrast with
484 * g_list_remove() which removes only the first node
485 * matching the given data.
487 * Returns: new head of @list
490 g_list_remove_all (GList *list,
497 if (tmp->data != data)
501 GList *next = tmp->next;
504 tmp->prev->next = next;
508 next->prev = tmp->prev;
518 _g_list_remove_link (GList *list,
524 link->prev->next = link->next;
526 link->next->prev = link->prev;
539 * g_list_remove_link:
541 * @llink: an element in the #GList
543 * Removes an element from a #GList, without freeing the element.
544 * The removed element's prev and next links are set to %NULL, so
545 * that it becomes a self-contained list with one element.
547 * Returns: the new start of the #GList, without the element
550 g_list_remove_link (GList *list,
553 return _g_list_remove_link (list, llink);
557 * g_list_delete_link:
559 * @link_: node to delete from @list
561 * Removes the node link_ from the list and frees it.
562 * Compare this to g_list_remove_link() which removes the node
563 * without freeing it.
565 * Returns: the new head of @list
568 g_list_delete_link (GList *list,
571 list = _g_list_remove_link (list, link_);
572 _g_list_free1 (link_);
584 * Note that this is a "shallow" copy. If the list elements
585 * consist of pointers to data, the pointers are copied but
586 * the actual data is not.
589 * Returns: a copy of @list
592 g_list_copy (GList *list)
594 GList *new_list = NULL;
600 new_list = _g_list_alloc ();
601 new_list->data = list->data;
602 new_list->prev = NULL;
607 last->next = _g_list_alloc ();
608 last->next->prev = last;
610 last->data = list->data;
624 * It simply switches the next and prev pointers of each element.
626 * Returns: the start of the reversed #GList
629 g_list_reverse (GList *list)
638 last->next = last->prev;
648 * @n: the position of the element, counting from 0
650 * Gets the element at the given position in a #GList.
652 * Returns: the element, or %NULL if the position is off
653 * the end of the #GList
656 g_list_nth (GList *list,
659 while ((n-- > 0) && list)
668 * @n: the position of the element, counting from 0
670 * Gets the element @n places before @list.
672 * Returns: the element, or %NULL if the position is
673 * off the end of the #GList
676 g_list_nth_prev (GList *list,
679 while ((n-- > 0) && list)
688 * @n: the position of the element
690 * Gets the data of the element at the given position.
692 * Returns: the element's data, or %NULL if the position
693 * is off the end of the #GList
696 g_list_nth_data (GList *list,
699 while ((n-- > 0) && list)
702 return list ? list->data : NULL;
708 * @data: the element data to find
710 * Finds the element in a #GList which
711 * contains the given data.
713 * Returns: the found #GList element,
714 * or %NULL if it is not found
717 g_list_find (GList *list,
722 if (list->data == data)
731 * g_list_find_custom:
733 * @data: user data passed to the function
734 * @func: the function to call for each element.
735 * It should return 0 when the desired element is found
737 * Finds an element in a #GList, using a supplied function to
738 * find the desired element. It iterates over the list, calling
739 * the given function which should return 0 when the desired
740 * element is found. The function takes two #gconstpointer arguments,
741 * the #GList element's data as the first argument and the
744 * Returns: the found #GList element, or %NULL if it is not found
747 g_list_find_custom (GList *list,
751 g_return_val_if_fail (func != NULL, list);
755 if (! func (list->data, data))
767 * @llink: an element in the #GList
769 * Gets the position of the given element
770 * in the #GList (starting from 0).
772 * Returns: the position of the element in the #GList,
773 * or -1 if the element is not found
776 g_list_position (GList *list,
796 * @data: the data to find
798 * Gets the position of the element containing
799 * the given data (starting from 0).
801 * Returns: the index of the element containing the data,
802 * or -1 if the data is not found
805 g_list_index (GList *list,
813 if (list->data == data)
826 * Gets the last element in a #GList.
828 * Returns: the last element in the #GList,
829 * or %NULL if the #GList has no elements
832 g_list_last (GList *list)
847 * Gets the first element in a #GList.
849 * Returns: the first element in the #GList,
850 * or %NULL if the #GList has no elements
853 g_list_first (GList *list)
868 * Gets the number of elements in a #GList.
871 * This function iterates over the whole list to
872 * count its elements.
875 * Returns: the number of elements in the #GList
878 g_list_length (GList *list)
895 * @func: the function to call with each element's data
896 * @user_data: user data to pass to the function
898 * Calls a function for each element of a #GList.
902 * @data: the element's data.
903 * @user_data: user data passed to g_list_foreach() or
906 * Specifies the type of functions passed to g_list_foreach() and
910 g_list_foreach (GList *list,
916 GList *next = list->next;
917 (*func) (list->data, user_data);
923 g_list_insert_sorted_real (GList *list,
928 GList *tmp_list = list;
932 g_return_val_if_fail (func != NULL, list);
936 new_list = _g_list_alloc0 ();
937 new_list->data = data;
941 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
943 while ((tmp_list->next) && (cmp > 0))
945 tmp_list = tmp_list->next;
947 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
950 new_list = _g_list_alloc0 ();
951 new_list->data = data;
953 if ((!tmp_list->next) && (cmp > 0))
955 tmp_list->next = new_list;
956 new_list->prev = tmp_list;
962 tmp_list->prev->next = new_list;
963 new_list->prev = tmp_list->prev;
965 new_list->next = tmp_list;
966 tmp_list->prev = new_list;
968 if (tmp_list == list)
975 * g_list_insert_sorted:
976 * @list: a pointer to a #GList
977 * @data: the data for the new element
978 * @func: the function to compare elements in the list. It should
979 * return a number > 0 if the first parameter comes after the
980 * second parameter in the sort order.
982 * Inserts a new element into the list, using the given comparison
983 * function to determine its position.
985 * Returns: the new start of the #GList
988 g_list_insert_sorted (GList *list,
992 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
996 * g_list_insert_sorted_with_data:
997 * @list: a pointer to a #GList
998 * @data: the data for the new element
999 * @func: the function to compare elements in the list.
1000 * It should return a number > 0 if the first parameter
1001 * comes after the second parameter in the sort order.
1002 * @user_data: user data to pass to comparison function.
1004 * Inserts a new element into the list, using the given comparison
1005 * function to determine its position.
1007 * Returns: the new start of the #GList
1012 g_list_insert_sorted_with_data (GList *list,
1014 GCompareDataFunc func,
1017 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1021 g_list_sort_merge (GList *l1,
1026 GList list, *l, *lprev;
1034 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1050 l->next = l1 ? l1 : l2;
1057 g_list_sort_real (GList *list,
1071 while ((l2 = l2->next) != NULL)
1073 if ((l2 = l2->next) == NULL)
1080 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1081 g_list_sort_real (l2, compare_func, user_data),
1089 * @compare_func: the comparison function used to sort the #GList.
1090 * This function is passed the data from 2 elements of the #GList
1091 * and should return 0 if they are equal, a negative value if the
1092 * first element comes before the second, or a positive value if
1093 * the first element comes after the second.
1095 * Sorts a #GList using the given comparison function.
1097 * Returns: the start of the sorted #GList
1102 * @b: a value to compare with.
1103 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1106 * Specifies the type of a comparison function used to compare two
1107 * values. The function should return a negative integer if the first
1108 * value comes before the second, 0 if they are equal, or a positive
1109 * integer if the first value comes after the second.
1112 g_list_sort (GList *list,
1113 GCompareFunc compare_func)
1115 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1120 * g_list_sort_with_data:
1122 * @compare_func: comparison function
1123 * @user_data: user data to pass to comparison function
1125 * Like g_list_sort(), but the comparison function accepts
1126 * a user data argument.
1128 * Returns: the new head of @list
1133 * @b: a value to compare with.
1134 * @user_data: user data to pass to comparison function.
1135 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1138 * Specifies the type of a comparison function used to compare two
1139 * values. The function should return a negative integer if the first
1140 * value comes before the second, 0 if they are equal, or a positive
1141 * integer if the first value comes after the second.
1144 g_list_sort_with_data (GList *list,
1145 GCompareDataFunc compare_func,
1148 return g_list_sort_real (list, (GFunc) compare_func, user_data);