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/.
37 * SECTION: linked_lists_double
38 * @title: Doubly-Linked Lists
39 * @short_description: linked lists containing integer values or
40 * pointers to data, with the ability to iterate
41 * over the list in both directions
43 * The #GList structure and its associated functions provide a standard
44 * doubly-linked list data structure.
46 * Each element in the list contains a piece of data, together with
47 * pointers which link to the previous and next elements in the list.
48 * Using these pointers it is possible to move through the list in both
49 * directions (unlike the <link
50 * linkend="glib-Singly-Linked-lists">Singly-Linked Lists</link> which
51 * only allows movement through the list in the forward direction).
53 * The data contained in each element can be either integer values, by
54 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
55 * Conversion Macros</link>, or simply pointers to any type of data.
57 * List elements are allocated from the <link
58 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
59 * efficient than allocating elements individually.
61 * Note that most of the #GList functions expect to be passed a pointer
62 * to the first element in the list. The functions which insert
63 * elements return the new start of the list, which may have changed.
65 * There is no function to create a #GList. %NULL is considered to be
66 * the empty list so you simply set a #GList* to %NULL.
68 * To add elements, use g_list_append(), g_list_prepend(),
69 * g_list_insert() and g_list_insert_sorted().
71 * To remove elements, use g_list_remove().
73 * To find elements in the list use g_list_first(), g_list_last(),
74 * g_list_next(), g_list_previous(), g_list_nth(), g_list_nth_data(),
75 * g_list_find() and g_list_find_custom().
77 * To find the index of an element use g_list_position() and
80 * To call a function for each element in the list use g_list_foreach().
82 * To free the entire list, use g_list_free().
87 * @data: holds the element's data, which can be a pointer to any kind
88 * of data, or any integer value using the <link
89 * linkend="glib-Type-Conversion-Macros">Type Conversion
91 * @next: contains the link to the next element in the list.
92 * @prev: contains the link to the previous element in the list.
94 * The #GList struct is used for each element in a doubly-linked list.
99 * @list: an element in a #GList.
100 * @Returns: the previous element, or %NULL if there are no previous
103 * A convenience macro to get the previous element in a #GList.
108 * @list: an element in a #GList.
109 * @Returns: the next element, or %NULL if there are no more elements.
111 * A convenience macro to get the next element in a #GList.
117 * g_list_push_allocator:
118 * @allocator: the #GAllocator to use when allocating #GList elements.
120 * Sets the allocator to use to allocate #GList elements. Use
121 * g_list_pop_allocator() to restore the previous allocator.
123 * Note that this function is not available if GLib has been compiled
124 * with <option>--disable-mem-pools</option>
126 * Deprecated:2.10: It does nothing, since #GList has been converted
127 * to the <link linkend="glib-Memory-Slices">slice
130 void g_list_push_allocator (gpointer dummy) { /* present for binary compat only */ }
133 * g_list_pop_allocator:
135 * Restores the previous #GAllocator, used when allocating #GList
138 * Note that this function is not available if GLib has been compiled
139 * with <option>--disable-mem-pools</option>
141 * Deprecated:2.10: It does nothing, since #GList has been converted
142 * to the <link linkend="glib-Memory-Slices">slice
145 void g_list_pop_allocator (void) { /* present for binary compat only */ }
147 #define _g_list_alloc() g_slice_new (GList)
148 #define _g_list_alloc0() g_slice_new0 (GList)
149 #define _g_list_free1(list) g_slice_free (GList, list)
153 * @Returns: a pointer to the newly-allocated #GList element.
155 * Allocates space for one #GList element. It is called by
156 * g_list_append(), g_list_prepend(), g_list_insert() and
157 * g_list_insert_sorted() and so is rarely used on its own.
162 return _g_list_alloc0 ();
169 * Frees all of the memory used by a #GList.
170 * The freed elements are returned to the slice allocator.
173 * If list elements contain dynamically-allocated memory,
174 * they should be freed first.
178 g_list_free (GList *list)
180 g_slice_free_chain (GList, list, next);
185 * @list: a #GList element
187 * Frees one #GList element.
188 * It is usually used after g_list_remove_link().
193 * Another name for g_list_free_1().
196 g_list_free_1 (GList *list)
198 _g_list_free1 (list);
203 * @list: a pointer to a #GList
204 * @data: the data for the new element
206 * Adds a new element on to the end of the list.
209 * The return value is the new start of the list, which
210 * may have changed, so make sure you store the new value.
214 * Note that g_list_append() has to traverse the entire list
215 * to find the end, which is inefficient when adding multiple
216 * elements. A common idiom to avoid the inefficiency is to prepend
217 * the elements and reverse the list when all elements have been added.
221 * /* Notice that these are initialized to the empty list. */
222 * GList *list = NULL, *number_list = NULL;
224 * /* This is a list of strings. */
225 * list = g_list_append (list, "first");
226 * list = g_list_append (list, "second");
228 * /* This is a list of integers. */
229 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
230 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
233 * Returns: the new start of the #GList
236 g_list_append (GList *list,
242 new_list = _g_list_alloc ();
243 new_list->data = data;
244 new_list->next = NULL;
248 last = g_list_last (list);
249 /* g_assert (last != NULL); */
250 last->next = new_list;
251 new_list->prev = last;
257 new_list->prev = NULL;
264 * @list: a pointer to a #GList
265 * @data: the data for the new element
267 * Adds a new element on to the start of the list.
270 * The return value is the new start of the list, which
271 * may have changed, so make sure you store the new value.
275 * /* Notice that it is initialized to the empty list. */
276 * GList *list = NULL;
277 * list = g_list_prepend (list, "last");
278 * list = g_list_prepend (list, "first");
281 * Returns: the new start of the #GList
284 g_list_prepend (GList *list,
289 new_list = _g_list_alloc ();
290 new_list->data = data;
291 new_list->next = list;
295 new_list->prev = list->prev;
297 list->prev->next = new_list;
298 list->prev = new_list;
301 new_list->prev = NULL;
308 * @list: a pointer to a #GList
309 * @data: the data for the new element
310 * @position: the position to insert the element. If this is
311 * negative, or is larger than the number of elements in the
312 * list, the new element is added on to the end of the list.
314 * Inserts a new element into the list at the given position.
316 * Returns: the new start of the #GList
319 g_list_insert (GList *list,
327 return g_list_append (list, data);
328 else if (position == 0)
329 return g_list_prepend (list, data);
331 tmp_list = g_list_nth (list, position);
333 return g_list_append (list, data);
335 new_list = _g_list_alloc ();
336 new_list->data = data;
337 new_list->prev = tmp_list->prev;
339 tmp_list->prev->next = new_list;
340 new_list->next = tmp_list;
341 tmp_list->prev = new_list;
343 if (tmp_list == list)
350 * g_list_insert_before:
351 * @list: a pointer to a #GList
352 * @sibling: the list element before which the new element
353 * is inserted or %NULL to insert at the end of the list
354 * @data: the data for the new element
356 * Inserts a new element into the list before the given position.
358 * Returns: the new start of the #GList
361 g_list_insert_before (GList *list,
367 list = g_list_alloc ();
369 g_return_val_if_fail (sibling == NULL, list);
376 node = _g_list_alloc ();
378 node->prev = sibling->prev;
379 node->next = sibling;
380 sibling->prev = node;
383 node->prev->next = node;
388 g_return_val_if_fail (sibling == list, node);
400 last->next = _g_list_alloc ();
401 last->next->data = data;
402 last->next->prev = last;
403 last->next->next = NULL;
412 * @list2: the #GList to add to the end of the first #GList
414 * Adds the second #GList onto the end of the first #GList.
415 * Note that the elements of the second #GList are not copied.
416 * They are used directly.
418 * Returns: the start of the new #GList
421 g_list_concat (GList *list1, GList *list2)
427 tmp_list = g_list_last (list1);
429 tmp_list->next = list2;
432 list2->prev = tmp_list;
441 * @data: the data of the element to remove
443 * Removes an element from a #GList.
444 * If two elements contain the same data, only the first is removed.
445 * If none of the elements contain the data, the #GList is unchanged.
447 * Returns: the new start of the #GList
450 g_list_remove (GList *list,
458 if (tmp->data != data)
463 tmp->prev->next = tmp->next;
465 tmp->next->prev = tmp->prev;
481 * @data: data to remove
483 * Removes all list nodes with data equal to @data.
484 * Returns the new head of the list. Contrast with
485 * g_list_remove() which removes only the first node
486 * matching the given data.
488 * Returns: new head of @list
491 g_list_remove_all (GList *list,
498 if (tmp->data != data)
502 GList *next = tmp->next;
505 tmp->prev->next = next;
509 next->prev = tmp->prev;
519 _g_list_remove_link (GList *list,
525 link->prev->next = link->next;
527 link->next->prev = link->prev;
540 * g_list_remove_link:
542 * @llink: an element in the #GList
544 * Removes an element from a #GList, without freeing the element.
545 * The removed element's prev and next links are set to %NULL, so
546 * that it becomes a self-contained list with one element.
548 * Returns: the new start of the #GList, without the element
551 g_list_remove_link (GList *list,
554 return _g_list_remove_link (list, llink);
558 * g_list_delete_link:
560 * @link_: node to delete from @list
562 * Removes the node link_ from the list and frees it.
563 * Compare this to g_list_remove_link() which removes the node
564 * without freeing it.
566 * Returns: the new head of @list
569 g_list_delete_link (GList *list,
572 list = _g_list_remove_link (list, link_);
573 _g_list_free1 (link_);
585 * Note that this is a "shallow" copy. If the list elements
586 * consist of pointers to data, the pointers are copied but
587 * the actual data is not.
590 * Returns: a copy of @list
593 g_list_copy (GList *list)
595 GList *new_list = NULL;
601 new_list = _g_list_alloc ();
602 new_list->data = list->data;
603 new_list->prev = NULL;
608 last->next = _g_list_alloc ();
609 last->next->prev = last;
611 last->data = list->data;
625 * It simply switches the next and prev pointers of each element.
627 * Returns: the start of the reversed #GList
630 g_list_reverse (GList *list)
639 last->next = last->prev;
649 * @n: the position of the element, counting from 0
651 * Gets the element at the given position in a #GList.
653 * Returns: the element, or %NULL if the position is off
654 * the end of the #GList
657 g_list_nth (GList *list,
660 while ((n-- > 0) && list)
669 * @n: the position of the element, counting from 0
671 * Gets the element @n places before @list.
673 * Returns: the element, or %NULL if the position is
674 * off the end of the #GList
677 g_list_nth_prev (GList *list,
680 while ((n-- > 0) && list)
689 * @n: the position of the element
691 * Gets the data of the element at the given position.
693 * Returns: the element's data, or %NULL if the position
694 * is off the end of the #GList
697 g_list_nth_data (GList *list,
700 while ((n-- > 0) && list)
703 return list ? list->data : NULL;
709 * @data: the element data to find
711 * Finds the element in a #GList which
712 * contains the given data.
714 * Returns: the found #GList element,
715 * or %NULL if it is not found
718 g_list_find (GList *list,
723 if (list->data == data)
732 * g_list_find_custom:
734 * @data: user data passed to the function
735 * @func: the function to call for each element.
736 * It should return 0 when the desired element is found
738 * Finds an element in a #GList, using a supplied function to
739 * find the desired element. It iterates over the list, calling
740 * the given function which should return 0 when the desired
741 * element is found. The function takes two #gconstpointer arguments,
742 * the #GList element's data as the first argument and the
745 * Returns: the found #GList element, or %NULL if it is not found
748 g_list_find_custom (GList *list,
752 g_return_val_if_fail (func != NULL, list);
756 if (! func (list->data, data))
768 * @llink: an element in the #GList
770 * Gets the position of the given element
771 * in the #GList (starting from 0).
773 * Returns: the position of the element in the #GList,
774 * or -1 if the element is not found
777 g_list_position (GList *list,
797 * @data: the data to find
799 * Gets the position of the element containing
800 * the given data (starting from 0).
802 * Returns: the index of the element containing the data,
803 * or -1 if the data is not found
806 g_list_index (GList *list,
814 if (list->data == data)
827 * Gets the last element in a #GList.
829 * Returns: the last element in the #GList,
830 * or %NULL if the #GList has no elements
833 g_list_last (GList *list)
848 * Gets the first element in a #GList.
850 * Returns: the first element in the #GList,
851 * or %NULL if the #GList has no elements
854 g_list_first (GList *list)
869 * Gets the number of elements in a #GList.
872 * This function iterates over the whole list to
873 * count its elements.
876 * Returns: the number of elements in the #GList
879 g_list_length (GList *list)
896 * @func: the function to call with each element's data
897 * @user_data: user data to pass to the function
899 * Calls a function for each element of a #GList.
903 * @data: the element's data.
904 * @user_data: user data passed to g_list_foreach() or
907 * Specifies the type of functions passed to g_list_foreach() and
911 g_list_foreach (GList *list,
917 GList *next = list->next;
918 (*func) (list->data, user_data);
924 g_list_insert_sorted_real (GList *list,
929 GList *tmp_list = list;
933 g_return_val_if_fail (func != NULL, list);
937 new_list = _g_list_alloc0 ();
938 new_list->data = data;
942 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
944 while ((tmp_list->next) && (cmp > 0))
946 tmp_list = tmp_list->next;
948 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
951 new_list = _g_list_alloc0 ();
952 new_list->data = data;
954 if ((!tmp_list->next) && (cmp > 0))
956 tmp_list->next = new_list;
957 new_list->prev = tmp_list;
963 tmp_list->prev->next = new_list;
964 new_list->prev = tmp_list->prev;
966 new_list->next = tmp_list;
967 tmp_list->prev = new_list;
969 if (tmp_list == list)
976 * g_list_insert_sorted:
977 * @list: a pointer to a #GList
978 * @data: the data for the new element
979 * @func: the function to compare elements in the list. It should
980 * return a number > 0 if the first parameter comes after the
981 * second parameter in the sort order.
983 * Inserts a new element into the list, using the given comparison
984 * function to determine its position.
986 * Returns: the new start of the #GList
989 g_list_insert_sorted (GList *list,
993 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
997 * g_list_insert_sorted_with_data:
998 * @list: a pointer to a #GList
999 * @data: the data for the new element
1000 * @func: the function to compare elements in the list.
1001 * It should return a number > 0 if the first parameter
1002 * comes after the second parameter in the sort order.
1003 * @user_data: user data to pass to comparison function.
1005 * Inserts a new element into the list, using the given comparison
1006 * function to determine its position.
1008 * Returns: the new start of the #GList
1013 g_list_insert_sorted_with_data (GList *list,
1015 GCompareDataFunc func,
1018 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1022 g_list_sort_merge (GList *l1,
1027 GList list, *l, *lprev;
1035 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1051 l->next = l1 ? l1 : l2;
1058 g_list_sort_real (GList *list,
1072 while ((l2 = l2->next) != NULL)
1074 if ((l2 = l2->next) == NULL)
1081 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1082 g_list_sort_real (l2, compare_func, user_data),
1090 * @compare_func: the comparison function used to sort the #GList.
1091 * This function is passed the data from 2 elements of the #GList
1092 * and should return 0 if they are equal, a negative value if the
1093 * first element comes before the second, or a positive value if
1094 * the first element comes after the second.
1096 * Sorts a #GList using the given comparison function.
1098 * Returns: the start of the sorted #GList
1103 * @b: a value to compare with.
1104 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1107 * Specifies the type of a comparison function used to compare two
1108 * values. The function should return a negative integer if the first
1109 * value comes before the second, 0 if they are equal, or a positive
1110 * integer if the first value comes after the second.
1113 g_list_sort (GList *list,
1114 GCompareFunc compare_func)
1116 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1121 * g_list_sort_with_data:
1123 * @compare_func: comparison function
1124 * @user_data: user data to pass to comparison function
1126 * Like g_list_sort(), but the comparison function accepts
1127 * a user data argument.
1129 * Returns: the new head of @list
1134 * @b: a value to compare with.
1135 * @user_data: user data to pass to comparison function.
1136 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1139 * Specifies the type of a comparison function used to compare two
1140 * values. The function should return a negative integer if the first
1141 * value comes before the second, 0 if they are equal, or a positive
1142 * integer if the first value comes after the second.
1145 g_list_sort_with_data (GList *list,
1146 GCompareDataFunc compare_func,
1149 return g_list_sort_real (list, (GFunc) compare_func, user_data);
1152 #define __G_LIST_C__
1153 #include "galiasdef.c"