Blame glib/glist.c

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