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/.
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);
192 * @data: the data for the new element
194 * Adds a new element on to the end of the list.
197 * The return value is the new start of the list, which may
198 * have changed, so make sure you store the new value.
202 * Note that g_slist_append() has to traverse the entire list
203 * to find the end, which is inefficient when adding multiple
204 * elements. A common idiom to avoid the inefficiency is to prepend
205 * the elements and reverse the list when all elements have been added.
209 * /* Notice that these are initialized to the empty list. */
210 * GSList *list = NULL, *number_list = NULL;
212 * /* This is a list of strings. */
213 * list = g_slist_append (list, "first");
214 * list = g_slist_append (list, "second");
216 * /* This is a list of integers. */
217 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
218 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
221 * Returns: the new start of the #GSList
224 g_slist_append (GSList *list,
230 new_list = _g_slist_alloc ();
231 new_list->data = data;
232 new_list->next = NULL;
236 last = g_slist_last (list);
237 /* g_assert (last != NULL); */
238 last->next = new_list;
249 * @data: the data for the new element
251 * Adds a new element on to the start of the list.
254 * The return value is the new start of the list, which
255 * may have changed, so make sure you store the new value.
259 * /* Notice that it is initialized to the empty list. */
260 * GSList *list = NULL;
261 * list = g_slist_prepend (list, "last");
262 * list = g_slist_prepend (list, "first");
265 * Returns: the new start of the #GSList
268 g_slist_prepend (GSList *list,
273 new_list = _g_slist_alloc ();
274 new_list->data = data;
275 new_list->next = list;
283 * @data: the data for the new element
284 * @position: the position to insert the element.
285 * If this is negative, or is larger than the number
286 * of elements in the list, the new element is added on
287 * to the end of the list.
289 * Inserts a new element into the list at the given position.
291 * Returns: the new start of the #GSList
294 g_slist_insert (GSList *list,
303 return g_slist_append (list, data);
304 else if (position == 0)
305 return g_slist_prepend (list, data);
307 new_list = _g_slist_alloc ();
308 new_list->data = data;
312 new_list->next = NULL;
319 while ((position-- > 0) && tmp_list)
321 prev_list = tmp_list;
322 tmp_list = tmp_list->next;
327 new_list->next = prev_list->next;
328 prev_list->next = new_list;
332 new_list->next = list;
340 * g_slist_insert_before:
342 * @sibling: node to insert @data before
343 * @data: data to put in the newly-inserted node
345 * Inserts a node before @sibling containing @data.
347 * Returns: the new head of the list.
350 g_slist_insert_before (GSList *slist,
356 slist = _g_slist_alloc ();
359 g_return_val_if_fail (sibling == NULL, slist);
364 GSList *node, *last = NULL;
366 for (node = slist; node; last = node, node = last->next)
371 node = _g_slist_alloc ();
379 node = _g_slist_alloc ();
381 node->next = last->next;
392 * @list2: the #GSList to add to the end of the first #GSList
394 * Adds the second #GSList onto the end of the first #GSList.
395 * Note that the elements of the second #GSList are not copied.
396 * They are used directly.
398 * Returns: the start of the new #GSList
401 g_slist_concat (GSList *list1, GSList *list2)
406 g_slist_last (list1)->next = list2;
417 * @data: the data of the element to remove
419 * Removes an element from a #GSList.
420 * If two elements contain the same data, only the first is removed.
421 * If none of the elements contain the data, the #GSList is unchanged.
423 * Returns: the new start of the #GSList
426 g_slist_remove (GSList *list,
429 GSList *tmp, *prev = NULL;
434 if (tmp->data == data)
437 prev->next = tmp->next;
441 g_slist_free_1 (tmp);
452 * g_slist_remove_all:
454 * @data: data to remove
456 * Removes all list nodes with data equal to @data.
457 * Returns the new head of the list. Contrast with
458 * g_slist_remove() which removes only the first node
459 * matching the given data.
461 * Returns: new head of @list
464 g_slist_remove_all (GSList *list,
467 GSList *tmp, *prev = NULL;
472 if (tmp->data == data)
474 GSList *next = tmp->next;
481 g_slist_free_1 (tmp);
494 static inline GSList*
495 _g_slist_remove_link (GSList *list,
509 prev->next = tmp->next;
525 * g_slist_remove_link:
527 * @link_: an element in the #GSList
529 * Removes an element from a #GSList, without
530 * freeing the element. The removed element's next
531 * link is set to %NULL, so that it becomes a
532 * self-contained list with one element.
534 * Returns: the new start of the #GSList, without the element
537 g_slist_remove_link (GSList *list,
540 return _g_slist_remove_link (list, link_);
544 * g_slist_delete_link:
546 * @link_: node to delete
548 * Removes the node link_ from the list and frees it.
549 * Compare this to g_slist_remove_link() which removes the node
550 * without freeing it.
552 * Returns: the new head of @list
555 g_slist_delete_link (GSList *list,
558 list = _g_slist_remove_link (list, link_);
559 _g_slist_free1 (link_);
571 * Note that this is a "shallow" copy. If the list elements
572 * consist of pointers to data, the pointers are copied but
573 * the actual data isn't.
576 * Returns: a copy of @list
579 g_slist_copy (GSList *list)
581 GSList *new_list = NULL;
587 new_list = _g_slist_alloc ();
588 new_list->data = list->data;
593 last->next = _g_slist_alloc ();
595 last->data = list->data;
608 * Reverses a #GSList.
610 * Returns: the start of the reversed #GSList
613 g_slist_reverse (GSList *list)
619 GSList *next = list->next;
633 * @n: the position of the element, counting from 0
635 * Gets the element at the given position in a #GSList.
637 * Returns: the element, or %NULL if the position is off
638 * the end of the #GSList
641 g_slist_nth (GSList *list,
644 while (n-- > 0 && list)
653 * @n: the position of the element
655 * Gets the data of the element at the given position.
657 * Returns: the element's data, or %NULL if the position
658 * is off the end of the #GSList
661 g_slist_nth_data (GSList *list,
664 while (n-- > 0 && list)
667 return list ? list->data : NULL;
673 * @data: the element data to find
675 * Finds the element in a #GSList which
676 * contains the given data.
678 * Returns: the found #GSList element,
679 * or %NULL if it is not found
682 g_slist_find (GSList *list,
687 if (list->data == data)
697 * g_slist_find_custom:
699 * @data: user data passed to the function
700 * @func: the function to call for each element.
701 * It should return 0 when the desired element is found
703 * Finds an element in a #GSList, using a supplied function to
704 * find the desired element. It iterates over the list, calling
705 * the given function which should return 0 when the desired
706 * element is found. The function takes two #gconstpointer arguments,
707 * the #GSList element's data as the first argument and the
710 * Returns: the found #GSList element, or %NULL if it is not found
713 g_slist_find_custom (GSList *list,
717 g_return_val_if_fail (func != NULL, list);
721 if (! func (list->data, data))
732 * @llink: an element in the #GSList
734 * Gets the position of the given element
735 * in the #GSList (starting from 0).
737 * Returns: the position of the element in the #GSList,
738 * or -1 if the element is not found
741 g_slist_position (GSList *list,
761 * @data: the data to find
763 * Gets the position of the element containing
764 * the given data (starting from 0).
766 * Returns: the index of the element containing the data,
767 * or -1 if the data is not found
770 g_slist_index (GSList *list,
778 if (list->data == data)
791 * Gets the last element in a #GSList.
794 * This function iterates over the whole list.
797 * Returns: the last element in the #GSList,
798 * or %NULL if the #GSList has no elements
801 g_slist_last (GSList *list)
816 * Gets the number of elements in a #GSList.
819 * This function iterates over the whole list to
820 * count its elements.
823 * Returns: the number of elements in the #GSList
826 g_slist_length (GSList *list)
843 * @func: the function to call with each element's data
844 * @user_data: user data to pass to the function
846 * Calls a function for each element of a #GSList.
849 g_slist_foreach (GSList *list,
855 GSList *next = list->next;
856 (*func) (list->data, user_data);
862 g_slist_insert_sorted_real (GSList *list,
867 GSList *tmp_list = list;
868 GSList *prev_list = NULL;
872 g_return_val_if_fail (func != NULL, list);
876 new_list = _g_slist_alloc ();
877 new_list->data = data;
878 new_list->next = NULL;
882 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
884 while ((tmp_list->next) && (cmp > 0))
886 prev_list = tmp_list;
887 tmp_list = tmp_list->next;
889 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
892 new_list = _g_slist_alloc ();
893 new_list->data = data;
895 if ((!tmp_list->next) && (cmp > 0))
897 tmp_list->next = new_list;
898 new_list->next = NULL;
904 prev_list->next = new_list;
905 new_list->next = tmp_list;
910 new_list->next = list;
916 * g_slist_insert_sorted:
918 * @data: the data for the new element
919 * @func: the function to compare elements in the list.
920 * It should return a number > 0 if the first parameter
921 * comes after the second parameter in the sort order.
923 * Inserts a new element into the list, using the given
924 * comparison function to determine its position.
926 * Returns: the new start of the #GSList
929 g_slist_insert_sorted (GSList *list,
933 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
937 * g_slist_insert_sorted_with_data:
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.
943 * @user_data: data to pass to comparison function
945 * Inserts a new element into the list, using the given
946 * comparison function to determine its position.
948 * Returns: the new start of the #GSList
953 g_slist_insert_sorted_with_data (GSList *list,
955 GCompareDataFunc func,
958 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
962 g_slist_sort_merge (GSList *l1,
974 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
987 l->next= l1 ? l1 : l2;
993 g_slist_sort_real (GSList *list,
1007 while ((l2 = l2->next) != NULL)
1009 if ((l2 = l2->next) == NULL)
1016 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1017 g_slist_sort_real (l2, compare_func, user_data),
1025 * @compare_func: the comparison function used to sort the #GSList.
1026 * This function is passed the data from 2 elements of the #GSList
1027 * and should return 0 if they are equal, a negative value if the
1028 * first element comes before the second, or a positive value if
1029 * the first element comes after the second.
1031 * Sorts a #GSList using the given comparison function.
1033 * Returns: the start of the sorted #GSList
1036 g_slist_sort (GSList *list,
1037 GCompareFunc compare_func)
1039 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1043 * g_slist_sort_with_data:
1045 * @compare_func: comparison function
1046 * @user_data: data to pass to comparison function
1048 * Like g_slist_sort(), but the sort function accepts a user data argument.
1050 * Returns: new head of the list
1053 g_slist_sort_with_data (GSList *list,
1054 GCompareDataFunc compare_func,
1057 return g_slist_sort_real (list, (GFunc) compare_func, user_data);
1060 #define __G_SLIST_C__
1061 #include "galiasdef.c"