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/.
34 #include "gtestutils.h"
37 * SECTION:linked_lists_single
38 * @title: Singly-Linked Lists
39 * @short_description: linked lists containing integer values or
40 * pointers to data, limited to iterating over the
41 * list in one direction
43 * The #GSList structure and its associated functions provide a
44 * standard singly-linked list data structure.
46 * Each element in the list contains a piece of data, together with a
47 * pointer which links to the next element in the list. Using this
48 * pointer it is possible to move through the list in one direction
49 * only (unlike the <link
50 * linkend="glib-Doubly-Linked-Lists">Doubly-Linked Lists</link> which
51 * allow movement in both directions).
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 #GSList functions expect to be passed a
62 * pointer 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 #GSList. %NULL is considered to be
66 * the empty list so you simply set a #GSList* to %NULL.
68 * To add elements, use g_slist_append(), g_slist_prepend(),
69 * g_slist_insert() and g_slist_insert_sorted().
71 * To remove elements, use g_slist_remove().
73 * To find elements in the list use g_slist_last(), g_slist_next(),
74 * g_slist_nth(), g_slist_nth_data(), g_slist_find() and
75 * g_slist_find_custom().
77 * To find the index of an element use g_slist_position() and
80 * To call a function for each element in the list use
83 * To free the entire list, use g_slist_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.
94 * The #GSList struct is used for each element in the singly-linked
100 * @slist: an element in a #GSList.
101 * @Returns: the next element, or %NULL if there are no more elements.
103 * A convenience macro to get the next element in a #GSList.
108 * g_slist_push_allocator:
109 * @dummy: the #GAllocator to use when allocating #GSList elements.
111 * Sets the allocator to use to allocate #GSList elements. Use
112 * g_slist_pop_allocator() to restore the previous allocator.
114 * Note that this function is not available if GLib has been compiled
115 * with <option>--disable-mem-pools</option>
117 * Deprecated: 2.10: It does nothing, since #GSList has been converted
118 * to the <link linkend="glib-Memory-Slices">slice
121 void g_slist_push_allocator (gpointer dummy) { /* present for binary compat only */ }
124 * g_slist_pop_allocator:
126 * Restores the previous #GAllocator, used when allocating #GSList
129 * Note that this function is not available if GLib has been compiled
130 * with <option>--disable-mem-pools</option>
132 * Deprecated: 2.10: It does nothing, since #GSList has been converted
133 * to the <link linkend="glib-Memory-Slices">slice
136 void g_slist_pop_allocator (void) { /* present for binary compat only */ }
138 #define _g_slist_alloc0() g_slice_new0 (GSList)
139 #define _g_slist_alloc() g_slice_new (GSList)
140 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
144 * @Returns: a pointer to the newly-allocated #GSList element.
146 * Allocates space for one #GSList element. It is called by the
147 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
148 * g_slist_insert_sorted() functions and so is rarely used on its own.
153 return _g_slist_alloc0 ();
160 * Frees all of the memory used by a #GSList.
161 * The freed elements are returned to the slice allocator.
164 * If list elements contain dynamically-allocated memory,
165 * you should either use g_slist_free_full() or free them manually
170 g_slist_free (GSList *list)
172 g_slice_free_chain (GSList, list, next);
177 * @list: a #GSList element
179 * Frees one #GSList element.
180 * It is usually used after g_slist_remove_link().
185 * A macro which does the same as g_slist_free_1().
190 g_slist_free_1 (GSList *list)
192 _g_slist_free1 (list);
197 * @list: a pointer to a #GSList
198 * @free_func: the function to be called to free each element's data
200 * Convenience method, which frees all the memory used by a #GSList, and
201 * calls the specified destroy function on every element's data.
206 g_slist_free_full (GSList *list,
207 GDestroyNotify free_func)
209 g_slist_foreach (list, (GFunc) free_func, NULL);
216 * @data: the data for the new element
218 * Adds a new element on to the end of the list.
221 * The return value is the new start of the list, which may
222 * have changed, so make sure you store the new value.
226 * Note that g_slist_append() has to traverse the entire list
227 * to find the end, which is inefficient when adding multiple
228 * elements. A common idiom to avoid the inefficiency is to prepend
229 * the elements and reverse the list when all elements have been added.
233 * /* Notice that these are initialized to the empty list. */
234 * GSList *list = NULL, *number_list = NULL;
236 * /* This is a list of strings. */
237 * list = g_slist_append (list, "first");
238 * list = g_slist_append (list, "second");
240 * /* This is a list of integers. */
241 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
242 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
245 * Returns: the new start of the #GSList
248 g_slist_append (GSList *list,
254 new_list = _g_slist_alloc ();
255 new_list->data = data;
256 new_list->next = NULL;
260 last = g_slist_last (list);
261 /* g_assert (last != NULL); */
262 last->next = new_list;
273 * @data: the data for the new element
275 * Adds a new element on to the start of the list.
278 * The return value is the new start of the list, which
279 * may have changed, so make sure you store the new value.
283 * /* Notice that it is initialized to the empty list. */
284 * GSList *list = NULL;
285 * list = g_slist_prepend (list, "last");
286 * list = g_slist_prepend (list, "first");
289 * Returns: the new start of the #GSList
292 g_slist_prepend (GSList *list,
297 new_list = _g_slist_alloc ();
298 new_list->data = data;
299 new_list->next = list;
307 * @data: the data for the new element
308 * @position: the position to insert the element.
309 * If this is negative, or is larger than the number
310 * of elements in the list, the new element is added on
311 * to the end of the list.
313 * Inserts a new element into the list at the given position.
315 * Returns: the new start of the #GSList
318 g_slist_insert (GSList *list,
327 return g_slist_append (list, data);
328 else if (position == 0)
329 return g_slist_prepend (list, data);
331 new_list = _g_slist_alloc ();
332 new_list->data = data;
336 new_list->next = NULL;
343 while ((position-- > 0) && tmp_list)
345 prev_list = tmp_list;
346 tmp_list = tmp_list->next;
351 new_list->next = prev_list->next;
352 prev_list->next = new_list;
356 new_list->next = list;
364 * g_slist_insert_before:
366 * @sibling: node to insert @data before
367 * @data: data to put in the newly-inserted node
369 * Inserts a node before @sibling containing @data.
371 * Returns: the new head of the list.
374 g_slist_insert_before (GSList *slist,
380 slist = _g_slist_alloc ();
383 g_return_val_if_fail (sibling == NULL, slist);
388 GSList *node, *last = NULL;
390 for (node = slist; node; last = node, node = last->next)
395 node = _g_slist_alloc ();
403 node = _g_slist_alloc ();
405 node->next = last->next;
416 * @list2: the #GSList to add to the end of the first #GSList
418 * Adds the second #GSList onto the end of the first #GSList.
419 * Note that the elements of the second #GSList are not copied.
420 * They are used directly.
422 * Returns: the start of the new #GSList
425 g_slist_concat (GSList *list1, GSList *list2)
430 g_slist_last (list1)->next = list2;
441 * @data: the data of the element to remove
443 * Removes an element from a #GSList.
444 * If two elements contain the same data, only the first is removed.
445 * If none of the elements contain the data, the #GSList is unchanged.
447 * Returns: the new start of the #GSList
450 g_slist_remove (GSList *list,
453 GSList *tmp, *prev = NULL;
458 if (tmp->data == data)
461 prev->next = tmp->next;
465 g_slist_free_1 (tmp);
476 * g_slist_remove_all:
478 * @data: data to remove
480 * Removes all list nodes with data equal to @data.
481 * Returns the new head of the list. Contrast with
482 * g_slist_remove() which removes only the first node
483 * matching the given data.
485 * Returns: new head of @list
488 g_slist_remove_all (GSList *list,
491 GSList *tmp, *prev = NULL;
496 if (tmp->data == data)
498 GSList *next = tmp->next;
505 g_slist_free_1 (tmp);
518 static inline GSList*
519 _g_slist_remove_link (GSList *list,
533 prev->next = tmp->next;
549 * g_slist_remove_link:
551 * @link_: an element in the #GSList
553 * Removes an element from a #GSList, without
554 * freeing the element. The removed element's next
555 * link is set to %NULL, so that it becomes a
556 * self-contained list with one element.
558 * Returns: the new start of the #GSList, without the element
561 g_slist_remove_link (GSList *list,
564 return _g_slist_remove_link (list, link_);
568 * g_slist_delete_link:
570 * @link_: node to delete
572 * Removes the node link_ from the list and frees it.
573 * Compare this to g_slist_remove_link() which removes the node
574 * without freeing it.
576 * Returns: the new head of @list
579 g_slist_delete_link (GSList *list,
582 list = _g_slist_remove_link (list, link_);
583 _g_slist_free1 (link_);
595 * Note that this is a "shallow" copy. If the list elements
596 * consist of pointers to data, the pointers are copied but
597 * the actual data isn't.
600 * Returns: a copy of @list
603 g_slist_copy (GSList *list)
605 GSList *new_list = NULL;
611 new_list = _g_slist_alloc ();
612 new_list->data = list->data;
617 last->next = _g_slist_alloc ();
619 last->data = list->data;
632 * Reverses a #GSList.
634 * Returns: the start of the reversed #GSList
637 g_slist_reverse (GSList *list)
643 GSList *next = list->next;
657 * @n: the position of the element, counting from 0
659 * Gets the element at the given position in a #GSList.
661 * Returns: the element, or %NULL if the position is off
662 * the end of the #GSList
665 g_slist_nth (GSList *list,
668 while (n-- > 0 && list)
677 * @n: the position of the element
679 * Gets the data of the element at the given position.
681 * Returns: the element's data, or %NULL if the position
682 * is off the end of the #GSList
685 g_slist_nth_data (GSList *list,
688 while (n-- > 0 && list)
691 return list ? list->data : NULL;
697 * @data: the element data to find
699 * Finds the element in a #GSList which
700 * contains the given data.
702 * Returns: the found #GSList element,
703 * or %NULL if it is not found
706 g_slist_find (GSList *list,
711 if (list->data == data)
721 * g_slist_find_custom:
723 * @data: user data passed to the function
724 * @func: the function to call for each element.
725 * It should return 0 when the desired element is found
727 * Finds an element in a #GSList, using a supplied function to
728 * find the desired element. It iterates over the list, calling
729 * the given function which should return 0 when the desired
730 * element is found. The function takes two #gconstpointer arguments,
731 * the #GSList element's data as the first argument and the
734 * Returns: the found #GSList element, or %NULL if it is not found
737 g_slist_find_custom (GSList *list,
741 g_return_val_if_fail (func != NULL, list);
745 if (! func (list->data, data))
756 * @llink: an element in the #GSList
758 * Gets the position of the given element
759 * in the #GSList (starting from 0).
761 * Returns: the position of the element in the #GSList,
762 * or -1 if the element is not found
765 g_slist_position (GSList *list,
785 * @data: the data to find
787 * Gets the position of the element containing
788 * the given data (starting from 0).
790 * Returns: the index of the element containing the data,
791 * or -1 if the data is not found
794 g_slist_index (GSList *list,
802 if (list->data == data)
815 * Gets the last element in a #GSList.
818 * This function iterates over the whole list.
821 * Returns: the last element in the #GSList,
822 * or %NULL if the #GSList has no elements
825 g_slist_last (GSList *list)
840 * Gets the number of elements in a #GSList.
843 * This function iterates over the whole list to
844 * count its elements.
847 * Returns: the number of elements in the #GSList
850 g_slist_length (GSList *list)
867 * @func: the function to call with each element's data
868 * @user_data: user data to pass to the function
870 * Calls a function for each element of a #GSList.
873 g_slist_foreach (GSList *list,
879 GSList *next = list->next;
880 (*func) (list->data, user_data);
886 g_slist_insert_sorted_real (GSList *list,
891 GSList *tmp_list = list;
892 GSList *prev_list = NULL;
896 g_return_val_if_fail (func != NULL, list);
900 new_list = _g_slist_alloc ();
901 new_list->data = data;
902 new_list->next = NULL;
906 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
908 while ((tmp_list->next) && (cmp > 0))
910 prev_list = tmp_list;
911 tmp_list = tmp_list->next;
913 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
916 new_list = _g_slist_alloc ();
917 new_list->data = data;
919 if ((!tmp_list->next) && (cmp > 0))
921 tmp_list->next = new_list;
922 new_list->next = NULL;
928 prev_list->next = new_list;
929 new_list->next = tmp_list;
934 new_list->next = list;
940 * g_slist_insert_sorted:
942 * @data: the data for the new element
943 * @func: the function to compare elements in the list.
944 * It should return a number > 0 if the first parameter
945 * comes after the second parameter in the sort order.
947 * Inserts a new element into the list, using the given
948 * comparison function to determine its position.
950 * Returns: the new start of the #GSList
953 g_slist_insert_sorted (GSList *list,
957 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
961 * g_slist_insert_sorted_with_data:
963 * @data: the data for the new element
964 * @func: the function to compare elements in the list.
965 * It should return a number > 0 if the first parameter
966 * comes after the second parameter in the sort order.
967 * @user_data: data to pass to comparison function
969 * Inserts a new element into the list, using the given
970 * comparison function to determine its position.
972 * Returns: the new start of the #GSList
977 g_slist_insert_sorted_with_data (GSList *list,
979 GCompareDataFunc func,
982 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
986 g_slist_sort_merge (GSList *l1,
998 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1011 l->next= l1 ? l1 : l2;
1017 g_slist_sort_real (GSList *list,
1031 while ((l2 = l2->next) != NULL)
1033 if ((l2 = l2->next) == NULL)
1040 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1041 g_slist_sort_real (l2, compare_func, user_data),
1049 * @compare_func: the comparison function used to sort the #GSList.
1050 * This function is passed the data from 2 elements of the #GSList
1051 * and should return 0 if they are equal, a negative value if the
1052 * first element comes before the second, or a positive value if
1053 * the first element comes after the second.
1055 * Sorts a #GSList using the given comparison function.
1057 * Returns: the start of the sorted #GSList
1060 g_slist_sort (GSList *list,
1061 GCompareFunc compare_func)
1063 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1067 * g_slist_sort_with_data:
1069 * @compare_func: comparison function
1070 * @user_data: data to pass to comparison function
1072 * Like g_slist_sort(), but the sort function accepts a user data argument.
1074 * Returns: new head of the list
1077 g_slist_sort_with_data (GSList *list,
1078 GCompareDataFunc compare_func,
1081 return g_slist_sort_real (list, (GFunc) compare_func, user_data);