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 <link
51 * linkend="glib-Singly-Linked-Lists">Singly-Linked Lists</link> which
52 * only allows movement through the list in the forward direction).
54 * The data contained in each element can be either integer values, by
55 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
56 * Conversion Macros</link>, or simply pointers to any type of data.
58 * List elements are allocated from the <link
59 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
60 * efficient than allocating elements individually.
62 * Note that most of the #GList functions expect to be passed a pointer
63 * to the first element in the list. The functions which insert
64 * elements return the new start of the list, which may have changed.
66 * There is no function to create a #GList. %NULL is considered to be
67 * the empty list so you simply set a #GList* to %NULL.
69 * To add elements, use g_list_append(), g_list_prepend(),
70 * g_list_insert() and g_list_insert_sorted().
72 * To remove elements, use g_list_remove().
74 * To find elements in the list use g_list_first(), g_list_last(),
75 * g_list_next(), g_list_previous(), g_list_nth(), g_list_nth_data(),
76 * g_list_find() and g_list_find_custom().
78 * To find the index of an element use g_list_position() and
81 * To call a function for each element in the list use g_list_foreach().
83 * To free the entire list, use g_list_free().
88 * @data: holds the element's data, which can be a pointer to any kind
89 * of data, or any integer value using the <link
90 * linkend="glib-Type-Conversion-Macros">Type Conversion
92 * @next: contains the link to the next element in the list.
93 * @prev: contains the link to the previous element in the list.
95 * The #GList struct is used for each element in a doubly-linked list.
100 * @list: an element in a #GList.
102 * A convenience macro to get the previous element in a #GList.
104 * Returns: the previous element, or %NULL if there are no previous
110 * @list: an element in a #GList.
112 * A convenience macro to get the next element in a #GList.
114 * Returns: the next element, or %NULL if there are no more elements.
117 #define _g_list_alloc() g_slice_new (GList)
118 #define _g_list_alloc0() g_slice_new0 (GList)
119 #define _g_list_free1(list) g_slice_free (GList, list)
124 * Allocates space for one #GList element. It is called by
125 * g_list_append(), g_list_prepend(), g_list_insert() and
126 * g_list_insert_sorted() and so is rarely used on its own.
128 * Returns: a pointer to the newly-allocated #GList element.
133 return _g_list_alloc0 ();
140 * Frees all of the memory used by a #GList.
141 * The freed elements are returned to the slice allocator.
144 * If list elements contain dynamically-allocated memory,
145 * you should either use g_list_free_full() or free them manually
150 g_list_free (GList *list)
152 g_slice_free_chain (GList, list, next);
157 * @list: a #GList element
159 * Frees one #GList element.
160 * It is usually used after g_list_remove_link().
165 * Another name for g_list_free_1().
168 g_list_free_1 (GList *list)
170 _g_list_free1 (list);
175 * @list: a pointer to a #GList
176 * @free_func: the function to be called to free each element's data
178 * Convenience method, which frees all the memory used by a #GList, and
179 * calls the specified destroy function on every element's data.
184 g_list_free_full (GList *list,
185 GDestroyNotify free_func)
187 g_list_foreach (list, (GFunc) free_func, NULL);
193 * @list: a pointer to a #GList
194 * @data: the data for the new element
196 * Adds a new element on to the end of the list.
199 * The return value is the new start of the list, which
200 * may have changed, so make sure you store the new value.
204 * Note that g_list_append() has to traverse the entire list
205 * to find the end, which is inefficient when adding multiple
206 * elements. A common idiom to avoid the inefficiency is to prepend
207 * the elements and reverse the list when all elements have been added.
211 * /* Notice that these are initialized to the empty list. */
212 * GList *list = NULL, *number_list = NULL;
214 * /* This is a list of strings. */
215 * list = g_list_append (list, "first");
216 * list = g_list_append (list, "second");
218 * /* This is a list of integers. */
219 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
220 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
223 * Returns: the new start of the #GList
226 g_list_append (GList *list,
232 new_list = _g_list_alloc ();
233 new_list->data = data;
234 new_list->next = NULL;
238 last = g_list_last (list);
239 /* g_assert (last != NULL); */
240 last->next = new_list;
241 new_list->prev = last;
247 new_list->prev = NULL;
254 * @list: a pointer to a #GList
255 * @data: the data for the new element
257 * Adds a new element on to the start of the list.
260 * The return value is the new start of the list, which
261 * may have changed, so make sure you store the new value.
265 * /* Notice that it is initialized to the empty list. */
266 * GList *list = NULL;
267 * list = g_list_prepend (list, "last");
268 * list = g_list_prepend (list, "first");
271 * Returns: the new start of the #GList
274 g_list_prepend (GList *list,
279 new_list = _g_list_alloc ();
280 new_list->data = data;
281 new_list->next = list;
285 new_list->prev = list->prev;
287 list->prev->next = new_list;
288 list->prev = new_list;
291 new_list->prev = NULL;
298 * @list: a pointer to a #GList
299 * @data: the data for the new element
300 * @position: the position to insert the element. If this is
301 * negative, or is larger than the number of elements in the
302 * list, the new element is added on to the end of the list.
304 * Inserts a new element into the list at the given position.
306 * Returns: the new start of the #GList
309 g_list_insert (GList *list,
317 return g_list_append (list, data);
318 else if (position == 0)
319 return g_list_prepend (list, data);
321 tmp_list = g_list_nth (list, position);
323 return g_list_append (list, data);
325 new_list = _g_list_alloc ();
326 new_list->data = data;
327 new_list->prev = tmp_list->prev;
328 tmp_list->prev->next = new_list;
329 new_list->next = tmp_list;
330 tmp_list->prev = new_list;
336 * g_list_insert_before:
337 * @list: a pointer to a #GList
338 * @sibling: the list element before which the new element
339 * is inserted or %NULL to insert at the end of the list
340 * @data: the data for the new element
342 * Inserts a new element into the list before the given position.
344 * Returns: the new start of the #GList
347 g_list_insert_before (GList *list,
353 list = g_list_alloc ();
355 g_return_val_if_fail (sibling == NULL, list);
362 node = _g_list_alloc ();
364 node->prev = sibling->prev;
365 node->next = sibling;
366 sibling->prev = node;
369 node->prev->next = node;
374 g_return_val_if_fail (sibling == list, node);
386 last->next = _g_list_alloc ();
387 last->next->data = data;
388 last->next->prev = last;
389 last->next->next = NULL;
398 * @list2: the #GList to add to the end of the first #GList
400 * Adds the second #GList onto the end of the first #GList.
401 * Note that the elements of the second #GList are not copied.
402 * They are used directly.
404 * Returns: the start of the new #GList
407 g_list_concat (GList *list1, GList *list2)
413 tmp_list = g_list_last (list1);
415 tmp_list->next = list2;
418 list2->prev = tmp_list;
425 _g_list_remove_link (GList *list,
430 if (link->prev->next == link)
431 link->prev->next = link->next;
433 g_warning ("corrupted double-linked list detected");
437 if (link->next->prev == link)
438 link->next->prev = link->prev;
440 g_warning ("corrupted double-linked list detected");
455 * @data: the data of the element to remove
457 * Removes an element from a #GList.
458 * If two elements contain the same data, only the first is removed.
459 * If none of the elements contain the data, the #GList is unchanged.
461 * Returns: the new start of the #GList
464 g_list_remove (GList *list,
472 if (tmp->data != data)
476 list = _g_list_remove_link (list, tmp);
488 * @data: data to remove
490 * Removes all list nodes with data equal to @data.
491 * Returns the new head of the list. Contrast with
492 * g_list_remove() which removes only the first node
493 * matching the given data.
495 * Returns: new head of @list
498 g_list_remove_all (GList *list,
505 if (tmp->data != data)
509 GList *next = tmp->next;
512 tmp->prev->next = next;
516 next->prev = tmp->prev;
526 * g_list_remove_link:
528 * @llink: an element in the #GList
530 * Removes an element from a #GList, without freeing the element.
531 * The removed element's prev and next links are set to %NULL, so
532 * that it becomes a self-contained list with one element.
534 * Returns: the new start of the #GList, without the element
537 g_list_remove_link (GList *list,
540 return _g_list_remove_link (list, llink);
544 * g_list_delete_link:
546 * @link_: node to delete from @list
548 * Removes the node link_ from the list and frees it.
549 * Compare this to g_list_remove_link() which removes the node
550 * without freeing it.
552 * Returns: the new head of @list
555 g_list_delete_link (GList *list,
558 list = _g_list_remove_link (list, link_);
559 _g_list_free1 (link_);
571 * Note that this is a "shallow" copy. If the list elements
572 * consist of pointers to data, the pointers are copied but
573 * the actual data is not. See g_list_copy_deep() if you need
574 * to copy the data as well.
577 * Returns: a copy of @list
580 g_list_copy (GList *list)
582 return g_list_copy_deep (list, NULL, NULL);
588 * @func: a copy function used to copy every element in the list
589 * @user_data: user data passed to the copy function @func, or #NULL
591 * Makes a full (deep) copy of a #GList.
593 * In contrast with g_list_copy(), this function uses @func to make a copy of
594 * each list element, in addition to copying the list container itself.
596 * @func, as a #GCopyFunc, takes two arguments, the data to be copied and a user
597 * pointer. It's safe to pass #NULL as user_data, if the copy function takes only
600 * For instance, if @list holds a list of GObjects, you can do:
602 * another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
605 * And, to entirely free the new list, you could do:
607 * g_list_free_full (another_list, g_object_unref);
610 * Returns: a full copy of @list, use #g_list_free_full to free it
615 g_list_copy_deep (GList *list, GCopyFunc func, gpointer user_data)
617 GList *new_list = NULL;
623 new_list = _g_list_alloc ();
625 new_list->data = func (list->data, user_data);
627 new_list->data = list->data;
628 new_list->prev = NULL;
633 last->next = _g_list_alloc ();
634 last->next->prev = last;
637 last->data = func (list->data, user_data);
639 last->data = list->data;
653 * It simply switches the next and prev pointers of each element.
655 * Returns: the start of the reversed #GList
658 g_list_reverse (GList *list)
667 last->next = last->prev;
677 * @n: the position of the element, counting from 0
679 * Gets the element at the given position in a #GList.
681 * Returns: the element, or %NULL if the position is off
682 * the end of the #GList
685 g_list_nth (GList *list,
688 while ((n-- > 0) && list)
697 * @n: the position of the element, counting from 0
699 * Gets the element @n places before @list.
701 * Returns: the element, or %NULL if the position is
702 * off the end of the #GList
705 g_list_nth_prev (GList *list,
708 while ((n-- > 0) && list)
717 * @n: the position of the element
719 * Gets the data of the element at the given position.
721 * Returns: the element's data, or %NULL if the position
722 * is off the end of the #GList
725 g_list_nth_data (GList *list,
728 while ((n-- > 0) && list)
731 return list ? list->data : NULL;
737 * @data: the element data to find
739 * Finds the element in a #GList which
740 * contains the given data.
742 * Returns: the found #GList element,
743 * or %NULL if it is not found
746 g_list_find (GList *list,
751 if (list->data == data)
760 * g_list_find_custom:
762 * @data: user data passed to the function
763 * @func: the function to call for each element.
764 * It should return 0 when the desired element is found
766 * Finds an element in a #GList, using a supplied function to
767 * find the desired element. It iterates over the list, calling
768 * the given function which should return 0 when the desired
769 * element is found. The function takes two #gconstpointer arguments,
770 * the #GList element's data as the first argument and the
773 * Returns: the found #GList element, or %NULL if it is not found
776 g_list_find_custom (GList *list,
780 g_return_val_if_fail (func != NULL, list);
784 if (! func (list->data, data))
796 * @llink: an element in the #GList
798 * Gets the position of the given element
799 * in the #GList (starting from 0).
801 * Returns: the position of the element in the #GList,
802 * or -1 if the element is not found
805 g_list_position (GList *list,
825 * @data: the data to find
827 * Gets the position of the element containing
828 * the given data (starting from 0).
830 * Returns: the index of the element containing the data,
831 * or -1 if the data is not found
834 g_list_index (GList *list,
842 if (list->data == data)
855 * Gets the last element in a #GList.
857 * Returns: the last element in the #GList,
858 * or %NULL if the #GList has no elements
861 g_list_last (GList *list)
876 * Gets the first element in a #GList.
878 * Returns: the first element in the #GList,
879 * or %NULL if the #GList has no elements
882 g_list_first (GList *list)
897 * Gets the number of elements in a #GList.
900 * This function iterates over the whole list to
901 * count its elements.
904 * Returns: the number of elements in the #GList
907 g_list_length (GList *list)
924 * @func: the function to call with each element's data
925 * @user_data: user data to pass to the function
927 * Calls a function for each element of a #GList.
931 * @data: the element's data.
932 * @user_data: user data passed to g_list_foreach() or
935 * Specifies the type of functions passed to g_list_foreach() and
939 g_list_foreach (GList *list,
945 GList *next = list->next;
946 (*func) (list->data, user_data);
952 g_list_insert_sorted_real (GList *list,
957 GList *tmp_list = list;
961 g_return_val_if_fail (func != NULL, list);
965 new_list = _g_list_alloc0 ();
966 new_list->data = data;
970 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
972 while ((tmp_list->next) && (cmp > 0))
974 tmp_list = tmp_list->next;
976 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
979 new_list = _g_list_alloc0 ();
980 new_list->data = data;
982 if ((!tmp_list->next) && (cmp > 0))
984 tmp_list->next = new_list;
985 new_list->prev = tmp_list;
991 tmp_list->prev->next = new_list;
992 new_list->prev = tmp_list->prev;
994 new_list->next = tmp_list;
995 tmp_list->prev = new_list;
997 if (tmp_list == list)
1004 * g_list_insert_sorted:
1005 * @list: a pointer to a #GList
1006 * @data: the data for the new element
1007 * @func: the function to compare elements in the list. It should
1008 * return a number > 0 if the first parameter comes after the
1009 * second parameter in the sort order.
1011 * Inserts a new element into the list, using the given comparison
1012 * function to determine its position.
1014 * Returns: the new start of the #GList
1017 g_list_insert_sorted (GList *list,
1021 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
1025 * g_list_insert_sorted_with_data:
1026 * @list: a pointer to a #GList
1027 * @data: the data for the new element
1028 * @func: the function to compare elements in the list.
1029 * It should return a number > 0 if the first parameter
1030 * comes after the second parameter in the sort order.
1031 * @user_data: user data to pass to comparison function.
1033 * Inserts a new element into the list, using the given comparison
1034 * function to determine its position.
1036 * Returns: the new start of the #GList
1041 g_list_insert_sorted_with_data (GList *list,
1043 GCompareDataFunc func,
1046 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1050 g_list_sort_merge (GList *l1,
1055 GList list, *l, *lprev;
1063 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1079 l->next = l1 ? l1 : l2;
1086 g_list_sort_real (GList *list,
1100 while ((l2 = l2->next) != NULL)
1102 if ((l2 = l2->next) == NULL)
1109 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1110 g_list_sort_real (l2, compare_func, user_data),
1118 * @compare_func: the comparison function used to sort the #GList.
1119 * This function is passed the data from 2 elements of the #GList
1120 * and should return 0 if they are equal, a negative value if the
1121 * first element comes before the second, or a positive value if
1122 * the first element comes after the second.
1124 * Sorts a #GList using the given comparison function. The algorithm
1125 * used is a stable sort.
1127 * Returns: the start of the sorted #GList
1132 * @b: a value to compare with.
1134 * Specifies the type of a comparison function used to compare two
1135 * values. The function should return a negative integer if the first
1136 * value comes before the second, 0 if they are equal, or a positive
1137 * integer if the first value comes after the second.
1139 * Returns: negative value if @a < @b; zero if @a = @b; positive
1143 g_list_sort (GList *list,
1144 GCompareFunc compare_func)
1146 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1151 * g_list_sort_with_data:
1153 * @compare_func: comparison function
1154 * @user_data: user data to pass to comparison function
1156 * Like g_list_sort(), but the comparison function accepts
1157 * a user data argument.
1159 * Returns: the new head of @list
1164 * @b: a value to compare with.
1165 * @user_data: user data to pass to comparison function.
1167 * Specifies the type of a comparison function used to compare two
1168 * values. The function should return a negative integer if the first
1169 * value comes before the second, 0 if they are equal, or a positive
1170 * integer if the first value comes after the second.
1172 * Returns: negative value if @a < @b; zero if @a = @b; positive
1176 g_list_sort_with_data (GList *list,
1177 GCompareDataFunc compare_func,
1180 return g_list_sort_real (list, (GFunc) compare_func, user_data);