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 "gtestutils.h"
37 * SECTION:linked_lists_single
38 * @title: Singly-Linked Lists
39 * @short_description: linked lists that can be iterated in one direction
41 * The #GSList structure and its associated functions provide a
42 * standard singly-linked list data structure.
44 * Each element in the list contains a piece of data, together with a
45 * pointer which links to the next element in the list. Using this
46 * pointer it is possible to move through the list in one direction
47 * only (unlike the [double-linked lists][glib-Doubly-Linked-Lists],
48 * which allow movement in both directions).
50 * The data contained in each element can be either integer values, by
51 * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
52 * or simply pointers to any type of data.
54 * List elements are allocated from the [slice allocator][glib-Memory-Slices],
55 * which is more efficient than allocating elements individually.
57 * Note that most of the #GSList functions expect to be passed a
58 * pointer to the first element in the list. The functions which insert
59 * elements return the new start of the list, which may have changed.
61 * There is no function to create a #GSList. %NULL is considered to be
62 * the empty list so you simply set a #GSList* to %NULL.
64 * To add elements, use g_slist_append(), g_slist_prepend(),
65 * g_slist_insert() and g_slist_insert_sorted().
67 * To remove elements, use g_slist_remove().
69 * To find elements in the list use g_slist_last(), g_slist_next(),
70 * g_slist_nth(), g_slist_nth_data(), g_slist_find() and
71 * g_slist_find_custom().
73 * To find the index of an element use g_slist_position() and
76 * To call a function for each element in the list use
79 * To free the entire list, use g_slist_free().
84 * @data: holds the element's data, which can be a pointer to any kind
85 * of data, or any integer value using the
86 * [Type Conversion Macros][glib-Type-Conversion-Macros]
87 * @next: contains the link to the next element in the list.
89 * The #GSList struct is used for each element in the singly-linked
95 * @slist: an element in a #GSList.
97 * A convenience macro to get the next element in a #GSList.
99 * Returns: the next element, or %NULL if there are no more elements.
102 #define _g_slist_alloc0() g_slice_new0 (GSList)
103 #define _g_slist_alloc() g_slice_new (GSList)
104 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
109 * Allocates space for one #GSList element. It is called by the
110 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
111 * g_slist_insert_sorted() functions and so is rarely used on its own.
113 * Returns: a pointer to the newly-allocated #GSList element.
118 return _g_slist_alloc0 ();
125 * Frees all of the memory used by a #GSList.
126 * The freed elements are returned to the slice allocator.
128 * If list elements contain dynamically-allocated memory,
129 * you should either use g_slist_free_full() or free them manually
133 g_slist_free (GSList *list)
135 g_slice_free_chain (GSList, list, next);
140 * @list: a #GSList element
142 * Frees one #GSList element.
143 * It is usually used after g_slist_remove_link().
148 * A macro which does the same as g_slist_free_1().
153 g_slist_free_1 (GSList *list)
155 _g_slist_free1 (list);
160 * @list: a pointer to a #GSList
161 * @free_func: the function to be called to free each element's data
163 * Convenience method, which frees all the memory used by a #GSList, and
164 * calls the specified destroy function on every element's data.
169 g_slist_free_full (GSList *list,
170 GDestroyNotify free_func)
172 g_slist_foreach (list, (GFunc) free_func, NULL);
179 * @data: the data for the new element
181 * Adds a new element on to the end of the list.
183 * The return value is the new start of the list, which may
184 * have changed, so make sure you store the new value.
186 * Note that g_slist_append() has to traverse the entire list
187 * to find the end, which is inefficient when adding multiple
188 * elements. A common idiom to avoid the inefficiency is to prepend
189 * the elements and reverse the list when all elements have been added.
191 * |[<!-- language="C" -->
192 * // Notice that these are initialized to the empty list.
193 * GSList *list = NULL, *number_list = NULL;
195 * // This is a list of strings.
196 * list = g_slist_append (list, "first");
197 * list = g_slist_append (list, "second");
199 * // This is a list of integers.
200 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
201 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
204 * Returns: the new start of the #GSList
207 g_slist_append (GSList *list,
213 new_list = _g_slist_alloc ();
214 new_list->data = data;
215 new_list->next = NULL;
219 last = g_slist_last (list);
220 /* g_assert (last != NULL); */
221 last->next = new_list;
232 * @data: the data for the new element
234 * Adds a new element on to the start of the list.
236 * The return value is the new start of the list, which
237 * may have changed, so make sure you store the new value.
239 * |[<!-- language="C" -->
240 * // Notice that it is initialized to the empty list.
241 * GSList *list = NULL;
242 * list = g_slist_prepend (list, "last");
243 * list = g_slist_prepend (list, "first");
246 * Returns: the new start of the #GSList
249 g_slist_prepend (GSList *list,
254 new_list = _g_slist_alloc ();
255 new_list->data = data;
256 new_list->next = list;
264 * @data: the data for the new element
265 * @position: the position to insert the element.
266 * If this is negative, or is larger than the number
267 * of elements in the list, the new element is added on
268 * to the end of the list.
270 * Inserts a new element into the list at the given position.
272 * Returns: the new start of the #GSList
275 g_slist_insert (GSList *list,
284 return g_slist_append (list, data);
285 else if (position == 0)
286 return g_slist_prepend (list, data);
288 new_list = _g_slist_alloc ();
289 new_list->data = data;
293 new_list->next = NULL;
300 while ((position-- > 0) && tmp_list)
302 prev_list = tmp_list;
303 tmp_list = tmp_list->next;
306 new_list->next = prev_list->next;
307 prev_list->next = new_list;
313 * g_slist_insert_before:
315 * @sibling: node to insert @data before
316 * @data: data to put in the newly-inserted node
318 * Inserts a node before @sibling containing @data.
320 * Returns: the new head of the list.
323 g_slist_insert_before (GSList *slist,
329 slist = _g_slist_alloc ();
332 g_return_val_if_fail (sibling == NULL, slist);
337 GSList *node, *last = NULL;
339 for (node = slist; node; last = node, node = last->next)
344 node = _g_slist_alloc ();
352 node = _g_slist_alloc ();
354 node->next = last->next;
365 * @list2: the #GSList to add to the end of the first #GSList
367 * Adds the second #GSList onto the end of the first #GSList.
368 * Note that the elements of the second #GSList are not copied.
369 * They are used directly.
371 * Returns: the start of the new #GSList
374 g_slist_concat (GSList *list1, GSList *list2)
379 g_slist_last (list1)->next = list2;
390 * @data: the data of the element to remove
392 * Removes an element from a #GSList.
393 * If two elements contain the same data, only the first is removed.
394 * If none of the elements contain the data, the #GSList is unchanged.
396 * Returns: the new start of the #GSList
399 g_slist_remove (GSList *list,
402 GSList *tmp, *prev = NULL;
407 if (tmp->data == data)
410 prev->next = tmp->next;
414 g_slist_free_1 (tmp);
425 * g_slist_remove_all:
427 * @data: data to remove
429 * Removes all list nodes with data equal to @data.
430 * Returns the new head of the list. Contrast with
431 * g_slist_remove() which removes only the first node
432 * matching the given data.
434 * Returns: new head of @list
437 g_slist_remove_all (GSList *list,
440 GSList *tmp, *prev = NULL;
445 if (tmp->data == data)
447 GSList *next = tmp->next;
454 g_slist_free_1 (tmp);
467 static inline GSList*
468 _g_slist_remove_link (GSList *list,
482 prev->next = tmp->next;
498 * g_slist_remove_link:
500 * @link_: an element in the #GSList
502 * Removes an element from a #GSList, without
503 * freeing the element. The removed element's next
504 * link is set to %NULL, so that it becomes a
505 * self-contained list with one element.
507 * Removing arbitrary nodes from a singly-linked list
508 * requires time that is proportional to the length of the list
509 * (ie. O(n)). If you find yourself using g_slist_remove_link()
510 * frequently, you should consider a different data structure,
511 * such as the doubly-linked #GList.
513 * Returns: the new start of the #GSList, without the element
516 g_slist_remove_link (GSList *list,
519 return _g_slist_remove_link (list, link_);
523 * g_slist_delete_link:
525 * @link_: node to delete
527 * Removes the node link_ from the list and frees it.
528 * Compare this to g_slist_remove_link() which removes the node
529 * without freeing it.
531 * Removing arbitrary nodes from a singly-linked list requires time
532 * that is proportional to the length of the list (ie. O(n)). If you
533 * find yourself using g_slist_delete_link() frequently, you should
534 * consider a different data structure, such as the doubly-linked
537 * Returns: the new head of @list
540 g_slist_delete_link (GSList *list,
543 list = _g_slist_remove_link (list, link_);
544 _g_slist_free1 (link_);
555 * Note that this is a "shallow" copy. If the list elements
556 * consist of pointers to data, the pointers are copied but
557 * the actual data isn't. See g_slist_copy_deep() if you need
558 * to copy the data as well.
560 * Returns: a copy of @list
563 g_slist_copy (GSList *list)
565 return g_slist_copy_deep (list, NULL, NULL);
571 * @func: a copy function used to copy every element in the list
572 * @user_data: user data passed to the copy function @func, or #NULL
574 * Makes a full (deep) copy of a #GSList.
576 * In contrast with g_slist_copy(), this function uses @func to make a copy of
577 * each list element, in addition to copying the list container itself.
579 * @func, as a #GCopyFunc, takes two arguments, the data to be copied and a user
580 * pointer. It's safe to pass #NULL as user_data, if the copy function takes only
583 * For instance, if @list holds a list of GObjects, you can do:
584 * |[<!-- language="C" -->
585 * another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
588 * And, to entirely free the new list, you could do:
589 * |[<!-- language="C" -->
590 * g_slist_free_full (another_list, g_object_unref);
593 * Returns: a full copy of @list, use #g_slist_free_full to free it
598 g_slist_copy_deep (GSList *list, GCopyFunc func, gpointer user_data)
600 GSList *new_list = NULL;
606 new_list = _g_slist_alloc ();
608 new_list->data = func (list->data, user_data);
610 new_list->data = list->data;
615 last->next = _g_slist_alloc ();
618 last->data = func (list->data, user_data);
620 last->data = list->data;
633 * Reverses a #GSList.
635 * Returns: the start of the reversed #GSList
638 g_slist_reverse (GSList *list)
644 GSList *next = list->next;
658 * @n: the position of the element, counting from 0
660 * Gets the element at the given position in a #GSList.
662 * Returns: the element, or %NULL if the position is off
663 * the end of the #GSList
666 g_slist_nth (GSList *list,
669 while (n-- > 0 && list)
678 * @n: the position of the element
680 * Gets the data of the element at the given position.
682 * Returns: the element's data, or %NULL if the position
683 * is off the end of the #GSList
686 g_slist_nth_data (GSList *list,
689 while (n-- > 0 && list)
692 return list ? list->data : NULL;
698 * @data: the element data to find
700 * Finds the element in a #GSList which
701 * contains the given data.
703 * Returns: the found #GSList element,
704 * or %NULL if it is not found
707 g_slist_find (GSList *list,
712 if (list->data == data)
722 * g_slist_find_custom:
724 * @data: user data passed to the function
725 * @func: the function to call for each element.
726 * It should return 0 when the desired element is found
728 * Finds an element in a #GSList, using a supplied function to
729 * find the desired element. It iterates over the list, calling
730 * the given function which should return 0 when the desired
731 * element is found. The function takes two #gconstpointer arguments,
732 * the #GSList element's data as the first argument and the
735 * Returns: the found #GSList element, or %NULL if it is not found
738 g_slist_find_custom (GSList *list,
742 g_return_val_if_fail (func != NULL, list);
746 if (! func (list->data, data))
757 * @llink: an element in the #GSList
759 * Gets the position of the given element
760 * in the #GSList (starting from 0).
762 * Returns: the position of the element in the #GSList,
763 * or -1 if the element is not found
766 g_slist_position (GSList *list,
786 * @data: the data to find
788 * Gets the position of the element containing
789 * the given data (starting from 0).
791 * Returns: the index of the element containing the data,
792 * or -1 if the data is not found
795 g_slist_index (GSList *list,
803 if (list->data == data)
816 * Gets the last element in a #GSList.
818 * This function iterates over the whole list.
820 * Returns: the last element in the #GSList,
821 * or %NULL if the #GSList has no elements
824 g_slist_last (GSList *list)
839 * Gets the number of elements in a #GSList.
841 * This function iterates over the whole list to
842 * count its elements.
844 * Returns: the number of elements in the #GSList
847 g_slist_length (GSList *list)
864 * @func: the function to call with each element's data
865 * @user_data: user data to pass to the function
867 * Calls a function for each element of a #GSList.
870 g_slist_foreach (GSList *list,
876 GSList *next = list->next;
877 (*func) (list->data, user_data);
883 g_slist_insert_sorted_real (GSList *list,
888 GSList *tmp_list = list;
889 GSList *prev_list = NULL;
893 g_return_val_if_fail (func != NULL, list);
897 new_list = _g_slist_alloc ();
898 new_list->data = data;
899 new_list->next = NULL;
903 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
905 while ((tmp_list->next) && (cmp > 0))
907 prev_list = tmp_list;
908 tmp_list = tmp_list->next;
910 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
913 new_list = _g_slist_alloc ();
914 new_list->data = data;
916 if ((!tmp_list->next) && (cmp > 0))
918 tmp_list->next = new_list;
919 new_list->next = NULL;
925 prev_list->next = new_list;
926 new_list->next = tmp_list;
931 new_list->next = list;
937 * g_slist_insert_sorted:
939 * @data: the data for the new element
940 * @func: the function to compare elements in the list.
941 * It should return a number > 0 if the first parameter
942 * comes after the second parameter in the sort order.
944 * Inserts a new element into the list, using the given
945 * comparison function to determine its position.
947 * Returns: the new start of the #GSList
950 g_slist_insert_sorted (GSList *list,
954 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
958 * g_slist_insert_sorted_with_data:
960 * @data: the data for the new element
961 * @func: the function to compare elements in the list.
962 * It should return a number > 0 if the first parameter
963 * comes after the second parameter in the sort order.
964 * @user_data: data to pass to comparison function
966 * Inserts a new element into the list, using the given
967 * comparison function to determine its position.
969 * Returns: the new start of the #GSList
974 g_slist_insert_sorted_with_data (GSList *list,
976 GCompareDataFunc func,
979 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
983 g_slist_sort_merge (GSList *l1,
995 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1008 l->next= l1 ? l1 : l2;
1014 g_slist_sort_real (GSList *list,
1028 while ((l2 = l2->next) != NULL)
1030 if ((l2 = l2->next) == NULL)
1037 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1038 g_slist_sort_real (l2, compare_func, user_data),
1046 * @compare_func: the comparison function used to sort the #GSList.
1047 * This function is passed the data from 2 elements of the #GSList
1048 * and should return 0 if they are equal, a negative value if the
1049 * first element comes before the second, or a positive value if
1050 * the first element comes after the second.
1052 * Sorts a #GSList using the given comparison function.
1054 * Returns: the start of the sorted #GSList
1057 g_slist_sort (GSList *list,
1058 GCompareFunc compare_func)
1060 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1064 * g_slist_sort_with_data:
1066 * @compare_func: comparison function
1067 * @user_data: data to pass to comparison function
1069 * Like g_slist_sort(), but the sort function accepts a user data argument.
1071 * Returns: new head of the list
1074 g_slist_sort_with_data (GSList *list,
1075 GCompareDataFunc compare_func,
1078 return g_slist_sort_real (list, (GFunc) compare_func, user_data);