Blame glib/glib/gslist.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 "gslist.h"
Packit db3073
Packit db3073
#include "gtestutils.h"
Packit db3073
#include "gslice.h"
Packit db3073
Packit db3073
/**
Packit db3073
 * SECTION:linked_lists_single
Packit db3073
 * @title: Singly-Linked Lists
Packit db3073
 * @short_description: linked lists that can be iterated in one direction
Packit db3073
 *
Packit db3073
 * The #GSList structure and its associated functions provide a
Packit db3073
 * standard singly-linked list data structure.
Packit db3073
 *
Packit db3073
 * Each element in the list contains a piece of data, together with a
Packit db3073
 * pointer which links to the next element in the list. Using this
Packit db3073
 * pointer it is possible to move through the list in one direction
Packit db3073
 * only (unlike the 
Packit db3073
 * linkend="glib-Doubly-Linked-Lists">Doubly-Linked Lists</link> which
Packit db3073
 * allow movement in both directions).
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 #GSList functions expect to be passed a
Packit db3073
 * pointer 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 #GSList. %NULL is considered to be
Packit db3073
 * the empty list so you simply set a #GSList* to %NULL.
Packit db3073
 *
Packit db3073
 * To add elements, use g_slist_append(), g_slist_prepend(),
Packit db3073
 * g_slist_insert() and g_slist_insert_sorted().
Packit db3073
 *
Packit db3073
 * To remove elements, use g_slist_remove().
Packit db3073
 *
Packit db3073
 * To find elements in the list use g_slist_last(), g_slist_next(),
Packit db3073
 * g_slist_nth(), g_slist_nth_data(), g_slist_find() and
Packit db3073
 * g_slist_find_custom().
Packit db3073
 *
Packit db3073
 * To find the index of an element use g_slist_position() and
Packit db3073
 * g_slist_index().
Packit db3073
 *
Packit db3073
 * To call a function for each element in the list use
Packit db3073
 * g_slist_foreach().
Packit db3073
 *
Packit db3073
 * To free the entire list, use g_slist_free().
Packit db3073
 **/
Packit db3073
Packit db3073
/**
Packit db3073
 * GSList:
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
 *
Packit db3073
 * The #GSList struct is used for each element in the singly-linked
Packit db3073
 * list.
Packit db3073
 **/
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_next:
Packit db3073
 * @slist: an element in a #GSList.
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 #GSList.
Packit db3073
 **/
Packit db3073
Packit db3073
#define _g_slist_alloc0()       g_slice_new0 (GSList)
Packit db3073
#define _g_slist_alloc()        g_slice_new (GSList)
Packit db3073
#define _g_slist_free1(slist)   g_slice_free (GSList, slist)
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_alloc:
Packit db3073
 * @Returns: a pointer to the newly-allocated #GSList element.
Packit db3073
 *
Packit db3073
 * Allocates space for one #GSList element. It is called by the
Packit db3073
 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
Packit db3073
 * g_slist_insert_sorted() functions and so is rarely used on its own.
Packit db3073
 **/
Packit db3073
GSList*
Packit db3073
g_slist_alloc (void)
Packit db3073
{
Packit db3073
  return _g_slist_alloc0 ();
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_free:
Packit db3073
 * @list: a #GSList
Packit db3073
 *
Packit db3073
 * Frees all of the memory used by a #GSList.
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_slist_free_full() or free them manually
Packit db3073
 * first.
Packit db3073
 * </para></note>
Packit db3073
 */
Packit db3073
void
Packit db3073
g_slist_free (GSList *list)
Packit db3073
{
Packit db3073
  g_slice_free_chain (GSList, list, next);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_free_1:
Packit db3073
 * @list: a #GSList element
Packit db3073
 *
Packit db3073
 * Frees one #GSList element.
Packit db3073
 * It is usually used after g_slist_remove_link().
Packit db3073
 */
Packit db3073
/**
Packit db3073
 * g_slist_free1:
Packit db3073
 *
Packit db3073
 * A macro which does the same as g_slist_free_1().
Packit db3073
 *
Packit db3073
 * Since: 2.10
Packit db3073
 **/
Packit db3073
void
Packit db3073
g_slist_free_1 (GSList *list)
Packit db3073
{
Packit db3073
  _g_slist_free1 (list);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_free_full:
Packit db3073
 * @list: a pointer to a #GSList
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 #GSList, 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_slist_free_full (GSList         *list,
Packit db3073
		   GDestroyNotify  free_func)
Packit db3073
{
Packit db3073
  g_slist_foreach (list, (GFunc) free_func, NULL);
Packit db3073
  g_slist_free (list);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_append:
Packit db3073
 * @list: a #GSList
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 may
Packit db3073
 * 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_slist_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
 * GSList *list = NULL, *number_list = NULL;
Packit db3073
 *
Packit db3073
 * /* This is a list of strings. */
Packit db3073
 * list = g_slist_append (list, "first");
Packit db3073
 * list = g_slist_append (list, "second");
Packit db3073
 *
Packit db3073
 * /* This is a list of integers. */
Packit db3073
 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
Packit db3073
 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_append (GSList   *list,
Packit db3073
                gpointer  data)
Packit db3073
{
Packit db3073
  GSList *new_list;
Packit db3073
  GSList *last;
Packit db3073
Packit db3073
  new_list = _g_slist_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_slist_last (list);
Packit db3073
      /* g_assert (last != NULL); */
Packit db3073
      last->next = new_list;
Packit db3073
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    return new_list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_prepend:
Packit db3073
 * @list: a #GSList
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
 * GSList *list = NULL;
Packit db3073
 * list = g_slist_prepend (list, "last");
Packit db3073
 * list = g_slist_prepend (list, "first");
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_prepend (GSList   *list,
Packit db3073
                 gpointer  data)
Packit db3073
{
Packit db3073
  GSList *new_list;
Packit db3073
Packit db3073
  new_list = _g_slist_alloc ();
Packit db3073
  new_list->data = data;
Packit db3073
  new_list->next = list;
Packit db3073
Packit db3073
  return new_list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_insert:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @data: the data for the new element
Packit db3073
 * @position: the position to insert the element.
Packit db3073
 *     If this is negative, or is larger than the number
Packit db3073
 *     of elements in the list, the new element is added on
Packit db3073
 *     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 #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_insert (GSList   *list,
Packit db3073
                gpointer  data,
Packit db3073
                gint      position)
Packit db3073
{
Packit db3073
  GSList *prev_list;
Packit db3073
  GSList *tmp_list;
Packit db3073
  GSList *new_list;
Packit db3073
Packit db3073
  if (position < 0)
Packit db3073
    return g_slist_append (list, data);
Packit db3073
  else if (position == 0)
Packit db3073
    return g_slist_prepend (list, data);
Packit db3073
Packit db3073
  new_list = _g_slist_alloc ();
Packit db3073
  new_list->data = data;
Packit db3073
Packit db3073
  if (!list)
Packit db3073
    {
Packit db3073
      new_list->next = NULL;
Packit db3073
      return new_list;
Packit db3073
    }
Packit db3073
Packit db3073
  prev_list = NULL;
Packit db3073
  tmp_list = list;
Packit db3073
Packit db3073
  while ((position-- > 0) && tmp_list)
Packit db3073
    {
Packit db3073
      prev_list = tmp_list;
Packit db3073
      tmp_list = tmp_list->next;
Packit db3073
    }
Packit db3073
Packit db3073
  new_list->next = prev_list->next;
Packit db3073
  prev_list->next = new_list;
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_insert_before:
Packit db3073
 * @slist: a #GSList
Packit db3073
 * @sibling: node to insert @data before
Packit db3073
 * @data: data to put in the newly-inserted node
Packit db3073
 *
Packit db3073
 * Inserts a node before @sibling containing @data.
Packit db3073
 *
Packit db3073
 * Returns: the new head of the list.
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_insert_before (GSList  *slist,
Packit db3073
                       GSList  *sibling,
Packit db3073
                       gpointer data)
Packit db3073
{
Packit db3073
  if (!slist)
Packit db3073
    {
Packit db3073
      slist = _g_slist_alloc ();
Packit db3073
      slist->data = data;
Packit db3073
      slist->next = NULL;
Packit db3073
      g_return_val_if_fail (sibling == NULL, slist);
Packit db3073
      return slist;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      GSList *node, *last = NULL;
Packit db3073
Packit db3073
      for (node = slist; node; last = node, node = last->next)
Packit db3073
        if (node == sibling)
Packit db3073
          break;
Packit db3073
      if (!last)
Packit db3073
        {
Packit db3073
          node = _g_slist_alloc ();
Packit db3073
          node->data = data;
Packit db3073
          node->next = slist;
Packit db3073
Packit db3073
          return node;
Packit db3073
        }
Packit db3073
      else
Packit db3073
        {
Packit db3073
          node = _g_slist_alloc ();
Packit db3073
          node->data = data;
Packit db3073
          node->next = last->next;
Packit db3073
          last->next = node;
Packit db3073
Packit db3073
          return slist;
Packit db3073
        }
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_concat:
Packit db3073
 * @list1: a #GSList
Packit db3073
 * @list2: the #GSList to add to the end of the first #GSList
Packit db3073
 *
Packit db3073
 * Adds the second #GSList onto the end of the first #GSList.
Packit db3073
 * Note that the elements of the second #GSList are not copied.
Packit db3073
 * They are used directly.
Packit db3073
 *
Packit db3073
 * Returns: the start of the new #GSList
Packit db3073
 */
Packit db3073
GSList *
Packit db3073
g_slist_concat (GSList *list1, GSList *list2)
Packit db3073
{
Packit db3073
  if (list2)
Packit db3073
    {
Packit db3073
      if (list1)
Packit db3073
        g_slist_last (list1)->next = list2;
Packit db3073
      else
Packit db3073
        list1 = list2;
Packit db3073
    }
Packit db3073
Packit db3073
  return list1;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_remove:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @data: the data of the element to remove
Packit db3073
 *
Packit db3073
 * Removes an element from a #GSList.
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 #GSList is unchanged.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_remove (GSList        *list,
Packit db3073
                gconstpointer  data)
Packit db3073
{
Packit db3073
  GSList *tmp, *prev = NULL;
Packit db3073
Packit db3073
  tmp = list;
Packit db3073
  while (tmp)
Packit db3073
    {
Packit db3073
      if (tmp->data == data)
Packit db3073
        {
Packit db3073
          if (prev)
Packit db3073
            prev->next = tmp->next;
Packit db3073
          else
Packit db3073
            list = tmp->next;
Packit db3073
Packit db3073
          g_slist_free_1 (tmp);
Packit db3073
          break;
Packit db3073
        }
Packit db3073
      prev = tmp;
Packit db3073
      tmp = prev->next;
Packit db3073
    }
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_remove_all:
Packit db3073
 * @list: a #GSList
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_slist_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
GSList*
Packit db3073
g_slist_remove_all (GSList        *list,
Packit db3073
                    gconstpointer  data)
Packit db3073
{
Packit db3073
  GSList *tmp, *prev = NULL;
Packit db3073
Packit db3073
  tmp = list;
Packit db3073
  while (tmp)
Packit db3073
    {
Packit db3073
      if (tmp->data == data)
Packit db3073
        {
Packit db3073
          GSList *next = tmp->next;
Packit db3073
Packit db3073
          if (prev)
Packit db3073
            prev->next = next;
Packit db3073
          else
Packit db3073
            list = next;
Packit db3073
Packit db3073
          g_slist_free_1 (tmp);
Packit db3073
          tmp = next;
Packit db3073
        }
Packit db3073
      else
Packit db3073
        {
Packit db3073
          prev = tmp;
Packit db3073
          tmp = prev->next;
Packit db3073
        }
Packit db3073
    }
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
static inline GSList*
Packit db3073
_g_slist_remove_link (GSList *list,
Packit db3073
                      GSList *link)
Packit db3073
{
Packit db3073
  GSList *tmp;
Packit db3073
  GSList *prev;
Packit db3073
Packit db3073
  prev = NULL;
Packit db3073
  tmp = list;
Packit db3073
Packit db3073
  while (tmp)
Packit db3073
    {
Packit db3073
      if (tmp == link)
Packit db3073
        {
Packit db3073
          if (prev)
Packit db3073
            prev->next = tmp->next;
Packit db3073
          if (list == tmp)
Packit db3073
            list = list->next;
Packit db3073
Packit db3073
          tmp->next = NULL;
Packit db3073
          break;
Packit db3073
        }
Packit db3073
Packit db3073
      prev = tmp;
Packit db3073
      tmp = tmp->next;
Packit db3073
    }
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_remove_link:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @link_: an element in the #GSList
Packit db3073
 *
Packit db3073
 * Removes an element from a #GSList, without
Packit db3073
 * freeing the element. The removed element's next
Packit db3073
 * link is set to %NULL, so that it becomes a
Packit db3073
 * self-contained list with one element.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GSList, without the element
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_remove_link (GSList *list,
Packit db3073
                     GSList *link_)
Packit db3073
{
Packit db3073
  return _g_slist_remove_link (list, link_);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_delete_link:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @link_: node to delete
Packit db3073
 *
Packit db3073
 * Removes the node link_ from the list and frees it.
Packit db3073
 * Compare this to g_slist_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
GSList*
Packit db3073
g_slist_delete_link (GSList *list,
Packit db3073
                     GSList *link_)
Packit db3073
{
Packit db3073
  list = _g_slist_remove_link (list, link_);
Packit db3073
  _g_slist_free1 (link_);
Packit db3073
Packit db3073
  return list;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_copy:
Packit db3073
 * @list: a #GSList
Packit db3073
 *
Packit db3073
 * Copies a #GSList.
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 isn't.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * Returns: a copy of @list
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_copy (GSList *list)
Packit db3073
{
Packit db3073
  GSList *new_list = NULL;
Packit db3073
Packit db3073
  if (list)
Packit db3073
    {
Packit db3073
      GSList *last;
Packit db3073
Packit db3073
      new_list = _g_slist_alloc ();
Packit db3073
      new_list->data = list->data;
Packit db3073
      last = new_list;
Packit db3073
      list = list->next;
Packit db3073
      while (list)
Packit db3073
        {
Packit db3073
          last->next = _g_slist_alloc ();
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_slist_reverse:
Packit db3073
 * @list: a #GSList
Packit db3073
 *
Packit db3073
 * Reverses a #GSList.
Packit db3073
 *
Packit db3073
 * Returns: the start of the reversed #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_reverse (GSList *list)
Packit db3073
{
Packit db3073
  GSList *prev = NULL;
Packit db3073
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      GSList *next = list->next;
Packit db3073
Packit db3073
      list->next = prev;
Packit db3073
Packit db3073
      prev = list;
Packit db3073
      list = next;
Packit db3073
    }
Packit db3073
Packit db3073
  return prev;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_nth:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @n: the position of the element, counting from 0
Packit db3073
 *
Packit db3073
 * Gets the element at the given position in a #GSList.
Packit db3073
 *
Packit db3073
 * Returns: the element, or %NULL if the position is off
Packit db3073
 *     the end of the #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_nth (GSList *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_slist_nth_data:
Packit db3073
 * @list: a #GSList
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 #GSList
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_slist_nth_data (GSList   *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_slist_find:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @data: the element data to find
Packit db3073
 *
Packit db3073
 * Finds the element in a #GSList which
Packit db3073
 * contains the given data.
Packit db3073
 *
Packit db3073
 * Returns: the found #GSList element,
Packit db3073
 *     or %NULL if it is not found
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_find (GSList        *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
/**
Packit db3073
 * g_slist_find_custom:
Packit db3073
 * @list: a #GSList
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 #GSList, 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 #GSList element's data as the first argument and the
Packit db3073
 * given user data.
Packit db3073
 *
Packit db3073
 * Returns: the found #GSList element, or %NULL if it is not found
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_find_custom (GSList        *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
 * g_slist_position:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @llink: an element in the #GSList
Packit db3073
 *
Packit db3073
 * Gets the position of the given element
Packit db3073
 * in the #GSList (starting from 0).
Packit db3073
 *
Packit db3073
 * Returns: the position of the element in the #GSList,
Packit db3073
 *     or -1 if the element is not found
Packit db3073
 */
Packit db3073
gint
Packit db3073
g_slist_position (GSList *list,
Packit db3073
                  GSList *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_slist_index:
Packit db3073
 * @list: a #GSList
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_slist_index (GSList        *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_slist_last:
Packit db3073
 * @list: a #GSList
Packit db3073
 *
Packit db3073
 * Gets the last element in a #GSList.
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 * This function iterates over the whole list.
Packit db3073
 * </para></note>
Packit db3073
 *
Packit db3073
 * Returns: the last element in the #GSList,
Packit db3073
 *     or %NULL if the #GSList has no elements
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_last (GSList *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_slist_length:
Packit db3073
 * @list: a #GSList
Packit db3073
 *
Packit db3073
 * Gets the number of elements in a #GSList.
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 #GSList
Packit db3073
 */
Packit db3073
guint
Packit db3073
g_slist_length (GSList *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_slist_foreach:
Packit db3073
 * @list: a #GSList
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 #GSList.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_slist_foreach (GSList   *list,
Packit db3073
                 GFunc     func,
Packit db3073
                 gpointer  user_data)
Packit db3073
{
Packit db3073
  while (list)
Packit db3073
    {
Packit db3073
      GSList *next = list->next;
Packit db3073
      (*func) (list->data, user_data);
Packit db3073
      list = next;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
static GSList*
Packit db3073
g_slist_insert_sorted_real (GSList   *list,
Packit db3073
                            gpointer  data,
Packit db3073
                            GFunc     func,
Packit db3073
                            gpointer  user_data)
Packit db3073
{
Packit db3073
  GSList *tmp_list = list;
Packit db3073
  GSList *prev_list = NULL;
Packit db3073
  GSList *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_slist_alloc ();
Packit db3073
      new_list->data = data;
Packit db3073
      new_list->next = NULL;
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
      prev_list = tmp_list;
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_slist_alloc ();
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->next = NULL;
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
Packit db3073
  if (prev_list)
Packit db3073
    {
Packit db3073
      prev_list->next = new_list;
Packit db3073
      new_list->next = tmp_list;
Packit db3073
      return list;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      new_list->next = list;
Packit db3073
      return new_list;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_insert_sorted:
Packit db3073
 * @list: a #GSList
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
 *
Packit db3073
 * Inserts a new element into the list, using the given
Packit db3073
 * comparison function to determine its position.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GSList
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_insert_sorted (GSList       *list,
Packit db3073
                       gpointer      data,
Packit db3073
                       GCompareFunc  func)
Packit db3073
{
Packit db3073
  return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_insert_sorted_with_data:
Packit db3073
 * @list: a #GSList
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: data to pass to comparison function
Packit db3073
 *
Packit db3073
 * Inserts a new element into the list, using the given
Packit db3073
 * comparison function to determine its position.
Packit db3073
 *
Packit db3073
 * Returns: the new start of the #GSList
Packit db3073
 *
Packit db3073
 * Since: 2.10
Packit db3073
 */
Packit db3073
GSList*
Packit db3073
g_slist_insert_sorted_with_data (GSList           *list,
Packit db3073
                                 gpointer          data,
Packit db3073
                                 GCompareDataFunc  func,
Packit db3073
                                 gpointer          user_data)
Packit db3073
{
Packit db3073
  return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
Packit db3073
}
Packit db3073
Packit db3073
static GSList *
Packit db3073
g_slist_sort_merge (GSList   *l1,
Packit db3073
                    GSList   *l2,
Packit db3073
                    GFunc     compare_func,
Packit db3073
                    gpointer  user_data)
Packit db3073
{
Packit db3073
  GSList list, *l;
Packit db3073
  gint cmp;
Packit db3073
Packit db3073
  l=&list;
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=l->next=l1;
Packit db3073
          l1=l1->next;
Packit db3073
        }
Packit db3073
      else
Packit db3073
        {
Packit db3073
          l=l->next=l2;
Packit db3073
          l2=l2->next;
Packit db3073
        }
Packit db3073
    }
Packit db3073
  l->next= l1 ? l1 : l2;
Packit db3073
Packit db3073
  return list.next;
Packit db3073
}
Packit db3073
Packit db3073
static GSList *
Packit db3073
g_slist_sort_real (GSList   *list,
Packit db3073
                   GFunc     compare_func,
Packit db3073
                   gpointer  user_data)
Packit db3073
{
Packit db3073
  GSList *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_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
Packit db3073
                             g_slist_sort_real (l2, compare_func, user_data),
Packit db3073
                             compare_func,
Packit db3073
                             user_data);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_sort:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @compare_func: the comparison function used to sort the #GSList.
Packit db3073
 *     This function is passed the data from 2 elements of the #GSList
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 #GSList using the given comparison function.
Packit db3073
 *
Packit db3073
 * Returns: the start of the sorted #GSList
Packit db3073
 */
Packit db3073
GSList *
Packit db3073
g_slist_sort (GSList       *list,
Packit db3073
              GCompareFunc  compare_func)
Packit db3073
{
Packit db3073
  return g_slist_sort_real (list, (GFunc) compare_func, NULL);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_slist_sort_with_data:
Packit db3073
 * @list: a #GSList
Packit db3073
 * @compare_func: comparison function
Packit db3073
 * @user_data: data to pass to comparison function
Packit db3073
 *
Packit db3073
 * Like g_slist_sort(), but the sort function accepts a user data argument.
Packit db3073
 *
Packit db3073
 * Returns: new head of the list
Packit db3073
 */
Packit db3073
GSList *
Packit db3073
g_slist_sort_with_data (GSList           *list,
Packit db3073
                        GCompareDataFunc  compare_func,
Packit db3073
                        gpointer          user_data)
Packit db3073
{
Packit db3073
  return g_slist_sort_real (list, (GFunc) compare_func, user_data);
Packit db3073
}