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_alloc0() g_slice_new0 (GList)
41 #define _g_list_free1(list) g_slice_free (GList, list)
46 return _g_list_alloc0 ();
50 g_list_free (GList *list)
52 g_slice_free_chain (sizeof (GList), list, G_STRUCT_OFFSET (GList, next));
56 g_list_free_1 (GList *list)
62 g_list_append (GList *list,
68 new_list = _g_list_alloc0 ();
69 new_list->data = data;
73 last = g_list_last (list);
74 /* g_assert (last != NULL); */
75 last->next = new_list;
76 new_list->prev = last;
85 g_list_prepend (GList *list,
90 new_list = _g_list_alloc0 ();
91 new_list->data = data;
97 list->prev->next = new_list;
98 new_list->prev = list->prev;
100 list->prev = new_list;
101 new_list->next = list;
108 g_list_insert (GList *list,
116 return g_list_append (list, data);
117 else if (position == 0)
118 return g_list_prepend (list, data);
120 tmp_list = g_list_nth (list, position);
122 return g_list_append (list, data);
124 new_list = _g_list_alloc0 ();
125 new_list->data = data;
129 tmp_list->prev->next = new_list;
130 new_list->prev = tmp_list->prev;
132 new_list->next = tmp_list;
133 tmp_list->prev = new_list;
135 if (tmp_list == list)
142 g_list_insert_before (GList *list,
148 list = g_list_alloc ();
150 g_return_val_if_fail (sibling == NULL, list);
157 node = g_list_alloc ();
161 node->prev = sibling->prev;
162 node->prev->next = node;
163 node->next = sibling;
164 sibling->prev = node;
169 node->next = sibling;
170 sibling->prev = 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;
192 g_list_concat (GList *list1, GList *list2)
198 tmp_list = g_list_last (list1);
200 tmp_list->next = list2;
203 list2->prev = tmp_list;
210 g_list_remove (GList *list,
218 if (tmp->data != data)
223 tmp->prev->next = tmp->next;
225 tmp->next->prev = tmp->prev;
239 g_list_remove_all (GList *list,
246 if (tmp->data != data)
250 GList *next = tmp->next;
253 tmp->prev->next = next;
257 next->prev = tmp->prev;
267 _g_list_remove_link (GList *list,
273 link->prev->next = link->next;
275 link->next->prev = link->prev;
288 g_list_remove_link (GList *list,
291 return _g_list_remove_link (list, link);
295 g_list_delete_link (GList *list,
298 list = _g_list_remove_link (list, link);
299 _g_list_free1 (link);
305 g_list_copy (GList *list)
307 GList *new_list = NULL;
313 new_list = _g_list_alloc0 ();
314 new_list->data = list->data;
319 last->next = _g_list_alloc0 ();
320 last->next->prev = last;
322 last->data = list->data;
331 g_list_reverse (GList *list)
340 last->next = last->prev;
348 g_list_nth (GList *list,
351 while ((n-- > 0) && list)
358 g_list_nth_prev (GList *list,
361 while ((n-- > 0) && list)
368 g_list_nth_data (GList *list,
371 while ((n-- > 0) && list)
374 return list ? list->data : NULL;
378 g_list_find (GList *list,
383 if (list->data == data)
392 g_list_find_custom (GList *list,
396 g_return_val_if_fail (func != NULL, list);
400 if (! func (list->data, data))
410 g_list_position (GList *list,
428 g_list_index (GList *list,
436 if (list->data == data)
446 g_list_last (GList *list)
458 g_list_first (GList *list)
470 g_list_length (GList *list)
485 g_list_foreach (GList *list,
491 GList *next = list->next;
492 (*func) (list->data, user_data);
499 g_list_insert_sorted (GList *list,
503 GList *tmp_list = list;
507 g_return_val_if_fail (func != NULL, list);
511 new_list = _g_list_alloc0 ();
512 new_list->data = data;
516 cmp = (*func) (data, tmp_list->data);
518 while ((tmp_list->next) && (cmp > 0))
520 tmp_list = tmp_list->next;
521 cmp = (*func) (data, tmp_list->data);
524 new_list = _g_list_alloc0 ();
525 new_list->data = data;
527 if ((!tmp_list->next) && (cmp > 0))
529 tmp_list->next = new_list;
530 new_list->prev = tmp_list;
536 tmp_list->prev->next = new_list;
537 new_list->prev = tmp_list->prev;
539 new_list->next = tmp_list;
540 tmp_list->prev = new_list;
542 if (tmp_list == list)
549 g_list_sort_merge (GList *l1,
555 GList list, *l, *lprev;
564 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
566 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
582 l->next = l1 ? l1 : l2;
589 g_list_sort_real (GList *list,
604 while ((l2 = l2->next) != NULL)
606 if ((l2 = l2->next) == NULL)
613 return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
614 g_list_sort_real (l2, compare_func, use_data, user_data),
621 g_list_sort (GList *list,
622 GCompareFunc compare_func)
624 return g_list_sort_real (list, (GFunc) compare_func, FALSE, NULL);
629 g_list_sort_with_data (GList *list,
630 GCompareDataFunc compare_func,
633 return g_list_sort_real (list, (GFunc) compare_func, TRUE, user_data);
637 #include "galiasdef.c"