Avoid forcing extra newlines when using template files. (#171005)
[glib.git] / glib / gslist.c
blobbdba1f333493a097593e273d44b1bd7c6fb5bfb0
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/.
27 /*
28 * MT safe
31 #include "config.h"
33 #include "glib.h"
34 #include "galias.h"
37 #ifndef DISABLE_MEM_POOLS
38 struct _GAllocator /* from gmem.c */
40 gchar *name;
41 guint16 n_preallocs;
42 guint is_unused : 1;
43 guint type : 4;
44 GAllocator *last;
45 GMemChunk *mem_chunk;
46 GSList *free_lists; /* implementation specific */
49 G_LOCK_DEFINE_STATIC (current_allocator);
50 static GAllocator *current_allocator = NULL;
52 /* HOLDS: current_allocator_lock */
53 static void
54 g_slist_validate_allocator (GAllocator *allocator)
56 g_return_if_fail (allocator != NULL);
57 g_return_if_fail (allocator->is_unused == TRUE);
59 if (allocator->type != G_ALLOCATOR_SLIST)
61 allocator->type = G_ALLOCATOR_SLIST;
62 if (allocator->mem_chunk)
64 g_mem_chunk_destroy (allocator->mem_chunk);
65 allocator->mem_chunk = NULL;
69 if (!allocator->mem_chunk)
71 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
72 sizeof (GSList),
73 sizeof (GSList) * allocator->n_preallocs,
74 G_ALLOC_ONLY);
75 allocator->free_lists = NULL;
78 allocator->is_unused = FALSE;
81 void
82 g_slist_push_allocator (GAllocator *allocator)
84 G_LOCK (current_allocator);
85 g_slist_validate_allocator (allocator);
86 allocator->last = current_allocator;
87 current_allocator = allocator;
88 G_UNLOCK (current_allocator);
91 void
92 g_slist_pop_allocator (void)
94 G_LOCK (current_allocator);
95 if (current_allocator)
97 GAllocator *allocator;
99 allocator = current_allocator;
100 current_allocator = allocator->last;
101 allocator->last = NULL;
102 allocator->is_unused = TRUE;
104 G_UNLOCK (current_allocator);
107 static inline GSList*
108 _g_slist_alloc (void)
110 GSList *list;
112 G_LOCK (current_allocator);
113 if (!current_allocator)
115 GAllocator *allocator = g_allocator_new ("GLib default GSList allocator",
116 128);
117 g_slist_validate_allocator (allocator);
118 allocator->last = NULL;
119 current_allocator = allocator;
121 if (!current_allocator->free_lists)
123 list = g_chunk_new (GSList, current_allocator->mem_chunk);
124 list->data = NULL;
126 else
128 if (current_allocator->free_lists->data)
130 list = current_allocator->free_lists->data;
131 current_allocator->free_lists->data = list->next;
132 list->data = NULL;
134 else
136 list = current_allocator->free_lists;
137 current_allocator->free_lists = list->next;
140 G_UNLOCK (current_allocator);
142 list->next = NULL;
144 return list;
147 GSList*
148 g_slist_alloc (void)
150 return _g_slist_alloc ();
153 void
154 g_slist_free (GSList *list)
156 if (list)
158 GSList *last_node = list;
160 #ifdef ENABLE_GC_FRIENDLY
161 while (last_node->next)
163 last_node->data = NULL;
164 last_node = last_node->next;
166 last_node->data = NULL;
167 #else /* !ENABLE_GC_FRIENDLY */
168 list->data = list->next;
169 #endif /* ENABLE_GC_FRIENDLY */
171 G_LOCK (current_allocator);
172 last_node->next = current_allocator->free_lists;
173 current_allocator->free_lists = list;
174 G_UNLOCK (current_allocator);
178 static inline void
179 _g_slist_free_1 (GSList *list)
181 if (list)
183 list->data = NULL;
184 G_LOCK (current_allocator);
185 list->next = current_allocator->free_lists;
186 current_allocator->free_lists = list;
187 G_UNLOCK (current_allocator);
191 void
192 g_slist_free_1 (GSList *list)
194 _g_slist_free_1 (list);
196 #else /* DISABLE_MEM_POOLS */
198 #define _g_slist_alloc g_slist_alloc
199 GSList*
200 g_slist_alloc (void)
202 GSList *list;
204 list = g_new0 (GSList, 1);
206 return list;
209 void
210 g_slist_free (GSList *list)
212 GSList *last;
214 while (list)
216 last = list;
217 list = list->next;
218 g_free (last);
222 #define _g_slist_free_1 g_slist_free_1
223 void
224 g_slist_free_1 (GSList *list)
226 g_free (list);
229 #endif
231 GSList*
232 g_slist_append (GSList *list,
233 gpointer data)
235 GSList *new_list;
236 GSList *last;
238 new_list = _g_slist_alloc ();
239 new_list->data = data;
241 if (list)
243 last = g_slist_last (list);
244 /* g_assert (last != NULL); */
245 last->next = new_list;
247 return list;
249 else
250 return new_list;
253 GSList*
254 g_slist_prepend (GSList *list,
255 gpointer data)
257 GSList *new_list;
259 new_list = _g_slist_alloc ();
260 new_list->data = data;
261 new_list->next = list;
263 return new_list;
266 GSList*
267 g_slist_insert (GSList *list,
268 gpointer data,
269 gint position)
271 GSList *prev_list;
272 GSList *tmp_list;
273 GSList *new_list;
275 if (position < 0)
276 return g_slist_append (list, data);
277 else if (position == 0)
278 return g_slist_prepend (list, data);
280 new_list = _g_slist_alloc ();
281 new_list->data = data;
283 if (!list)
284 return new_list;
286 prev_list = NULL;
287 tmp_list = list;
289 while ((position-- > 0) && tmp_list)
291 prev_list = tmp_list;
292 tmp_list = tmp_list->next;
295 if (prev_list)
297 new_list->next = prev_list->next;
298 prev_list->next = new_list;
300 else
302 new_list->next = list;
303 list = new_list;
306 return list;
309 GSList*
310 g_slist_insert_before (GSList *slist,
311 GSList *sibling,
312 gpointer data)
314 if (!slist)
316 slist = g_slist_alloc ();
317 slist->data = data;
318 g_return_val_if_fail (sibling == NULL, slist);
319 return slist;
321 else
323 GSList *node, *last = NULL;
325 for (node = slist; node; last = node, node = last->next)
326 if (node == sibling)
327 break;
328 if (!last)
330 node = g_slist_alloc ();
331 node->data = data;
332 node->next = slist;
334 return node;
336 else
338 node = g_slist_alloc ();
339 node->data = data;
340 node->next = last->next;
341 last->next = node;
343 return slist;
348 GSList *
349 g_slist_concat (GSList *list1, GSList *list2)
351 if (list2)
353 if (list1)
354 g_slist_last (list1)->next = list2;
355 else
356 list1 = list2;
359 return list1;
362 GSList*
363 g_slist_remove (GSList *list,
364 gconstpointer data)
366 GSList *tmp, *prev = NULL;
368 tmp = list;
369 while (tmp)
371 if (tmp->data == data)
373 if (prev)
374 prev->next = tmp->next;
375 else
376 list = tmp->next;
378 g_slist_free_1 (tmp);
379 break;
381 prev = tmp;
382 tmp = prev->next;
385 return list;
388 GSList*
389 g_slist_remove_all (GSList *list,
390 gconstpointer data)
392 GSList *tmp, *prev = NULL;
394 tmp = list;
395 while (tmp)
397 if (tmp->data == data)
399 GSList *next = tmp->next;
401 if (prev)
402 prev->next = next;
403 else
404 list = next;
406 g_slist_free_1 (tmp);
407 tmp = next;
409 else
411 prev = tmp;
412 tmp = prev->next;
416 return list;
419 static inline GSList*
420 _g_slist_remove_link (GSList *list,
421 GSList *link)
423 GSList *tmp;
424 GSList *prev;
426 prev = NULL;
427 tmp = list;
429 while (tmp)
431 if (tmp == link)
433 if (prev)
434 prev->next = tmp->next;
435 if (list == tmp)
436 list = list->next;
438 tmp->next = NULL;
439 break;
442 prev = tmp;
443 tmp = tmp->next;
446 return list;
449 GSList*
450 g_slist_remove_link (GSList *list,
451 GSList *link)
453 return _g_slist_remove_link (list, link);
456 GSList*
457 g_slist_delete_link (GSList *list,
458 GSList *link)
460 list = _g_slist_remove_link (list, link);
461 _g_slist_free_1 (link);
463 return list;
466 GSList*
467 g_slist_copy (GSList *list)
469 GSList *new_list = NULL;
471 if (list)
473 GSList *last;
475 new_list = _g_slist_alloc ();
476 new_list->data = list->data;
477 last = new_list;
478 list = list->next;
479 while (list)
481 last->next = _g_slist_alloc ();
482 last = last->next;
483 last->data = list->data;
484 list = list->next;
488 return new_list;
491 GSList*
492 g_slist_reverse (GSList *list)
494 GSList *prev = NULL;
496 while (list)
498 GSList *next = list->next;
500 list->next = prev;
502 prev = list;
503 list = next;
506 return prev;
509 GSList*
510 g_slist_nth (GSList *list,
511 guint n)
513 while (n-- > 0 && list)
514 list = list->next;
516 return list;
519 gpointer
520 g_slist_nth_data (GSList *list,
521 guint n)
523 while (n-- > 0 && list)
524 list = list->next;
526 return list ? list->data : NULL;
529 GSList*
530 g_slist_find (GSList *list,
531 gconstpointer data)
533 while (list)
535 if (list->data == data)
536 break;
537 list = list->next;
540 return list;
543 GSList*
544 g_slist_find_custom (GSList *list,
545 gconstpointer data,
546 GCompareFunc func)
548 g_return_val_if_fail (func != NULL, list);
550 while (list)
552 if (! func (list->data, data))
553 return list;
554 list = list->next;
557 return NULL;
560 gint
561 g_slist_position (GSList *list,
562 GSList *link)
564 gint i;
566 i = 0;
567 while (list)
569 if (list == link)
570 return i;
571 i++;
572 list = list->next;
575 return -1;
578 gint
579 g_slist_index (GSList *list,
580 gconstpointer data)
582 gint i;
584 i = 0;
585 while (list)
587 if (list->data == data)
588 return i;
589 i++;
590 list = list->next;
593 return -1;
596 GSList*
597 g_slist_last (GSList *list)
599 if (list)
601 while (list->next)
602 list = list->next;
605 return list;
608 guint
609 g_slist_length (GSList *list)
611 guint length;
613 length = 0;
614 while (list)
616 length++;
617 list = list->next;
620 return length;
623 void
624 g_slist_foreach (GSList *list,
625 GFunc func,
626 gpointer user_data)
628 while (list)
630 GSList *next = list->next;
631 (*func) (list->data, user_data);
632 list = next;
636 GSList*
637 g_slist_insert_sorted (GSList *list,
638 gpointer data,
639 GCompareFunc func)
641 GSList *tmp_list = list;
642 GSList *prev_list = NULL;
643 GSList *new_list;
644 gint cmp;
646 g_return_val_if_fail (func != NULL, list);
648 if (!list)
650 new_list = _g_slist_alloc ();
651 new_list->data = data;
652 return new_list;
655 cmp = (*func) (data, tmp_list->data);
657 while ((tmp_list->next) && (cmp > 0))
659 prev_list = tmp_list;
660 tmp_list = tmp_list->next;
661 cmp = (*func) (data, tmp_list->data);
664 new_list = _g_slist_alloc ();
665 new_list->data = data;
667 if ((!tmp_list->next) && (cmp > 0))
669 tmp_list->next = new_list;
670 return list;
673 if (prev_list)
675 prev_list->next = new_list;
676 new_list->next = tmp_list;
677 return list;
679 else
681 new_list->next = list;
682 return new_list;
686 static GSList *
687 g_slist_sort_merge (GSList *l1,
688 GSList *l2,
689 GFunc compare_func,
690 gboolean use_data,
691 gpointer user_data)
693 GSList list, *l;
694 gint cmp;
696 l=&list;
698 while (l1 && l2)
700 if (use_data)
701 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
702 else
703 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
705 if (cmp <= 0)
707 l=l->next=l1;
708 l1=l1->next;
710 else
712 l=l->next=l2;
713 l2=l2->next;
716 l->next= l1 ? l1 : l2;
718 return list.next;
721 static GSList *
722 g_slist_sort_real (GSList *list,
723 GFunc compare_func,
724 gboolean use_data,
725 gpointer user_data)
727 GSList *l1, *l2;
729 if (!list)
730 return NULL;
731 if (!list->next)
732 return list;
734 l1 = list;
735 l2 = list->next;
737 while ((l2 = l2->next) != NULL)
739 if ((l2 = l2->next) == NULL)
740 break;
741 l1=l1->next;
743 l2 = l1->next;
744 l1->next = NULL;
746 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, use_data, user_data),
747 g_slist_sort_real (l2, compare_func, use_data, user_data),
748 compare_func,
749 use_data,
750 user_data);
753 GSList *
754 g_slist_sort (GSList *list,
755 GCompareFunc compare_func)
757 return g_slist_sort_real (list, (GFunc) compare_func, FALSE, NULL);
760 GSList *
761 g_slist_sort_with_data (GSList *list,
762 GCompareDataFunc compare_func,
763 gpointer user_data)
765 return g_slist_sort_real (list, (GFunc) compare_func, TRUE, user_data);
768 #define __G_SLIST_C__
769 #include "galiasdef.c"