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_list_push_allocator (gpointer dummy) { /* present for binary compat only */ }
38 void g_list_pop_allocator (void) { /* present for binary compat only */ }
40 #define _g_list_alloc() g_slice_new (GList)
41 #define _g_list_alloc0() g_slice_new0 (GList)
42 #define _g_list_free1(list) g_slice_free (GList, list)
47 return _g_list_alloc0 ();
51 g_list_free (GList *list)
53 g_slice_free_chain (GList, list, next);
57 g_list_free_1 (GList *list)
63 g_list_append (GList *list,
69 new_list = _g_list_alloc ();
70 new_list->data = data;
71 new_list->next = NULL;
75 last = g_list_last (list);
76 /* g_assert (last != NULL); */
77 last->next = new_list;
78 new_list->prev = last;
84 new_list->prev = NULL;
90 g_list_prepend (GList *list,
95 new_list = _g_list_alloc ();
96 new_list->data = data;
97 new_list->next = list;
101 new_list->prev = list->prev;
103 list->prev->next = new_list;
104 list->prev = new_list;
107 new_list->prev = NULL;
113 g_list_insert (GList *list,
121 return g_list_append (list, data);
122 else if (position == 0)
123 return g_list_prepend (list, data);
125 tmp_list = g_list_nth (list, position);
127 return g_list_append (list, data);
129 new_list = _g_list_alloc ();
130 new_list->data = data;
131 new_list->prev = tmp_list->prev;
133 tmp_list->prev->next = new_list;
134 new_list->next = tmp_list;
135 tmp_list->prev = new_list;
137 if (tmp_list == list)
144 g_list_insert_before (GList *list,
150 list = g_list_alloc ();
152 g_return_val_if_fail (sibling == NULL, list);
159 node = _g_list_alloc ();
161 node->prev = sibling->prev;
162 node->next = sibling;
163 sibling->prev = node;
166 node->prev->next = node;
171 g_return_val_if_fail (sibling == list, node);
183 last->next = _g_list_alloc ();
184 last->next->data = data;
185 last->next->prev = last;
186 last->next->next = NULL;
193 g_list_concat (GList *list1, GList *list2)
199 tmp_list = g_list_last (list1);
201 tmp_list->next = list2;
204 list2->prev = tmp_list;
211 g_list_remove (GList *list,
219 if (tmp->data != data)
224 tmp->prev->next = tmp->next;
226 tmp->next->prev = tmp->prev;
240 g_list_remove_all (GList *list,
247 if (tmp->data != data)
251 GList *next = tmp->next;
254 tmp->prev->next = next;
258 next->prev = tmp->prev;
268 _g_list_remove_link (GList *list,
274 link->prev->next = link->next;
276 link->next->prev = link->prev;
289 g_list_remove_link (GList *list,
292 return _g_list_remove_link (list, link);
296 g_list_delete_link (GList *list,
299 list = _g_list_remove_link (list, link);
300 _g_list_free1 (link);
306 g_list_copy (GList *list)
308 GList *new_list = NULL;
314 new_list = _g_list_alloc ();
315 new_list->data = list->data;
316 new_list->prev = NULL;
321 last->next = _g_list_alloc ();
322 last->next->prev = last;
324 last->data = list->data;
334 g_list_reverse (GList *list)
343 last->next = last->prev;
351 g_list_nth (GList *list,
354 while ((n-- > 0) && list)
361 g_list_nth_prev (GList *list,
364 while ((n-- > 0) && list)
371 g_list_nth_data (GList *list,
374 while ((n-- > 0) && list)
377 return list ? list->data : NULL;
381 g_list_find (GList *list,
386 if (list->data == data)
395 g_list_find_custom (GList *list,
399 g_return_val_if_fail (func != NULL, list);
403 if (! func (list->data, data))
413 g_list_position (GList *list,
431 g_list_index (GList *list,
439 if (list->data == data)
449 g_list_last (GList *list)
461 g_list_first (GList *list)
473 g_list_length (GList *list)
488 g_list_foreach (GList *list,
494 GList *next = list->next;
495 (*func) (list->data, user_data);
501 g_list_insert_sorted_real (GList *list,
506 GList *tmp_list = list;
510 g_return_val_if_fail (func != NULL, list);
514 new_list = _g_list_alloc0 ();
515 new_list->data = data;
519 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
521 while ((tmp_list->next) && (cmp > 0))
523 tmp_list = tmp_list->next;
525 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
528 new_list = _g_list_alloc0 ();
529 new_list->data = data;
531 if ((!tmp_list->next) && (cmp > 0))
533 tmp_list->next = new_list;
534 new_list->prev = tmp_list;
540 tmp_list->prev->next = new_list;
541 new_list->prev = tmp_list->prev;
543 new_list->next = tmp_list;
544 tmp_list->prev = new_list;
546 if (tmp_list == list)
553 g_list_insert_sorted (GList *list,
557 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
561 g_list_insert_sorted_with_data (GList *list,
563 GCompareDataFunc func,
566 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
570 g_list_sort_merge (GList *l1,
575 GList list, *l, *lprev;
583 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
599 l->next = l1 ? l1 : l2;
606 g_list_sort_real (GList *list,
620 while ((l2 = l2->next) != NULL)
622 if ((l2 = l2->next) == NULL)
629 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
630 g_list_sort_real (l2, compare_func, user_data),
636 g_list_sort (GList *list,
637 GCompareFunc compare_func)
639 return g_list_sort_real (list, (GFunc) compare_func, NULL);
644 g_list_sort_with_data (GList *list,
645 GCompareDataFunc compare_func,
648 return g_list_sort_real (list, (GFunc) compare_func, user_data);
652 #include "galiasdef.c"