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 g_slist_free (GSList *list)
166 g_slice_free_chain (GSList, list, next);
171 * @list: a #GSList element
173 * Frees one #GSList element.
174 * It is usually used after g_slist_remove_link().
179 * A macro which does the same as g_slist_free_1().
184 g_slist_free_1 (GSList *list)
186 _g_slist_free1 (list);
191 * @list: a pointer to a #GSList
192 * @free_func: the function to be called to free each element's data
194 * Convenience method, which frees all the memory used by a #GSList, and
195 * calls the specified destroy function on every element's data.
200 g_slist_free_full (GSList *list,
201 GDestroyNotify free_func)
203 g_slist_foreach (list, (GFunc) free_func, NULL);
210 * @data: the data for the new element
212 * Adds a new element on to the end of the list.
215 * The return value is the new start of the list, which may
216 * have changed, so make sure you store the new value.
220 * Note that g_slist_append() has to traverse the entire list
221 * to find the end, which is inefficient when adding multiple
222 * elements. A common idiom to avoid the inefficiency is to prepend
223 * the elements and reverse the list when all elements have been added.
227 * /* Notice that these are initialized to the empty list. */
228 * GSList *list = NULL, *number_list = NULL;
230 * /* This is a list of strings. */
231 * list = g_slist_append (list, "first");
232 * list = g_slist_append (list, "second");
234 * /* This is a list of integers. */
235 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
236 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
239 * Returns: the new start of the #GSList
242 g_slist_append (GSList *list,
248 new_list = _g_slist_alloc ();
249 new_list->data = data;
250 new_list->next = NULL;
254 last = g_slist_last (list);
255 /* g_assert (last != NULL); */
256 last->next = new_list;
267 * @data: the data for the new element
269 * Adds a new element on to the start of the list.
272 * The return value is the new start of the list, which
273 * may have changed, so make sure you store the new value.
277 * /* Notice that it is initialized to the empty list. */
278 * GSList *list = NULL;
279 * list = g_slist_prepend (list, "last");
280 * list = g_slist_prepend (list, "first");
283 * Returns: the new start of the #GSList
286 g_slist_prepend (GSList *list,
291 new_list = _g_slist_alloc ();
292 new_list->data = data;
293 new_list->next = list;
301 * @data: the data for the new element
302 * @position: the position to insert the element.
303 * If this is negative, or is larger than the number
304 * of elements in the list, the new element is added on
305 * to the end of the list.
307 * Inserts a new element into the list at the given position.
309 * Returns: the new start of the #GSList
312 g_slist_insert (GSList *list,
321 return g_slist_append (list, data);
322 else if (position == 0)
323 return g_slist_prepend (list, data);
325 new_list = _g_slist_alloc ();
326 new_list->data = data;
330 new_list->next = NULL;
337 while ((position-- > 0) && tmp_list)
339 prev_list = tmp_list;
340 tmp_list = tmp_list->next;
345 new_list->next = prev_list->next;
346 prev_list->next = new_list;
350 new_list->next = list;
358 * g_slist_insert_before:
360 * @sibling: node to insert @data before
361 * @data: data to put in the newly-inserted node
363 * Inserts a node before @sibling containing @data.
365 * Returns: the new head of the list.
368 g_slist_insert_before (GSList *slist,
374 slist = _g_slist_alloc ();
377 g_return_val_if_fail (sibling == NULL, slist);
382 GSList *node, *last = NULL;
384 for (node = slist; node; last = node, node = last->next)
389 node = _g_slist_alloc ();
397 node = _g_slist_alloc ();
399 node->next = last->next;
410 * @list2: the #GSList to add to the end of the first #GSList
412 * Adds the second #GSList onto the end of the first #GSList.
413 * Note that the elements of the second #GSList are not copied.
414 * They are used directly.
416 * Returns: the start of the new #GSList
419 g_slist_concat (GSList *list1, GSList *list2)
424 g_slist_last (list1)->next = list2;
435 * @data: the data of the element to remove
437 * Removes an element from a #GSList.
438 * If two elements contain the same data, only the first is removed.
439 * If none of the elements contain the data, the #GSList is unchanged.
441 * Returns: the new start of the #GSList
444 g_slist_remove (GSList *list,
447 GSList *tmp, *prev = NULL;
452 if (tmp->data == data)
455 prev->next = tmp->next;
459 g_slist_free_1 (tmp);
470 * g_slist_remove_all:
472 * @data: data to remove
474 * Removes all list nodes with data equal to @data.
475 * Returns the new head of the list. Contrast with
476 * g_slist_remove() which removes only the first node
477 * matching the given data.
479 * Returns: new head of @list
482 g_slist_remove_all (GSList *list,
485 GSList *tmp, *prev = NULL;
490 if (tmp->data == data)
492 GSList *next = tmp->next;
499 g_slist_free_1 (tmp);
512 static inline GSList*
513 _g_slist_remove_link (GSList *list,
527 prev->next = tmp->next;
543 * g_slist_remove_link:
545 * @link_: an element in the #GSList
547 * Removes an element from a #GSList, without
548 * freeing the element. The removed element's next
549 * link is set to %NULL, so that it becomes a
550 * self-contained list with one element.
552 * Returns: the new start of the #GSList, without the element
555 g_slist_remove_link (GSList *list,
558 return _g_slist_remove_link (list, link_);
562 * g_slist_delete_link:
564 * @link_: node to delete
566 * Removes the node link_ from the list and frees it.
567 * Compare this to g_slist_remove_link() which removes the node
568 * without freeing it.
570 * Returns: the new head of @list
573 g_slist_delete_link (GSList *list,
576 list = _g_slist_remove_link (list, link_);
577 _g_slist_free1 (link_);
589 * Note that this is a "shallow" copy. If the list elements
590 * consist of pointers to data, the pointers are copied but
591 * the actual data isn't.
594 * Returns: a copy of @list
597 g_slist_copy (GSList *list)
599 GSList *new_list = NULL;
605 new_list = _g_slist_alloc ();
606 new_list->data = list->data;
611 last->next = _g_slist_alloc ();
613 last->data = list->data;
626 * Reverses a #GSList.
628 * Returns: the start of the reversed #GSList
631 g_slist_reverse (GSList *list)
637 GSList *next = list->next;
651 * @n: the position of the element, counting from 0
653 * Gets the element at the given position in a #GSList.
655 * Returns: the element, or %NULL if the position is off
656 * the end of the #GSList
659 g_slist_nth (GSList *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 #GSList
679 g_slist_nth_data (GSList *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 #GSList which
694 * contains the given data.
696 * Returns: the found #GSList element,
697 * or %NULL if it is not found
700 g_slist_find (GSList *list,
705 if (list->data == data)
715 * g_slist_find_custom:
717 * @data: user data passed to the function
718 * @func: the function to call for each element.
719 * It should return 0 when the desired element is found
721 * Finds an element in a #GSList, using a supplied function to
722 * find the desired element. It iterates over the list, calling
723 * the given function which should return 0 when the desired
724 * element is found. The function takes two #gconstpointer arguments,
725 * the #GSList element's data as the first argument and the
728 * Returns: the found #GSList element, or %NULL if it is not found
731 g_slist_find_custom (GSList *list,
735 g_return_val_if_fail (func != NULL, list);
739 if (! func (list->data, data))
750 * @llink: an element in the #GSList
752 * Gets the position of the given element
753 * in the #GSList (starting from 0).
755 * Returns: the position of the element in the #GSList,
756 * or -1 if the element is not found
759 g_slist_position (GSList *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_slist_index (GSList *list,
796 if (list->data == data)
809 * Gets the last element in a #GSList.
812 * This function iterates over the whole list.
815 * Returns: the last element in the #GSList,
816 * or %NULL if the #GSList has no elements
819 g_slist_last (GSList *list)
834 * Gets the number of elements in a #GSList.
837 * This function iterates over the whole list to
838 * count its elements.
841 * Returns: the number of elements in the #GSList
844 g_slist_length (GSList *list)
861 * @func: the function to call with each element's data
862 * @user_data: user data to pass to the function
864 * Calls a function for each element of a #GSList.
867 g_slist_foreach (GSList *list,
873 GSList *next = list->next;
874 (*func) (list->data, user_data);
880 g_slist_insert_sorted_real (GSList *list,
885 GSList *tmp_list = list;
886 GSList *prev_list = NULL;
890 g_return_val_if_fail (func != NULL, list);
894 new_list = _g_slist_alloc ();
895 new_list->data = data;
896 new_list->next = NULL;
900 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
902 while ((tmp_list->next) && (cmp > 0))
904 prev_list = tmp_list;
905 tmp_list = tmp_list->next;
907 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
910 new_list = _g_slist_alloc ();
911 new_list->data = data;
913 if ((!tmp_list->next) && (cmp > 0))
915 tmp_list->next = new_list;
916 new_list->next = NULL;
922 prev_list->next = new_list;
923 new_list->next = tmp_list;
928 new_list->next = list;
934 * g_slist_insert_sorted:
936 * @data: the data for the new element
937 * @func: the function to compare elements in the list.
938 * It should return a number > 0 if the first parameter
939 * comes after the second parameter in the sort order.
941 * Inserts a new element into the list, using the given
942 * comparison function to determine its position.
944 * Returns: the new start of the #GSList
947 g_slist_insert_sorted (GSList *list,
951 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
955 * g_slist_insert_sorted_with_data:
957 * @data: the data for the new element
958 * @func: the function to compare elements in the list.
959 * It should return a number > 0 if the first parameter
960 * comes after the second parameter in the sort order.
961 * @user_data: data to pass to comparison function
963 * Inserts a new element into the list, using the given
964 * comparison function to determine its position.
966 * Returns: the new start of the #GSList
971 g_slist_insert_sorted_with_data (GSList *list,
973 GCompareDataFunc func,
976 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
980 g_slist_sort_merge (GSList *l1,
992 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1005 l->next= l1 ? l1 : l2;
1011 g_slist_sort_real (GSList *list,
1025 while ((l2 = l2->next) != NULL)
1027 if ((l2 = l2->next) == NULL)
1034 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1035 g_slist_sort_real (l2, compare_func, user_data),
1043 * @compare_func: the comparison function used to sort the #GSList.
1044 * This function is passed the data from 2 elements of the #GSList
1045 * and should return 0 if they are equal, a negative value if the
1046 * first element comes before the second, or a positive value if
1047 * the first element comes after the second.
1049 * Sorts a #GSList using the given comparison function.
1051 * Returns: the start of the sorted #GSList
1054 g_slist_sort (GSList *list,
1055 GCompareFunc compare_func)
1057 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1061 * g_slist_sort_with_data:
1063 * @compare_func: comparison function
1064 * @user_data: data to pass to comparison function
1066 * Like g_slist_sort(), but the sort function accepts a user data argument.
1068 * Returns: new head of the list
1071 g_slist_sort_with_data (GSList *list,
1072 GCompareDataFunc compare_func,
1075 return g_slist_sort_real (list, (GFunc) compare_func, user_data);