forgot to include submitter.
[glib.git] / gslist.c
blobef5615f67e4ac73b652c89966db1d0e6e60e326b
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.
21 * Modified by the GLib Team and others 1997-1999. 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/.
27 /*
28 * MT safe
31 #include "glib.h"
34 struct _GAllocator /* from gmem.c */
36 gchar *name;
37 guint16 n_preallocs;
38 guint is_unused : 1;
39 guint type : 4;
40 GAllocator *last;
41 GMemChunk *mem_chunk;
42 GSList *free_lists; /* implementation specific */
45 G_LOCK_DEFINE_STATIC (current_allocator);
46 static GAllocator *current_allocator = NULL;
48 /* HOLDS: current_allocator_lock */
49 static void
50 g_slist_validate_allocator (GAllocator *allocator)
52 g_return_if_fail (allocator != NULL);
53 g_return_if_fail (allocator->is_unused == TRUE);
55 if (allocator->type != G_ALLOCATOR_SLIST)
57 allocator->type = G_ALLOCATOR_SLIST;
58 if (allocator->mem_chunk)
60 g_mem_chunk_destroy (allocator->mem_chunk);
61 allocator->mem_chunk = NULL;
65 if (!allocator->mem_chunk)
67 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
68 sizeof (GSList),
69 sizeof (GSList) * allocator->n_preallocs,
70 G_ALLOC_ONLY);
71 allocator->free_lists = NULL;
74 allocator->is_unused = FALSE;
77 void
78 g_slist_push_allocator (GAllocator *allocator)
80 G_LOCK (current_allocator);
81 g_slist_validate_allocator (allocator);
82 allocator->last = current_allocator;
83 current_allocator = allocator;
84 G_UNLOCK (current_allocator);
87 void
88 g_slist_pop_allocator (void)
90 G_LOCK (current_allocator);
91 if (current_allocator)
93 GAllocator *allocator;
95 allocator = current_allocator;
96 current_allocator = allocator->last;
97 allocator->last = NULL;
98 allocator->is_unused = TRUE;
100 G_UNLOCK (current_allocator);
103 GSList*
104 g_slist_alloc (void)
106 GSList *list;
108 G_LOCK (current_allocator);
109 if (!current_allocator)
111 GAllocator *allocator = g_allocator_new ("GLib default GSList allocator",
112 128);
113 g_slist_validate_allocator (allocator);
114 allocator->last = NULL;
115 current_allocator = allocator;
117 if (!current_allocator->free_lists)
119 list = g_chunk_new (GSList, current_allocator->mem_chunk);
120 list->data = NULL;
122 else
124 if (current_allocator->free_lists->data)
126 list = current_allocator->free_lists->data;
127 current_allocator->free_lists->data = list->next;
128 list->data = NULL;
130 else
132 list = current_allocator->free_lists;
133 current_allocator->free_lists = list->next;
136 G_UNLOCK (current_allocator);
138 list->next = NULL;
140 return list;
143 void
144 g_slist_free (GSList *list)
146 if (list)
148 list->data = list->next;
149 G_LOCK (current_allocator);
150 list->next = current_allocator->free_lists;
151 current_allocator->free_lists = list;
152 G_UNLOCK (current_allocator);
156 void
157 g_slist_free_1 (GSList *list)
159 if (list)
161 list->data = NULL;
162 G_LOCK (current_allocator);
163 list->next = current_allocator->free_lists;
164 current_allocator->free_lists = list;
165 G_UNLOCK (current_allocator);
169 GSList*
170 g_slist_append (GSList *list,
171 gpointer data)
173 GSList *new_list;
174 GSList *last;
176 new_list = g_slist_alloc ();
177 new_list->data = data;
179 if (list)
181 last = g_slist_last (list);
182 /* g_assert (last != NULL); */
183 last->next = new_list;
185 return list;
187 else
188 return new_list;
191 GSList*
192 g_slist_prepend (GSList *list,
193 gpointer data)
195 GSList *new_list;
197 new_list = g_slist_alloc ();
198 new_list->data = data;
199 new_list->next = list;
201 return new_list;
204 GSList*
205 g_slist_insert (GSList *list,
206 gpointer data,
207 gint position)
209 GSList *prev_list;
210 GSList *tmp_list;
211 GSList *new_list;
213 if (position < 0)
214 return g_slist_append (list, data);
215 else if (position == 0)
216 return g_slist_prepend (list, data);
218 new_list = g_slist_alloc ();
219 new_list->data = data;
221 if (!list)
222 return new_list;
224 prev_list = NULL;
225 tmp_list = list;
227 while ((position-- > 0) && tmp_list)
229 prev_list = tmp_list;
230 tmp_list = tmp_list->next;
233 if (prev_list)
235 new_list->next = prev_list->next;
236 prev_list->next = new_list;
238 else
240 new_list->next = list;
241 list = new_list;
244 return list;
247 GSList *
248 g_slist_concat (GSList *list1, GSList *list2)
250 if (list2)
252 if (list1)
253 g_slist_last (list1)->next = list2;
254 else
255 list1 = list2;
258 return list1;
261 GSList*
262 g_slist_remove (GSList *list,
263 gpointer data)
265 GSList *tmp;
266 GSList *prev;
268 prev = NULL;
269 tmp = list;
271 while (tmp)
273 if (tmp->data == data)
275 if (prev)
276 prev->next = tmp->next;
277 if (list == tmp)
278 list = list->next;
280 tmp->next = NULL;
281 g_slist_free (tmp);
283 break;
286 prev = tmp;
287 tmp = tmp->next;
290 return list;
293 GSList*
294 g_slist_remove_link (GSList *list,
295 GSList *link)
297 GSList *tmp;
298 GSList *prev;
300 prev = NULL;
301 tmp = list;
303 while (tmp)
305 if (tmp == link)
307 if (prev)
308 prev->next = tmp->next;
309 if (list == tmp)
310 list = list->next;
312 tmp->next = NULL;
313 break;
316 prev = tmp;
317 tmp = tmp->next;
320 return list;
323 GSList*
324 g_slist_copy (GSList *list)
326 GSList *new_list = NULL;
328 if (list)
330 GSList *last;
332 new_list = g_slist_alloc ();
333 new_list->data = list->data;
334 last = new_list;
335 list = list->next;
336 while (list)
338 last->next = g_slist_alloc ();
339 last = last->next;
340 last->data = list->data;
341 list = list->next;
345 return new_list;
348 GSList*
349 g_slist_reverse (GSList *list)
351 GSList *tmp;
352 GSList *prev;
353 GSList *last;
355 last = NULL;
356 prev = NULL;
358 while (list)
360 last = list;
362 tmp = list->next;
363 list->next = prev;
365 prev = list;
366 list = tmp;
369 return last;
372 GSList*
373 g_slist_nth (GSList *list,
374 guint n)
376 while ((n-- > 0) && list)
377 list = list->next;
379 return list;
382 gpointer
383 g_slist_nth_data (GSList *list,
384 guint n)
386 while ((n-- > 0) && list)
387 list = list->next;
389 return list ? list->data : NULL;
392 GSList*
393 g_slist_find (GSList *list,
394 gpointer data)
396 while (list)
398 if (list->data == data)
399 break;
400 list = list->next;
403 return list;
406 GSList*
407 g_slist_find_custom (GSList *list,
408 gpointer data,
409 GCompareFunc func)
411 g_return_val_if_fail (func != NULL, list);
413 while (list)
415 if (! func (list->data, data))
416 return list;
417 list = list->next;
420 return NULL;
423 gint
424 g_slist_position (GSList *list,
425 GSList *link)
427 gint i;
429 i = 0;
430 while (list)
432 if (list == link)
433 return i;
434 i++;
435 list = list->next;
438 return -1;
441 gint
442 g_slist_index (GSList *list,
443 gpointer data)
445 gint i;
447 i = 0;
448 while (list)
450 if (list->data == data)
451 return i;
452 i++;
453 list = list->next;
456 return -1;
459 GSList*
460 g_slist_last (GSList *list)
462 if (list)
464 while (list->next)
465 list = list->next;
468 return list;
471 guint
472 g_slist_length (GSList *list)
474 guint length;
476 length = 0;
477 while (list)
479 length++;
480 list = list->next;
483 return length;
486 void
487 g_slist_foreach (GSList *list,
488 GFunc func,
489 gpointer user_data)
491 while (list)
493 (*func) (list->data, user_data);
494 list = list->next;
498 GSList*
499 g_slist_insert_sorted (GSList *list,
500 gpointer data,
501 GCompareFunc func)
503 GSList *tmp_list = list;
504 GSList *prev_list = NULL;
505 GSList *new_list;
506 gint cmp;
508 g_return_val_if_fail (func != NULL, list);
510 if (!list)
512 new_list = g_slist_alloc();
513 new_list->data = data;
514 return new_list;
517 cmp = (*func) (data, tmp_list->data);
519 while ((tmp_list->next) && (cmp > 0))
521 prev_list = tmp_list;
522 tmp_list = tmp_list->next;
523 cmp = (*func) (data, tmp_list->data);
526 new_list = g_slist_alloc();
527 new_list->data = data;
529 if ((!tmp_list->next) && (cmp > 0))
531 tmp_list->next = new_list;
532 return list;
535 if (prev_list)
537 prev_list->next = new_list;
538 new_list->next = tmp_list;
539 return list;
541 else
543 new_list->next = list;
544 return new_list;
548 static GSList*
549 g_slist_sort_merge (GSList *l1,
550 GSList *l2,
551 GCompareFunc compare_func)
553 GSList list, *l;
555 l=&list;
557 while (l1 && l2)
559 if (compare_func(l1->data,l2->data) < 0)
561 l=l->next=l1;
562 l1=l1->next;
564 else
566 l=l->next=l2;
567 l2=l2->next;
570 l->next= l1 ? l1 : l2;
572 return list.next;
575 GSList*
576 g_slist_sort (GSList *list,
577 GCompareFunc compare_func)
579 GSList *l1, *l2;
581 if (!list)
582 return NULL;
583 if (!list->next)
584 return list;
586 l1 = list;
587 l2 = list->next;
589 while ((l2 = l2->next) != NULL)
591 if ((l2 = l2->next) == NULL)
592 break;
593 l1=l1->next;
595 l2 = l1->next;
596 l1->next = NULL;
598 return g_slist_sort_merge (g_slist_sort (list, compare_func),
599 g_slist_sort (l2, compare_func),
600 compare_func);