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/.
35 #include "gtestutils.h"
38 * SECTION: linked_lists_double
39 * @title: Doubly-Linked Lists
40 * @short_description: linked lists containing integer values or
41 * pointers to data, with the ability to iterate
42 * over the list in both directions
44 * The #GList structure and its associated functions provide a standard
45 * doubly-linked list data structure.
47 * Each element in the list contains a piece of data, together with
48 * pointers which link to the previous and next elements in the list.
49 * Using these pointers it is possible to move through the list in both
50 * directions (unlike the <link
51 * linkend="glib-Singly-Linked-lists">Singly-Linked Lists</link> which
52 * only allows movement through the list in the forward direction).
54 * The data contained in each element can be either integer values, by
55 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
56 * Conversion Macros</link>, or simply pointers to any type of data.
58 * List elements are allocated from the <link
59 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
60 * efficient than allocating elements individually.
62 * Note that most of the #GList functions expect to be passed a pointer
63 * to the first element in the list. The functions which insert
64 * elements return the new start of the list, which may have changed.
66 * There is no function to create a #GList. %NULL is considered to be
67 * the empty list so you simply set a #GList* to %NULL.
69 * To add elements, use g_list_append(), g_list_prepend(),
70 * g_list_insert() and g_list_insert_sorted().
72 * To remove elements, use g_list_remove().
74 * To find elements in the list use g_list_first(), g_list_last(),
75 * g_list_next(), g_list_previous(), g_list_nth(), g_list_nth_data(),
76 * g_list_find() and g_list_find_custom().
78 * To find the index of an element use g_list_position() and
81 * To call a function for each element in the list use g_list_foreach().
83 * To free the entire list, use g_list_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.
93 * @prev: contains the link to the previous element in the list.
95 * The #GList struct is used for each element in a doubly-linked list.
100 * @list: an element in a #GList.
101 * @Returns: the previous element, or %NULL if there are no previous
104 * A convenience macro to get the previous element in a #GList.
109 * @list: an element in a #GList.
110 * @Returns: the next element, or %NULL if there are no more elements.
112 * A convenience macro to get the next element in a #GList.
118 * g_list_push_allocator:
119 * @allocator: the #GAllocator to use when allocating #GList elements.
121 * Sets the allocator to use to allocate #GList elements. Use
122 * g_list_pop_allocator() to restore the previous allocator.
124 * Note that this function is not available if GLib has been compiled
125 * with <option>--disable-mem-pools</option>
127 * Deprecated:2.10: It does nothing, since #GList has been converted
128 * to the <link linkend="glib-Memory-Slices">slice
131 void g_list_push_allocator (gpointer dummy) { /* present for binary compat only */ }
134 * g_list_pop_allocator:
136 * Restores the previous #GAllocator, used when allocating #GList
139 * Note that this function is not available if GLib has been compiled
140 * with <option>--disable-mem-pools</option>
142 * Deprecated:2.10: It does nothing, since #GList has been converted
143 * to the <link linkend="glib-Memory-Slices">slice
146 void g_list_pop_allocator (void) { /* present for binary compat only */ }
148 #define _g_list_alloc() g_slice_new (GList)
149 #define _g_list_alloc0() g_slice_new0 (GList)
150 #define _g_list_free1(list) g_slice_free (GList, list)
154 * @Returns: a pointer to the newly-allocated #GList element.
156 * Allocates space for one #GList element. It is called by
157 * g_list_append(), g_list_prepend(), g_list_insert() and
158 * g_list_insert_sorted() and so is rarely used on its own.
163 return _g_list_alloc0 ();
170 * Frees all of the memory used by a #GList.
171 * The freed elements are returned to the slice allocator.
174 * If list elements contain dynamically-allocated memory,
175 * they should be freed first.
179 g_list_free (GList *list)
181 g_slice_free_chain (GList, list, next);
186 * @list: a #GList element
188 * Frees one #GList element.
189 * It is usually used after g_list_remove_link().
194 * Another name for g_list_free_1().
197 g_list_free_1 (GList *list)
199 _g_list_free1 (list);
204 * @list: a pointer to a #GList
205 * @data: the data for the new element
207 * Adds a new element on to the end of the list.
210 * The return value is the new start of the list, which
211 * may have changed, so make sure you store the new value.
215 * Note that g_list_append() has to traverse the entire list
216 * to find the end, which is inefficient when adding multiple
217 * elements. A common idiom to avoid the inefficiency is to prepend
218 * the elements and reverse the list when all elements have been added.
222 * /* Notice that these are initialized to the empty list. */
223 * GList *list = NULL, *number_list = NULL;
225 * /* This is a list of strings. */
226 * list = g_list_append (list, "first");
227 * list = g_list_append (list, "second");
229 * /* This is a list of integers. */
230 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
231 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
234 * Returns: the new start of the #GList
237 g_list_append (GList *list,
243 new_list = _g_list_alloc ();
244 new_list->data = data;
245 new_list->next = NULL;
249 last = g_list_last (list);
250 /* g_assert (last != NULL); */
251 last->next = new_list;
252 new_list->prev = last;
258 new_list->prev = NULL;
265 * @list: a pointer to a #GList
266 * @data: the data for the new element
268 * Adds a new element on to the start of the list.
271 * The return value is the new start of the list, which
272 * may have changed, so make sure you store the new value.
276 * /* Notice that it is initialized to the empty list. */
277 * GList *list = NULL;
278 * list = g_list_prepend (list, "last");
279 * list = g_list_prepend (list, "first");
282 * Returns: the new start of the #GList
285 g_list_prepend (GList *list,
290 new_list = _g_list_alloc ();
291 new_list->data = data;
292 new_list->next = list;
296 new_list->prev = list->prev;
298 list->prev->next = new_list;
299 list->prev = new_list;
302 new_list->prev = NULL;
309 * @list: a pointer to a #GList
310 * @data: the data for the new element
311 * @position: the position to insert the element. If this is
312 * negative, or is larger than the number of elements in the
313 * list, the new element is added on to the end of the list.
315 * Inserts a new element into the list at the given position.
317 * Returns: the new start of the #GList
320 g_list_insert (GList *list,
328 return g_list_append (list, data);
329 else if (position == 0)
330 return g_list_prepend (list, data);
332 tmp_list = g_list_nth (list, position);
334 return g_list_append (list, data);
336 new_list = _g_list_alloc ();
337 new_list->data = data;
338 new_list->prev = tmp_list->prev;
340 tmp_list->prev->next = new_list;
341 new_list->next = tmp_list;
342 tmp_list->prev = new_list;
344 if (tmp_list == list)
351 * g_list_insert_before:
352 * @list: a pointer to a #GList
353 * @sibling: the list element before which the new element
354 * is inserted or %NULL to insert at the end of the list
355 * @data: the data for the new element
357 * Inserts a new element into the list before the given position.
359 * Returns: the new start of the #GList
362 g_list_insert_before (GList *list,
368 list = g_list_alloc ();
370 g_return_val_if_fail (sibling == NULL, list);
377 node = _g_list_alloc ();
379 node->prev = sibling->prev;
380 node->next = sibling;
381 sibling->prev = node;
384 node->prev->next = node;
389 g_return_val_if_fail (sibling == list, node);
401 last->next = _g_list_alloc ();
402 last->next->data = data;
403 last->next->prev = last;
404 last->next->next = NULL;
413 * @list2: the #GList to add to the end of the first #GList
415 * Adds the second #GList onto the end of the first #GList.
416 * Note that the elements of the second #GList are not copied.
417 * They are used directly.
419 * Returns: the start of the new #GList
422 g_list_concat (GList *list1, GList *list2)
428 tmp_list = g_list_last (list1);
430 tmp_list->next = list2;
433 list2->prev = tmp_list;
442 * @data: the data of the element to remove
444 * Removes an element from a #GList.
445 * If two elements contain the same data, only the first is removed.
446 * If none of the elements contain the data, the #GList is unchanged.
448 * Returns: the new start of the #GList
451 g_list_remove (GList *list,
459 if (tmp->data != data)
464 tmp->prev->next = tmp->next;
466 tmp->next->prev = tmp->prev;
482 * @data: data to remove
484 * Removes all list nodes with data equal to @data.
485 * Returns the new head of the list. Contrast with
486 * g_list_remove() which removes only the first node
487 * matching the given data.
489 * Returns: new head of @list
492 g_list_remove_all (GList *list,
499 if (tmp->data != data)
503 GList *next = tmp->next;
506 tmp->prev->next = next;
510 next->prev = tmp->prev;
520 _g_list_remove_link (GList *list,
526 link->prev->next = link->next;
528 link->next->prev = link->prev;
541 * g_list_remove_link:
543 * @llink: an element in the #GList
545 * Removes an element from a #GList, without freeing the element.
546 * The removed element's prev and next links are set to %NULL, so
547 * that it becomes a self-contained list with one element.
549 * Returns: the new start of the #GList, without the element
552 g_list_remove_link (GList *list,
555 return _g_list_remove_link (list, llink);
559 * g_list_delete_link:
561 * @link_: node to delete from @list
563 * Removes the node link_ from the list and frees it.
564 * Compare this to g_list_remove_link() which removes the node
565 * without freeing it.
567 * Returns: the new head of @list
570 g_list_delete_link (GList *list,
573 list = _g_list_remove_link (list, link_);
574 _g_list_free1 (link_);
586 * Note that this is a "shallow" copy. If the list elements
587 * consist of pointers to data, the pointers are copied but
588 * the actual data is not.
591 * Returns: a copy of @list
594 g_list_copy (GList *list)
596 GList *new_list = NULL;
602 new_list = _g_list_alloc ();
603 new_list->data = list->data;
604 new_list->prev = NULL;
609 last->next = _g_list_alloc ();
610 last->next->prev = last;
612 last->data = list->data;
626 * It simply switches the next and prev pointers of each element.
628 * Returns: the start of the reversed #GList
631 g_list_reverse (GList *list)
640 last->next = last->prev;
650 * @n: the position of the element, counting from 0
652 * Gets the element at the given position in a #GList.
654 * Returns: the element, or %NULL if the position is off
655 * the end of the #GList
658 g_list_nth (GList *list,
661 while ((n-- > 0) && list)
670 * @n: the position of the element, counting from 0
672 * Gets the element @n places before @list.
674 * Returns: the element, or %NULL if the position is
675 * off the end of the #GList
678 g_list_nth_prev (GList *list,
681 while ((n-- > 0) && list)
690 * @n: the position of the element
692 * Gets the data of the element at the given position.
694 * Returns: the element's data, or %NULL if the position
695 * is off the end of the #GList
698 g_list_nth_data (GList *list,
701 while ((n-- > 0) && list)
704 return list ? list->data : NULL;
710 * @data: the element data to find
712 * Finds the element in a #GList which
713 * contains the given data.
715 * Returns: the found #GList element,
716 * or %NULL if it is not found
719 g_list_find (GList *list,
724 if (list->data == data)
733 * g_list_find_custom:
735 * @data: user data passed to the function
736 * @func: the function to call for each element.
737 * It should return 0 when the desired element is found
739 * Finds an element in a #GList, using a supplied function to
740 * find the desired element. It iterates over the list, calling
741 * the given function which should return 0 when the desired
742 * element is found. The function takes two #gconstpointer arguments,
743 * the #GList element's data as the first argument and the
746 * Returns: the found #GList element, or %NULL if it is not found
749 g_list_find_custom (GList *list,
753 g_return_val_if_fail (func != NULL, list);
757 if (! func (list->data, data))
769 * @llink: an element in the #GList
771 * Gets the position of the given element
772 * in the #GList (starting from 0).
774 * Returns: the position of the element in the #GList,
775 * or -1 if the element is not found
778 g_list_position (GList *list,
798 * @data: the data to find
800 * Gets the position of the element containing
801 * the given data (starting from 0).
803 * Returns: the index of the element containing the data,
804 * or -1 if the data is not found
807 g_list_index (GList *list,
815 if (list->data == data)
828 * Gets the last element in a #GList.
830 * Returns: the last element in the #GList,
831 * or %NULL if the #GList has no elements
834 g_list_last (GList *list)
849 * Gets the first element in a #GList.
851 * Returns: the first element in the #GList,
852 * or %NULL if the #GList has no elements
855 g_list_first (GList *list)
870 * Gets the number of elements in a #GList.
873 * This function iterates over the whole list to
874 * count its elements.
877 * Returns: the number of elements in the #GList
880 g_list_length (GList *list)
897 * @func: the function to call with each element's data
898 * @user_data: user data to pass to the function
900 * Calls a function for each element of a #GList.
904 * @data: the element's data.
905 * @user_data: user data passed to g_list_foreach() or
908 * Specifies the type of functions passed to g_list_foreach() and
912 g_list_foreach (GList *list,
918 GList *next = list->next;
919 (*func) (list->data, user_data);
925 g_list_insert_sorted_real (GList *list,
930 GList *tmp_list = list;
934 g_return_val_if_fail (func != NULL, list);
938 new_list = _g_list_alloc0 ();
939 new_list->data = data;
943 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
945 while ((tmp_list->next) && (cmp > 0))
947 tmp_list = tmp_list->next;
949 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
952 new_list = _g_list_alloc0 ();
953 new_list->data = data;
955 if ((!tmp_list->next) && (cmp > 0))
957 tmp_list->next = new_list;
958 new_list->prev = tmp_list;
964 tmp_list->prev->next = new_list;
965 new_list->prev = tmp_list->prev;
967 new_list->next = tmp_list;
968 tmp_list->prev = new_list;
970 if (tmp_list == list)
977 * g_list_insert_sorted:
978 * @list: a pointer to a #GList
979 * @data: the data for the new element
980 * @func: the function to compare elements in the list. It should
981 * return a number > 0 if the first parameter comes after the
982 * second parameter in the sort order.
984 * Inserts a new element into the list, using the given comparison
985 * function to determine its position.
987 * Returns: the new start of the #GList
990 g_list_insert_sorted (GList *list,
994 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
998 * g_list_insert_sorted_with_data:
999 * @list: a pointer to a #GList
1000 * @data: the data for the new element
1001 * @func: the function to compare elements in the list.
1002 * It should return a number > 0 if the first parameter
1003 * comes after the second parameter in the sort order.
1004 * @user_data: user data to pass to comparison function.
1006 * Inserts a new element into the list, using the given comparison
1007 * function to determine its position.
1009 * Returns: the new start of the #GList
1014 g_list_insert_sorted_with_data (GList *list,
1016 GCompareDataFunc func,
1019 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
1023 g_list_sort_merge (GList *l1,
1028 GList list, *l, *lprev;
1036 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1052 l->next = l1 ? l1 : l2;
1059 g_list_sort_real (GList *list,
1073 while ((l2 = l2->next) != NULL)
1075 if ((l2 = l2->next) == NULL)
1082 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
1083 g_list_sort_real (l2, compare_func, user_data),
1091 * @compare_func: the comparison function used to sort the #GList.
1092 * This function is passed the data from 2 elements of the #GList
1093 * and should return 0 if they are equal, a negative value if the
1094 * first element comes before the second, or a positive value if
1095 * the first element comes after the second.
1097 * Sorts a #GList using the given comparison function.
1099 * Returns: the start of the sorted #GList
1104 * @b: a value to compare with.
1105 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1108 * Specifies the type of a comparison function used to compare two
1109 * values. The function should return a negative integer if the first
1110 * value comes before the second, 0 if they are equal, or a positive
1111 * integer if the first value comes after the second.
1114 g_list_sort (GList *list,
1115 GCompareFunc compare_func)
1117 return g_list_sort_real (list, (GFunc) compare_func, NULL);
1122 * g_list_sort_with_data:
1124 * @compare_func: comparison function
1125 * @user_data: user data to pass to comparison function
1127 * Like g_list_sort(), but the comparison function accepts
1128 * a user data argument.
1130 * Returns: the new head of @list
1135 * @b: a value to compare with.
1136 * @user_data: user data to pass to comparison function.
1137 * @Returns: negative value if @a < @b; zero if @a = @b; positive
1140 * Specifies the type of a comparison function used to compare two
1141 * values. The function should return a negative integer if the first
1142 * value comes before the second, 0 if they are equal, or a positive
1143 * integer if the first value comes after the second.
1146 g_list_sort_with_data (GList *list,
1147 GCompareDataFunc compare_func,
1150 return g_list_sort_real (list, (GFunc) compare_func, user_data);