1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
37 void g_slist_push_allocator (gpointer dummy) { /* present for binary compat only */ }
38 void g_slist_pop_allocator (void) { /* present for binary compat only */ }
40 #define _g_slist_alloc0() g_slice_new0 (GSList)
41 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
46 return _g_slist_alloc0 ();
50 g_slist_free (GSList *slist)
52 g_slice_free_chain (GSList, slist, next);
56 g_slist_free_1 (GSList *slist)
58 _g_slist_free1 (slist);
62 g_slist_append (GSList *list,
68 new_list = _g_slist_alloc0 ();
69 new_list->data = data;
73 last = g_slist_last (list);
74 /* g_assert (last != NULL); */
75 last->next = new_list;
84 g_slist_prepend (GSList *list,
89 new_list = _g_slist_alloc0 ();
90 new_list->data = data;
91 new_list->next = list;
97 g_slist_insert (GSList *list,
106 return g_slist_append (list, data);
107 else if (position == 0)
108 return g_slist_prepend (list, data);
110 new_list = _g_slist_alloc0 ();
111 new_list->data = data;
119 while ((position-- > 0) && tmp_list)
121 prev_list = tmp_list;
122 tmp_list = tmp_list->next;
127 new_list->next = prev_list->next;
128 prev_list->next = new_list;
132 new_list->next = list;
140 g_slist_insert_before (GSList *slist,
146 slist = g_slist_alloc ();
148 g_return_val_if_fail (sibling == NULL, slist);
153 GSList *node, *last = NULL;
155 for (node = slist; node; last = node, node = last->next)
160 node = g_slist_alloc ();
168 node = g_slist_alloc ();
170 node->next = last->next;
179 g_slist_concat (GSList *list1, GSList *list2)
184 g_slist_last (list1)->next = list2;
193 g_slist_remove (GSList *list,
196 GSList *tmp, *prev = NULL;
201 if (tmp->data == data)
204 prev->next = tmp->next;
208 g_slist_free_1 (tmp);
219 g_slist_remove_all (GSList *list,
222 GSList *tmp, *prev = NULL;
227 if (tmp->data == data)
229 GSList *next = tmp->next;
236 g_slist_free_1 (tmp);
249 static inline GSList*
250 _g_slist_remove_link (GSList *list,
264 prev->next = tmp->next;
280 g_slist_remove_link (GSList *list,
283 return _g_slist_remove_link (list, link);
287 g_slist_delete_link (GSList *list,
290 list = _g_slist_remove_link (list, link);
291 _g_slist_free1 (link);
297 g_slist_copy (GSList *list)
299 GSList *new_list = NULL;
305 new_list = _g_slist_alloc0 ();
306 new_list->data = list->data;
311 last->next = _g_slist_alloc0 ();
313 last->data = list->data;
322 g_slist_reverse (GSList *list)
328 GSList *next = list->next;
340 g_slist_nth (GSList *list,
343 while (n-- > 0 && list)
350 g_slist_nth_data (GSList *list,
353 while (n-- > 0 && list)
356 return list ? list->data : NULL;
360 g_slist_find (GSList *list,
365 if (list->data == data)
374 g_slist_find_custom (GSList *list,
378 g_return_val_if_fail (func != NULL, list);
382 if (! func (list->data, data))
391 g_slist_position (GSList *list,
409 g_slist_index (GSList *list,
417 if (list->data == data)
427 g_slist_last (GSList *list)
439 g_slist_length (GSList *list)
454 g_slist_foreach (GSList *list,
460 GSList *next = list->next;
461 (*func) (list->data, user_data);
467 g_slist_insert_sorted_real (GSList *list,
472 GSList *tmp_list = list;
473 GSList *prev_list = NULL;
477 g_return_val_if_fail (func != NULL, list);
481 new_list = _g_slist_alloc0 ();
482 new_list->data = data;
486 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
488 while ((tmp_list->next) && (cmp > 0))
490 prev_list = tmp_list;
491 tmp_list = tmp_list->next;
493 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
496 new_list = _g_slist_alloc0 ();
497 new_list->data = data;
499 if ((!tmp_list->next) && (cmp > 0))
501 tmp_list->next = new_list;
507 prev_list->next = new_list;
508 new_list->next = tmp_list;
513 new_list->next = list;
519 g_slist_insert_sorted (GSList *list,
523 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
527 g_slist_insert_sorted_with_data (GSList *list,
529 GCompareDataFunc func,
532 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
536 g_slist_sort_merge (GSList *l1,
548 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
561 l->next= l1 ? l1 : l2;
567 g_slist_sort_real (GSList *list,
581 while ((l2 = l2->next) != NULL)
583 if ((l2 = l2->next) == NULL)
590 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
591 g_slist_sort_real (l2, compare_func, user_data),
597 g_slist_sort (GSList *list,
598 GCompareFunc compare_func)
600 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
604 g_slist_sort_with_data (GSList *list,
605 GCompareDataFunc compare_func,
608 return g_slist_sort_real (list, (GFunc) compare_func, user_data);
611 #define __G_SLIST_C__
612 #include "galiasdef.c"