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_alloc() g_slice_new (GSList)
42 #define _g_slist_free1(slist) g_slice_free (GSList, slist)
47 return _g_slist_alloc0 ();
51 g_slist_free (GSList *slist)
53 g_slice_free_chain (GSList, slist, next);
57 g_slist_free_1 (GSList *slist)
59 _g_slist_free1 (slist);
63 g_slist_append (GSList *list,
69 new_list = _g_slist_alloc ();
70 new_list->data = data;
71 new_list->next = NULL;
75 last = g_slist_last (list);
76 /* g_assert (last != NULL); */
77 last->next = new_list;
86 g_slist_prepend (GSList *list,
91 new_list = _g_slist_alloc ();
92 new_list->data = data;
93 new_list->next = list;
99 g_slist_insert (GSList *list,
108 return g_slist_append (list, data);
109 else if (position == 0)
110 return g_slist_prepend (list, data);
112 new_list = _g_slist_alloc ();
113 new_list->data = data;
117 new_list->next = NULL;
124 while ((position-- > 0) && tmp_list)
126 prev_list = tmp_list;
127 tmp_list = tmp_list->next;
132 new_list->next = prev_list->next;
133 prev_list->next = new_list;
137 new_list->next = list;
145 g_slist_insert_before (GSList *slist,
151 slist = _g_slist_alloc ();
154 g_return_val_if_fail (sibling == NULL, slist);
159 GSList *node, *last = NULL;
161 for (node = slist; node; last = node, node = last->next)
166 node = _g_slist_alloc ();
174 node = _g_slist_alloc ();
176 node->next = last->next;
185 g_slist_concat (GSList *list1, GSList *list2)
190 g_slist_last (list1)->next = list2;
199 g_slist_remove (GSList *list,
202 GSList *tmp, *prev = NULL;
207 if (tmp->data == data)
210 prev->next = tmp->next;
214 g_slist_free_1 (tmp);
225 g_slist_remove_all (GSList *list,
228 GSList *tmp, *prev = NULL;
233 if (tmp->data == data)
235 GSList *next = tmp->next;
242 g_slist_free_1 (tmp);
255 static inline GSList*
256 _g_slist_remove_link (GSList *list,
270 prev->next = tmp->next;
286 g_slist_remove_link (GSList *list,
289 return _g_slist_remove_link (list, link);
293 g_slist_delete_link (GSList *list,
296 list = _g_slist_remove_link (list, link);
297 _g_slist_free1 (link);
303 g_slist_copy (GSList *list)
305 GSList *new_list = NULL;
311 new_list = _g_slist_alloc ();
312 new_list->data = list->data;
317 last->next = _g_slist_alloc ();
319 last->data = list->data;
329 g_slist_reverse (GSList *list)
335 GSList *next = list->next;
347 g_slist_nth (GSList *list,
350 while (n-- > 0 && list)
357 g_slist_nth_data (GSList *list,
360 while (n-- > 0 && list)
363 return list ? list->data : NULL;
367 g_slist_find (GSList *list,
372 if (list->data == data)
381 g_slist_find_custom (GSList *list,
385 g_return_val_if_fail (func != NULL, list);
389 if (! func (list->data, data))
398 g_slist_position (GSList *list,
416 g_slist_index (GSList *list,
424 if (list->data == data)
434 g_slist_last (GSList *list)
446 g_slist_length (GSList *list)
461 g_slist_foreach (GSList *list,
467 GSList *next = list->next;
468 (*func) (list->data, user_data);
474 g_slist_insert_sorted_real (GSList *list,
479 GSList *tmp_list = list;
480 GSList *prev_list = NULL;
484 g_return_val_if_fail (func != NULL, list);
488 new_list = _g_slist_alloc ();
489 new_list->data = data;
490 new_list->next = NULL;
494 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
496 while ((tmp_list->next) && (cmp > 0))
498 prev_list = tmp_list;
499 tmp_list = tmp_list->next;
501 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
504 new_list = _g_slist_alloc ();
505 new_list->data = data;
507 if ((!tmp_list->next) && (cmp > 0))
509 tmp_list->next = new_list;
510 new_list->next = NULL;
516 prev_list->next = new_list;
517 new_list->next = tmp_list;
522 new_list->next = list;
528 g_slist_insert_sorted (GSList *list,
532 return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
536 g_slist_insert_sorted_with_data (GSList *list,
538 GCompareDataFunc func,
541 return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
545 g_slist_sort_merge (GSList *l1,
557 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
570 l->next= l1 ? l1 : l2;
576 g_slist_sort_real (GSList *list,
590 while ((l2 = l2->next) != NULL)
592 if ((l2 = l2->next) == NULL)
599 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
600 g_slist_sort_real (l2, compare_func, user_data),
606 g_slist_sort (GSList *list,
607 GCompareFunc compare_func)
609 return g_slist_sort_real (list, (GFunc) compare_func, NULL);
613 g_slist_sort_with_data (GSList *list,
614 GCompareDataFunc compare_func,
617 return g_slist_sort_real (list, (GFunc) compare_func, user_data);
620 #define __G_SLIST_C__
621 #include "galiasdef.c"