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 #include "gtestutils.h"
39 * SECTION:linked_lists_double
40 * @title: Doubly-Linked Lists
41 * @short_description: linked lists that can be iterated over 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.
114 #define _g_list_alloc() g_slice_new (GList)
115 #define _g_list_alloc0() g_slice_new0 (GList)
116 #define _g_list_free1(list) g_slice_free (GList, list)
120 * @Returns: a pointer to the newly-allocated #GList element.
122 * Allocates space for one #GList element. It is called by
123 * g_list_append(), g_list_prepend(), g_list_insert() and
124 * g_list_insert_sorted() and so is rarely used on its own.
129 return _g_list_alloc0 ();
136 * Frees all of the memory used by a #GList.
137 * The freed elements are returned to the slice allocator.
140 * If list elements contain dynamically-allocated memory,
141 * you should either use g_list_free_full() or free them manually
146 g_list_free (GList *list)
148 g_slice_free_chain (GList, list, next);
153 * @list: a #GList element
155 * Frees one #GList element.
156 * It is usually used after g_list_remove_link().
161 * Another name for g_list_free_1().
164 g_list_free_1 (GList *list)
166 _g_list_free1 (list);
171 * @list: a pointer to a #GList
172 * @free_func: the function to be called to free each element's data
174 * Convenience method, which frees all the memory used by a #GList, and
175 * calls the specified destroy function on every element's data.
180 g_list_free_full (GList *list,
181 GDestroyNotify free_func)
183 g_list_foreach (list, (GFunc) free_func, NULL);
189 * @list: a pointer to a #GList
190 * @data: the data for the new element
192 * Adds a new element on to the end of the list.
195 * The return value is the new start of the list, which
196 * may have changed, so make sure you store the new value.
200 * Note that g_list_append() has to traverse the entire list
201 * to find the end, which is inefficient when adding multiple
202 * elements. A common idiom to avoid the inefficiency is to prepend
203 * the elements and reverse the list when all elements have been added.
207 * /* Notice that these are initialized to the empty list. */
208 * GList *list = NULL, *number_list = NULL;
210 * /* This is a list of strings. */
211 * list = g_list_append (list, "first");
212 * list = g_list_append (list, "second");
214 * /* This is a list of integers. */
215 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
216 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
219 * Returns: the new start of the #GList
222 g_list_append (GList *list,
228 new_list = _g_list_alloc ();
229 new_list->data = data;
230 new_list->next = NULL;
234 last = g_list_last (list);
235 /* g_assert (last != NULL); */
236 last->next = new_list;
237 new_list->prev = last;
243 new_list->prev = NULL;
250 * @list: a pointer to a #GList
251 * @data: the data for the new element
253 * Adds a new element on to the start of the list.
256 * The return value is the new start of the list, which
257 * may have changed, so make sure you store the new value.
261 * /* Notice that it is initialized to the empty list. */
262 * GList *list = NULL;
263 * list = g_list_prepend (list, "last");
264 * list = g_list_prepend (list, "first");
267 * Returns: the new start of the #GList
270 g_list_prepend (GList *list,
275 new_list = _g_list_alloc ();
276 new_list->data = data;
277 new_list->next = list;
281 new_list->prev = list->prev;
283 list->prev->next = new_list;
284 list->prev = new_list;
287 new_list->prev = NULL;
294 * @list: a pointer to a #GList
295 * @data: the data for the new element
296 * @position: the position to insert the element. If this is
297 * negative, or is larger than the number of elements in the
298 * list, the new element is added on to the end of the list.
300 * Inserts a new element into the list at the given position.
302 * Returns: the new start of the #GList
305 g_list_insert (GList *list,
313 return g_list_append (list, data);
314 else if (position == 0)
315 return g_list_prepend (list, data);
317 tmp_list = g_list_nth (list, position);
319 return g_list_append (list, data);
321 new_list = _g_list_alloc ();
322 new_list->data = data;
323 new_list->prev = tmp_list->prev;
324 tmp_list->prev->next = new_list;
325 new_list->next = tmp_list;
326 tmp_list->prev = new_list;
332 * g_list_insert_before:
333 * @list: a pointer to a #GList
334 * @sibling: the list element before which the new element
335 * is inserted or %NULL to insert at the end of the list
336 * @data: the data for the new element
338 * Inserts a new element into the list before the given position.
340 * Returns: the new start of the #GList
343 g_list_insert_before (GList *list,
349 list = g_list_alloc ();
351 g_return_val_if_fail (sibling == NULL, list);
358 node = _g_list_alloc ();
360 node->prev = sibling->prev;
361 node->next = sibling;
362 sibling->prev = node;
365 node->prev->next = node;
370 g_return_val_if_fail (sibling == list, node);
382 last->next = _g_list_alloc ();
383 last->next->data = data;
384 last->next->prev = last;
385 last->next->next = NULL;
394 * @list2: the #GList to add to the end of the first #GList
396 * Adds the second #GList onto the end of the first #GList.
397 * Note that the elements of the second #GList are not copied.
398 * They are used directly.
400 * Returns: the start of the new #GList
403 g_list_concat (GList *list1, GList *list2)
409 tmp_list = g_list_last (list1);
411 tmp_list->next = list2;
414 list2->prev = tmp_list;
423 * @data: the data of the element to remove
425 * Removes an element from a #GList.
426 * If two elements contain the same data, only the first is removed.
427 * If none of the elements contain the data, the #GList is unchanged.
429 * Returns: the new start of the #GList
432 g_list_remove (GList *list,
440 if (tmp->data != data)
445 tmp->prev->next = tmp->next;
447 tmp->next->prev = tmp->prev;
463 * @data: data to remove
465 * Removes all list nodes with data equal to @data.
466 * Returns the new head of the list. Contrast with
467 * g_list_remove() which removes only the first node
468 * matching the given data.
470 * Returns: new head of @list
473 g_list_remove_all (GList *list,
480 if (tmp->data != data)
484 GList *next = tmp->next;
487 tmp->prev->next = next;
491 next->prev = tmp->prev;
501 _g_list_remove_link (GList *list,
507 link->prev->next = link->next;
509 link->next->prev = link->prev;
522 * g_list_remove_link:
524 * @llink: an element in the #GList
526 * Removes an element from a #GList, without freeing the element.
527 * The removed element's prev and next links are set to %NULL, so
528 * that it becomes a self-contained list with one element.
530 * Returns: the new start of the #GList, without the element
533 g_list_remove_link (GList *list,
536 return _g_list_remove_link (list, llink);
540 * g_list_delete_link:
542 * @link_: node to delete from @list
544 * Removes the node link_ from the list and frees it.
545 * Compare this to g_list_remove_link() which removes the node
546 * without freeing it.
548 * Returns: the new head of @list
551 g_list_delete_link (GList *list,
554 list = _g_list_remove_link (list, link_);
555 _g_list_free1 (link_);
567 * Note that this is a "shallow" copy. If the list elements
568 * consist of pointers to data, the pointers are copied but
569 * the actual data is not.
572 * Returns: a copy of @list
575 g_list_copy (GList *list)
577 GList *new_list = NULL;
583 new_list = _g_list_alloc ();
584 new_list->data = list->data;
585 new_list->prev = NULL;
590 last->next = _g_list_alloc ();
591 last->next->prev = last;
593 last->data = list->data;
607 * It simply switches the next and prev pointers of each element.
609 * Returns: the start of the reversed #GList
612 g_list_reverse (GList *list)
621 last->next = last->prev;
631 * @n: the position of the element, counting from 0
633 * Gets the element at the given position in a #GList.
635 * Returns: the element, or %NULL if the position is off
636 * the end of the #GList
639 g_list_nth (GList *list,
642 while ((n-- > 0) && list)
651 * @n: the position of the element, counting from 0
653 * Gets the element @n places before @list.
655 * Returns: the element, or %NULL if the position is
656 * off the end of the #GList
659 g_list_nth_prev (GList *list,
662 while ((n-- > 0) && list)
671 * @n: the position of the element
673 * Gets the data of the element at the given position.
675 * Returns: the element's data, or %NULL if the position
676 * is off the end of the #GList
679 g_list_nth_data (GList *list,
682 while ((n-- > 0) && list)
685 return list ? list->data : NULL;
691 * @data: the element data to find
693 * Finds the element in a #GList which
694 * contains the given data.
696 * Returns: the found #GList element,
697 * or %NULL if it is not found
700 g_list_find (GList *list,
705 if (list->data == data)
714 * g_list_find_custom:
716 * @data: user data passed to the function
717 * @func: the function to call for each element.
718 * It should return 0 when the desired element is found
720 * Finds an element in a #GList, using a supplied function to
721 * find the desired element. It iterates over the list, calling
722 * the given function which should return 0 when the desired
723 * element is found. The function takes two #gconstpointer arguments,
724 * the #GList element's data as the first argument and the
727 * Returns: the found #GList element, or %NULL if it is not found
730 g_list_find_custom (GList *list,
734 g_return_val_if_fail (func != NULL, list);
738 if (! func (list->data, data))
750 * @llink: an element in the #GList
752 * Gets the position of the given element
753 * in the #GList (starting from 0).
755 * Returns: the position of the element in the #GList,
756 * or -1 if the element is not found
759 g_list_position (GList *list,
779 * @data: the data to find
781 * Gets the position of the element containing
782 * the given data (starting from 0).
784 * Returns: the index of the element containing the data,
785 * or -1 if the data is not found
788 g_list_index (GList *list,
796 if (list->data == data)
809 * Gets the last element in a #GList.
811 * Returns: the last element in the #GList,
812 * or %NULL if the #GList has no elements
815 g_list_last (GList *list)
830 * Gets the first element in a #GList.
832 * Returns: the first element in the #GList,
833 * or %NULL if the #GList has no elements
836 g_list_first (GList *list)
851 * Gets the number of elements in a #GList.
854 * This function iterates over the whole list to
855 * count its elements.
858 * Returns: the number of elements in the #GList
861 g_list_length (GList *list)
878 * @func: the function to call with each element's data
879 * @user_data: user data to pass to the function
881 * Calls a function for each element of a #GList.
885 * @data: the element's data.
886 * @user_data: user data passed to g_list_foreach() or
889 * Specifies the type of functions passed to g_list_foreach() and
893 g_list_foreach (GList *list,
899 GList *next = list->next;
900 (*func) (list->data, user_data);
906 g_list_insert_sorted_real (GList *list,
911 GList *tmp_list = list;
915 g_return_val_if_fail (func != NULL, list);
919 new_list = _g_list_alloc0 ();
920 new_list->data = data;
924 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
926 while ((tmp_list->next) && (cmp > 0))
928 tmp_list = tmp_list->next;
930 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
933 new_list = _g_list_alloc0 ();
934 new_list->data = data;
936 if ((!tmp_list->next) && (cmp > 0))
938 tmp_list->next = new_list;
939 new_list->prev = tmp_list;
945 tmp_list->prev->next = new_list;
946 new_list->prev = tmp_list->prev;
948 new_list->next = tmp_list;
949 tmp_list->prev = new_list;
951 if (tmp_list == list)
958 * g_list_insert_sorted:
959 * @list: a pointer to a #GList
960 * @data: the data for the new element
961 * @func: the function to compare elements in the list. It should
962 * return a number > 0 if the first parameter comes after the
963 * second parameter in the sort order.
965 * Inserts a new element into the list, using the given comparison
966 * function to determine its position.
968 * Returns: the new start of the #GList
971 g_list_insert_sorted (GList *list,
975 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
979 * g_list_insert_sorted_with_data:
980 * @list: a pointer to a #GList
981 * @data: the data for the new element
982 * @func: the function to compare elements in the list.
983 * It should return a number > 0 if the first parameter
984 * comes after the second parameter in the sort order.
985 * @user_data: user data to pass to comparison function.
987 * Inserts a new element into the list, using the given comparison
988 * function to determine its position.
990 * Returns: the new start of the #GList
995 g_list_insert_sorted_with_data (GList *list,
997 GCompareDataFunc func,
1000 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1004 g_list_sort_merge (GList *l1,
1009 GList list, *l, *lprev;
1017 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1033 l->next = l1 ? l1 : l2;
1040 g_list_sort_real (GList *list,
1054 while ((l2 = l2->next) != NULL)
1056 if ((l2 = l2->next) == NULL)
1063 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1064 g_list_sort_real (l2, compare_func, user_data),
1072 * @compare_func: the comparison function used to sort the #GList.
1073 * This function is passed the data from 2 elements of the #GList
1074 * and should return 0 if they are equal, a negative value if the
1075 * first element comes before the second, or a positive value if
1076 * the first element comes after the second.
1078 * Sorts a #GList using the given comparison function. The algorithm
1079 * used is a stable sort.
1081 * Returns: the start of the sorted #GList
1086 * @b: a value to compare with.
1087 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1090 * Specifies the type of a comparison function used to compare two
1091 * values. The function should return a negative integer if the first
1092 * value comes before the second, 0 if they are equal, or a positive
1093 * integer if the first value comes after the second.
1096 g_list_sort (GList *list,
1097 GCompareFunc compare_func)
1099 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1104 * g_list_sort_with_data:
1106 * @compare_func: comparison function
1107 * @user_data: user data to pass to comparison function
1109 * Like g_list_sort(), but the comparison function accepts
1110 * a user data argument.
1112 * Returns: the new head of @list
1117 * @b: a value to compare with.
1118 * @user_data: user data to pass to comparison function.
1119 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1122 * Specifies the type of a comparison function used to compare two
1123 * values. The function should return a negative integer if the first
1124 * value comes before the second, 0 if they are equal, or a positive
1125 * integer if the first value comes after the second.
1128 g_list_sort_with_data (GList *list,
1129 GCompareDataFunc compare_func,
1132 return g_list_sort_real (list, (GFunc) compare_func, user_data);