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 General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 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 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/.
28 * Modified by Bruno Haible for use as a gnulib module.
44 void g_list_push_allocator (gpointer dummy) { /* present for binary compat only */ }
45 void g_list_pop_allocator (void) { /* present for binary compat only */ }
48 #define _g_list_alloc() g_slice_new (GList)
49 #define _g_list_alloc0() g_slice_new0 (GList)
50 #define _g_list_free1(list) g_slice_free (GList, list)
57 return _g_list_alloc0 ();
63 g_list_free (GList *list)
67 GList *n = list->next;
68 g_slice_free (GList, list);
76 g_list_free_1 (GList *list)
84 g_list_append (GList *list,
90 new_list = _g_list_alloc ();
91 new_list->data = data;
92 new_list->next = NULL;
96 last = g_list_last (list);
97 /* g_assert (last != NULL); */
98 last->next = new_list;
99 new_list->prev = last;
105 new_list->prev = NULL;
111 g_list_prepend (GList *list,
116 new_list = _g_list_alloc ();
117 new_list->data = data;
118 new_list->next = list;
122 new_list->prev = list->prev;
124 list->prev->next = new_list;
125 list->prev = new_list;
128 new_list->prev = NULL;
136 g_list_insert (GList *list,
144 return g_list_append (list, data);
145 else if (position == 0)
146 return g_list_prepend (list, data);
148 tmp_list = g_list_nth (list, position);
150 return g_list_append (list, data);
152 new_list = _g_list_alloc ();
153 new_list->data = data;
154 new_list->prev = tmp_list->prev;
156 tmp_list->prev->next = new_list;
157 new_list->next = tmp_list;
158 tmp_list->prev = new_list;
160 if (tmp_list == list)
167 g_list_insert_before (GList *list,
173 list = g_list_alloc ();
175 g_return_val_if_fail (sibling == NULL, list);
182 node = _g_list_alloc ();
184 node->prev = sibling->prev;
185 node->next = sibling;
186 sibling->prev = node;
189 node->prev->next = node;
194 g_return_val_if_fail (sibling == list, node);
206 last->next = _g_list_alloc ();
207 last->next->data = data;
208 last->next->prev = last;
209 last->next->next = NULL;
216 g_list_concat (GList *list1, GList *list2)
222 tmp_list = g_list_last (list1);
224 tmp_list->next = list2;
227 list2->prev = tmp_list;
234 g_list_remove (GList *list,
242 if (tmp->data != data)
247 tmp->prev->next = tmp->next;
249 tmp->next->prev = tmp->prev;
263 g_list_remove_all (GList *list,
270 if (tmp->data != data)
274 GList *next = tmp->next;
277 tmp->prev->next = next;
281 next->prev = tmp->prev;
293 _g_list_remove_link (GList *list,
299 link->prev->next = link->next;
301 link->next->prev = link->prev;
316 g_list_remove_link (GList *list,
319 return _g_list_remove_link (list, link);
325 g_list_delete_link (GList *list,
328 list = _g_list_remove_link (list, link);
329 _g_list_free1 (link);
337 g_list_copy (GList *list)
339 GList *new_list = NULL;
345 new_list = _g_list_alloc ();
346 new_list->data = list->data;
347 new_list->prev = NULL;
352 last->next = _g_list_alloc ();
353 last->next->prev = last;
355 last->data = list->data;
365 g_list_reverse (GList *list)
374 last->next = last->prev;
382 g_list_nth (GList *list,
385 while ((n-- > 0) && list)
392 g_list_nth_prev (GList *list,
395 while ((n-- > 0) && list)
402 g_list_nth_data (GList *list,
405 while ((n-- > 0) && list)
408 return list ? list->data : NULL;
412 g_list_find (GList *list,
417 if (list->data == data)
426 g_list_find_custom (GList *list,
430 g_return_val_if_fail (func != NULL, list);
434 if (! func (list->data, data))
444 g_list_position (GList *list,
462 g_list_index (GList *list,
470 if (list->data == data)
482 g_list_last (GList *list)
496 g_list_first (GList *list)
508 g_list_length (GList *list)
523 g_list_foreach (GList *list,
529 GList *next = list->next;
530 (*func) (list->data, user_data);
536 g_list_insert_sorted_real (GList *list,
541 GList *tmp_list = list;
545 g_return_val_if_fail (func != NULL, list);
549 new_list = _g_list_alloc0 ();
550 new_list->data = data;
554 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
556 while ((tmp_list->next) && (cmp > 0))
558 tmp_list = tmp_list->next;
560 cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
563 new_list = _g_list_alloc0 ();
564 new_list->data = data;
566 if ((!tmp_list->next) && (cmp > 0))
568 tmp_list->next = new_list;
569 new_list->prev = tmp_list;
575 tmp_list->prev->next = new_list;
576 new_list->prev = tmp_list->prev;
578 new_list->next = tmp_list;
579 tmp_list->prev = new_list;
581 if (tmp_list == list)
588 g_list_insert_sorted (GList *list,
592 return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
596 g_list_insert_sorted_with_data (GList *list,
598 GCompareDataFunc func,
601 return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
605 g_list_sort_merge (GList *l1,
610 GList list, *l, *lprev;
618 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
634 l->next = l1 ? l1 : l2;
641 g_list_sort_real (GList *list,
655 while ((l2 = l2->next) != NULL)
657 if ((l2 = l2->next) == NULL)
664 return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
665 g_list_sort_real (l2, compare_func, user_data),
671 g_list_sort (GList *list,
672 GCompareFunc compare_func)
674 return g_list_sort_real (list, (GFunc) compare_func, NULL);
679 g_list_sort_with_data (GList *list,
680 GCompareDataFunc compare_func,
683 return g_list_sort_real (list, (GFunc) compare_func, user_data);
687 #include "galiasdef.c"