Blame glib/glib/glist.c

Packit db3073
/* GLIB - Library of useful routines for C programming
Packit db3073
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit db3073
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit db3073
 * files for a list of changes.  These files are distributed with
Packit db3073
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * MT safe
Packit db3073
 */
Packit db3073
Packit db3073
#include "config.h"
Packit db3073
Packit db3073
#include "glist.h"
Packit db3073
#include "gslice.h"
Packit db3073
Packit db3073
#include "gtestutils.h"
Packit db3073
Packit db3073
/**
Packit db3073
 * SECTION:linked_lists_double
Packit db3073
 * @title: Doubly-Linked Lists
Packit db3073
 * @short_description: linked lists that can be iterated over in both directions
Packit db3073
 *
Packit db3073
 * The #GList structure and its associated functions provide a standard
Packit db3073
 * doubly-linked list data structure.
Packit db3073
 *
Packit db3073
 * Each element in the list contains a piece of data, together with
Packit db3073
 * pointers which link to the previous and next elements in the list.
Packit db3073
 * Using these pointers it is possible to move through the list in both
Packit db3073
 * directions (unlike the 
Packit db3073
 * linkend="glib-Singly-Linked-Lists">Singly-Linked Lists</link> which
Packit db3073
 * only allows movement through the list in the forward direction).
Packit db3073
 *
Packit db3073
 * The data contained in each element can be either integer values, by
Packit db3073
 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
Packit db3073
 * Conversion Macros</link>, or simply pointers to any type of data.
Packit db3073
 *
Packit db3073
 * List elements are allocated from the 
Packit db3073
 * linkend="glib-Memory-Slices">slice allocator</link>, which is more
Packit db3073
 * efficient than allocating elements individually.
Packit db3073
 *
Packit db3073
 * Note that most of the #GList functions expect to be passed a pointer
Packit db3073
 * to the first element in the list. The functions which insert
Packit db3073
 * elements return the new start of the list, which may have changed.
Packit db3073
 *
Packit db3073
 * There is no function to create a #GList. %NULL is considered to be
Packit db3073
 * the empty list so you simply set a #GList* to %NULL.
Packit db3073
 *
Packit db3073
 * To add elements, use g_list_append(), g_list_prepend(),
Packit db3073
 * g_list_insert() and g_list_insert_sorted().
Packit db3073
 *
Packit db3073
 * To remove elements, use g_list_remove().
Packit db3073
 *
Packit db3073
 * To find elements in the list use g_list_first(), g_list_last(),
Packit db3073
 * g_list_next(), g_list_previous(), g_list_nth(), g_list_nth_data(),
Packit db3073
 * g_list_find() and g_list_find_custom().
Packit db3073
 *
Packit db3073
 * To find the index of an element use g_list_position() and
Packit db3073
 * g_list_index().
Packit db3073
 *
Packit db3073
 * To call a function for each element in the list use g_list_foreach().
Packit db3073
 *
Packit db3073
 * To free the entire list, use g_list_free().
Packit db3073
 **/
Packit db3073
Packit db3073
/**
Packit db3073
 * GList:
Packit db3073
 * @data: holds the element's data, which can be a pointer to any kind
Packit db3073
 *        of data, or any integer value using the 
Packit db3073
 *        linkend="glib-Type-Conversion-Macros">Type Conversion
Packit db3073
 *        Macros</link>.
Packit db3073
 * @next: contains the link to the next element in the list.
Packit db3073
 * @prev: contains the link to the previous element in the list.
Packit db3073
 *
Packit db3073
 * The #GList struct is used for each element in a doubly-linked list.
Packit db3073
 **/
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_previous:
Packit db3073
 * @list: an element in a #GList.
Packit db3073
 * @Returns: the previous element, or %NULL if there are no previous
Packit db3073
 *           elements.
Packit db3073
 *
Packit db3073
 * A convenience macro to get the previous element in a #GList.
Packit db3073
 **/
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_next:
Packit db3073
 * @list: an element in a #GList.
Packit db3073
 * @Returns: the next element, or %NULL if there are no more elements.
Packit db3073
 *
Packit db3073
 * A convenience macro to get the next element in a #GList.
Packit db3073
 **/
Packit db3073
Packit db3073
#define _g_list_alloc()         g_slice_new (GList)
Packit db3073
#define _g_list_alloc0()        g_slice_new0 (GList)
Packit db3073
#define _g_list_free1(list)     g_slice_free (GList, list)
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_alloc:
Packit db3073
 * @Returns: a pointer to the newly-allocated #GList element.
Packit db3073
 *
Packit db3073
 * Allocates space for one #GList element. It is called by
Packit db3073
 * g_list_append(), g_list_prepend(), g_list_insert() and
Packit db3073
 * g_list_insert_sorted() and so is rarely used on its own.
Packit db3073
 **/
Packit db3073
GList*
Packit db3073
g_list_alloc (void)
Packit db3073
{
Packit db3073
  return _g_list_alloc0 ();
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_free: 
Packit db3073
 * @list: a #GList
Packit db3073
 *
Packit db3073
 * Frees all of the memory used by a #GList.
Packit db3073
 * The freed elements are returned to the slice allocator.
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * If list elements contain dynamically-allocated memory, 
Packit db3073
 * you should either use g_list_free_full() or free them manually
Packit db3073
 * first.
Packit db3073
 * </para></note>
Packit db3073
 */
Packit db3073
void
Packit db3073
g_list_free (GList *list)
Packit db3073
{
Packit db3073
  g_slice_free_chain (GList, list, next);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_free_1:
Packit db3073
 * @list: a #GList element
Packit db3073
 *
Packit db3073
 * Frees one #GList element.
Packit db3073
 * It is usually used after g_list_remove_link().
Packit db3073
 */
Packit db3073
/**
Packit db3073
 * g_list_free1:
Packit db3073
 *
Packit db3073
 * Another name for g_list_free_1().
Packit db3073
 **/
Packit db3073
void
Packit db3073
g_list_free_1 (GList *list)
Packit db3073
{
Packit db3073
  _g_list_free1 (list);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_free_full:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @free_func: the function to be called to free each element's data
Packit db3073
 *
Packit db3073
 * Convenience method, which frees all the memory used by a #GList, and
Packit db3073
 * calls the specified destroy function on every element's data.
Packit db3073
 *
Packit db3073
 * Since: 2.28
Packit db3073
 */
Packit db3073
void
Packit db3073
g_list_free_full (GList          *list,
Packit db3073
		  GDestroyNotify  free_func)
Packit db3073
{
Packit db3073
  g_list_foreach (list, (GFunc) free_func, NULL);
Packit db3073
  g_list_free (list);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_append:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @data: the data for the new element
Packit db3073
 *
Packit db3073
 * Adds a new element on to the end of the list.
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * The return value is the new start of the list, which 
Packit db3073
 * may have changed, so make sure you store the new value.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * Note that g_list_append() has to traverse the entire list 
Packit db3073
 * to find the end, which is inefficient when adding multiple 
Packit db3073
 * elements. A common idiom to avoid the inefficiency is to prepend 
Packit db3073
 * the elements and reverse the list when all elements have been added.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * |[
Packit db3073
 * /* Notice that these are initialized to the empty list. */
Packit db3073
 * GList *list = NULL, *number_list = NULL;
Packit db3073
 *
Packit db3073
 * /* This is a list of strings. */
Packit db3073
 * list = g_list_append (list, "first");
Packit db3073
 * list = g_list_append (list, "second");
Packit db3073
 * 
Packit db3073
 * /* This is a list of integers. */
Packit db3073
 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
Packit db3073
 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_append (GList	*list,
Packit db3073
	       gpointer	 data)
Packit db3073
{
Packit db3073
  GList *new_list;
Packit db3073
  GList *last;
Packit db3073
  
Packit db3073
  new_list = _g_list_alloc ();
Packit db3073
  new_list->data = data;
Packit db3073
  new_list->next = NULL;
Packit db3073
  
Packit db3073
  if (list)
Packit db3073
    {
Packit db3073
      last = g_list_last (list);
Packit db3073
      /* g_assert (last != NULL); */
Packit db3073
      last->next = new_list;
Packit db3073
      new_list->prev = last;
Packit db3073
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      new_list->prev = NULL;
Packit db3073
      return new_list;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_prepend:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @data: the data for the new element
Packit db3073
 *
Packit db3073
 * Adds a new element on to the start of the list.
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * The return value is the new start of the list, which 
Packit db3073
 * may have changed, so make sure you store the new value.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * |[ 
Packit db3073
 * /* Notice that it is initialized to the empty list. */
Packit db3073
 * GList *list = NULL;
Packit db3073
 * list = g_list_prepend (list, "last");
Packit db3073
 * list = g_list_prepend (list, "first");
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_prepend (GList	 *list,
Packit db3073
		gpointer  data)
Packit db3073
{
Packit db3073
  GList *new_list;
Packit db3073
  
Packit db3073
  new_list = _g_list_alloc ();
Packit db3073
  new_list->data = data;
Packit db3073
  new_list->next = list;
Packit db3073
  
Packit db3073
  if (list)
Packit db3073
    {
Packit db3073
      new_list->prev = list->prev;
Packit db3073
      if (list->prev)
Packit db3073
	list->prev->next = new_list;
Packit db3073
      list->prev = new_list;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    new_list->prev = NULL;
Packit db3073
  
Packit db3073
  return new_list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_insert:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @data: the data for the new element
Packit db3073
 * @position: the position to insert the element. If this is 
Packit db3073
 *     negative, or is larger than the number of elements in the 
Packit db3073
 *     list, the new element is added on to the end of the list.
Packit db3073
 * 
Packit db3073
 * Inserts a new element into the list at the given position.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_insert (GList	*list,
Packit db3073
	       gpointer	 data,
Packit db3073
	       gint	 position)
Packit db3073
{
Packit db3073
  GList *new_list;
Packit db3073
  GList *tmp_list;
Packit db3073
Packit db3073
  if (position < 0)
Packit db3073
    return g_list_append (list, data);
Packit db3073
  else if (position == 0)
Packit db3073
    return g_list_prepend (list, data);
Packit db3073
Packit db3073
  tmp_list = g_list_nth (list, position);
Packit db3073
  if (!tmp_list)
Packit db3073
    return g_list_append (list, data);
Packit db3073
Packit db3073
  new_list = _g_list_alloc ();
Packit db3073
  new_list->data = data;
Packit db3073
  new_list->prev = tmp_list->prev;
Packit db3073
  tmp_list->prev->next = new_list;
Packit db3073
  new_list->next = tmp_list;
Packit db3073
  tmp_list->prev = new_list;
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_insert_before:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @sibling: the list element before which the new element 
Packit db3073
 *     is inserted or %NULL to insert at the end of the list
Packit db3073
 * @data: the data for the new element
Packit db3073
 *
Packit db3073
 * Inserts a new element into the list before the given position.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_insert_before (GList   *list,
Packit db3073
		      GList   *sibling,
Packit db3073
		      gpointer data)
Packit db3073
{
Packit db3073
  if (!list)
Packit db3073
    {
Packit db3073
      list = g_list_alloc ();
Packit db3073
      list->data = data;
Packit db3073
      g_return_val_if_fail (sibling == NULL, list);
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
  else if (sibling)
Packit db3073
    {
Packit db3073
      GList *node;
Packit db3073
Packit db3073
      node = _g_list_alloc ();
Packit db3073
      node->data = data;
Packit db3073
      node->prev = sibling->prev;
Packit db3073
      node->next = sibling;
Packit db3073
      sibling->prev = node;
Packit db3073
      if (node->prev)
Packit db3073
	{
Packit db3073
	  node->prev->next = node;
Packit db3073
	  return list;
Packit db3073
	}
Packit db3073
      else
Packit db3073
	{
Packit db3073
	  g_return_val_if_fail (sibling == list, node);
Packit db3073
	  return node;
Packit db3073
	}
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      GList *last;
Packit db3073
Packit db3073
      last = list;
Packit db3073
      while (last->next)
Packit db3073
	last = last->next;
Packit db3073
Packit db3073
      last->next = _g_list_alloc ();
Packit db3073
      last->next->data = data;
Packit db3073
      last->next->prev = last;
Packit db3073
      last->next->next = NULL;
Packit db3073
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_concat:
Packit db3073
 * @list1: a #GList
Packit db3073
 * @list2: the #GList to add to the end of the first #GList
Packit db3073
 *
Packit db3073
 * Adds the second #GList onto the end of the first #GList.
Packit db3073
 * Note that the elements of the second #GList are not copied.
Packit db3073
 * They are used directly.
Packit db3073
 *
Packit db3073
 * Returns: the start of the new #GList
Packit db3073
 */
Packit db3073
GList *
Packit db3073
g_list_concat (GList *list1, GList *list2)
Packit db3073
{
Packit db3073
  GList *tmp_list;
Packit db3073
  
Packit db3073
  if (list2)
Packit db3073
    {
Packit db3073
      tmp_list = g_list_last (list1);
Packit db3073
      if (tmp_list)
Packit db3073
	tmp_list->next = list2;
Packit db3073
      else
Packit db3073
	list1 = list2;
Packit db3073
      list2->prev = tmp_list;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return list1;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_remove:
Packit db3073
 * @list: a #GList
Packit db3073
 * @data: the data of the element to remove
Packit db3073
 *
Packit db3073
 * Removes an element from a #GList.
Packit db3073
 * If two elements contain the same data, only the first is removed.
Packit db3073
 * If none of the elements contain the data, the #GList is unchanged.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_remove (GList	     *list,
Packit db3073
	       gconstpointer  data)
Packit db3073
{
Packit db3073
  GList *tmp;
Packit db3073
  
Packit db3073
  tmp = list;
Packit db3073
  while (tmp)
Packit db3073
    {
Packit db3073
      if (tmp->data != data)
Packit db3073
	tmp = tmp->next;
Packit db3073
      else
Packit db3073
	{
Packit db3073
	  if (tmp->prev)
Packit db3073
	    tmp->prev->next = tmp->next;
Packit db3073
	  if (tmp->next)
Packit db3073
	    tmp->next->prev = tmp->prev;
Packit db3073
	  
Packit db3073
	  if (list == tmp)
Packit db3073
	    list = list->next;
Packit db3073
	  
Packit db3073
	  _g_list_free1 (tmp);
Packit db3073
	  
Packit db3073
	  break;
Packit db3073
	}
Packit db3073
    }
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_remove_all:
Packit db3073
 * @list: a #GList
Packit db3073
 * @data: data to remove
Packit db3073
 *
Packit db3073
 * Removes all list nodes with data equal to @data. 
Packit db3073
 * Returns the new head of the list. Contrast with 
Packit db3073
 * g_list_remove() which removes only the first node 
Packit db3073
 * matching the given data.
Packit db3073
 *
Packit db3073
 * Returns: new head of @list
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_remove_all (GList	*list,
Packit db3073
		   gconstpointer data)
Packit db3073
{
Packit db3073
  GList *tmp = list;
Packit db3073
Packit db3073
  while (tmp)
Packit db3073
    {
Packit db3073
      if (tmp->data != data)
Packit db3073
	tmp = tmp->next;
Packit db3073
      else
Packit db3073
	{
Packit db3073
	  GList *next = tmp->next;
Packit db3073
Packit db3073
	  if (tmp->prev)
Packit db3073
	    tmp->prev->next = next;
Packit db3073
	  else
Packit db3073
	    list = next;
Packit db3073
	  if (next)
Packit db3073
	    next->prev = tmp->prev;
Packit db3073
Packit db3073
	  _g_list_free1 (tmp);
Packit db3073
	  tmp = next;
Packit db3073
	}
Packit db3073
    }
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
static inline GList*
Packit db3073
_g_list_remove_link (GList *list,
Packit db3073
		     GList *link)
Packit db3073
{
Packit db3073
  if (link)
Packit db3073
    {
Packit db3073
      if (link->prev)
Packit db3073
	link->prev->next = link->next;
Packit db3073
      if (link->next)
Packit db3073
	link->next->prev = link->prev;
Packit db3073
      
Packit db3073
      if (link == list)
Packit db3073
	list = list->next;
Packit db3073
      
Packit db3073
      link->next = NULL;
Packit db3073
      link->prev = NULL;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_remove_link:
Packit db3073
 * @list: a #GList
Packit db3073
 * @llink: an element in the #GList
Packit db3073
 *
Packit db3073
 * Removes an element from a #GList, without freeing the element.
Packit db3073
 * The removed element's prev and next links are set to %NULL, so 
Packit db3073
 * that it becomes a self-contained list with one element.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList, without the element
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_remove_link (GList *list,
Packit db3073
		    GList *llink)
Packit db3073
{
Packit db3073
  return _g_list_remove_link (list, llink);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_delete_link:
Packit db3073
 * @list: a #GList
Packit db3073
 * @link_: node to delete from @list
Packit db3073
 *
Packit db3073
 * Removes the node link_ from the list and frees it. 
Packit db3073
 * Compare this to g_list_remove_link() which removes the node 
Packit db3073
 * without freeing it.
Packit db3073
 *
Packit db3073
 * Returns: the new head of @list
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_delete_link (GList *list,
Packit db3073
		    GList *link_)
Packit db3073
{
Packit db3073
  list = _g_list_remove_link (list, link_);
Packit db3073
  _g_list_free1 (link_);
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_copy:
Packit db3073
 * @list: a #GList
Packit db3073
 *
Packit db3073
 * Copies a #GList.
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * Note that this is a "shallow" copy. If the list elements 
Packit db3073
 * consist of pointers to data, the pointers are copied but 
Packit db3073
 * the actual data is not.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * Returns: a copy of @list
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_copy (GList *list)
Packit db3073
{
Packit db3073
  GList *new_list = NULL;
Packit db3073
Packit db3073
  if (list)
Packit db3073
    {
Packit db3073
      GList *last;
Packit db3073
Packit db3073
      new_list = _g_list_alloc ();
Packit db3073
      new_list->data = list->data;
Packit db3073
      new_list->prev = NULL;
Packit db3073
      last = new_list;
Packit db3073
      list = list->next;
Packit db3073
      while (list)
Packit db3073
	{
Packit db3073
	  last->next = _g_list_alloc ();
Packit db3073
	  last->next->prev = last;
Packit db3073
	  last = last->next;
Packit db3073
	  last->data = list->data;
Packit db3073
	  list = list->next;
Packit db3073
	}
Packit db3073
      last->next = NULL;
Packit db3073
    }
Packit db3073
Packit db3073
  return new_list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_reverse:
Packit db3073
 * @list: a #GList
Packit db3073
 *
Packit db3073
 * Reverses a #GList.
Packit db3073
 * It simply switches the next and prev pointers of each element.
Packit db3073
 *
Packit db3073
 * Returns: the start of the reversed #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_reverse (GList *list)
Packit db3073
{
Packit db3073
  GList *last;
Packit db3073
  
Packit db3073
  last = NULL;
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      last = list;
Packit db3073
      list = last->next;
Packit db3073
      last->next = last->prev;
Packit db3073
      last->prev = list;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return last;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_nth:
Packit db3073
 * @list: a #GList
Packit db3073
 * @n: the position of the element, counting from 0
Packit db3073
 *
Packit db3073
 * Gets the element at the given position in a #GList.
Packit db3073
 *
Packit db3073
 * Returns: the element, or %NULL if the position is off 
Packit db3073
 *     the end of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_nth (GList *list,
Packit db3073
	    guint  n)
Packit db3073
{
Packit db3073
  while ((n-- > 0) && list)
Packit db3073
    list = list->next;
Packit db3073
  
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_nth_prev:
Packit db3073
 * @list: a #GList
Packit db3073
 * @n: the position of the element, counting from 0
Packit db3073
 *
Packit db3073
 * Gets the element @n places before @list.
Packit db3073
 *
Packit db3073
 * Returns: the element, or %NULL if the position is 
Packit db3073
 *     off the end of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_nth_prev (GList *list,
Packit db3073
		 guint  n)
Packit db3073
{
Packit db3073
  while ((n-- > 0) && list)
Packit db3073
    list = list->prev;
Packit db3073
  
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_nth_data:
Packit db3073
 * @list: a #GList
Packit db3073
 * @n: the position of the element
Packit db3073
 *
Packit db3073
 * Gets the data of the element at the given position.
Packit db3073
 *
Packit db3073
 * Returns: the element's data, or %NULL if the position 
Packit db3073
 *     is off the end of the #GList
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_list_nth_data (GList     *list,
Packit db3073
		 guint      n)
Packit db3073
{
Packit db3073
  while ((n-- > 0) && list)
Packit db3073
    list = list->next;
Packit db3073
  
Packit db3073
  return list ? list->data : NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_find:
Packit db3073
 * @list: a #GList
Packit db3073
 * @data: the element data to find
Packit db3073
 *
Packit db3073
 * Finds the element in a #GList which 
Packit db3073
 * contains the given data.
Packit db3073
 *
Packit db3073
 * Returns: the found #GList element, 
Packit db3073
 *     or %NULL if it is not found
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_find (GList         *list,
Packit db3073
	     gconstpointer  data)
Packit db3073
{
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      if (list->data == data)
Packit db3073
	break;
Packit db3073
      list = list->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_find_custom:
Packit db3073
 * @list: a #GList
Packit db3073
 * @data: user data passed to the function
Packit db3073
 * @func: the function to call for each element. 
Packit db3073
 *     It should return 0 when the desired element is found
Packit db3073
 *
Packit db3073
 * Finds an element in a #GList, using a supplied function to 
Packit db3073
 * find the desired element. It iterates over the list, calling 
Packit db3073
 * the given function which should return 0 when the desired 
Packit db3073
 * element is found. The function takes two #gconstpointer arguments, 
Packit db3073
 * the #GList element's data as the first argument and the 
Packit db3073
 * given user data.
Packit db3073
 *
Packit db3073
 * Returns: the found #GList element, or %NULL if it is not found
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_find_custom (GList         *list,
Packit db3073
		    gconstpointer  data,
Packit db3073
		    GCompareFunc   func)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (func != NULL, list);
Packit db3073
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      if (! func (list->data, data))
Packit db3073
	return list;
Packit db3073
      list = list->next;
Packit db3073
    }
Packit db3073
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_position:
Packit db3073
 * @list: a #GList
Packit db3073
 * @llink: an element in the #GList
Packit db3073
 *
Packit db3073
 * Gets the position of the given element 
Packit db3073
 * in the #GList (starting from 0).
Packit db3073
 *
Packit db3073
 * Returns: the position of the element in the #GList, 
Packit db3073
 *     or -1 if the element is not found
Packit db3073
 */
Packit db3073
gint
Packit db3073
g_list_position (GList *list,
Packit db3073
		 GList *llink)
Packit db3073
{
Packit db3073
  gint i;
Packit db3073
Packit db3073
  i = 0;
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      if (list == llink)
Packit db3073
	return i;
Packit db3073
      i++;
Packit db3073
      list = list->next;
Packit db3073
    }
Packit db3073
Packit db3073
  return -1;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_index:
Packit db3073
 * @list: a #GList
Packit db3073
 * @data: the data to find
Packit db3073
 *
Packit db3073
 * Gets the position of the element containing 
Packit db3073
 * the given data (starting from 0).
Packit db3073
 *
Packit db3073
 * Returns: the index of the element containing the data, 
Packit db3073
 *     or -1 if the data is not found
Packit db3073
 */
Packit db3073
gint
Packit db3073
g_list_index (GList         *list,
Packit db3073
	      gconstpointer  data)
Packit db3073
{
Packit db3073
  gint i;
Packit db3073
Packit db3073
  i = 0;
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      if (list->data == data)
Packit db3073
	return i;
Packit db3073
      i++;
Packit db3073
      list = list->next;
Packit db3073
    }
Packit db3073
Packit db3073
  return -1;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_last:
Packit db3073
 * @list: a #GList
Packit db3073
 *
Packit db3073
 * Gets the last element in a #GList.
Packit db3073
 *
Packit db3073
 * Returns: the last element in the #GList, 
Packit db3073
 *     or %NULL if the #GList has no elements
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_last (GList *list)
Packit db3073
{
Packit db3073
  if (list)
Packit db3073
    {
Packit db3073
      while (list->next)
Packit db3073
	list = list->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_first:
Packit db3073
 * @list: a #GList
Packit db3073
 *
Packit db3073
 * Gets the first element in a #GList.
Packit db3073
 *
Packit db3073
 * Returns: the first element in the #GList, 
Packit db3073
 *     or %NULL if the #GList has no elements
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_first (GList *list)
Packit db3073
{
Packit db3073
  if (list)
Packit db3073
    {
Packit db3073
      while (list->prev)
Packit db3073
	list = list->prev;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_length:
Packit db3073
 * @list: a #GList
Packit db3073
 *
Packit db3073
 * Gets the number of elements in a #GList.
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * This function iterates over the whole list to 
Packit db3073
 * count its elements.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * Returns: the number of elements in the #GList
Packit db3073
 */
Packit db3073
guint
Packit db3073
g_list_length (GList *list)
Packit db3073
{
Packit db3073
  guint length;
Packit db3073
  
Packit db3073
  length = 0;
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      length++;
Packit db3073
      list = list->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return length;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_foreach:
Packit db3073
 * @list: a #GList
Packit db3073
 * @func: the function to call with each element's data
Packit db3073
 * @user_data: user data to pass to the function
Packit db3073
 *
Packit db3073
 * Calls a function for each element of a #GList.
Packit db3073
 */
Packit db3073
/**
Packit db3073
 * GFunc:
Packit db3073
 * @data: the element's data.
Packit db3073
 * @user_data: user data passed to g_list_foreach() or
Packit db3073
 *             g_slist_foreach().
Packit db3073
 *
Packit db3073
 * Specifies the type of functions passed to g_list_foreach() and
Packit db3073
 * g_slist_foreach().
Packit db3073
 **/
Packit db3073
void
Packit db3073
g_list_foreach (GList	 *list,
Packit db3073
		GFunc	  func,
Packit db3073
		gpointer  user_data)
Packit db3073
{
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      GList *next = list->next;
Packit db3073
      (*func) (list->data, user_data);
Packit db3073
      list = next;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
static GList*
Packit db3073
g_list_insert_sorted_real (GList    *list,
Packit db3073
			   gpointer  data,
Packit db3073
			   GFunc     func,
Packit db3073
			   gpointer  user_data)
Packit db3073
{
Packit db3073
  GList *tmp_list = list;
Packit db3073
  GList *new_list;
Packit db3073
  gint cmp;
Packit db3073
Packit db3073
  g_return_val_if_fail (func != NULL, list);
Packit db3073
  
Packit db3073
  if (!list) 
Packit db3073
    {
Packit db3073
      new_list = _g_list_alloc0 ();
Packit db3073
      new_list->data = data;
Packit db3073
      return new_list;
Packit db3073
    }
Packit db3073
  
Packit db3073
  cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
Packit db3073
Packit db3073
  while ((tmp_list->next) && (cmp > 0))
Packit db3073
    {
Packit db3073
      tmp_list = tmp_list->next;
Packit db3073
Packit db3073
      cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
Packit db3073
    }
Packit db3073
Packit db3073
  new_list = _g_list_alloc0 ();
Packit db3073
  new_list->data = data;
Packit db3073
Packit db3073
  if ((!tmp_list->next) && (cmp > 0))
Packit db3073
    {
Packit db3073
      tmp_list->next = new_list;
Packit db3073
      new_list->prev = tmp_list;
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
   
Packit db3073
  if (tmp_list->prev)
Packit db3073
    {
Packit db3073
      tmp_list->prev->next = new_list;
Packit db3073
      new_list->prev = tmp_list->prev;
Packit db3073
    }
Packit db3073
  new_list->next = tmp_list;
Packit db3073
  tmp_list->prev = new_list;
Packit db3073
 
Packit db3073
  if (tmp_list == list)
Packit db3073
    return new_list;
Packit db3073
  else
Packit db3073
    return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_insert_sorted:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @data: the data for the new element
Packit db3073
 * @func: the function to compare elements in the list. It should 
Packit db3073
 *     return a number > 0 if the first parameter comes after the 
Packit db3073
 *     second parameter in the sort order.
Packit db3073
 *
Packit db3073
 * Inserts a new element into the list, using the given comparison 
Packit db3073
 * function to determine its position.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_insert_sorted (GList        *list,
Packit db3073
		      gpointer      data,
Packit db3073
		      GCompareFunc  func)
Packit db3073
{
Packit db3073
  return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_insert_sorted_with_data:
Packit db3073
 * @list: a pointer to a #GList
Packit db3073
 * @data: the data for the new element
Packit db3073
 * @func: the function to compare elements in the list. 
Packit db3073
 *     It should return a number > 0 if the first parameter 
Packit db3073
 *     comes after the second parameter in the sort order.
Packit db3073
 * @user_data: user data to pass to comparison function.
Packit db3073
 *
Packit db3073
 * Inserts a new element into the list, using the given comparison 
Packit db3073
 * function to determine its position.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GList
Packit db3073
 *
Packit db3073
 * Since: 2.10
Packit db3073
 */
Packit db3073
GList*
Packit db3073
g_list_insert_sorted_with_data (GList            *list,
Packit db3073
				gpointer          data,
Packit db3073
				GCompareDataFunc  func,
Packit db3073
				gpointer          user_data)
Packit db3073
{
Packit db3073
  return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
Packit db3073
}
Packit db3073
Packit db3073
static GList *
Packit db3073
g_list_sort_merge (GList     *l1, 
Packit db3073
		   GList     *l2,
Packit db3073
		   GFunc     compare_func,
Packit db3073
		   gpointer  user_data)
Packit db3073
{
Packit db3073
  GList list, *l, *lprev;
Packit db3073
  gint cmp;
Packit db3073
Packit db3073
  l = &list; 
Packit db3073
  lprev = NULL;
Packit db3073
Packit db3073
  while (l1 && l2)
Packit db3073
    {
Packit db3073
      cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
Packit db3073
Packit db3073
      if (cmp <= 0)
Packit db3073
        {
Packit db3073
	  l->next = l1;
Packit db3073
	  l1 = l1->next;
Packit db3073
        } 
Packit db3073
      else 
Packit db3073
	{
Packit db3073
	  l->next = l2;
Packit db3073
	  l2 = l2->next;
Packit db3073
        }
Packit db3073
      l = l->next;
Packit db3073
      l->prev = lprev; 
Packit db3073
      lprev = l;
Packit db3073
    }
Packit db3073
  l->next = l1 ? l1 : l2;
Packit db3073
  l->next->prev = l;
Packit db3073
Packit db3073
  return list.next;
Packit db3073
}
Packit db3073
Packit db3073
static GList* 
Packit db3073
g_list_sort_real (GList    *list,
Packit db3073
		  GFunc     compare_func,
Packit db3073
		  gpointer  user_data)
Packit db3073
{
Packit db3073
  GList *l1, *l2;
Packit db3073
  
Packit db3073
  if (!list) 
Packit db3073
    return NULL;
Packit db3073
  if (!list->next) 
Packit db3073
    return list;
Packit db3073
  
Packit db3073
  l1 = list; 
Packit db3073
  l2 = list->next;
Packit db3073
Packit db3073
  while ((l2 = l2->next) != NULL)
Packit db3073
    {
Packit db3073
      if ((l2 = l2->next) == NULL) 
Packit db3073
	break;
Packit db3073
      l1 = l1->next;
Packit db3073
    }
Packit db3073
  l2 = l1->next; 
Packit db3073
  l1->next = NULL; 
Packit db3073
Packit db3073
  return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
Packit db3073
			    g_list_sort_real (l2, compare_func, user_data),
Packit db3073
			    compare_func,
Packit db3073
			    user_data);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_sort:
Packit db3073
 * @list: a #GList
Packit db3073
 * @compare_func: the comparison function used to sort the #GList.
Packit db3073
 *     This function is passed the data from 2 elements of the #GList 
Packit db3073
 *     and should return 0 if they are equal, a negative value if the 
Packit db3073
 *     first element comes before the second, or a positive value if 
Packit db3073
 *     the first element comes after the second.
Packit db3073
 *
Packit db3073
 * Sorts a #GList using the given comparison function. The algorithm 
Packit db3073
 * used is a stable sort.
Packit db3073
 *
Packit db3073
 * Returns: the start of the sorted #GList
Packit db3073
 */
Packit db3073
/**
Packit db3073
 * GCompareFunc:
Packit db3073
 * @a: a value.
Packit db3073
 * @b: a value to compare with.
Packit db3073
 * @Returns: negative value if @a < @b; zero if @a = @b; positive
Packit db3073
 *           value if @a > @b.
Packit db3073
 *
Packit db3073
 * Specifies the type of a comparison function used to compare two
Packit db3073
 * values.  The function should return a negative integer if the first
Packit db3073
 * value comes before the second, 0 if they are equal, or a positive
Packit db3073
 * integer if the first value comes after the second.
Packit db3073
 **/
Packit db3073
GList *
Packit db3073
g_list_sort (GList        *list,
Packit db3073
	     GCompareFunc  compare_func)
Packit db3073
{
Packit db3073
  return g_list_sort_real (list, (GFunc) compare_func, NULL);
Packit db3073
			    
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_list_sort_with_data:
Packit db3073
 * @list: a #GList
Packit db3073
 * @compare_func: comparison function
Packit db3073
 * @user_data: user data to pass to comparison function
Packit db3073
 *
Packit db3073
 * Like g_list_sort(), but the comparison function accepts 
Packit db3073
 * a user data argument.
Packit db3073
 *
Packit db3073
 * Returns: the new head of @list
Packit db3073
 */
Packit db3073
/**
Packit db3073
 * GCompareDataFunc:
Packit db3073
 * @a: a value.
Packit db3073
 * @b: a value to compare with.
Packit db3073
 * @user_data: user data to pass to comparison function.
Packit db3073
 * @Returns: negative value if @a < @b; zero if @a = @b; positive
Packit db3073
 *           value if @a > @b.
Packit db3073
 *
Packit db3073
 * Specifies the type of a comparison function used to compare two
Packit db3073
 * values.  The function should return a negative integer if the first
Packit db3073
 * value comes before the second, 0 if they are equal, or a positive
Packit db3073
 * integer if the first value comes after the second.
Packit db3073
 **/
Packit db3073
GList *
Packit db3073
g_list_sort_with_data (GList            *list,
Packit db3073
		       GCompareDataFunc  compare_func,
Packit db3073
		       gpointer          user_data)
Packit db3073
{
Packit db3073
  return g_list_sort_real (list, (GFunc) compare_func, user_data);
Packit db3073
}