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 * SECTION: linked_lists_single
37 * @title: Singly-Linked Lists
38 * @short_description: linked lists containing integer values or
39 * pointers to data, limited to iterating over the
40 * list in one direction
42 * The #GSList structure and its associated functions provide a
43 * standard singly-linked list data structure.
45 * Each element in the list contains a piece of data, together with a
46 * pointer which links to the next element in the list. Using this
47 * pointer it is possible to move through the list in one direction
48 * only (unlike the <link
49 * linkend="glib-Doubly-Linked-lists">Doubly-Linked Lists</link> which
50 * allow movement in both directions).
52 * The data contained in each element can be either integer values, by
53 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
54 * Conversion Macros</link>, or simply pointers to any type of data.
56 * List elements are allocated from the <link
57 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
58 * efficient than allocating elements individually.
60 * Note that most of the #GSList functions expect to be passed a
61 * pointer to the first element in the list. The functions which insert
62 * elements return the new start of the list, which may have changed.
64 * There is no function to create a #GSList. %NULL is considered to be
65 * the empty list so you simply set a #GSList* to %NULL.
67 * To add elements, use g_slist_append(), g_slist_prepend(),
68 * g_slist_insert() and g_slist_insert_sorted().
70 * To remove elements, use g_slist_remove().
72 * To find elements in the list use g_slist_last(), g_slist_next(),
73 * g_slist_nth(), g_slist_nth_data(), g_slist_find() and
74 * g_slist_find_custom().
76 * To find the index of an element use g_slist_position() and
79 * To call a function for each element in the list use
82 * To free the entire list, use g_slist_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.
93 * The #GSList struct is used for each element in the singly-linked
99 * @slist: an element in a #GSList.
100 * @Returns: the next element, or %NULL if there are no more elements.
102 * A convenience macro to get the next element in a #GSList.
107 * g_slist_push_allocator:
108 * @dummy: the #GAllocator to use when allocating #GSList elements.
110 * Sets the allocator to use to allocate #GSList elements. Use
111 * g_slist_pop_allocator() to restore the previous allocator.
113 * Note that this function is not available if GLib has been compiled
114 * with <option>--disable-mem-pools</option>
116 * Deprecated: 2.10: It does nothing, since #GSList has been converted
117 * to the <link linkend="glib-Memory-Slices">slice
120 void g_slist_push_allocator (gpointer dummy) { /* present for binary compat only */ }
123 * g_slist_pop_allocator:
125 * Restores the previous #GAllocator, used when allocating #GSList
128 * Note that this function is not available if GLib has been compiled
129 * with <option>--disable-mem-pools</option>
131 * Deprecated: 2.10: It does nothing, since #GSList has been converted
132 * to the <link linkend="glib-Memory-Slices">slice
135 void g_slist_pop_allocator (void) { /* present for binary compat only */ }
137 #define _g_slist_alloc0() g_slice_new0 (GSList)
138 #define _g_slist_alloc() g_slice_new (GSList)
139 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
143 * @Returns: a pointer to the newly-allocated #GSList element.
145 * Allocates space for one #GSList element. It is called by the
146 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
147 * g_slist_insert_sorted() functions and so is rarely used on its own.
152 return _g_slist_alloc0 ();
159 * Frees all of the memory used by a #GSList.
160 * The freed elements are returned to the slice allocator.
163 g_slist_free (GSList *list)
165 g_slice_free_chain (GSList, list, next);
170 * @list: a #GSList element
172 * Frees one #GSList element.
173 * It is usually used after g_slist_remove_link().
178 * A macro which does the same as g_slist_free_1().
183 g_slist_free_1 (GSList *list)
185 _g_slist_free1 (list);
191 * @data: the data for the new element
193 * Adds a new element on to the end of the list.
196 * The return value is the new start of the list, which may
197 * have changed, so make sure you store the new value.
201 * Note that g_slist_append() has to traverse the entire list
202 * to find the end, which is inefficient when adding multiple
203 * elements. A common idiom to avoid the inefficiency is to prepend
204 * the elements and reverse the list when all elements have been added.
208 * /* Notice that these are initialized to the empty list. */
209 * GSList *list = NULL, *number_list = NULL;
211 * /* This is a list of strings. */
212 * list = g_slist_append (list, "first");
213 * list = g_slist_append (list, "second");
215 * /* This is a list of integers. */
216 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
217 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
220 * Returns: the new start of the #GSList
223 g_slist_append (GSList *list,
229 new_list = _g_slist_alloc ();
230 new_list->data = data;
231 new_list->next = NULL;
235 last = g_slist_last (list);
236 /* g_assert (last != NULL); */
237 last->next = new_list;
248 * @data: the data for the new element
250 * Adds a new element on to the start of the list.
253 * The return value is the new start of the list, which
254 * may have changed, so make sure you store the new value.
258 * /* Notice that it is initialized to the empty list. */
259 * GSList *list = NULL;
260 * list = g_slist_prepend (list, "last");
261 * list = g_slist_prepend (list, "first");
264 * Returns: the new start of the #GSList
267 g_slist_prepend (GSList *list,
272 new_list = _g_slist_alloc ();
273 new_list->data = data;
274 new_list->next = list;
282 * @data: the data for the new element
283 * @position: the position to insert the element.
284 * If this is negative, or is larger than the number
285 * of elements in the list, the new element is added on
286 * to the end of the list.
288 * Inserts a new element into the list at the given position.
290 * Returns: the new start of the #GSList
293 g_slist_insert (GSList *list,
302 return g_slist_append (list, data);
303 else if (position == 0)
304 return g_slist_prepend (list, data);
306 new_list = _g_slist_alloc ();
307 new_list->data = data;
311 new_list->next = NULL;
318 while ((position-- > 0) && tmp_list)
320 prev_list = tmp_list;
321 tmp_list = tmp_list->next;
326 new_list->next = prev_list->next;
327 prev_list->next = new_list;
331 new_list->next = list;
339 * g_slist_insert_before:
341 * @sibling: node to insert @data before
342 * @data: data to put in the newly-inserted node
344 * Inserts a node before @sibling containing @data.
346 * Returns: the new head of the list.
349 g_slist_insert_before (GSList *slist,
355 slist = _g_slist_alloc ();
358 g_return_val_if_fail (sibling == NULL, slist);
363 GSList *node, *last = NULL;
365 for (node = slist; node; last = node, node = last->next)
370 node = _g_slist_alloc ();
378 node = _g_slist_alloc ();
380 node->next = last->next;
391 * @list2: the #GSList to add to the end of the first #GSList
393 * Adds the second #GSList onto the end of the first #GSList.
394 * Note that the elements of the second #GSList are not copied.
395 * They are used directly.
397 * Returns: the start of the new #GSList
400 g_slist_concat (GSList *list1, GSList *list2)
405 g_slist_last (list1)->next = list2;
416 * @data: the data of the element to remove
418 * Removes an element from a #GSList.
419 * If two elements contain the same data, only the first is removed.
420 * If none of the elements contain the data, the #GSList is unchanged.
422 * Returns: the new start of the #GSList
425 g_slist_remove (GSList *list,
428 GSList *tmp, *prev = NULL;
433 if (tmp->data == data)
436 prev->next = tmp->next;
440 g_slist_free_1 (tmp);
451 * g_slist_remove_all:
453 * @data: data to remove
455 * Removes all list nodes with data equal to @data.
456 * Returns the new head of the list. Contrast with
457 * g_slist_remove() which removes only the first node
458 * matching the given data.
460 * Returns: new head of @list
463 g_slist_remove_all (GSList *list,
466 GSList *tmp, *prev = NULL;
471 if (tmp->data == data)
473 GSList *next = tmp->next;
480 g_slist_free_1 (tmp);
493 static inline GSList*
494 _g_slist_remove_link (GSList *list,
508 prev->next = tmp->next;
524 * g_slist_remove_link:
526 * @link_: an element in the #GSList
528 * Removes an element from a #GSList, without
529 * freeing the element. The removed element's next
530 * link is set to %NULL, so that it becomes a
531 * self-contained list with one element.
533 * Returns: the new start of the #GSList, without the element
536 g_slist_remove_link (GSList *list,
539 return _g_slist_remove_link (list, link_);
543 * g_slist_delete_link:
545 * @link_: node to delete
547 * Removes the node link_ from the list and frees it.
548 * Compare this to g_slist_remove_link() which removes the node
549 * without freeing it.
551 * Returns: the new head of @list
554 g_slist_delete_link (GSList *list,
557 list = _g_slist_remove_link (list, link_);
558 _g_slist_free1 (link_);
570 * Note that this is a "shallow" copy. If the list elements
571 * consist of pointers to data, the pointers are copied but
572 * the actual data isn't.
575 * Returns: a copy of @list
578 g_slist_copy (GSList *list)
580 GSList *new_list = NULL;
586 new_list = _g_slist_alloc ();
587 new_list->data = list->data;
592 last->next = _g_slist_alloc ();
594 last->data = list->data;
607 * Reverses a #GSList.
609 * Returns: the start of the reversed #GSList
612 g_slist_reverse (GSList *list)
618 GSList *next = list->next;
632 * @n: the position of the element, counting from 0
634 * Gets the element at the given position in a #GSList.
636 * Returns: the element, or %NULL if the position is off
637 * the end of the #GSList
640 g_slist_nth (GSList *list,
643 while (n-- > 0 && list)
652 * @n: the position of the element
654 * Gets the data of the element at the given position.
656 * Returns: the element's data, or %NULL if the position
657 * is off the end of the #GSList
660 g_slist_nth_data (GSList *list,
663 while (n-- > 0 && list)
666 return list ? list->data : NULL;
672 * @data: the element data to find
674 * Finds the element in a #GSList which
675 * contains the given data.
677 * Returns: the found #GSList element,
678 * or %NULL if it is not found
681 g_slist_find (GSList *list,
686 if (list->data == data)
696 * g_slist_find_custom:
698 * @data: user data passed to the function
699 * @func: the function to call for each element.
700 * It should return 0 when the desired element is found
702 * Finds an element in a #GSList, using a supplied function to
703 * find the desired element. It iterates over the list, calling
704 * the given function which should return 0 when the desired
705 * element is found. The function takes two #gconstpointer arguments,
706 * the #GSList element's data as the first argument and the
709 * Returns: the found #GSList element, or %NULL if it is not found
712 g_slist_find_custom (GSList *list,
716 g_return_val_if_fail (func != NULL, list);
720 if (! func (list->data, data))
731 * @llink: an element in the #GSList
733 * Gets the position of the given element
734 * in the #GSList (starting from 0).
736 * Returns: the position of the element in the #GSList,
737 * or -1 if the element is not found
740 g_slist_position (GSList *list,
760 * @data: the data to find
762 * Gets the position of the element containing
763 * the given data (starting from 0).
765 * Returns: the index of the element containing the data,
766 * or -1 if the data is not found
769 g_slist_index (GSList *list,
777 if (list->data == data)
790 * Gets the last element in a #GSList.
793 * This function iterates over the whole list.
796 * Returns: the last element in the #GSList,
797 * or %NULL if the #GSList has no elements
800 g_slist_last (GSList *list)
815 * Gets the number of elements in a #GSList.
818 * This function iterates over the whole list to
819 * count its elements.
822 * Returns: the number of elements in the #GSList
825 g_slist_length (GSList *list)
842 * @func: the function to call with each element's data
843 * @user_data: user data to pass to the function
845 * Calls a function for each element of a #GSList.
848 g_slist_foreach (GSList *list,
854 GSList *next = list->next;
855 (*func) (list->data, user_data);
861 g_slist_insert_sorted_real (GSList *list,
866 GSList *tmp_list = list;
867 GSList *prev_list = NULL;
871 g_return_val_if_fail (func != NULL, list);
875 new_list = _g_slist_alloc ();
876 new_list->data = data;
877 new_list->next = NULL;
881 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
883 while ((tmp_list->next) && (cmp > 0))
885 prev_list = tmp_list;
886 tmp_list = tmp_list->next;
888 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
891 new_list = _g_slist_alloc ();
892 new_list->data = data;
894 if ((!tmp_list->next) && (cmp > 0))
896 tmp_list->next = new_list;
897 new_list->next = NULL;
903 prev_list->next = new_list;
904 new_list->next = tmp_list;
909 new_list->next = list;
915 * g_slist_insert_sorted:
917 * @data: the data for the new element
918 * @func: the function to compare elements in the list.
919 * It should return a number > 0 if the first parameter
920 * comes after the second parameter in the sort order.
922 * Inserts a new element into the list, using the given
923 * comparison function to determine its position.
925 * Returns: the new start of the #GSList
928 g_slist_insert_sorted (GSList *list,
932 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
936 * g_slist_insert_sorted_with_data:
938 * @data: the data for the new element
939 * @func: the function to compare elements in the list.
940 * It should return a number > 0 if the first parameter
941 * comes after the second parameter in the sort order.
942 * @user_data: data to pass to comparison function
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
952 g_slist_insert_sorted_with_data (GSList *list,
954 GCompareDataFunc func,
957 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
961 g_slist_sort_merge (GSList *l1,
973 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
986 l->next= l1 ? l1 : l2;
992 g_slist_sort_real (GSList *list,
1006 while ((l2 = l2->next) != NULL)
1008 if ((l2 = l2->next) == NULL)
1015 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1016 g_slist_sort_real (l2, compare_func, user_data),
1024 * @compare_func: the comparison function used to sort the #GSList.
1025 * This function is passed the data from 2 elements of the #GSList
1026 * and should return 0 if they are equal, a negative value if the
1027 * first element comes before the second, or a positive value if
1028 * the first element comes after the second.
1030 * Sorts a #GSList using the given comparison function.
1032 * Returns: the start of the sorted #GSList
1035 g_slist_sort (GSList *list,
1036 GCompareFunc compare_func)
1038 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1042 * g_slist_sort_with_data:
1044 * @compare_func: comparison function
1045 * @user_data: data to pass to comparison function
1047 * Like g_slist_sort(), but the sort function accepts a user data argument.
1049 * Returns: new head of the list
1052 g_slist_sort_with_data (GSList *list,
1053 GCompareDataFunc compare_func,
1056 return g_slist_sort_real (list, (GFunc) compare_func, user_data);