1998-06-11 01:21:14 +02:00
|
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-06-11 01:21:14 +02:00
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
1998-06-11 01:21:14 +02:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* Lesser General Public License for more details.
|
1998-06-11 01:21:14 +02:00
|
|
|
|
*
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
1998-06-11 01:21:14 +02:00
|
|
|
|
*/
|
1998-12-15 06:28:02 +01:00
|
|
|
|
|
1999-02-24 07:14:27 +01:00
|
|
|
|
/*
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
1999-02-24 07:14:27 +01:00
|
|
|
|
* file for a list of people on the GLib Team. See the ChangeLog
|
|
|
|
|
* files for a list of changes. These files are distributed with
|
2010-09-04 02:57:05 +02:00
|
|
|
|
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
1999-02-24 07:14:27 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2010-09-04 02:57:05 +02:00
|
|
|
|
/*
|
1998-12-15 06:28:02 +01:00
|
|
|
|
* MT safe
|
|
|
|
|
*/
|
|
|
|
|
|
2002-12-04 02:27:44 +01:00
|
|
|
|
#include "config.h"
|
2000-12-19 10:35:44 +01:00
|
|
|
|
|
2010-09-04 02:57:05 +02:00
|
|
|
|
#include "glist.h"
|
2011-09-19 00:59:20 +02:00
|
|
|
|
#include "gslice.h"
|
2013-02-24 22:54:09 +01:00
|
|
|
|
#include "gmessages.h"
|
2010-09-04 02:57:05 +02:00
|
|
|
|
|
|
|
|
|
#include "gtestutils.h"
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
2011-02-01 19:17:23 +01:00
|
|
|
|
* SECTION:linked_lists_double
|
2010-01-31 19:07:16 +01:00
|
|
|
|
* @title: Doubly-Linked Lists
|
2011-11-13 03:54:42 +01:00
|
|
|
|
* @short_description: linked lists that can be iterated over in both directions
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* The #GList structure and its associated functions provide a standard
|
2019-08-06 18:52:57 +02:00
|
|
|
|
* doubly-linked list data structure. The benefit of this data-structure
|
|
|
|
|
* is to provide insertion/deletion operations in O(1) complexity where
|
|
|
|
|
* access/search operations are in O(n). The benefit of #GList over
|
|
|
|
|
* #GSList (singly linked list) is that the worst case on access/search
|
|
|
|
|
* operations is divided by two which comes at a cost in space as we need
|
|
|
|
|
* to retain two pointers in place of one.
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* Each element in the list contains a piece of data, together with
|
|
|
|
|
* pointers which link to the previous and next elements in the list.
|
|
|
|
|
* Using these pointers it is possible to move through the list in both
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* directions (unlike the singly-linked [GSList][glib-Singly-Linked-Lists],
|
|
|
|
|
* which only allows movement through the list in the forward direction).
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* The double linked list does not keep track of the number of items
|
|
|
|
|
* and does not keep track of both the start and end of the list. If
|
|
|
|
|
* you want fast access to both the start and the end of the list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* and/or the number of items in the list, use a
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* [GQueue][glib-Double-ended-Queues] instead.
|
2012-09-05 09:52:23 +02:00
|
|
|
|
*
|
2010-01-31 19:07:16 +01:00
|
|
|
|
* The data contained in each element can be either integer values, by
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
|
|
|
|
|
* or simply pointers to any type of data.
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* List elements are allocated from the [slice allocator][glib-Memory-Slices],
|
|
|
|
|
* which is more efficient than allocating elements individually.
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* Note that most of the #GList functions expect to be passed a pointer
|
|
|
|
|
* to the first element in the list. The functions which insert
|
|
|
|
|
* elements return the new start of the list, which may have changed.
|
|
|
|
|
*
|
|
|
|
|
* There is no function to create a #GList. %NULL is considered to be
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* a valid, empty list so you simply set a #GList* to %NULL to initialize
|
|
|
|
|
* it.
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* To add elements, use g_list_append(), g_list_prepend(),
|
|
|
|
|
* g_list_insert() and g_list_insert_sorted().
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* To visit all elements in the list, use a loop over the list:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* GList *l;
|
|
|
|
|
* for (l = list; l != NULL; l = l->next)
|
|
|
|
|
* {
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // do something with l->data
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* }
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* ]|
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* To call a function for each element in the list, use g_list_foreach().
|
2012-09-05 09:52:23 +02:00
|
|
|
|
*
|
|
|
|
|
* To loop over the list and modify it (e.g. remove a certain element)
|
|
|
|
|
* a while loop is more appropriate, for example:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* GList *l = list;
|
|
|
|
|
* while (l != NULL)
|
|
|
|
|
* {
|
|
|
|
|
* GList *next = l->next;
|
|
|
|
|
* if (should_be_removed (l))
|
|
|
|
|
* {
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // possibly free l->data
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* list = g_list_delete_link (list, l);
|
|
|
|
|
* }
|
|
|
|
|
* l = next;
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* }
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2010-01-31 19:07:16 +01:00
|
|
|
|
* To remove elements, use g_list_remove().
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* To navigate in a list, use g_list_first(), g_list_last(),
|
|
|
|
|
* g_list_next(), g_list_previous().
|
|
|
|
|
*
|
|
|
|
|
* To find elements in the list use g_list_nth(), g_list_nth_data(),
|
2010-01-31 19:07:16 +01:00
|
|
|
|
* g_list_find() and g_list_find_custom().
|
|
|
|
|
*
|
|
|
|
|
* To find the index of an element use g_list_position() and
|
|
|
|
|
* g_list_index().
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* To free the entire list, use g_list_free() or g_list_free_full().
|
|
|
|
|
*/
|
2010-01-31 19:07:16 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GList:
|
|
|
|
|
* @data: holds the element's data, which can be a pointer to any kind
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* of data, or any integer value using the
|
|
|
|
|
* [Type Conversion Macros][glib-Type-Conversion-Macros]
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @next: contains the link to the next element in the list
|
|
|
|
|
* @prev: contains the link to the previous element in the list
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* The #GList struct is used for each element in a doubly-linked list.
|
|
|
|
|
**/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_previous:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @list: an element in a #GList
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* A convenience macro to get the previous element in a #GList.
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Note that it is considered perfectly acceptable to access
|
2017-08-07 21:33:25 +02:00
|
|
|
|
* @list->prev directly.
|
2012-10-31 02:56:00 +01:00
|
|
|
|
*
|
|
|
|
|
* Returns: the previous element, or %NULL if there are no previous
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* elements
|
2010-01-31 19:07:16 +01:00
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_list_next:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @list: an element in a #GList
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* A convenience macro to get the next element in a #GList.
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Note that it is considered perfectly acceptable to access
|
|
|
|
|
* @list->next directly.
|
2012-10-31 02:56:00 +01:00
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Returns: the next element, or %NULL if there are no more elements
|
2010-01-31 19:07:16 +01:00
|
|
|
|
**/
|
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
#define _g_list_alloc() g_slice_new (GList)
|
2005-11-01 19:10:31 +01:00
|
|
|
|
#define _g_list_alloc0() g_slice_new0 (GList)
|
|
|
|
|
#define _g_list_free1(list) g_slice_free (GList, list)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_alloc:
|
|
|
|
|
*
|
|
|
|
|
* Allocates space for one #GList element. It is called by
|
|
|
|
|
* g_list_append(), g_list_prepend(), g_list_insert() and
|
|
|
|
|
* g_list_insert_sorted() and so is rarely used on its own.
|
2012-10-31 02:56:00 +01:00
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Returns: a pointer to the newly-allocated #GList element
|
2010-01-31 19:07:16 +01:00
|
|
|
|
**/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1999-08-17 19:41:01 +02:00
|
|
|
|
g_list_alloc (void)
|
|
|
|
|
{
|
2005-11-01 19:10:31 +01:00
|
|
|
|
return _g_list_alloc0 ();
|
1999-08-17 19:41:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_free:
|
2020-09-22 17:41:55 +02:00
|
|
|
|
* @list: the first link of a #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Frees all of the memory used by a #GList.
|
2014-01-31 20:56:10 +01:00
|
|
|
|
* The freed elements are returned to the slice allocator.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-01-31 20:56:10 +01:00
|
|
|
|
* If list elements contain dynamically-allocated memory, you should
|
|
|
|
|
* either use g_list_free_full() or free them manually first.
|
2020-02-07 15:09:41 +01:00
|
|
|
|
*
|
|
|
|
|
* It can be combined with g_steal_pointer() to ensure the list head pointer
|
|
|
|
|
* is not left dangling:
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* GList *list_of_borrowed_things = …; /<!-- -->* (transfer container) *<!-- -->/
|
|
|
|
|
* g_list_free (g_steal_pointer (&list_of_borrowed_things));
|
|
|
|
|
* ]|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
void
|
|
|
|
|
g_list_free (GList *list)
|
|
|
|
|
{
|
2005-12-05 16:01:27 +01:00
|
|
|
|
g_slice_free_chain (GList, list, next);
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_free_1:
|
|
|
|
|
* @list: a #GList element
|
|
|
|
|
*
|
2014-12-19 19:53:22 +01:00
|
|
|
|
* Frees one #GList element, but does not update links from the next and
|
|
|
|
|
* previous elements in the list, so you should not call this function on an
|
|
|
|
|
* element that is currently part of a list.
|
|
|
|
|
*
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* It is usually used after g_list_remove_link().
|
|
|
|
|
*/
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_free1:
|
|
|
|
|
*
|
|
|
|
|
* Another name for g_list_free_1().
|
|
|
|
|
**/
|
1999-08-17 19:41:01 +02:00
|
|
|
|
void
|
|
|
|
|
g_list_free_1 (GList *list)
|
|
|
|
|
{
|
2005-11-01 19:10:31 +01:00
|
|
|
|
_g_list_free1 (list);
|
1999-08-17 19:41:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-10-16 12:19:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_list_free_full:
|
2020-09-22 17:41:55 +02:00
|
|
|
|
* @list: the first link of a #GList
|
2009-10-16 12:19:06 +02:00
|
|
|
|
* @free_func: the function to be called to free each element's data
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Convenience method, which frees all the memory used by a #GList,
|
|
|
|
|
* and calls @free_func on every element's data.
|
2009-10-16 12:19:06 +02:00
|
|
|
|
*
|
2014-02-14 22:03:49 +01:00
|
|
|
|
* @free_func must not modify the list (eg, by removing the freed
|
|
|
|
|
* element from it).
|
|
|
|
|
*
|
2020-02-07 15:09:41 +01:00
|
|
|
|
* It can be combined with g_steal_pointer() to ensure the list head pointer
|
|
|
|
|
* is not left dangling — this also has the nice property that the head pointer
|
|
|
|
|
* is cleared before any of the list elements are freed, to prevent double frees
|
|
|
|
|
* from @free_func:
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* GList *list_of_owned_things = …; /<!-- -->* (transfer full) (element-type GObject) *<!-- -->/
|
|
|
|
|
* g_list_free_full (g_steal_pointer (&list_of_owned_things), g_object_unref);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2009-10-16 12:19:06 +02:00
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_list_free_full (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GDestroyNotify free_func)
|
2009-10-16 12:19:06 +02:00
|
|
|
|
{
|
2011-07-28 05:27:24 +02:00
|
|
|
|
g_list_foreach (list, (GFunc) free_func, NULL);
|
|
|
|
|
g_list_free (list);
|
2009-10-16 12:19:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_append:
|
|
|
|
|
* @list: a pointer to a #GList
|
|
|
|
|
* @data: the data for the new element
|
|
|
|
|
*
|
|
|
|
|
* Adds a new element on to the end of the list.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Note that the return value is the new start of the list,
|
|
|
|
|
* if @list was empty; make sure you store the new value.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* g_list_append() has to traverse the entire list to find the end,
|
|
|
|
|
* which is inefficient when adding multiple elements. A common idiom
|
|
|
|
|
* to avoid the inefficiency is to use g_list_prepend() and reverse
|
|
|
|
|
* the list with g_list_reverse() when all elements have been added.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // Notice that these are initialized to the empty list.
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* GList *string_list = NULL, *number_list = NULL;
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // This is a list of strings.
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* string_list = g_list_append (string_list, "first");
|
|
|
|
|
* string_list = g_list_append (string_list, "second");
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // This is a list of integers.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* number_list = g_list_append (number_list, GINT_TO_POINTER (27));
|
|
|
|
|
* number_list = g_list_append (number_list, GINT_TO_POINTER (14));
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: either @list or the new start of the #GList if @list was %NULL
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_append (GList *list,
|
|
|
|
|
gpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
GList *new_list;
|
|
|
|
|
GList *last;
|
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list = _g_list_alloc ();
|
1998-06-11 01:21:14 +02:00
|
|
|
|
new_list->data = data;
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list->next = NULL;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
|
|
|
|
if (list)
|
|
|
|
|
{
|
|
|
|
|
last = g_list_last (list);
|
|
|
|
|
/* g_assert (last != NULL); */
|
|
|
|
|
last->next = new_list;
|
|
|
|
|
new_list->prev = last;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-03-20 19:43:32 +01:00
|
|
|
|
{
|
|
|
|
|
new_list->prev = NULL;
|
|
|
|
|
return new_list;
|
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_prepend:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a pointer to a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the data for the new element
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Prepends a new element on to the start of the list.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Note that the return value is the new start of the list,
|
|
|
|
|
* which will have changed, so make sure you store the new value.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // Notice that it is initialized to the empty list.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* GList *list = NULL;
|
2014-01-20 03:10:25 +01:00
|
|
|
|
*
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* list = g_list_prepend (list, "last");
|
|
|
|
|
* list = g_list_prepend (list, "first");
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2014-01-31 20:56:10 +01:00
|
|
|
|
* Do not use this function to prepend a new element to a different
|
|
|
|
|
* element than the start of the list. Use g_list_insert_before() instead.
|
2012-09-05 09:52:23 +02:00
|
|
|
|
*
|
|
|
|
|
* Returns: a pointer to the newly prepended element, which is the new
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_prepend (GList *list,
|
|
|
|
|
gpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
GList *new_list;
|
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list = _g_list_alloc ();
|
1998-06-11 01:21:14 +02:00
|
|
|
|
new_list->data = data;
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list->next = list;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
|
|
|
|
if (list)
|
|
|
|
|
{
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list->prev = list->prev;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
if (list->prev)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
list->prev->next = new_list;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
list->prev = new_list;
|
|
|
|
|
}
|
2006-03-20 19:43:32 +01:00
|
|
|
|
else
|
|
|
|
|
new_list->prev = NULL;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
|
|
|
|
return new_list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_insert:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a pointer to a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the data for the new element
|
|
|
|
|
* @position: the position to insert the element. If this is
|
|
|
|
|
* negative, or is larger than the number of elements in the
|
|
|
|
|
* list, the new element is added on to the end of the list.
|
|
|
|
|
*
|
|
|
|
|
* Inserts a new element into the list at the given position.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_insert (GList *list,
|
|
|
|
|
gpointer data,
|
|
|
|
|
gint position)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
GList *new_list;
|
|
|
|
|
GList *tmp_list;
|
2011-10-04 05:54:08 +02:00
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
|
if (position < 0)
|
|
|
|
|
return g_list_append (list, data);
|
|
|
|
|
else if (position == 0)
|
|
|
|
|
return g_list_prepend (list, data);
|
2011-10-04 05:54:08 +02:00
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
|
tmp_list = g_list_nth (list, position);
|
|
|
|
|
if (!tmp_list)
|
|
|
|
|
return g_list_append (list, data);
|
2011-10-04 05:54:08 +02:00
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list = _g_list_alloc ();
|
1998-06-11 01:21:14 +02:00
|
|
|
|
new_list->data = data;
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list->prev = tmp_list->prev;
|
2011-10-04 05:54:08 +02:00
|
|
|
|
tmp_list->prev->next = new_list;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
new_list->next = tmp_list;
|
|
|
|
|
tmp_list->prev = new_list;
|
2011-10-04 05:54:08 +02:00
|
|
|
|
|
|
|
|
|
return list;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 20:43:12 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_insert_before_link:
|
|
|
|
|
* @list: a pointer to a #GList, this must point to the top of the list
|
|
|
|
|
* @sibling: (nullable): the list element before which the new element
|
|
|
|
|
* is inserted or %NULL to insert at the end of the list
|
|
|
|
|
* @link_: the list element to be added, which must not be part of
|
|
|
|
|
* any other list
|
|
|
|
|
*
|
|
|
|
|
* Inserts @link_ into the list before the given position.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.62
|
|
|
|
|
*/
|
|
|
|
|
GList *
|
|
|
|
|
g_list_insert_before_link (GList *list,
|
|
|
|
|
GList *sibling,
|
|
|
|
|
GList *link_)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (link_ != NULL, list);
|
|
|
|
|
g_return_val_if_fail (link_->prev == NULL, list);
|
|
|
|
|
g_return_val_if_fail (link_->next == NULL, list);
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (sibling == NULL, list);
|
|
|
|
|
return link_;
|
|
|
|
|
}
|
|
|
|
|
else if (sibling != NULL)
|
|
|
|
|
{
|
|
|
|
|
link_->prev = sibling->prev;
|
|
|
|
|
link_->next = sibling;
|
|
|
|
|
sibling->prev = link_;
|
|
|
|
|
if (link_->prev != NULL)
|
|
|
|
|
{
|
|
|
|
|
link_->prev->next = link_;
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (sibling == list, link_);
|
|
|
|
|
return link_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GList *last;
|
|
|
|
|
|
|
|
|
|
for (last = list; last->next != NULL; last = last->next) {}
|
|
|
|
|
|
|
|
|
|
last->next = link_;
|
|
|
|
|
last->next->prev = last;
|
|
|
|
|
last->next->next = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_insert_before:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a pointer to a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @sibling: the list element before which the new element
|
|
|
|
|
* is inserted or %NULL to insert at the end of the list
|
|
|
|
|
* @data: the data for the new element
|
|
|
|
|
*
|
|
|
|
|
* Inserts a new element into the list before the given position.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_insert_before (GList *list,
|
|
|
|
|
GList *sibling,
|
|
|
|
|
gpointer data)
|
2001-07-02 07:02:13 +02:00
|
|
|
|
{
|
2019-04-29 21:57:01 +02:00
|
|
|
|
if (list == NULL)
|
2001-07-02 07:02:13 +02:00
|
|
|
|
{
|
|
|
|
|
list = g_list_alloc ();
|
|
|
|
|
list->data = data;
|
|
|
|
|
g_return_val_if_fail (sibling == NULL, list);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2019-04-29 21:57:01 +02:00
|
|
|
|
else if (sibling != NULL)
|
2001-07-02 07:02:13 +02:00
|
|
|
|
{
|
|
|
|
|
GList *node;
|
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
node = _g_list_alloc ();
|
2001-07-02 07:02:13 +02:00
|
|
|
|
node->data = data;
|
2006-03-20 19:43:32 +01:00
|
|
|
|
node->prev = sibling->prev;
|
|
|
|
|
node->next = sibling;
|
|
|
|
|
sibling->prev = node;
|
2019-04-29 21:57:01 +02:00
|
|
|
|
if (node->prev != NULL)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
{
|
|
|
|
|
node->prev->next = node;
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2001-07-02 07:02:13 +02:00
|
|
|
|
else
|
2014-01-20 03:10:25 +01:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (sibling == list, node);
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2001-07-02 07:02:13 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GList *last;
|
|
|
|
|
|
2019-04-29 21:57:01 +02:00
|
|
|
|
for (last = list; last->next != NULL; last = last->next) {}
|
2001-07-02 07:02:13 +02:00
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
last->next = _g_list_alloc ();
|
2001-07-02 07:02:13 +02:00
|
|
|
|
last->next->data = data;
|
|
|
|
|
last->next->prev = last;
|
2006-03-20 19:43:32 +01:00
|
|
|
|
last->next->next = NULL;
|
2001-07-02 07:02:13 +02:00
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_concat:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list1: a #GList, this must point to the top of the list
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @list2: the #GList to add to the end of the first #GList,
|
|
|
|
|
* this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Adds the second #GList onto the end of the first #GList.
|
|
|
|
|
* Note that the elements of the second #GList are not copied.
|
|
|
|
|
* They are used directly.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* This function is for example used to move an element in the list.
|
|
|
|
|
* The following example moves an element to the top of the list:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* list = g_list_remove_link (list, llink);
|
|
|
|
|
* list = g_list_concat (llink, list);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
|
|
|
|
* Returns: the start of the new #GList, which equals @list1 if not %NULL
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
GList *
|
2014-01-20 03:10:25 +01:00
|
|
|
|
g_list_concat (GList *list1,
|
|
|
|
|
GList *list2)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
GList *tmp_list;
|
|
|
|
|
|
|
|
|
|
if (list2)
|
|
|
|
|
{
|
|
|
|
|
tmp_list = g_list_last (list1);
|
|
|
|
|
if (tmp_list)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
tmp_list->next = list2;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
else
|
2014-01-20 03:10:25 +01:00
|
|
|
|
list1 = list2;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
list2->prev = tmp_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-20 03:10:25 +01:00
|
|
|
|
static inline GList *
|
2013-02-24 22:54:09 +01:00
|
|
|
|
_g_list_remove_link (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *link)
|
2013-02-24 22:54:09 +01:00
|
|
|
|
{
|
2013-03-02 01:11:11 +01:00
|
|
|
|
if (link == NULL)
|
|
|
|
|
return list;
|
|
|
|
|
|
2013-02-24 22:54:09 +01:00
|
|
|
|
if (link->prev)
|
|
|
|
|
{
|
|
|
|
|
if (link->prev->next == link)
|
|
|
|
|
link->prev->next = link->next;
|
|
|
|
|
else
|
|
|
|
|
g_warning ("corrupted double-linked list detected");
|
|
|
|
|
}
|
|
|
|
|
if (link->next)
|
|
|
|
|
{
|
|
|
|
|
if (link->next->prev == link)
|
|
|
|
|
link->next->prev = link->prev;
|
|
|
|
|
else
|
|
|
|
|
g_warning ("corrupted double-linked list detected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (link == list)
|
|
|
|
|
list = list->next;
|
|
|
|
|
|
|
|
|
|
link->next = NULL;
|
|
|
|
|
link->prev = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_remove:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the data of the element to remove
|
|
|
|
|
*
|
|
|
|
|
* Removes an element from a #GList.
|
|
|
|
|
* If two elements contain the same data, only the first is removed.
|
|
|
|
|
* If none of the elements contain the data, the #GList is unchanged.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_remove (GList *list,
|
|
|
|
|
gconstpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
GList *tmp;
|
2013-02-24 22:54:09 +01:00
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
|
tmp = list;
|
|
|
|
|
while (tmp)
|
|
|
|
|
{
|
|
|
|
|
if (tmp->data != data)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
tmp = tmp->next;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
else
|
2014-01-20 03:10:25 +01:00
|
|
|
|
{
|
2013-02-24 22:54:09 +01:00
|
|
|
|
list = _g_list_remove_link (list, tmp);
|
2014-01-20 03:10:25 +01:00
|
|
|
|
_g_list_free1 (tmp);
|
2013-02-24 22:54:09 +01:00
|
|
|
|
|
2014-01-20 03:10:25 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_remove_all:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: data to remove
|
|
|
|
|
*
|
2013-02-24 22:54:09 +01:00
|
|
|
|
* Removes all list nodes with data equal to @data.
|
|
|
|
|
* Returns the new head of the list. Contrast with
|
|
|
|
|
* g_list_remove() which removes only the first node
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* matching the given data.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_remove_all (GList *list,
|
|
|
|
|
gconstpointer data)
|
2001-03-18 05:44:38 +01:00
|
|
|
|
{
|
|
|
|
|
GList *tmp = list;
|
|
|
|
|
|
|
|
|
|
while (tmp)
|
|
|
|
|
{
|
|
|
|
|
if (tmp->data != data)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
tmp = tmp->next;
|
2001-03-18 05:44:38 +01:00
|
|
|
|
else
|
2014-01-20 03:10:25 +01:00
|
|
|
|
{
|
|
|
|
|
GList *next = tmp->next;
|
|
|
|
|
|
|
|
|
|
if (tmp->prev)
|
|
|
|
|
tmp->prev->next = next;
|
|
|
|
|
else
|
|
|
|
|
list = next;
|
|
|
|
|
if (next)
|
|
|
|
|
next->prev = tmp->prev;
|
|
|
|
|
|
|
|
|
|
_g_list_free1 (tmp);
|
|
|
|
|
tmp = next;
|
|
|
|
|
}
|
2001-03-18 05:44:38 +01:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_remove_link:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @llink: an element in the #GList
|
|
|
|
|
*
|
|
|
|
|
* Removes an element from a #GList, without freeing the element.
|
|
|
|
|
* The removed element's prev and next links are set to %NULL, so
|
|
|
|
|
* that it becomes a self-contained list with one element.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* This function is for example used to move an element in the list
|
|
|
|
|
* (see the example for g_list_concat()) or to remove an element in
|
|
|
|
|
* the list before freeing its data:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* list = g_list_remove_link (list, llink);
|
|
|
|
|
* free_some_data_that_may_access_the_list_again (llink->data);
|
|
|
|
|
* g_list_free (llink);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1999-08-17 19:41:01 +02:00
|
|
|
|
g_list_remove_link (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *llink)
|
1999-08-17 19:41:01 +02:00
|
|
|
|
{
|
2008-02-29 22:16:21 +01:00
|
|
|
|
return _g_list_remove_link (list, llink);
|
1999-08-17 19:41:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_delete_link:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @link_: node to delete from @list
|
|
|
|
|
*
|
|
|
|
|
* Removes the node link_ from the list and frees it.
|
|
|
|
|
* Compare this to g_list_remove_link() which removes the node
|
|
|
|
|
* without freeing it.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1999-07-24 20:50:58 +02:00
|
|
|
|
g_list_delete_link (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *link_)
|
1999-03-09 20:41:19 +01:00
|
|
|
|
{
|
2008-02-29 22:16:21 +01:00
|
|
|
|
list = _g_list_remove_link (list, link_);
|
|
|
|
|
_g_list_free1 (link_);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_copy:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Copies a #GList.
|
|
|
|
|
*
|
|
|
|
|
* Note that this is a "shallow" copy. If the list elements
|
|
|
|
|
* consist of pointers to data, the pointers are copied but
|
2012-06-21 17:23:23 +02:00
|
|
|
|
* the actual data is not. See g_list_copy_deep() if you need
|
|
|
|
|
* to copy the data as well.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the start of the new list that holds the same data as @list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1998-11-23 02:52:07 +01:00
|
|
|
|
g_list_copy (GList *list)
|
2012-06-21 17:23:23 +02:00
|
|
|
|
{
|
|
|
|
|
return g_list_copy_deep (list, NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_list_copy_deep:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2012-06-21 17:23:23 +02:00
|
|
|
|
* @func: a copy function used to copy every element in the list
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @user_data: user data passed to the copy function @func, or %NULL
|
2012-06-21 17:23:23 +02:00
|
|
|
|
*
|
|
|
|
|
* Makes a full (deep) copy of a #GList.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* In contrast with g_list_copy(), this function uses @func to make
|
|
|
|
|
* a copy of each list element, in addition to copying the list
|
|
|
|
|
* container itself.
|
2012-06-21 17:23:23 +02:00
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @func, as a #GCopyFunc, takes two arguments, the data to be copied
|
2018-08-20 12:00:29 +02:00
|
|
|
|
* and a @user_data pointer. On common processor architectures, it's safe to
|
|
|
|
|
* pass %NULL as @user_data if the copy function takes only one argument. You
|
|
|
|
|
* may get compiler warnings from this though if compiling with GCC’s
|
|
|
|
|
* `-Wcast-function-type` warning.
|
2012-06-21 17:23:23 +02:00
|
|
|
|
*
|
|
|
|
|
* For instance, if @list holds a list of GObjects, you can do:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2012-06-21 17:23:23 +02:00
|
|
|
|
* another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
|
|
|
|
* And, to entirely free the new list, you could do:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2012-06-21 17:23:23 +02:00
|
|
|
|
* g_list_free_full (another_list, g_object_unref);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the start of the new list that holds a full copy of @list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* use g_list_free_full() to free it
|
2012-06-21 17:23:23 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.34
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_copy_deep (GList *list,
|
|
|
|
|
GCopyFunc func,
|
|
|
|
|
gpointer user_data)
|
1998-11-23 02:52:07 +01:00
|
|
|
|
{
|
|
|
|
|
GList *new_list = NULL;
|
|
|
|
|
|
|
|
|
|
if (list)
|
|
|
|
|
{
|
|
|
|
|
GList *last;
|
|
|
|
|
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list = _g_list_alloc ();
|
2012-06-21 17:23:23 +02:00
|
|
|
|
if (func)
|
|
|
|
|
new_list->data = func (list->data, user_data);
|
|
|
|
|
else
|
|
|
|
|
new_list->data = list->data;
|
2006-03-20 19:43:32 +01:00
|
|
|
|
new_list->prev = NULL;
|
1998-11-23 02:52:07 +01:00
|
|
|
|
last = new_list;
|
|
|
|
|
list = list->next;
|
|
|
|
|
while (list)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
{
|
|
|
|
|
last->next = _g_list_alloc ();
|
|
|
|
|
last->next->prev = last;
|
|
|
|
|
last = last->next;
|
|
|
|
|
if (func)
|
|
|
|
|
last->data = func (list->data, user_data);
|
|
|
|
|
else
|
|
|
|
|
last->data = list->data;
|
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
2006-03-20 19:43:32 +01:00
|
|
|
|
last->next = NULL;
|
1998-11-23 02:52:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new_list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_reverse:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Reverses a #GList.
|
|
|
|
|
* It simply switches the next and prev pointers of each element.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the start of the reversed #GList
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1998-06-11 01:21:14 +02:00
|
|
|
|
g_list_reverse (GList *list)
|
|
|
|
|
{
|
|
|
|
|
GList *last;
|
|
|
|
|
|
|
|
|
|
last = NULL;
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
|
|
|
|
last = list;
|
|
|
|
|
list = last->next;
|
|
|
|
|
last->next = last->prev;
|
|
|
|
|
last->prev = list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return last;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_nth:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @n: the position of the element, counting from 0
|
|
|
|
|
*
|
|
|
|
|
* Gets the element at the given position in a #GList.
|
|
|
|
|
*
|
2014-12-19 19:53:54 +01:00
|
|
|
|
* This iterates over the list until it reaches the @n-th position. If you
|
|
|
|
|
* intend to iterate over every element, it is better to use a for-loop as
|
|
|
|
|
* described in the #GList introduction.
|
|
|
|
|
*
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* Returns: the element, or %NULL if the position is off
|
|
|
|
|
* the end of the #GList
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1998-06-11 01:21:14 +02:00
|
|
|
|
g_list_nth (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
guint n)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
while ((n-- > 0) && list)
|
|
|
|
|
list = list->next;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_nth_prev:
|
|
|
|
|
* @list: a #GList
|
|
|
|
|
* @n: the position of the element, counting from 0
|
|
|
|
|
*
|
|
|
|
|
* Gets the element @n places before @list.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the element, or %NULL if the position is
|
|
|
|
|
* off the end of the #GList
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
2001-04-03 15:15:41 +02:00
|
|
|
|
g_list_nth_prev (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
guint n)
|
2001-04-03 15:15:41 +02:00
|
|
|
|
{
|
|
|
|
|
while ((n-- > 0) && list)
|
|
|
|
|
list = list->prev;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_nth_data:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @n: the position of the element
|
|
|
|
|
*
|
|
|
|
|
* Gets the data of the element at the given position.
|
|
|
|
|
*
|
2014-12-19 19:53:54 +01:00
|
|
|
|
* This iterates over the list until it reaches the @n-th position. If you
|
|
|
|
|
* intend to iterate over every element, it is better to use a for-loop as
|
|
|
|
|
* described in the #GList introduction.
|
|
|
|
|
*
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* Returns: the element's data, or %NULL if the position
|
|
|
|
|
* is off the end of the #GList
|
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
gpointer
|
2014-01-20 03:10:25 +01:00
|
|
|
|
g_list_nth_data (GList *list,
|
|
|
|
|
guint n)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
while ((n-- > 0) && list)
|
|
|
|
|
list = list->next;
|
|
|
|
|
|
|
|
|
|
return list ? list->data : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_find:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the element data to find
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Finds the element in a #GList which contains the given data.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Returns: the found #GList element, or %NULL if it is not found
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
2000-04-18 16:01:33 +02:00
|
|
|
|
g_list_find (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
gconstpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
|
|
|
|
if (list->data == data)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
break;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_find_custom:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: user data passed to the function
|
|
|
|
|
* @func: the function to call for each element.
|
|
|
|
|
* It should return 0 when the desired element is found
|
|
|
|
|
*
|
|
|
|
|
* Finds an element in a #GList, using a supplied function to
|
|
|
|
|
* find the desired element. It iterates over the list, calling
|
|
|
|
|
* the given function which should return 0 when the desired
|
|
|
|
|
* element is found. The function takes two #gconstpointer arguments,
|
|
|
|
|
* the #GList element's data as the first argument and the
|
|
|
|
|
* given user data.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the found #GList element, or %NULL if it is not found
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
2000-04-18 16:01:33 +02:00
|
|
|
|
g_list_find_custom (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
gconstpointer data,
|
|
|
|
|
GCompareFunc func)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (func != NULL, list);
|
|
|
|
|
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
|
|
|
|
if (! func (list->data, data))
|
2014-01-20 03:10:25 +01:00
|
|
|
|
return list;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_position:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @llink: an element in the #GList
|
|
|
|
|
*
|
|
|
|
|
* Gets the position of the given element
|
|
|
|
|
* in the #GList (starting from 0).
|
|
|
|
|
*
|
|
|
|
|
* Returns: the position of the element in the #GList,
|
|
|
|
|
* or -1 if the element is not found
|
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
gint
|
|
|
|
|
g_list_position (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *llink)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
2008-02-29 22:16:21 +01:00
|
|
|
|
if (list == llink)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
return i;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
i++;
|
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_index:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the data to find
|
|
|
|
|
*
|
|
|
|
|
* Gets the position of the element containing
|
|
|
|
|
* the given data (starting from 0).
|
|
|
|
|
*
|
|
|
|
|
* Returns: the index of the element containing the data,
|
|
|
|
|
* or -1 if the data is not found
|
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
gint
|
2000-04-18 16:01:33 +02:00
|
|
|
|
g_list_index (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
gconstpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
|
|
|
|
if (list->data == data)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
return i;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
i++;
|
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_last:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: any #GList element
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Gets the last element in a #GList.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* Returns: the last element in the #GList,
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* or %NULL if the #GList has no elements
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1998-06-11 01:21:14 +02:00
|
|
|
|
g_list_last (GList *list)
|
|
|
|
|
{
|
|
|
|
|
if (list)
|
|
|
|
|
{
|
|
|
|
|
while (list->next)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
list = list->next;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_first:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: any #GList element
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Gets the first element in a #GList.
|
|
|
|
|
*
|
2008-04-03 06:07:44 +02:00
|
|
|
|
* Returns: the first element in the #GList,
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* or %NULL if the #GList has no elements
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
1998-06-11 01:21:14 +02:00
|
|
|
|
g_list_first (GList *list)
|
|
|
|
|
{
|
|
|
|
|
if (list)
|
|
|
|
|
{
|
|
|
|
|
while (list->prev)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
list = list->prev;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_length:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Gets the number of elements in a #GList.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* This function iterates over the whole list to count its elements.
|
2014-01-31 20:56:10 +01:00
|
|
|
|
* Use a #GQueue instead of a GList if you regularly need the number
|
2014-12-02 16:00:45 +01:00
|
|
|
|
* of items. To check whether the list is non-empty, it is faster to check
|
|
|
|
|
* @list against %NULL.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Returns: the number of elements in the #GList
|
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
guint
|
|
|
|
|
g_list_length (GList *list)
|
|
|
|
|
{
|
|
|
|
|
guint length;
|
|
|
|
|
|
|
|
|
|
length = 0;
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
|
|
|
|
length++;
|
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_foreach:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @func: the function to call with each element's data
|
|
|
|
|
* @user_data: user data to pass to the function
|
|
|
|
|
*
|
|
|
|
|
* Calls a function for each element of a #GList.
|
2014-02-14 22:03:49 +01:00
|
|
|
|
*
|
|
|
|
|
* It is safe for @func to remove the element from @list, but it must
|
|
|
|
|
* not modify any part of the list after that element.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* GFunc:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @data: the element's data
|
|
|
|
|
* @user_data: user data passed to g_list_foreach() or g_slist_foreach()
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* Specifies the type of functions passed to g_list_foreach() and
|
|
|
|
|
* g_slist_foreach().
|
2014-01-20 03:10:25 +01:00
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
|
void
|
2014-01-20 03:10:25 +01:00
|
|
|
|
g_list_foreach (GList *list,
|
|
|
|
|
GFunc func,
|
|
|
|
|
gpointer user_data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
2001-05-03 12:47:32 +02:00
|
|
|
|
GList *next = list->next;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
(*func) (list->data, user_data);
|
2001-05-03 12:47:32 +02:00
|
|
|
|
list = next;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-07 12:35:27 +01:00
|
|
|
|
static GList*
|
|
|
|
|
g_list_insert_sorted_real (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
gpointer data,
|
|
|
|
|
GFunc func,
|
|
|
|
|
gpointer user_data)
|
1998-06-11 01:21:14 +02:00
|
|
|
|
{
|
|
|
|
|
GList *tmp_list = list;
|
|
|
|
|
GList *new_list;
|
|
|
|
|
gint cmp;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (func != NULL, list);
|
|
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
|
{
|
2005-11-01 19:10:31 +01:00
|
|
|
|
new_list = _g_list_alloc0 ();
|
1998-06-11 01:21:14 +02:00
|
|
|
|
new_list->data = data;
|
|
|
|
|
return new_list;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-07 12:35:27 +01:00
|
|
|
|
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
|
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
|
while ((tmp_list->next) && (cmp > 0))
|
|
|
|
|
{
|
|
|
|
|
tmp_list = tmp_list->next;
|
2005-12-07 12:35:27 +01:00
|
|
|
|
|
|
|
|
|
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
|
1998-06-11 01:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-01 19:10:31 +01:00
|
|
|
|
new_list = _g_list_alloc0 ();
|
1998-06-11 01:21:14 +02:00
|
|
|
|
new_list->data = data;
|
|
|
|
|
|
|
|
|
|
if ((!tmp_list->next) && (cmp > 0))
|
|
|
|
|
{
|
|
|
|
|
tmp_list->next = new_list;
|
|
|
|
|
new_list->prev = tmp_list;
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tmp_list->prev)
|
|
|
|
|
{
|
|
|
|
|
tmp_list->prev->next = new_list;
|
|
|
|
|
new_list->prev = tmp_list->prev;
|
|
|
|
|
}
|
|
|
|
|
new_list->next = tmp_list;
|
|
|
|
|
tmp_list->prev = new_list;
|
|
|
|
|
|
|
|
|
|
if (tmp_list == list)
|
|
|
|
|
return new_list;
|
|
|
|
|
else
|
|
|
|
|
return list;
|
|
|
|
|
}
|
1998-11-13 21:50:41 +01:00
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_insert_sorted:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @list: a pointer to a #GList, this must point to the top of the
|
|
|
|
|
* already sorted list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the data for the new element
|
|
|
|
|
* @func: the function to compare elements in the list. It should
|
|
|
|
|
* return a number > 0 if the first parameter comes after the
|
|
|
|
|
* second parameter in the sort order.
|
|
|
|
|
*
|
|
|
|
|
* Inserts a new element into the list, using the given comparison
|
|
|
|
|
* function to determine its position.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* If you are adding many new elements to a list, and the number of
|
|
|
|
|
* new elements is much larger than the length of the list, use
|
|
|
|
|
* g_list_prepend() to add the new items and sort the list afterwards
|
2014-01-31 20:56:10 +01:00
|
|
|
|
* with g_list_sort().
|
2012-09-05 09:52:23 +02:00
|
|
|
|
*
|
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
2005-12-07 12:35:27 +01:00
|
|
|
|
g_list_insert_sorted (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
gpointer data,
|
|
|
|
|
GCompareFunc func)
|
2005-12-07 12:35:27 +01:00
|
|
|
|
{
|
|
|
|
|
return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_insert_sorted_with_data:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @list: a pointer to a #GList, this must point to the top of the
|
|
|
|
|
* already sorted list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @data: the data for the new element
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @func: the function to compare elements in the list. It should
|
|
|
|
|
* return a number > 0 if the first parameter comes after the
|
|
|
|
|
* second parameter in the sort order.
|
|
|
|
|
* @user_data: user data to pass to comparison function
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Inserts a new element into the list, using the given comparison
|
|
|
|
|
* function to determine its position.
|
|
|
|
|
*
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* If you are adding many new elements to a list, and the number of
|
|
|
|
|
* new elements is much larger than the length of the list, use
|
|
|
|
|
* g_list_prepend() to add the new items and sort the list afterwards
|
2014-01-31 20:56:10 +01:00
|
|
|
|
* with g_list_sort().
|
2014-01-20 03:10:25 +01:00
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.10
|
|
|
|
|
*/
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *
|
2005-12-07 12:35:27 +01:00
|
|
|
|
g_list_insert_sorted_with_data (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
gpointer data,
|
|
|
|
|
GCompareDataFunc func,
|
|
|
|
|
gpointer user_data)
|
2005-12-07 12:35:27 +01:00
|
|
|
|
{
|
|
|
|
|
return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
|
|
|
|
|
}
|
|
|
|
|
|
1998-11-13 21:50:41 +01:00
|
|
|
|
static GList *
|
2000-11-21 00:59:32 +01:00
|
|
|
|
g_list_sort_merge (GList *l1,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GList *l2,
|
|
|
|
|
GFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
1998-11-13 21:50:41 +01:00
|
|
|
|
{
|
|
|
|
|
GList list, *l, *lprev;
|
2000-11-21 00:59:32 +01:00
|
|
|
|
gint cmp;
|
1998-11-13 21:50:41 +01:00
|
|
|
|
|
|
|
|
|
l = &list;
|
|
|
|
|
lprev = NULL;
|
|
|
|
|
|
|
|
|
|
while (l1 && l2)
|
|
|
|
|
{
|
2005-12-07 12:35:27 +01:00
|
|
|
|
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
|
2000-11-21 00:59:32 +01:00
|
|
|
|
|
|
|
|
|
if (cmp <= 0)
|
1998-11-13 21:50:41 +01:00
|
|
|
|
{
|
2014-01-20 03:10:25 +01:00
|
|
|
|
l->next = l1;
|
|
|
|
|
l1 = l1->next;
|
1998-11-13 21:50:41 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2014-01-20 03:10:25 +01:00
|
|
|
|
{
|
|
|
|
|
l->next = l2;
|
|
|
|
|
l2 = l2->next;
|
1998-11-13 21:50:41 +01:00
|
|
|
|
}
|
2005-11-04 23:27:04 +01:00
|
|
|
|
l = l->next;
|
|
|
|
|
l->prev = lprev;
|
|
|
|
|
lprev = l;
|
1998-11-13 21:50:41 +01:00
|
|
|
|
}
|
|
|
|
|
l->next = l1 ? l1 : l2;
|
|
|
|
|
l->next->prev = l;
|
|
|
|
|
|
|
|
|
|
return list.next;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-20 03:10:25 +01:00
|
|
|
|
static GList *
|
2000-11-21 00:59:32 +01:00
|
|
|
|
g_list_sort_real (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
1998-11-13 21:50:41 +01:00
|
|
|
|
{
|
|
|
|
|
GList *l1, *l2;
|
|
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
|
return NULL;
|
|
|
|
|
if (!list->next)
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
|
|
l1 = list;
|
|
|
|
|
l2 = list->next;
|
|
|
|
|
|
|
|
|
|
while ((l2 = l2->next) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if ((l2 = l2->next) == NULL)
|
2014-01-20 03:10:25 +01:00
|
|
|
|
break;
|
1998-11-13 21:50:41 +01:00
|
|
|
|
l1 = l1->next;
|
|
|
|
|
}
|
|
|
|
|
l2 = l1->next;
|
|
|
|
|
l1->next = NULL;
|
|
|
|
|
|
2005-12-07 12:35:27 +01:00
|
|
|
|
return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
|
2014-01-20 03:10:25 +01:00
|
|
|
|
g_list_sort_real (l2, compare_func, user_data),
|
|
|
|
|
compare_func,
|
|
|
|
|
user_data);
|
2000-11-21 00:59:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_sort:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @compare_func: the comparison function used to sort the #GList.
|
|
|
|
|
* This function is passed the data from 2 elements of the #GList
|
|
|
|
|
* and should return 0 if they are equal, a negative value if the
|
|
|
|
|
* first element comes before the second, or a positive value if
|
|
|
|
|
* the first element comes after the second.
|
|
|
|
|
*
|
2011-11-20 16:53:36 +01:00
|
|
|
|
* Sorts a #GList using the given comparison function. The algorithm
|
|
|
|
|
* used is a stable sort.
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* GCompareFunc:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @a: a value
|
|
|
|
|
* @b: a value to compare with
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* Specifies the type of a comparison function used to compare two
|
|
|
|
|
* values. The function should return a negative integer if the first
|
|
|
|
|
* value comes before the second, 0 if they are equal, or a positive
|
|
|
|
|
* integer if the first value comes after the second.
|
2012-10-31 02:56:00 +01:00
|
|
|
|
*
|
2014-02-09 08:07:26 +01:00
|
|
|
|
* Returns: negative value if @a < @b; zero if @a = @b; positive
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* value if @a > @b
|
|
|
|
|
*/
|
2000-11-21 00:59:32 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_sort (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GCompareFunc compare_func)
|
2000-11-21 00:59:32 +01:00
|
|
|
|
{
|
2005-12-07 12:35:27 +01:00
|
|
|
|
return g_list_sort_real (list, (GFunc) compare_func, NULL);
|
2000-11-21 00:59:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 22:16:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_list_sort_with_data:
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* @list: a #GList, this must point to the top of the list
|
2008-02-29 22:16:21 +01:00
|
|
|
|
* @compare_func: comparison function
|
|
|
|
|
* @user_data: user data to pass to comparison function
|
|
|
|
|
*
|
|
|
|
|
* Like g_list_sort(), but the comparison function accepts
|
|
|
|
|
* a user data argument.
|
|
|
|
|
*
|
2012-09-05 09:52:23 +02:00
|
|
|
|
* Returns: the (possibly changed) start of the #GList
|
2008-02-29 22:16:21 +01:00
|
|
|
|
*/
|
2010-01-31 19:07:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* GCompareDataFunc:
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* @a: a value
|
|
|
|
|
* @b: a value to compare with
|
|
|
|
|
* @user_data: user data
|
2010-01-31 19:07:16 +01:00
|
|
|
|
*
|
|
|
|
|
* Specifies the type of a comparison function used to compare two
|
|
|
|
|
* values. The function should return a negative integer if the first
|
|
|
|
|
* value comes before the second, 0 if they are equal, or a positive
|
|
|
|
|
* integer if the first value comes after the second.
|
2012-10-31 02:56:00 +01:00
|
|
|
|
*
|
2014-02-09 08:07:26 +01:00
|
|
|
|
* Returns: negative value if @a < @b; zero if @a = @b; positive
|
2014-01-20 03:10:25 +01:00
|
|
|
|
* value if @a > @b
|
|
|
|
|
*/
|
2000-11-21 00:59:32 +01:00
|
|
|
|
GList *
|
|
|
|
|
g_list_sort_with_data (GList *list,
|
2014-01-20 03:10:25 +01:00
|
|
|
|
GCompareDataFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
2000-11-21 00:59:32 +01:00
|
|
|
|
{
|
2005-12-07 12:35:27 +01:00
|
|
|
|
return g_list_sort_real (list, (GFunc) compare_func, user_data);
|
1998-11-13 21:50:41 +01:00
|
|
|
|
}
|
2019-11-23 17:41:54 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_clear_list: (skip)
|
|
|
|
|
* @list_ptr: (not nullable): a #GList return location
|
|
|
|
|
* @destroy: (nullable): the function to pass to g_list_free_full() or %NULL to not free elements
|
|
|
|
|
*
|
|
|
|
|
* Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
|
|
|
|
|
*
|
|
|
|
|
* @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.64
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
(g_clear_list) (GList **list_ptr,
|
|
|
|
|
GDestroyNotify destroy)
|
|
|
|
|
{
|
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
|
|
list = *list_ptr;
|
|
|
|
|
if (list)
|
|
|
|
|
{
|
|
|
|
|
*list_ptr = NULL;
|
|
|
|
|
|
|
|
|
|
if (destroy)
|
|
|
|
|
g_list_free_full (list, destroy);
|
|
|
|
|
else
|
|
|
|
|
g_list_free (list);
|
|
|
|
|
}
|
|
|
|
|
}
|