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/.
35 #include "gmessages.h"
37 #include "gtestutils.h"
40 * SECTION:linked_lists_double
41 * @title: Doubly-Linked Lists
42 * @short_description: linked lists that can be iterated over in both directions
44 * The #GList structure and its associated functions provide a standard
45 * doubly-linked list data structure.
47 * Each element in the list contains a piece of data, together with
48 * pointers which link to the previous and next elements in the list.
49 * Using these pointers it is possible to move through the list in both
50 * directions (unlike the singly-linked <link
51 * linkend="glib-Singly-Linked-Lists">#GSList</link> which
52 * only allows movement through the list in the forward direction).
54 * The double linked list does not keep track of the number of items
55 * and does not keep track of both the start and end of the list. If
56 * you want fast access to both the start and the end of the list,
57 * and/or the number of items in the list, use a
58 * <link linkend="glib-Double-ended-Queues">GQueue</link> instead.
60 * The data contained in each element can be either integer values, by
61 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
62 * Conversion Macros</link>, or simply pointers to any type of data.
64 * List elements are allocated from the <link
65 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
66 * efficient than allocating elements individually.
68 * Note that most of the #GList functions expect to be passed a pointer
69 * to the first element in the list. The functions which insert
70 * elements return the new start of the list, which may have changed.
72 * There is no function to create a #GList. %NULL is considered to be
73 * a valid, empty list so you simply set a #GList* to %NULL to initialize
76 * To add elements, use g_list_append(), g_list_prepend(),
77 * g_list_insert() and g_list_insert_sorted().
79 * To visit all elements in the list, use a loop over the list:
82 * for (l = list; l != NULL; l = l->next)
84 * /* do something with l->data */
88 * To call a function for each element in the list, use g_list_foreach().
90 * To loop over the list and modify it (e.g. remove a certain element)
91 * a while loop is more appropriate, for example:
96 * GList *next = l->next;
97 * if (should_be_removed (l))
99 * /* possibly free l->data */
100 * list = g_list_delete_link (list, l);
106 * To remove elements, use g_list_remove().
108 * To navigate in a list, use g_list_first(), g_list_last(),
109 * g_list_next(), g_list_previous().
111 * To find elements in the list use g_list_nth(), g_list_nth_data(),
112 * g_list_find() and g_list_find_custom().
114 * To find the index of an element use g_list_position() and
117 * To free the entire list, use g_list_free() or g_list_free_full().
122 * @data: holds the element's data, which can be a pointer to any kind
123 * of data, or any integer value using the <link
124 * linkend="glib-Type-Conversion-Macros">Type Conversion
126 * @next: contains the link to the next element in the list
127 * @prev: contains the link to the previous element in the list
129 * The #GList struct is used for each element in a doubly-linked list.
134 * @list: an element in a #GList
136 * A convenience macro to get the previous element in a #GList.
137 * Note that it is considered perfectly acceptable to access
138 * @list->previous directly.
140 * Returns: the previous element, or %NULL if there are no previous
146 * @list: an element in a #GList
148 * A convenience macro to get the next element in a #GList.
149 * Note that it is considered perfectly acceptable to access
150 * @list->next directly.
152 * Returns: the next element, or %NULL if there are no more elements
155 #define _g_list_alloc() g_slice_new (GList)
156 #define _g_list_alloc0() g_slice_new0 (GList)
157 #define _g_list_free1(list) g_slice_free (GList, list)
162 * Allocates space for one #GList element. It is called by
163 * g_list_append(), g_list_prepend(), g_list_insert() and
164 * g_list_insert_sorted() and so is rarely used on its own.
166 * Returns: a pointer to the newly-allocated #GList element
171 return _g_list_alloc0 ();
178 * Frees all of the memory used by a #GList.
179 * The freed elements are returned to the slice allocator
182 * If list elements contain dynamically-allocated memory,
183 * you should either use g_list_free_full() or free them manually
188 g_list_free (GList *list)
190 g_slice_free_chain (GList, list, next);
195 * @list: a #GList element
197 * Frees one #GList element.
198 * It is usually used after g_list_remove_link().
203 * Another name for g_list_free_1().
206 g_list_free_1 (GList *list)
208 _g_list_free1 (list);
213 * @list: a pointer to a #GList
214 * @free_func: the function to be called to free each element's data
216 * Convenience method, which frees all the memory used by a #GList,
217 * and calls @free_func on every element's data.
222 g_list_free_full (GList *list,
223 GDestroyNotify free_func)
225 g_list_foreach (list, (GFunc) free_func, NULL);
231 * @list: a pointer to a #GList
232 * @data: the data for the new element
234 * Adds a new element on to the end of the list.
236 * Note that the return value is the new start of the list,
237 * if @list was empty; make sure you store the new value.
239 * g_list_append() has to traverse the entire list to find the end,
240 * which is inefficient when adding multiple elements. A common idiom
241 * to avoid the inefficiency is to use g_list_prepend() and reverse
242 * the list with g_list_reverse() when all elements have been added.
245 * /* Notice that these are initialized to the empty list. */
246 * GList *string_list = NULL, *number_list = NULL;
248 * /* This is a list of strings. */
249 * string_list = g_list_append (string_list, "first");
250 * string_list = g_list_append (string_list, "second");
252 * /* This is a list of integers. */
253 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
254 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
257 * Returns: either @list or the new start of the #GList if @list was %NULL
260 g_list_append (GList *list,
266 new_list = _g_list_alloc ();
267 new_list->data = data;
268 new_list->next = NULL;
272 last = g_list_last (list);
273 /* g_assert (last != NULL); */
274 last->next = new_list;
275 new_list->prev = last;
281 new_list->prev = NULL;
288 * @list: a pointer to a #GList, this must point to the top of the list
289 * @data: the data for the new element
291 * Prepends a new element on to the start of the list.
293 * Note that the return value is the new start of the list,
294 * which will have changed, so make sure you store the new value.
297 * /* Notice that it is initialized to the empty list. */
298 * GList *list = NULL;
300 * list = g_list_prepend (list, "last");
301 * list = g_list_prepend (list, "first");
305 * Do not use this function to prepend a new element to a different element
306 * than the start of the list. Use g_list_insert_before() instead.
309 * Returns: a pointer to the newly prepended element, which is the new
310 * start of the #GList
313 g_list_prepend (GList *list,
318 new_list = _g_list_alloc ();
319 new_list->data = data;
320 new_list->next = list;
324 new_list->prev = list->prev;
326 list->prev->next = new_list;
327 list->prev = new_list;
330 new_list->prev = NULL;
337 * @list: a pointer to a #GList, this must point to the top of the list
338 * @data: the data for the new element
339 * @position: the position to insert the element. If this is
340 * negative, or is larger than the number of elements in the
341 * list, the new element is added on to the end of the list.
343 * Inserts a new element into the list at the given position.
345 * Returns: the (possibly changed) start of the #GList
348 g_list_insert (GList *list,
356 return g_list_append (list, data);
357 else if (position == 0)
358 return g_list_prepend (list, data);
360 tmp_list = g_list_nth (list, position);
362 return g_list_append (list, data);
364 new_list = _g_list_alloc ();
365 new_list->data = data;
366 new_list->prev = tmp_list->prev;
367 tmp_list->prev->next = new_list;
368 new_list->next = tmp_list;
369 tmp_list->prev = new_list;
375 * g_list_insert_before:
376 * @list: a pointer to a #GList, this must point to the top of the list
377 * @sibling: the list element before which the new element
378 * is inserted or %NULL to insert at the end of the list
379 * @data: the data for the new element
381 * Inserts a new element into the list before the given position.
383 * Returns: the (possibly changed) start of the #GList
386 g_list_insert_before (GList *list,
392 list = g_list_alloc ();
394 g_return_val_if_fail (sibling == NULL, list);
401 node = _g_list_alloc ();
403 node->prev = sibling->prev;
404 node->next = sibling;
405 sibling->prev = node;
408 node->prev->next = node;
413 g_return_val_if_fail (sibling == list, node);
425 last->next = _g_list_alloc ();
426 last->next->data = data;
427 last->next->prev = last;
428 last->next->next = NULL;
436 * @list1: a #GList, this must point to the top of the list
437 * @list2: the #GList to add to the end of the first #GList,
438 * this must point to the top of the list
440 * Adds the second #GList onto the end of the first #GList.
441 * Note that the elements of the second #GList are not copied.
442 * They are used directly.
444 * This function is for example used to move an element in the list.
445 * The following example moves an element to the top of the list:
447 * list = g_list_remove_link (list, llink);
448 * list = g_list_concat (llink, list);
451 * Returns: the start of the new #GList, which equals @list1 if not %NULL
454 g_list_concat (GList *list1,
461 tmp_list = g_list_last (list1);
463 tmp_list->next = list2;
466 list2->prev = tmp_list;
472 static inline GList *
473 _g_list_remove_link (GList *list,
481 if (link->prev->next == link)
482 link->prev->next = link->next;
484 g_warning ("corrupted double-linked list detected");
488 if (link->next->prev == link)
489 link->next->prev = link->prev;
491 g_warning ("corrupted double-linked list detected");
505 * @list: a #GList, this must point to the top of the list
506 * @data: the data of the element to remove
508 * Removes an element from a #GList.
509 * If two elements contain the same data, only the first is removed.
510 * If none of the elements contain the data, the #GList is unchanged.
512 * Returns: the (possibly changed) start of the #GList
515 g_list_remove (GList *list,
523 if (tmp->data != data)
527 list = _g_list_remove_link (list, tmp);
538 * @list: a #GList, this must point to the top of the list
539 * @data: data to remove
541 * Removes all list nodes with data equal to @data.
542 * Returns the new head of the list. Contrast with
543 * g_list_remove() which removes only the first node
544 * matching the given data.
546 * Returns: the (possibly changed) start of the #GList
549 g_list_remove_all (GList *list,
556 if (tmp->data != data)
560 GList *next = tmp->next;
563 tmp->prev->next = next;
567 next->prev = tmp->prev;
577 * g_list_remove_link:
578 * @list: a #GList, this must point to the top of the list
579 * @llink: an element in the #GList
581 * Removes an element from a #GList, without freeing the element.
582 * The removed element's prev and next links are set to %NULL, so
583 * that it becomes a self-contained list with one element.
585 * This function is for example used to move an element in the list
586 * (see the example for g_list_concat()) or to remove an element in
587 * the list before freeing its data:
589 * list = g_list_remove_link (list, llink);
590 * free_some_data_that_may_access_the_list_again (llink->data);
591 * g_list_free (llink);
594 * Returns: the (possibly changed) start of the #GList
597 g_list_remove_link (GList *list,
600 return _g_list_remove_link (list, llink);
604 * g_list_delete_link:
605 * @list: a #GList, this must point to the top of the list
606 * @link_: node to delete from @list
608 * Removes the node link_ from the list and frees it.
609 * Compare this to g_list_remove_link() which removes the node
610 * without freeing it.
612 * Returns: the (possibly changed) start of the #GList
615 g_list_delete_link (GList *list,
618 list = _g_list_remove_link (list, link_);
619 _g_list_free1 (link_);
626 * @list: a #GList, this must point to the top of the list
631 * Note that this is a "shallow" copy. If the list elements
632 * consist of pointers to data, the pointers are copied but
633 * the actual data is not. See g_list_copy_deep() if you need
634 * to copy the data as well.
637 * Returns: the start of the new list that holds the same data as @list
640 g_list_copy (GList *list)
642 return g_list_copy_deep (list, NULL, NULL);
647 * @list: a #GList, this must point to the top of the list
648 * @func: a copy function used to copy every element in the list
649 * @user_data: user data passed to the copy function @func, or %NULL
651 * Makes a full (deep) copy of a #GList.
653 * In contrast with g_list_copy(), this function uses @func to make
654 * a copy of each list element, in addition to copying the list
657 * @func, as a #GCopyFunc, takes two arguments, the data to be copied
658 * and a @user_data pointer. It's safe to pass %NULL as user_data,
659 * if the copy function takes only one argument.
661 * For instance, if @list holds a list of GObjects, you can do:
663 * another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
666 * And, to entirely free the new list, you could do:
668 * g_list_free_full (another_list, g_object_unref);
671 * Returns: the start of the new list that holds a full copy of @list,
672 * use g_list_free_full() to free it
677 g_list_copy_deep (GList *list,
681 GList *new_list = NULL;
687 new_list = _g_list_alloc ();
689 new_list->data = func (list->data, user_data);
691 new_list->data = list->data;
692 new_list->prev = NULL;
697 last->next = _g_list_alloc ();
698 last->next->prev = last;
701 last->data = func (list->data, user_data);
703 last->data = list->data;
714 * @list: a #GList, this must point to the top of the list
717 * It simply switches the next and prev pointers of each element.
719 * Returns: the start of the reversed #GList
722 g_list_reverse (GList *list)
731 last->next = last->prev;
740 * @list: a #GList, this must point to the top of the list
741 * @n: the position of the element, counting from 0
743 * Gets the element at the given position in a #GList.
745 * Returns: the element, or %NULL if the position is off
746 * the end of the #GList
749 g_list_nth (GList *list,
752 while ((n-- > 0) && list)
761 * @n: the position of the element, counting from 0
763 * Gets the element @n places before @list.
765 * Returns: the element, or %NULL if the position is
766 * off the end of the #GList
769 g_list_nth_prev (GList *list,
772 while ((n-- > 0) && list)
780 * @list: a #GList, this must point to the top of the list
781 * @n: the position of the element
783 * Gets the data of the element at the given position.
785 * Returns: the element's data, or %NULL if the position
786 * is off the end of the #GList
789 g_list_nth_data (GList *list,
792 while ((n-- > 0) && list)
795 return list ? list->data : NULL;
800 * @list: a #GList, this must point to the top of the list
801 * @data: the element data to find
803 * Finds the element in a #GList which contains the given data.
805 * Returns: the found #GList element, or %NULL if it is not found
808 g_list_find (GList *list,
813 if (list->data == data)
822 * g_list_find_custom:
823 * @list: a #GList, this must point to the top of the list
824 * @data: user data passed to the function
825 * @func: the function to call for each element.
826 * It should return 0 when the desired element is found
828 * Finds an element in a #GList, using a supplied function to
829 * find the desired element. It iterates over the list, calling
830 * the given function which should return 0 when the desired
831 * element is found. The function takes two #gconstpointer arguments,
832 * the #GList element's data as the first argument and the
835 * Returns: the found #GList element, or %NULL if it is not found
838 g_list_find_custom (GList *list,
842 g_return_val_if_fail (func != NULL, list);
846 if (! func (list->data, data))
856 * @list: a #GList, this must point to the top of the list
857 * @llink: an element in the #GList
859 * Gets the position of the given element
860 * in the #GList (starting from 0).
862 * Returns: the position of the element in the #GList,
863 * or -1 if the element is not found
866 g_list_position (GList *list,
885 * @list: a #GList, this must point to the top of the list
886 * @data: the data to find
888 * Gets the position of the element containing
889 * the given data (starting from 0).
891 * Returns: the index of the element containing the data,
892 * or -1 if the data is not found
895 g_list_index (GList *list,
903 if (list->data == data)
914 * @list: any #GList element
916 * Gets the last element in a #GList.
918 * Returns: the last element in the #GList,
919 * or %NULL if the #GList has no elements
922 g_list_last (GList *list)
935 * @list: any #GList element
937 * Gets the first element in a #GList.
939 * Returns: the first element in the #GList,
940 * or %NULL if the #GList has no elements
943 g_list_first (GList *list)
956 * @list: a #GList, this must point to the top of the list
958 * Gets the number of elements in a #GList.
961 * This function iterates over the whole list to count its elements.
962 * Use a <link linkend="glib-Double-ended-Queues">GQueue</link> instead
963 * of a GList if you regularly need the number of items.
966 * Returns: the number of elements in the #GList
969 g_list_length (GList *list)
985 * @list: a #GList, this must point to the top of the list
986 * @func: the function to call with each element's data
987 * @user_data: user data to pass to the function
989 * Calls a function for each element of a #GList.
993 * @data: the element's data
994 * @user_data: user data passed to g_list_foreach() or g_slist_foreach()
996 * Specifies the type of functions passed to g_list_foreach() and
1000 g_list_foreach (GList *list,
1006 GList *next = list->next;
1007 (*func) (list->data, user_data);
1013 g_list_insert_sorted_real (GList *list,
1018 GList *tmp_list = list;
1022 g_return_val_if_fail (func != NULL, list);
1026 new_list = _g_list_alloc0 ();
1027 new_list->data = data;
1031 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
1033 while ((tmp_list->next) && (cmp > 0))
1035 tmp_list = tmp_list->next;
1037 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
1040 new_list = _g_list_alloc0 ();
1041 new_list->data = data;
1043 if ((!tmp_list->next) && (cmp > 0))
1045 tmp_list->next = new_list;
1046 new_list->prev = tmp_list;
1052 tmp_list->prev->next = new_list;
1053 new_list->prev = tmp_list->prev;
1055 new_list->next = tmp_list;
1056 tmp_list->prev = new_list;
1058 if (tmp_list == list)
1065 * g_list_insert_sorted:
1066 * @list: a pointer to a #GList, this must point to the top of the
1067 * already sorted list
1068 * @data: the data for the new element
1069 * @func: the function to compare elements in the list. It should
1070 * return a number > 0 if the first parameter comes after the
1071 * second parameter in the sort order.
1073 * Inserts a new element into the list, using the given comparison
1074 * function to determine its position.
1077 * If you are adding many new elements to a list, and the number of
1078 * new elements is much larger than the length of the list, use
1079 * g_list_prepend() to add the new items and sort the list afterwards
1080 * with g_list_sort()
1083 * Returns: the (possibly changed) start of the #GList
1086 g_list_insert_sorted (GList *list,
1090 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
1094 * g_list_insert_sorted_with_data:
1095 * @list: a pointer to a #GList, this must point to the top of the
1096 * already sorted list
1097 * @data: the data for the new element
1098 * @func: the function to compare elements in the list. It should
1099 * return a number > 0 if the first parameter comes after the
1100 * second parameter in the sort order.
1101 * @user_data: user data to pass to comparison function
1103 * Inserts a new element into the list, using the given comparison
1104 * function to determine its position.
1107 * If you are adding many new elements to a list, and the number of
1108 * new elements is much larger than the length of the list, use
1109 * g_list_prepend() to add the new items and sort the list afterwards
1110 * with g_list_sort()
1113 * Returns: the (possibly changed) start of the #GList
1118 g_list_insert_sorted_with_data (GList *list,
1120 GCompareDataFunc func,
1123 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1127 g_list_sort_merge (GList *l1,
1132 GList list, *l, *lprev;
1140 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1156 l->next = l1 ? l1 : l2;
1163 g_list_sort_real (GList *list,
1177 while ((l2 = l2->next) != NULL)
1179 if ((l2 = l2->next) == NULL)
1186 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1187 g_list_sort_real (l2, compare_func, user_data),
1194 * @list: a #GList, this must point to the top of the list
1195 * @compare_func: the comparison function used to sort the #GList.
1196 * This function is passed the data from 2 elements of the #GList
1197 * and should return 0 if they are equal, a negative value if the
1198 * first element comes before the second, or a positive value if
1199 * the first element comes after the second.
1201 * Sorts a #GList using the given comparison function. The algorithm
1202 * used is a stable sort.
1204 * Returns: the (possibly changed) start of the #GList
1209 * @b: a value to compare with
1211 * Specifies the type of a comparison function used to compare two
1212 * values. The function should return a negative integer if the first
1213 * value comes before the second, 0 if they are equal, or a positive
1214 * integer if the first value comes after the second.
1216 * Returns: negative value if @a < @b; zero if @a = @b; positive
1220 g_list_sort (GList *list,
1221 GCompareFunc compare_func)
1223 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1227 * g_list_sort_with_data:
1228 * @list: a #GList, this must point to the top of the list
1229 * @compare_func: comparison function
1230 * @user_data: user data to pass to comparison function
1232 * Like g_list_sort(), but the comparison function accepts
1233 * a user data argument.
1235 * Returns: the (possibly changed) start of the #GList
1240 * @b: a value to compare with
1241 * @user_data: user data
1243 * Specifies the type of a comparison function used to compare two
1244 * values. The function should return a negative integer if the first
1245 * value comes before the second, 0 if they are equal, or a positive
1246 * integer if the first value comes after the second.
1248 * Returns: negative value if @a < @b; zero if @a = @b; positive
1252 g_list_sort_with_data (GList *list,
1253 GCompareDataFunc compare_func,
1256 return g_list_sort_real (list, (GFunc) compare_func, user_data);