Rename exporter APIs
[glib.git] / glib / gslist.c
blob89e0f2d60019b0b201d46458d681652dc7357298
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/.
28 * MT safe
31 #include "config.h"
33 #include "gslist.h"
35 #include "gtestutils.h"
36 #include "gslice.h"
38 /**
39 * SECTION:linked_lists_single
40 * @title: Singly-Linked Lists
41 * @short_description: linked lists that can be iterated 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
78 * g_slist_index().
80 * To call a function for each element in the list use
81 * g_slist_foreach().
83 * To free the entire list, use g_slist_free().
84 **/
86 /**
87 * GSList:
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
91 * Macros</link>.
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
95 * list.
96 **/
98 /**
99 * g_slist_next:
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.
106 #define _g_slist_alloc0() g_slice_new0 (GSList)
107 #define _g_slist_alloc() g_slice_new (GSList)
108 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
111 * g_slist_alloc:
112 * @Returns: a pointer to the newly-allocated #GSList element.
114 * Allocates space for one #GSList element. It is called by the
115 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
116 * g_slist_insert_sorted() functions and so is rarely used on its own.
118 GSList*
119 g_slist_alloc (void)
121 return _g_slist_alloc0 ();
125 * g_slist_free:
126 * @list: a #GSList
128 * Frees all of the memory used by a #GSList.
129 * The freed elements are returned to the slice allocator.
131 * <note><para>
132 * If list elements contain dynamically-allocated memory,
133 * you should either use g_slist_free_full() or free them manually
134 * first.
135 * </para></note>
137 void
138 g_slist_free (GSList *list)
140 g_slice_free_chain (GSList, list, next);
144 * g_slist_free_1:
145 * @list: a #GSList element
147 * Frees one #GSList element.
148 * It is usually used after g_slist_remove_link().
151 * g_slist_free1:
153 * A macro which does the same as g_slist_free_1().
155 * Since: 2.10
157 void
158 g_slist_free_1 (GSList *list)
160 _g_slist_free1 (list);
164 * g_slist_free_full:
165 * @list: a pointer to a #GSList
166 * @free_func: the function to be called to free each element's data
168 * Convenience method, which frees all the memory used by a #GSList, and
169 * calls the specified destroy function on every element's data.
171 * Since: 2.28
173 void
174 g_slist_free_full (GSList *list,
175 GDestroyNotify free_func)
177 g_slist_foreach (list, (GFunc) free_func, NULL);
178 g_slist_free (list);
182 * g_slist_append:
183 * @list: a #GSList
184 * @data: the data for the new element
186 * Adds a new element on to the end of the list.
188 * <note><para>
189 * The return value is the new start of the list, which may
190 * have changed, so make sure you store the new value.
191 * </para></note>
193 * <note><para>
194 * Note that g_slist_append() has to traverse the entire list
195 * to find the end, which is inefficient when adding multiple
196 * elements. A common idiom to avoid the inefficiency is to prepend
197 * the elements and reverse the list when all elements have been added.
198 * </para></note>
200 * |[
201 * /&ast; Notice that these are initialized to the empty list. &ast;/
202 * GSList *list = NULL, *number_list = NULL;
204 * /&ast; This is a list of strings. &ast;/
205 * list = g_slist_append (list, "first");
206 * list = g_slist_append (list, "second");
208 * /&ast; This is a list of integers. &ast;/
209 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
210 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
211 * ]|
213 * Returns: the new start of the #GSList
215 GSList*
216 g_slist_append (GSList *list,
217 gpointer data)
219 GSList *new_list;
220 GSList *last;
222 new_list = _g_slist_alloc ();
223 new_list->data = data;
224 new_list->next = NULL;
226 if (list)
228 last = g_slist_last (list);
229 /* g_assert (last != NULL); */
230 last->next = new_list;
232 return list;
234 else
235 return new_list;
239 * g_slist_prepend:
240 * @list: a #GSList
241 * @data: the data for the new element
243 * Adds a new element on to the start of the list.
245 * <note><para>
246 * The return value is the new start of the list, which
247 * may have changed, so make sure you store the new value.
248 * </para></note>
250 * |[
251 * /&ast; Notice that it is initialized to the empty list. &ast;/
252 * GSList *list = NULL;
253 * list = g_slist_prepend (list, "last");
254 * list = g_slist_prepend (list, "first");
255 * ]|
257 * Returns: the new start of the #GSList
259 GSList*
260 g_slist_prepend (GSList *list,
261 gpointer data)
263 GSList *new_list;
265 new_list = _g_slist_alloc ();
266 new_list->data = data;
267 new_list->next = list;
269 return new_list;
273 * g_slist_insert:
274 * @list: a #GSList
275 * @data: the data for the new element
276 * @position: the position to insert the element.
277 * If this is negative, or is larger than the number
278 * of elements in the list, the new element is added on
279 * to the end of the list.
281 * Inserts a new element into the list at the given position.
283 * Returns: the new start of the #GSList
285 GSList*
286 g_slist_insert (GSList *list,
287 gpointer data,
288 gint position)
290 GSList *prev_list;
291 GSList *tmp_list;
292 GSList *new_list;
294 if (position < 0)
295 return g_slist_append (list, data);
296 else if (position == 0)
297 return g_slist_prepend (list, data);
299 new_list = _g_slist_alloc ();
300 new_list->data = data;
302 if (!list)
304 new_list->next = NULL;
305 return new_list;
308 prev_list = NULL;
309 tmp_list = list;
311 while ((position-- > 0) && tmp_list)
313 prev_list = tmp_list;
314 tmp_list = tmp_list->next;
317 new_list->next = prev_list->next;
318 prev_list->next = new_list;
320 return list;
324 * g_slist_insert_before:
325 * @slist: a #GSList
326 * @sibling: node to insert @data before
327 * @data: data to put in the newly-inserted node
329 * Inserts a node before @sibling containing @data.
331 * Returns: the new head of the list.
333 GSList*
334 g_slist_insert_before (GSList *slist,
335 GSList *sibling,
336 gpointer data)
338 if (!slist)
340 slist = _g_slist_alloc ();
341 slist->data = data;
342 slist->next = NULL;
343 g_return_val_if_fail (sibling == NULL, slist);
344 return slist;
346 else
348 GSList *node, *last = NULL;
350 for (node = slist; node; last = node, node = last->next)
351 if (node == sibling)
352 break;
353 if (!last)
355 node = _g_slist_alloc ();
356 node->data = data;
357 node->next = slist;
359 return node;
361 else
363 node = _g_slist_alloc ();
364 node->data = data;
365 node->next = last->next;
366 last->next = node;
368 return slist;
374 * g_slist_concat:
375 * @list1: a #GSList
376 * @list2: the #GSList to add to the end of the first #GSList
378 * Adds the second #GSList onto the end of the first #GSList.
379 * Note that the elements of the second #GSList are not copied.
380 * They are used directly.
382 * Returns: the start of the new #GSList
384 GSList *
385 g_slist_concat (GSList *list1, GSList *list2)
387 if (list2)
389 if (list1)
390 g_slist_last (list1)->next = list2;
391 else
392 list1 = list2;
395 return list1;
399 * g_slist_remove:
400 * @list: a #GSList
401 * @data: the data of the element to remove
403 * Removes an element from a #GSList.
404 * If two elements contain the same data, only the first is removed.
405 * If none of the elements contain the data, the #GSList is unchanged.
407 * Returns: the new start of the #GSList
409 GSList*
410 g_slist_remove (GSList *list,
411 gconstpointer data)
413 GSList *tmp, *prev = NULL;
415 tmp = list;
416 while (tmp)
418 if (tmp->data == data)
420 if (prev)
421 prev->next = tmp->next;
422 else
423 list = tmp->next;
425 g_slist_free_1 (tmp);
426 break;
428 prev = tmp;
429 tmp = prev->next;
432 return list;
436 * g_slist_remove_all:
437 * @list: a #GSList
438 * @data: data to remove
440 * Removes all list nodes with data equal to @data.
441 * Returns the new head of the list. Contrast with
442 * g_slist_remove() which removes only the first node
443 * matching the given data.
445 * Returns: new head of @list
447 GSList*
448 g_slist_remove_all (GSList *list,
449 gconstpointer data)
451 GSList *tmp, *prev = NULL;
453 tmp = list;
454 while (tmp)
456 if (tmp->data == data)
458 GSList *next = tmp->next;
460 if (prev)
461 prev->next = next;
462 else
463 list = next;
465 g_slist_free_1 (tmp);
466 tmp = next;
468 else
470 prev = tmp;
471 tmp = prev->next;
475 return list;
478 static inline GSList*
479 _g_slist_remove_link (GSList *list,
480 GSList *link)
482 GSList *tmp;
483 GSList *prev;
485 prev = NULL;
486 tmp = list;
488 while (tmp)
490 if (tmp == link)
492 if (prev)
493 prev->next = tmp->next;
494 if (list == tmp)
495 list = list->next;
497 tmp->next = NULL;
498 break;
501 prev = tmp;
502 tmp = tmp->next;
505 return list;
509 * g_slist_remove_link:
510 * @list: a #GSList
511 * @link_: an element in the #GSList
513 * Removes an element from a #GSList, without
514 * freeing the element. The removed element's next
515 * link is set to %NULL, so that it becomes a
516 * self-contained list with one element.
518 * Returns: the new start of the #GSList, without the element
520 GSList*
521 g_slist_remove_link (GSList *list,
522 GSList *link_)
524 return _g_slist_remove_link (list, link_);
528 * g_slist_delete_link:
529 * @list: a #GSList
530 * @link_: node to delete
532 * Removes the node link_ from the list and frees it.
533 * Compare this to g_slist_remove_link() which removes the node
534 * without freeing it.
536 * Returns: the new head of @list
538 GSList*
539 g_slist_delete_link (GSList *list,
540 GSList *link_)
542 list = _g_slist_remove_link (list, link_);
543 _g_slist_free1 (link_);
545 return list;
549 * g_slist_copy:
550 * @list: a #GSList
552 * Copies a #GSList.
554 * <note><para>
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.
558 * </para></note>
560 * Returns: a copy of @list
562 GSList*
563 g_slist_copy (GSList *list)
565 GSList *new_list = NULL;
567 if (list)
569 GSList *last;
571 new_list = _g_slist_alloc ();
572 new_list->data = list->data;
573 last = new_list;
574 list = list->next;
575 while (list)
577 last->next = _g_slist_alloc ();
578 last = last->next;
579 last->data = list->data;
580 list = list->next;
582 last->next = NULL;
585 return new_list;
589 * g_slist_reverse:
590 * @list: a #GSList
592 * Reverses a #GSList.
594 * Returns: the start of the reversed #GSList
596 GSList*
597 g_slist_reverse (GSList *list)
599 GSList *prev = NULL;
601 while (list)
603 GSList *next = list->next;
605 list->next = prev;
607 prev = list;
608 list = next;
611 return prev;
615 * g_slist_nth:
616 * @list: a #GSList
617 * @n: the position of the element, counting from 0
619 * Gets the element at the given position in a #GSList.
621 * Returns: the element, or %NULL if the position is off
622 * the end of the #GSList
624 GSList*
625 g_slist_nth (GSList *list,
626 guint n)
628 while (n-- > 0 && list)
629 list = list->next;
631 return list;
635 * g_slist_nth_data:
636 * @list: a #GSList
637 * @n: the position of the element
639 * Gets the data of the element at the given position.
641 * Returns: the element's data, or %NULL if the position
642 * is off the end of the #GSList
644 gpointer
645 g_slist_nth_data (GSList *list,
646 guint n)
648 while (n-- > 0 && list)
649 list = list->next;
651 return list ? list->data : NULL;
655 * g_slist_find:
656 * @list: a #GSList
657 * @data: the element data to find
659 * Finds the element in a #GSList which
660 * contains the given data.
662 * Returns: the found #GSList element,
663 * or %NULL if it is not found
665 GSList*
666 g_slist_find (GSList *list,
667 gconstpointer data)
669 while (list)
671 if (list->data == data)
672 break;
673 list = list->next;
676 return list;
681 * g_slist_find_custom:
682 * @list: a #GSList
683 * @data: user data passed to the function
684 * @func: the function to call for each element.
685 * It should return 0 when the desired element is found
687 * Finds an element in a #GSList, using a supplied function to
688 * find the desired element. It iterates over the list, calling
689 * the given function which should return 0 when the desired
690 * element is found. The function takes two #gconstpointer arguments,
691 * the #GSList element's data as the first argument and the
692 * given user data.
694 * Returns: the found #GSList element, or %NULL if it is not found
696 GSList*
697 g_slist_find_custom (GSList *list,
698 gconstpointer data,
699 GCompareFunc func)
701 g_return_val_if_fail (func != NULL, list);
703 while (list)
705 if (! func (list->data, data))
706 return list;
707 list = list->next;
710 return NULL;
714 * g_slist_position:
715 * @list: a #GSList
716 * @llink: an element in the #GSList
718 * Gets the position of the given element
719 * in the #GSList (starting from 0).
721 * Returns: the position of the element in the #GSList,
722 * or -1 if the element is not found
724 gint
725 g_slist_position (GSList *list,
726 GSList *llink)
728 gint i;
730 i = 0;
731 while (list)
733 if (list == llink)
734 return i;
735 i++;
736 list = list->next;
739 return -1;
743 * g_slist_index:
744 * @list: a #GSList
745 * @data: the data to find
747 * Gets the position of the element containing
748 * the given data (starting from 0).
750 * Returns: the index of the element containing the data,
751 * or -1 if the data is not found
753 gint
754 g_slist_index (GSList *list,
755 gconstpointer data)
757 gint i;
759 i = 0;
760 while (list)
762 if (list->data == data)
763 return i;
764 i++;
765 list = list->next;
768 return -1;
772 * g_slist_last:
773 * @list: a #GSList
775 * Gets the last element in a #GSList.
777 * <note><para>
778 * This function iterates over the whole list.
779 * </para></note>
781 * Returns: the last element in the #GSList,
782 * or %NULL if the #GSList has no elements
784 GSList*
785 g_slist_last (GSList *list)
787 if (list)
789 while (list->next)
790 list = list->next;
793 return list;
797 * g_slist_length:
798 * @list: a #GSList
800 * Gets the number of elements in a #GSList.
802 * <note><para>
803 * This function iterates over the whole list to
804 * count its elements.
805 * </para></note>
807 * Returns: the number of elements in the #GSList
809 guint
810 g_slist_length (GSList *list)
812 guint length;
814 length = 0;
815 while (list)
817 length++;
818 list = list->next;
821 return length;
825 * g_slist_foreach:
826 * @list: a #GSList
827 * @func: the function to call with each element's data
828 * @user_data: user data to pass to the function
830 * Calls a function for each element of a #GSList.
832 void
833 g_slist_foreach (GSList *list,
834 GFunc func,
835 gpointer user_data)
837 while (list)
839 GSList *next = list->next;
840 (*func) (list->data, user_data);
841 list = next;
845 static GSList*
846 g_slist_insert_sorted_real (GSList *list,
847 gpointer data,
848 GFunc func,
849 gpointer user_data)
851 GSList *tmp_list = list;
852 GSList *prev_list = NULL;
853 GSList *new_list;
854 gint cmp;
856 g_return_val_if_fail (func != NULL, list);
858 if (!list)
860 new_list = _g_slist_alloc ();
861 new_list->data = data;
862 new_list->next = NULL;
863 return new_list;
866 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
868 while ((tmp_list->next) && (cmp > 0))
870 prev_list = tmp_list;
871 tmp_list = tmp_list->next;
873 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
876 new_list = _g_slist_alloc ();
877 new_list->data = data;
879 if ((!tmp_list->next) && (cmp > 0))
881 tmp_list->next = new_list;
882 new_list->next = NULL;
883 return list;
886 if (prev_list)
888 prev_list->next = new_list;
889 new_list->next = tmp_list;
890 return list;
892 else
894 new_list->next = list;
895 return new_list;
900 * g_slist_insert_sorted:
901 * @list: a #GSList
902 * @data: the data for the new element
903 * @func: the function to compare elements in the list.
904 * It should return a number > 0 if the first parameter
905 * comes after the second parameter in the sort order.
907 * Inserts a new element into the list, using the given
908 * comparison function to determine its position.
910 * Returns: the new start of the #GSList
912 GSList*
913 g_slist_insert_sorted (GSList *list,
914 gpointer data,
915 GCompareFunc func)
917 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
921 * g_slist_insert_sorted_with_data:
922 * @list: a #GSList
923 * @data: the data for the new element
924 * @func: the function to compare elements in the list.
925 * It should return a number > 0 if the first parameter
926 * comes after the second parameter in the sort order.
927 * @user_data: data to pass to comparison function
929 * Inserts a new element into the list, using the given
930 * comparison function to determine its position.
932 * Returns: the new start of the #GSList
934 * Since: 2.10
936 GSList*
937 g_slist_insert_sorted_with_data (GSList *list,
938 gpointer data,
939 GCompareDataFunc func,
940 gpointer user_data)
942 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
945 static GSList *
946 g_slist_sort_merge (GSList *l1,
947 GSList *l2,
948 GFunc compare_func,
949 gpointer user_data)
951 GSList list, *l;
952 gint cmp;
954 l=&list;
956 while (l1 && l2)
958 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
960 if (cmp <= 0)
962 l=l->next=l1;
963 l1=l1->next;
965 else
967 l=l->next=l2;
968 l2=l2->next;
971 l->next= l1 ? l1 : l2;
973 return list.next;
976 static GSList *
977 g_slist_sort_real (GSList *list,
978 GFunc compare_func,
979 gpointer user_data)
981 GSList *l1, *l2;
983 if (!list)
984 return NULL;
985 if (!list->next)
986 return list;
988 l1 = list;
989 l2 = list->next;
991 while ((l2 = l2->next) != NULL)
993 if ((l2 = l2->next) == NULL)
994 break;
995 l1=l1->next;
997 l2 = l1->next;
998 l1->next = NULL;
1000 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1001 g_slist_sort_real (l2, compare_func, user_data),
1002 compare_func,
1003 user_data);
1007 * g_slist_sort:
1008 * @list: a #GSList
1009 * @compare_func: the comparison function used to sort the #GSList.
1010 * This function is passed the data from 2 elements of the #GSList
1011 * and should return 0 if they are equal, a negative value if the
1012 * first element comes before the second, or a positive value if
1013 * the first element comes after the second.
1015 * Sorts a #GSList using the given comparison function.
1017 * Returns: the start of the sorted #GSList
1019 GSList *
1020 g_slist_sort (GSList *list,
1021 GCompareFunc compare_func)
1023 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1027 * g_slist_sort_with_data:
1028 * @list: a #GSList
1029 * @compare_func: comparison function
1030 * @user_data: data to pass to comparison function
1032 * Like g_slist_sort(), but the sort function accepts a user data argument.
1034 * Returns: new head of the list
1036 GSList *
1037 g_slist_sort_with_data (GSList *list,
1038 GCompareDataFunc compare_func,
1039 gpointer user_data)
1041 return g_slist_sort_real (list, (GFunc) compare_func, user_data);