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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gmessages.h"
35 #include "gtestutils.h"
38 * SECTION:linked_lists_double
39 * @title: Doubly-Linked Lists
40 * @short_description: linked lists that can be iterated over 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 singly-linked <link
49 * linkend="glib-Singly-Linked-Lists">#GSList</link> which
50 * only allows movement through the list in the forward direction).
52 * The double linked list does not keep track of the number of items
53 * and does not keep track of both the start and end of the list. If
54 * you want fast access to both the start and the end of the list,
55 * and/or the number of items in the list, use a
56 * <link linkend="glib-Double-ended-Queues">GQueue</link> instead.
58 * The data contained in each element can be either integer values, by
59 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
60 * Conversion Macros</link>, or simply pointers to any type of data.
62 * List elements are allocated from the <link
63 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
64 * efficient than allocating elements individually.
66 * Note that most of the #GList functions expect to be passed a pointer
67 * to the first element in the list. The functions which insert
68 * elements return the new start of the list, which may have changed.
70 * There is no function to create a #GList. %NULL is considered to be
71 * a valid, empty list so you simply set a #GList* to %NULL to initialize
74 * To add elements, use g_list_append(), g_list_prepend(),
75 * g_list_insert() and g_list_insert_sorted().
77 * To visit all elements in the list, use a loop over the list:
80 * for (l = list; l != NULL; l = l->next)
82 * /* do something with l->data */
86 * To call a function for each element in the list, use g_list_foreach().
88 * To loop over the list and modify it (e.g. remove a certain element)
89 * a while loop is more appropriate, for example:
94 * GList *next = l->next;
95 * if (should_be_removed (l))
97 * /* possibly free l->data */
98 * list = g_list_delete_link (list, l);
104 * To remove elements, use g_list_remove().
106 * To navigate in a list, use g_list_first(), g_list_last(),
107 * g_list_next(), g_list_previous().
109 * To find elements in the list use g_list_nth(), g_list_nth_data(),
110 * g_list_find() and g_list_find_custom().
112 * To find the index of an element use g_list_position() and
115 * To free the entire list, use g_list_free() or g_list_free_full().
120 * @data: holds the element's data, which can be a pointer to any kind
121 * of data, or any integer value using the <link
122 * linkend="glib-Type-Conversion-Macros">Type Conversion
124 * @next: contains the link to the next element in the list
125 * @prev: contains the link to the previous element in the list
127 * The #GList struct is used for each element in a doubly-linked list.
132 * @list: an element in a #GList
134 * A convenience macro to get the previous element in a #GList.
135 * Note that it is considered perfectly acceptable to access
136 * @list->previous directly.
138 * Returns: the previous element, or %NULL if there are no previous
144 * @list: an element in a #GList
146 * A convenience macro to get the next element in a #GList.
147 * Note that it is considered perfectly acceptable to access
148 * @list->next directly.
150 * Returns: the next element, or %NULL if there are no more elements
153 #define _g_list_alloc() g_slice_new (GList)
154 #define _g_list_alloc0() g_slice_new0 (GList)
155 #define _g_list_free1(list) g_slice_free (GList, list)
160 * Allocates space for one #GList element. It is called by
161 * g_list_append(), g_list_prepend(), g_list_insert() and
162 * g_list_insert_sorted() and so is rarely used on its own.
164 * Returns: a pointer to the newly-allocated #GList element
169 return _g_list_alloc0 ();
176 * Frees all of the memory used by a #GList.
177 * The freed elements are returned to the slice allocator
180 * If list elements contain dynamically-allocated memory,
181 * you should either use g_list_free_full() or free them manually
186 g_list_free (GList *list)
188 g_slice_free_chain (GList, list, next);
193 * @list: a #GList element
195 * Frees one #GList element.
196 * It is usually used after g_list_remove_link().
201 * Another name for g_list_free_1().
204 g_list_free_1 (GList *list)
206 _g_list_free1 (list);
211 * @list: a pointer to a #GList
212 * @free_func: the function to be called to free each element's data
214 * Convenience method, which frees all the memory used by a #GList,
215 * and calls @free_func on every element's data.
220 g_list_free_full (GList *list,
221 GDestroyNotify free_func)
223 g_list_foreach (list, (GFunc) free_func, NULL);
229 * @list: a pointer to a #GList
230 * @data: the data for the new element
232 * Adds a new element on to the end of the list.
234 * Note that the return value is the new start of the list,
235 * if @list was empty; make sure you store the new value.
237 * g_list_append() has to traverse the entire list to find the end,
238 * which is inefficient when adding multiple elements. A common idiom
239 * to avoid the inefficiency is to use g_list_prepend() and reverse
240 * the list with g_list_reverse() when all elements have been added.
243 * /* Notice that these are initialized to the empty list. */
244 * GList *string_list = NULL, *number_list = NULL;
246 * /* This is a list of strings. */
247 * string_list = g_list_append (string_list, "first");
248 * string_list = g_list_append (string_list, "second");
250 * /* This is a list of integers. */
251 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
252 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
255 * Returns: either @list or the new start of the #GList if @list was %NULL
258 g_list_append (GList *list,
264 new_list = _g_list_alloc ();
265 new_list->data = data;
266 new_list->next = NULL;
270 last = g_list_last (list);
271 /* g_assert (last != NULL); */
272 last->next = new_list;
273 new_list->prev = last;
279 new_list->prev = NULL;
286 * @list: a pointer to a #GList, this must point to the top of the list
287 * @data: the data for the new element
289 * Prepends a new element on to the start of the list.
291 * Note that the return value is the new start of the list,
292 * which will have changed, so make sure you store the new value.
295 * /* Notice that it is initialized to the empty list. */
296 * GList *list = NULL;
298 * list = g_list_prepend (list, "last");
299 * list = g_list_prepend (list, "first");
303 * Do not use this function to prepend a new element to a different element
304 * than the start of the list. Use g_list_insert_before() instead.
307 * Returns: a pointer to the newly prepended element, which is the new
308 * start of the #GList
311 g_list_prepend (GList *list,
316 new_list = _g_list_alloc ();
317 new_list->data = data;
318 new_list->next = list;
322 new_list->prev = list->prev;
324 list->prev->next = new_list;
325 list->prev = new_list;
328 new_list->prev = NULL;
335 * @list: a pointer to a #GList, this must point to the top of the list
336 * @data: the data for the new element
337 * @position: the position to insert the element. If this is
338 * negative, or is larger than the number of elements in the
339 * list, the new element is added on to the end of the list.
341 * Inserts a new element into the list at the given position.
343 * Returns: the (possibly changed) start of the #GList
346 g_list_insert (GList *list,
354 return g_list_append (list, data);
355 else if (position == 0)
356 return g_list_prepend (list, data);
358 tmp_list = g_list_nth (list, position);
360 return g_list_append (list, data);
362 new_list = _g_list_alloc ();
363 new_list->data = data;
364 new_list->prev = tmp_list->prev;
365 tmp_list->prev->next = new_list;
366 new_list->next = tmp_list;
367 tmp_list->prev = new_list;
373 * g_list_insert_before:
374 * @list: a pointer to a #GList, this must point to the top of the list
375 * @sibling: the list element before which the new element
376 * is inserted or %NULL to insert at the end of the list
377 * @data: the data for the new element
379 * Inserts a new element into the list before the given position.
381 * Returns: the (possibly changed) start of the #GList
384 g_list_insert_before (GList *list,
390 list = g_list_alloc ();
392 g_return_val_if_fail (sibling == NULL, list);
399 node = _g_list_alloc ();
401 node->prev = sibling->prev;
402 node->next = sibling;
403 sibling->prev = node;
406 node->prev->next = node;
411 g_return_val_if_fail (sibling == list, node);
423 last->next = _g_list_alloc ();
424 last->next->data = data;
425 last->next->prev = last;
426 last->next->next = NULL;
434 * @list1: a #GList, this must point to the top of the list
435 * @list2: the #GList to add to the end of the first #GList,
436 * this must point to the top of the list
438 * Adds the second #GList onto the end of the first #GList.
439 * Note that the elements of the second #GList are not copied.
440 * They are used directly.
442 * This function is for example used to move an element in the list.
443 * The following example moves an element to the top of the list:
445 * list = g_list_remove_link (list, llink);
446 * list = g_list_concat (llink, list);
449 * Returns: the start of the new #GList, which equals @list1 if not %NULL
452 g_list_concat (GList *list1,
459 tmp_list = g_list_last (list1);
461 tmp_list->next = list2;
464 list2->prev = tmp_list;
470 static inline GList *
471 _g_list_remove_link (GList *list,
479 if (link->prev->next == link)
480 link->prev->next = link->next;
482 g_warning ("corrupted double-linked list detected");
486 if (link->next->prev == link)
487 link->next->prev = link->prev;
489 g_warning ("corrupted double-linked list detected");
503 * @list: a #GList, this must point to the top of the list
504 * @data: the data of the element to remove
506 * Removes an element from a #GList.
507 * If two elements contain the same data, only the first is removed.
508 * If none of the elements contain the data, the #GList is unchanged.
510 * Returns: the (possibly changed) start of the #GList
513 g_list_remove (GList *list,
521 if (tmp->data != data)
525 list = _g_list_remove_link (list, tmp);
536 * @list: a #GList, this must point to the top of the list
537 * @data: data to remove
539 * Removes all list nodes with data equal to @data.
540 * Returns the new head of the list. Contrast with
541 * g_list_remove() which removes only the first node
542 * matching the given data.
544 * Returns: the (possibly changed) start of the #GList
547 g_list_remove_all (GList *list,
554 if (tmp->data != data)
558 GList *next = tmp->next;
561 tmp->prev->next = next;
565 next->prev = tmp->prev;
575 * g_list_remove_link:
576 * @list: a #GList, this must point to the top of the list
577 * @llink: an element in the #GList
579 * Removes an element from a #GList, without freeing the element.
580 * The removed element's prev and next links are set to %NULL, so
581 * that it becomes a self-contained list with one element.
583 * This function is for example used to move an element in the list
584 * (see the example for g_list_concat()) or to remove an element in
585 * the list before freeing its data:
587 * list = g_list_remove_link (list, llink);
588 * free_some_data_that_may_access_the_list_again (llink->data);
589 * g_list_free (llink);
592 * Returns: the (possibly changed) start of the #GList
595 g_list_remove_link (GList *list,
598 return _g_list_remove_link (list, llink);
602 * g_list_delete_link:
603 * @list: a #GList, this must point to the top of the list
604 * @link_: node to delete from @list
606 * Removes the node link_ from the list and frees it.
607 * Compare this to g_list_remove_link() which removes the node
608 * without freeing it.
610 * Returns: the (possibly changed) start of the #GList
613 g_list_delete_link (GList *list,
616 list = _g_list_remove_link (list, link_);
617 _g_list_free1 (link_);
624 * @list: a #GList, this must point to the top of the list
629 * Note that this is a "shallow" copy. If the list elements
630 * consist of pointers to data, the pointers are copied but
631 * the actual data is not. See g_list_copy_deep() if you need
632 * to copy the data as well.
635 * Returns: the start of the new list that holds the same data as @list
638 g_list_copy (GList *list)
640 return g_list_copy_deep (list, NULL, NULL);
645 * @list: a #GList, this must point to the top of the list
646 * @func: a copy function used to copy every element in the list
647 * @user_data: user data passed to the copy function @func, or %NULL
649 * Makes a full (deep) copy of a #GList.
651 * In contrast with g_list_copy(), this function uses @func to make
652 * a copy of each list element, in addition to copying the list
655 * @func, as a #GCopyFunc, takes two arguments, the data to be copied
656 * and a @user_data pointer. It's safe to pass %NULL as user_data,
657 * if the copy function takes only one argument.
659 * For instance, if @list holds a list of GObjects, you can do:
661 * another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
664 * And, to entirely free the new list, you could do:
666 * g_list_free_full (another_list, g_object_unref);
669 * Returns: the start of the new list that holds a full copy of @list,
670 * use g_list_free_full() to free it
675 g_list_copy_deep (GList *list,
679 GList *new_list = NULL;
685 new_list = _g_list_alloc ();
687 new_list->data = func (list->data, user_data);
689 new_list->data = list->data;
690 new_list->prev = NULL;
695 last->next = _g_list_alloc ();
696 last->next->prev = last;
699 last->data = func (list->data, user_data);
701 last->data = list->data;
712 * @list: a #GList, this must point to the top of the list
715 * It simply switches the next and prev pointers of each element.
717 * Returns: the start of the reversed #GList
720 g_list_reverse (GList *list)
729 last->next = last->prev;
738 * @list: a #GList, this must point to the top of the list
739 * @n: the position of the element, counting from 0
741 * Gets the element at the given position in a #GList.
743 * Returns: the element, or %NULL if the position is off
744 * the end of the #GList
747 g_list_nth (GList *list,
750 while ((n-- > 0) && list)
759 * @n: the position of the element, counting from 0
761 * Gets the element @n places before @list.
763 * Returns: the element, or %NULL if the position is
764 * off the end of the #GList
767 g_list_nth_prev (GList *list,
770 while ((n-- > 0) && list)
778 * @list: a #GList, this must point to the top of the list
779 * @n: the position of the element
781 * Gets the data of the element at the given position.
783 * Returns: the element's data, or %NULL if the position
784 * is off the end of the #GList
787 g_list_nth_data (GList *list,
790 while ((n-- > 0) && list)
793 return list ? list->data : NULL;
798 * @list: a #GList, this must point to the top of the list
799 * @data: the element data to find
801 * Finds the element in a #GList which contains the given data.
803 * Returns: the found #GList element, or %NULL if it is not found
806 g_list_find (GList *list,
811 if (list->data == data)
820 * g_list_find_custom:
821 * @list: a #GList, this must point to the top of the list
822 * @data: user data passed to the function
823 * @func: the function to call for each element.
824 * It should return 0 when the desired element is found
826 * Finds an element in a #GList, using a supplied function to
827 * find the desired element. It iterates over the list, calling
828 * the given function which should return 0 when the desired
829 * element is found. The function takes two #gconstpointer arguments,
830 * the #GList element's data as the first argument and the
833 * Returns: the found #GList element, or %NULL if it is not found
836 g_list_find_custom (GList *list,
840 g_return_val_if_fail (func != NULL, list);
844 if (! func (list->data, data))
854 * @list: a #GList, this must point to the top of the list
855 * @llink: an element in the #GList
857 * Gets the position of the given element
858 * in the #GList (starting from 0).
860 * Returns: the position of the element in the #GList,
861 * or -1 if the element is not found
864 g_list_position (GList *list,
883 * @list: a #GList, this must point to the top of the list
884 * @data: the data to find
886 * Gets the position of the element containing
887 * the given data (starting from 0).
889 * Returns: the index of the element containing the data,
890 * or -1 if the data is not found
893 g_list_index (GList *list,
901 if (list->data == data)
912 * @list: any #GList element
914 * Gets the last element in a #GList.
916 * Returns: the last element in the #GList,
917 * or %NULL if the #GList has no elements
920 g_list_last (GList *list)
933 * @list: any #GList element
935 * Gets the first element in a #GList.
937 * Returns: the first element in the #GList,
938 * or %NULL if the #GList has no elements
941 g_list_first (GList *list)
954 * @list: a #GList, this must point to the top of the list
956 * Gets the number of elements in a #GList.
959 * This function iterates over the whole list to count its elements.
960 * Use a <link linkend="glib-Double-ended-Queues">GQueue</link> instead
961 * of a GList if you regularly need the number of items.
964 * Returns: the number of elements in the #GList
967 g_list_length (GList *list)
983 * @list: a #GList, this must point to the top of the list
984 * @func: the function to call with each element's data
985 * @user_data: user data to pass to the function
987 * Calls a function for each element of a #GList.
991 * @data: the element's data
992 * @user_data: user data passed to g_list_foreach() or g_slist_foreach()
994 * Specifies the type of functions passed to g_list_foreach() and
998 g_list_foreach (GList *list,
1004 GList *next = list->next;
1005 (*func) (list->data, user_data);
1011 g_list_insert_sorted_real (GList *list,
1016 GList *tmp_list = list;
1020 g_return_val_if_fail (func != NULL, list);
1024 new_list = _g_list_alloc0 ();
1025 new_list->data = data;
1029 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
1031 while ((tmp_list->next) && (cmp > 0))
1033 tmp_list = tmp_list->next;
1035 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
1038 new_list = _g_list_alloc0 ();
1039 new_list->data = data;
1041 if ((!tmp_list->next) && (cmp > 0))
1043 tmp_list->next = new_list;
1044 new_list->prev = tmp_list;
1050 tmp_list->prev->next = new_list;
1051 new_list->prev = tmp_list->prev;
1053 new_list->next = tmp_list;
1054 tmp_list->prev = new_list;
1056 if (tmp_list == list)
1063 * g_list_insert_sorted:
1064 * @list: a pointer to a #GList, this must point to the top of the
1065 * already sorted list
1066 * @data: the data for the new element
1067 * @func: the function to compare elements in the list. It should
1068 * return a number > 0 if the first parameter comes after the
1069 * second parameter in the sort order.
1071 * Inserts a new element into the list, using the given comparison
1072 * function to determine its position.
1075 * If you are adding many new elements to a list, and the number of
1076 * new elements is much larger than the length of the list, use
1077 * g_list_prepend() to add the new items and sort the list afterwards
1078 * with g_list_sort()
1081 * Returns: the (possibly changed) start of the #GList
1084 g_list_insert_sorted (GList *list,
1088 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
1092 * g_list_insert_sorted_with_data:
1093 * @list: a pointer to a #GList, this must point to the top of the
1094 * already sorted list
1095 * @data: the data for the new element
1096 * @func: the function to compare elements in the list. It should
1097 * return a number > 0 if the first parameter comes after the
1098 * second parameter in the sort order.
1099 * @user_data: user data to pass to comparison function
1101 * Inserts a new element into the list, using the given comparison
1102 * function to determine its position.
1105 * If you are adding many new elements to a list, and the number of
1106 * new elements is much larger than the length of the list, use
1107 * g_list_prepend() to add the new items and sort the list afterwards
1108 * with g_list_sort()
1111 * Returns: the (possibly changed) start of the #GList
1116 g_list_insert_sorted_with_data (GList *list,
1118 GCompareDataFunc func,
1121 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1125 g_list_sort_merge (GList *l1,
1130 GList list, *l, *lprev;
1138 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1154 l->next = l1 ? l1 : l2;
1161 g_list_sort_real (GList *list,
1175 while ((l2 = l2->next) != NULL)
1177 if ((l2 = l2->next) == NULL)
1184 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1185 g_list_sort_real (l2, compare_func, user_data),
1192 * @list: a #GList, this must point to the top of the list
1193 * @compare_func: the comparison function used to sort the #GList.
1194 * This function is passed the data from 2 elements of the #GList
1195 * and should return 0 if they are equal, a negative value if the
1196 * first element comes before the second, or a positive value if
1197 * the first element comes after the second.
1199 * Sorts a #GList using the given comparison function. The algorithm
1200 * used is a stable sort.
1202 * Returns: the (possibly changed) start of the #GList
1207 * @b: a value to compare with
1209 * Specifies the type of a comparison function used to compare two
1210 * values. The function should return a negative integer if the first
1211 * value comes before the second, 0 if they are equal, or a positive
1212 * integer if the first value comes after the second.
1214 * Returns: negative value if @a < @b; zero if @a = @b; positive
1218 g_list_sort (GList *list,
1219 GCompareFunc compare_func)
1221 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1225 * g_list_sort_with_data:
1226 * @list: a #GList, this must point to the top of the list
1227 * @compare_func: comparison function
1228 * @user_data: user data to pass to comparison function
1230 * Like g_list_sort(), but the comparison function accepts
1231 * a user data argument.
1233 * Returns: the (possibly changed) start of the #GList
1238 * @b: a value to compare with
1239 * @user_data: user data
1241 * Specifies the type of a comparison function used to compare two
1242 * values. The function should return a negative integer if the first
1243 * value comes before the second, 0 if they are equal, or a positive
1244 * integer if the first value comes after the second.
1246 * Returns: negative value if @a < @b; zero if @a = @b; positive
1250 g_list_sort_with_data (GList *list,
1251 GCompareDataFunc compare_func,
1254 return g_list_sort_real (list, (GFunc) compare_func, user_data);