1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 "gtestutils.h"
39 * SECTION:linked_lists_single
40 * @title: Singly-Linked Lists
41 * @short_description: linked lists that can be iterated in one direction
43 * The #GSList structure and its associated functions provide a
44 * standard singly-linked list data structure. The benefit of this
45 * data-structure is to provide insertion/deletion operations in O(1)
46 * complexity where access/search operations are in O(n). The benefit
47 * of #GSList over #GList (doubly linked list) is that they are lighter
48 * in space as they only need to retain one pointer but it double the
49 * cost of the worst case access/search operations.
51 * Each element in the list contains a piece of data, together with a
52 * pointer which links to the next element in the list. Using this
53 * pointer it is possible to move through the list in one direction
54 * only (unlike the [double-linked lists][glib-Doubly-Linked-Lists],
55 * which allow movement in both directions).
57 * The data contained in each element can be either integer values, by
58 * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
59 * or simply pointers to any type of data.
61 * List elements are allocated from the [slice allocator][glib-Memory-Slices],
62 * which is more efficient than allocating elements individually.
64 * Note that most of the #GSList functions expect to be passed a
65 * pointer to the first element in the list. The functions which insert
66 * elements return the new start of the list, which may have changed.
68 * There is no function to create a #GSList. %NULL is considered to be
69 * the empty list so you simply set a #GSList* to %NULL.
71 * To add elements, use g_slist_append(), g_slist_prepend(),
72 * g_slist_insert() and g_slist_insert_sorted().
74 * To remove elements, use g_slist_remove().
76 * To find elements in the list use g_slist_last(), g_slist_next(),
77 * g_slist_nth(), g_slist_nth_data(), g_slist_find() and
78 * g_slist_find_custom().
80 * To find the index of an element use g_slist_position() and
83 * To call a function for each element in the list use
86 * To free the entire list, use g_slist_free().
91 * @data: holds the element's data, which can be a pointer to any kind
92 * of data, or any integer value using the
93 * [Type Conversion Macros][glib-Type-Conversion-Macros]
94 * @next: contains the link to the next element in the list.
96 * The #GSList struct is used for each element in the singly-linked
102 * @slist: an element in a #GSList.
104 * A convenience macro to get the next element in a #GSList.
105 * Note that it is considered perfectly acceptable to access
106 * @slist->next directly.
108 * Returns: the next element, or %NULL if there are no more elements.
111 #define _g_slist_alloc0() g_slice_new0 (GSList)
112 #define _g_slist_alloc() g_slice_new (GSList)
113 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
118 * Allocates space for one #GSList element. It is called by the
119 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
120 * g_slist_insert_sorted() functions and so is rarely used on its own.
122 * Returns: a pointer to the newly-allocated #GSList element.
127 return _g_slist_alloc0 ();
132 * @list: the first link of a #GSList
134 * Frees all of the memory used by a #GSList.
135 * The freed elements are returned to the slice allocator.
137 * If list elements contain dynamically-allocated memory,
138 * you should either use g_slist_free_full() or free them manually
141 * It can be combined with g_steal_pointer() to ensure the list head pointer
142 * is not left dangling:
143 * |[<!-- language="C" -->
144 * GSList *list_of_borrowed_things = …; /<!-- -->* (transfer container) *<!-- -->/
145 * g_slist_free (g_steal_pointer (&list_of_borrowed_things));
149 g_slist_free (GSList *list)
151 g_slice_free_chain (GSList, list, next);
156 * @list: a #GSList element
158 * Frees one #GSList element.
159 * It is usually used after g_slist_remove_link().
164 * A macro which does the same as g_slist_free_1().
169 g_slist_free_1 (GSList *list)
171 _g_slist_free1 (list);
176 * @list: the first link of a #GSList
177 * @free_func: the function to be called to free each element's data
179 * Convenience method, which frees all the memory used by a #GSList, and
180 * calls the specified destroy function on every element's data.
182 * @free_func must not modify the list (eg, by removing the freed
185 * It can be combined with g_steal_pointer() to ensure the list head pointer
186 * is not left dangling — this also has the nice property that the head pointer
187 * is cleared before any of the list elements are freed, to prevent double frees
189 * |[<!-- language="C" -->
190 * GSList *list_of_owned_things = …; /<!-- -->* (transfer full) (element-type GObject) *<!-- -->/
191 * g_slist_free_full (g_steal_pointer (&list_of_owned_things), g_object_unref);
197 g_slist_free_full (GSList *list,
198 GDestroyNotify free_func)
200 g_slist_foreach (list, (GFunc) free_func, NULL);
207 * @data: the data for the new element
209 * Adds a new element on to the end of the list.
211 * The return value is the new start of the list, which may
212 * have changed, so make sure you store the new value.
214 * Note that g_slist_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.
219 * |[<!-- language="C" -->
220 * // Notice that these are initialized to the empty list.
221 * GSList *list = NULL, *number_list = NULL;
223 * // This is a list of strings.
224 * list = g_slist_append (list, "first");
225 * list = g_slist_append (list, "second");
227 * // This is a list of integers.
228 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
229 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
232 * Returns: the new start of the #GSList
235 g_slist_append (GSList *list,
241 new_list = _g_slist_alloc ();
242 new_list->data = data;
243 new_list->next = NULL;
247 last = g_slist_last (list);
248 /* g_assert (last != NULL); */
249 last->next = new_list;
260 * @data: the data for the new element
262 * Adds a new element on to the start of the list.
264 * The return value is the new start of the list, which
265 * may have changed, so make sure you store the new value.
267 * |[<!-- language="C" -->
268 * // Notice that it is initialized to the empty list.
269 * GSList *list = NULL;
270 * list = g_slist_prepend (list, "last");
271 * list = g_slist_prepend (list, "first");
274 * Returns: the new start of the #GSList
277 g_slist_prepend (GSList *list,
282 new_list = _g_slist_alloc ();
283 new_list->data = data;
284 new_list->next = list;
292 * @data: the data for the new element
293 * @position: the position to insert the element.
294 * If this is negative, or is larger than the number
295 * of elements in the list, the new element is added on
296 * to the end of the list.
298 * Inserts a new element into the list at the given position.
300 * Returns: the new start of the #GSList
303 g_slist_insert (GSList *list,
312 return g_slist_append (list, data);
313 else if (position == 0)
314 return g_slist_prepend (list, data);
316 new_list = _g_slist_alloc ();
317 new_list->data = data;
321 new_list->next = NULL;
328 while ((position-- > 0) && tmp_list)
330 prev_list = tmp_list;
331 tmp_list = tmp_list->next;
334 new_list->next = prev_list->next;
335 prev_list->next = new_list;
341 * g_slist_insert_before:
343 * @sibling: node to insert @data before
344 * @data: data to put in the newly-inserted node
346 * Inserts a node before @sibling containing @data.
348 * Returns: the new head of the list.
351 g_slist_insert_before (GSList *slist,
357 slist = _g_slist_alloc ();
360 g_return_val_if_fail (sibling == NULL, slist);
365 GSList *node, *last = NULL;
367 for (node = slist; node; last = node, node = last->next)
372 node = _g_slist_alloc ();
380 node = _g_slist_alloc ();
382 node->next = last->next;
393 * @list2: the #GSList to add to the end of the first #GSList
395 * Adds the second #GSList onto the end of the first #GSList.
396 * Note that the elements of the second #GSList are not copied.
397 * They are used directly.
399 * Returns: the start of the new #GSList
402 g_slist_concat (GSList *list1, GSList *list2)
407 g_slist_last (list1)->next = list2;
416 _g_slist_remove_data (GSList *list,
421 GSList **previous_ptr = &list;
423 while (*previous_ptr)
426 if (tmp->data == data)
428 *previous_ptr = tmp->next;
429 g_slist_free_1 (tmp);
435 previous_ptr = &tmp->next;
444 * @data: the data of the element to remove
446 * Removes an element from a #GSList.
447 * If two elements contain the same data, only the first is removed.
448 * If none of the elements contain the data, the #GSList is unchanged.
450 * Returns: the new start of the #GSList
453 g_slist_remove (GSList *list,
456 return _g_slist_remove_data (list, data, FALSE);
460 * g_slist_remove_all:
462 * @data: data to remove
464 * Removes all list nodes with data equal to @data.
465 * Returns the new head of the list. Contrast with
466 * g_slist_remove() which removes only the first node
467 * matching the given data.
469 * Returns: new head of @list
472 g_slist_remove_all (GSList *list,
475 return _g_slist_remove_data (list, data, TRUE);
478 static inline GSList*
479 _g_slist_remove_link (GSList *list,
483 GSList **previous_ptr = &list;
485 while (*previous_ptr)
490 *previous_ptr = tmp->next;
495 previous_ptr = &tmp->next;
502 * g_slist_remove_link:
504 * @link_: an element in the #GSList
506 * Removes an element from a #GSList, without
507 * freeing the element. The removed element's next
508 * link is set to %NULL, so that it becomes a
509 * self-contained list with one element.
511 * Removing arbitrary nodes from a singly-linked list
512 * requires time that is proportional to the length of the list
513 * (ie. O(n)). If you find yourself using g_slist_remove_link()
514 * frequently, you should consider a different data structure,
515 * such as the doubly-linked #GList.
517 * Returns: the new start of the #GSList, without the element
520 g_slist_remove_link (GSList *list,
523 return _g_slist_remove_link (list, link_);
527 * g_slist_delete_link:
529 * @link_: node to delete
531 * Removes the node link_ from the list and frees it.
532 * Compare this to g_slist_remove_link() which removes the node
533 * without freeing it.
535 * Removing arbitrary nodes from a singly-linked list requires time
536 * that is proportional to the length of the list (ie. O(n)). If you
537 * find yourself using g_slist_delete_link() frequently, you should
538 * consider a different data structure, such as the doubly-linked
541 * Returns: the new head of @list
544 g_slist_delete_link (GSList *list,
547 list = _g_slist_remove_link (list, link_);
548 _g_slist_free1 (link_);
559 * Note that this is a "shallow" copy. If the list elements
560 * consist of pointers to data, the pointers are copied but
561 * the actual data isn't. See g_slist_copy_deep() if you need
562 * to copy the data as well.
564 * Returns: a copy of @list
567 g_slist_copy (GSList *list)
569 return g_slist_copy_deep (list, NULL, NULL);
575 * @func: a copy function used to copy every element in the list
576 * @user_data: user data passed to the copy function @func, or #NULL
578 * Makes a full (deep) copy of a #GSList.
580 * In contrast with g_slist_copy(), this function uses @func to make a copy of
581 * each list element, in addition to copying the list container itself.
583 * @func, as a #GCopyFunc, takes two arguments, the data to be copied
584 * and a @user_data pointer. On common processor architectures, it's safe to
585 * pass %NULL as @user_data if the copy function takes only one argument. You
586 * may get compiler warnings from this though if compiling with GCC’s
587 * `-Wcast-function-type` warning.
589 * For instance, if @list holds a list of GObjects, you can do:
590 * |[<!-- language="C" -->
591 * another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
594 * And, to entirely free the new list, you could do:
595 * |[<!-- language="C" -->
596 * g_slist_free_full (another_list, g_object_unref);
599 * Returns: a full copy of @list, use g_slist_free_full() to free it
604 g_slist_copy_deep (GSList *list, GCopyFunc func, gpointer user_data)
606 GSList *new_list = NULL;
612 new_list = _g_slist_alloc ();
614 new_list->data = func (list->data, user_data);
616 new_list->data = list->data;
621 last->next = _g_slist_alloc ();
624 last->data = func (list->data, user_data);
626 last->data = list->data;
639 * Reverses a #GSList.
641 * Returns: the start of the reversed #GSList
644 g_slist_reverse (GSList *list)
650 GSList *next = list->next;
664 * @n: the position of the element, counting from 0
666 * Gets the element at the given position in a #GSList.
668 * Returns: the element, or %NULL if the position is off
669 * the end of the #GSList
672 g_slist_nth (GSList *list,
675 while (n-- > 0 && list)
684 * @n: the position of the element
686 * Gets the data of the element at the given position.
688 * Returns: the element's data, or %NULL if the position
689 * is off the end of the #GSList
692 g_slist_nth_data (GSList *list,
695 while (n-- > 0 && list)
698 return list ? list->data : NULL;
704 * @data: the element data to find
706 * Finds the element in a #GSList which
707 * contains the given data.
709 * Returns: the found #GSList element,
710 * or %NULL if it is not found
713 g_slist_find (GSList *list,
718 if (list->data == data)
728 * g_slist_find_custom:
730 * @data: user data passed to the function
731 * @func: the function to call for each element.
732 * It should return 0 when the desired element is found
734 * Finds an element in a #GSList, using a supplied function to
735 * find the desired element. It iterates over the list, calling
736 * the given function which should return 0 when the desired
737 * element is found. The function takes two #gconstpointer arguments,
738 * the #GSList element's data as the first argument and the
741 * Returns: the found #GSList element, or %NULL if it is not found
744 g_slist_find_custom (GSList *list,
748 g_return_val_if_fail (func != NULL, list);
752 if (! func (list->data, data))
763 * @llink: an element in the #GSList
765 * Gets the position of the given element
766 * in the #GSList (starting from 0).
768 * Returns: the position of the element in the #GSList,
769 * or -1 if the element is not found
772 g_slist_position (GSList *list,
792 * @data: the data to find
794 * Gets the position of the element containing
795 * the given data (starting from 0).
797 * Returns: the index of the element containing the data,
798 * or -1 if the data is not found
801 g_slist_index (GSList *list,
809 if (list->data == data)
822 * Gets the last element in a #GSList.
824 * This function iterates over the whole list.
826 * Returns: the last element in the #GSList,
827 * or %NULL if the #GSList has no elements
830 g_slist_last (GSList *list)
845 * Gets the number of elements in a #GSList.
847 * This function iterates over the whole list to
848 * count its elements. To check whether the list is non-empty, it is faster to
849 * check @list against %NULL.
851 * Returns: the number of elements in the #GSList
854 g_slist_length (GSList *list)
871 * @func: the function to call with each element's data
872 * @user_data: user data to pass to the function
874 * Calls a function for each element of a #GSList.
876 * It is safe for @func to remove the element from @list, but it must
877 * not modify any part of the list after that element.
880 g_slist_foreach (GSList *list,
886 GSList *next = list->next;
887 (*func) (list->data, user_data);
893 g_slist_insert_sorted_real (GSList *list,
898 GSList *tmp_list = list;
899 GSList *prev_list = NULL;
903 g_return_val_if_fail (func != NULL, list);
907 new_list = _g_slist_alloc ();
908 new_list->data = data;
909 new_list->next = NULL;
913 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
915 while ((tmp_list->next) && (cmp > 0))
917 prev_list = tmp_list;
918 tmp_list = tmp_list->next;
920 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
923 new_list = _g_slist_alloc ();
924 new_list->data = data;
926 if ((!tmp_list->next) && (cmp > 0))
928 tmp_list->next = new_list;
929 new_list->next = NULL;
935 prev_list->next = new_list;
936 new_list->next = tmp_list;
941 new_list->next = list;
947 * g_slist_insert_sorted:
949 * @data: the data for the new element
950 * @func: the function to compare elements in the list.
951 * It should return a number > 0 if the first parameter
952 * comes after the second parameter in the sort order.
954 * Inserts a new element into the list, using the given
955 * comparison function to determine its position.
957 * Returns: the new start of the #GSList
960 g_slist_insert_sorted (GSList *list,
964 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
968 * g_slist_insert_sorted_with_data:
970 * @data: the data for the new element
971 * @func: the function to compare elements in the list.
972 * It should return a number > 0 if the first parameter
973 * comes after the second parameter in the sort order.
974 * @user_data: data to pass to comparison function
976 * Inserts a new element into the list, using the given
977 * comparison function to determine its position.
979 * Returns: the new start of the #GSList
984 g_slist_insert_sorted_with_data (GSList *list,
986 GCompareDataFunc func,
989 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
993 g_slist_sort_merge (GSList *l1,
1005 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1018 l->next= l1 ? l1 : l2;
1024 g_slist_sort_real (GSList *list,
1038 while ((l2 = l2->next) != NULL)
1040 if ((l2 = l2->next) == NULL)
1047 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1048 g_slist_sort_real (l2, compare_func, user_data),
1056 * @compare_func: the comparison function used to sort the #GSList.
1057 * This function is passed the data from 2 elements of the #GSList
1058 * and should return 0 if they are equal, a negative value if the
1059 * first element comes before the second, or a positive value if
1060 * the first element comes after the second.
1062 * Sorts a #GSList using the given comparison function. The algorithm
1063 * used is a stable sort.
1065 * Returns: the start of the sorted #GSList
1068 g_slist_sort (GSList *list,
1069 GCompareFunc compare_func)
1071 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1075 * g_slist_sort_with_data:
1077 * @compare_func: comparison function
1078 * @user_data: data to pass to comparison function
1080 * Like g_slist_sort(), but the sort function accepts a user data argument.
1082 * Returns: new head of the list
1085 g_slist_sort_with_data (GSList *list,
1086 GCompareDataFunc compare_func,
1089 return g_slist_sort_real (list, (GFunc) compare_func, user_data);
1093 * g_clear_slist: (skip)
1094 * @slist_ptr: (not nullable): a #GSList return location
1095 * @destroy: (nullable): the function to pass to g_slist_free_full() or %NULL to not free elements
1097 * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
1099 * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
1104 (g_clear_slist) (GSList **slist_ptr,
1105 GDestroyNotify destroy)
1115 g_slist_free_full (slist, destroy);
1117 g_slist_free (slist);