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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
22 typedef struct _GRealListAllocator GRealListAllocator;
24 struct _GRealListAllocator
26 GMemChunk *list_mem_chunk;
31 static GRealListAllocator *default_allocator = NULL;
32 static GRealListAllocator *current_allocator = NULL;
35 g_slist_set_allocator (GListAllocator* fallocator)
37 GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
38 GRealListAllocator* old_allocator = current_allocator;
41 current_allocator = allocator;
44 if (!default_allocator)
45 default_allocator = (GRealListAllocator*) g_list_allocator_new ();
46 current_allocator = default_allocator;
49 if (!current_allocator->list_mem_chunk)
50 current_allocator->list_mem_chunk = g_mem_chunk_new ("slist mem chunk",
55 return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator);
64 g_slist_set_allocator (NULL);
65 if (current_allocator->free_list)
67 new_list = current_allocator->free_list;
68 current_allocator->free_list = current_allocator->free_list->next;
72 new_list = g_chunk_new (GSList, current_allocator->list_mem_chunk);
75 new_list->data = NULL;
76 new_list->next = NULL;
82 g_slist_free (GSList *list)
88 last = g_slist_last (list);
89 last->next = current_allocator->free_list;
90 current_allocator->free_list = list;
95 g_slist_free_1 (GSList *list)
99 list->next = current_allocator->free_list;
100 current_allocator->free_list = list;
105 g_slist_append (GSList *list,
111 new_list = g_slist_alloc ();
112 new_list->data = data;
116 last = g_slist_last (list);
117 /* g_assert (last != NULL); */
118 last->next = new_list;
127 g_slist_prepend (GSList *list,
132 new_list = g_slist_alloc ();
133 new_list->data = data;
134 new_list->next = list;
140 g_slist_insert (GSList *list,
149 return g_slist_append (list, data);
150 else if (position == 0)
151 return g_slist_prepend (list, data);
153 new_list = g_slist_alloc ();
154 new_list->data = data;
162 while ((position-- > 0) && tmp_list)
164 prev_list = tmp_list;
165 tmp_list = tmp_list->next;
170 new_list->next = prev_list->next;
171 prev_list->next = new_list;
175 new_list->next = list;
183 g_slist_concat (GSList *list1, GSList *list2)
188 g_slist_last (list1)->next = list2;
197 g_slist_remove (GSList *list,
208 if (tmp->data == data)
211 prev->next = tmp->next;
229 g_slist_remove_link (GSList *list,
243 prev->next = tmp->next;
259 g_slist_copy (GSList *list)
261 GSList *new_list = NULL;
267 new_list = g_slist_alloc ();
268 new_list->data = list->data;
273 last->next = g_slist_alloc ();
275 last->data = list->data;
284 g_slist_reverse (GSList *list)
308 g_slist_nth (GSList *list,
311 while ((n-- > 0) && list)
318 g_slist_nth_data (GSList *list,
321 while ((n-- > 0) && list)
324 return list ? list->data : NULL;
328 g_slist_find (GSList *list,
333 if (list->data == data)
342 g_slist_find_custom (GSList *list,
346 g_return_val_if_fail (func != NULL, list);
350 if (! func (list->data, data))
359 g_slist_position (GSList *list,
377 g_slist_index (GSList *list,
385 if (list->data == data)
395 g_slist_last (GSList *list)
407 g_slist_length (GSList *list)
422 g_slist_foreach (GSList *list,
428 (*func) (list->data, user_data);
434 g_slist_insert_sorted (GSList *list,
438 GSList *tmp_list = list;
439 GSList *prev_list = NULL;
443 g_return_val_if_fail (func != NULL, list);
447 new_list = g_slist_alloc();
448 new_list->data = data;
452 cmp = (*func) (data, tmp_list->data);
454 while ((tmp_list->next) && (cmp > 0))
456 prev_list = tmp_list;
457 tmp_list = tmp_list->next;
458 cmp = (*func) (data, tmp_list->data);
461 new_list = g_slist_alloc();
462 new_list->data = data;
464 if ((!tmp_list->next) && (cmp > 0))
466 tmp_list->next = new_list;
472 prev_list->next = new_list;
473 new_list->next = tmp_list;
478 new_list->next = list;
484 g_slist_sort_merge (GSList *l1,
486 GCompareFunc compare_func)
494 if (compare_func(l1->data,l2->data) < 0)
505 l->next= l1 ? l1 : l2;
511 g_slist_sort (GSList *list,
512 GCompareFunc compare_func)
524 while ((l2 = l2->next) != NULL)
526 if ((l2 = l2->next) == NULL)
533 return g_slist_sort_merge (g_slist_sort (list, compare_func),
534 g_slist_sort (l2, compare_func),