Add --deprecated-guards
[glib.git] / glist.c
blob3d24bd2a6094f2f3638074de2273c0cefe02a6d3
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 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "glib.h"
38 #ifndef DISABLE_MEM_POOLS
39 struct _GAllocator /* from gmem.c */
41 gchar *name;
42 guint16 n_preallocs;
43 guint is_unused : 1;
44 guint type : 4;
45 GAllocator *last;
46 GMemChunk *mem_chunk;
47 GList *free_lists; /* implementation specific */
50 static GAllocator *current_allocator = NULL;
51 G_LOCK_DEFINE_STATIC (current_allocator);
53 /* HOLDS: current_allocator_lock */
54 static void
55 g_list_validate_allocator (GAllocator *allocator)
57 g_return_if_fail (allocator != NULL);
58 g_return_if_fail (allocator->is_unused == TRUE);
60 if (allocator->type != G_ALLOCATOR_LIST)
62 allocator->type = G_ALLOCATOR_LIST;
63 if (allocator->mem_chunk)
65 g_mem_chunk_destroy (allocator->mem_chunk);
66 allocator->mem_chunk = NULL;
70 if (!allocator->mem_chunk)
72 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
73 sizeof (GList),
74 sizeof (GList) * allocator->n_preallocs,
75 G_ALLOC_ONLY);
76 allocator->free_lists = NULL;
79 allocator->is_unused = FALSE;
82 void
83 g_list_push_allocator(GAllocator *allocator)
85 G_LOCK (current_allocator);
86 g_list_validate_allocator (allocator);
87 allocator->last = current_allocator;
88 current_allocator = allocator;
89 G_UNLOCK (current_allocator);
92 void
93 g_list_pop_allocator (void)
95 G_LOCK (current_allocator);
96 if (current_allocator)
98 GAllocator *allocator;
100 allocator = current_allocator;
101 current_allocator = allocator->last;
102 allocator->last = NULL;
103 allocator->is_unused = TRUE;
105 G_UNLOCK (current_allocator);
108 static inline GList*
109 _g_list_alloc (void)
111 GList *list;
113 G_LOCK (current_allocator);
114 if (!current_allocator)
116 GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
117 128);
118 g_list_validate_allocator (allocator);
119 allocator->last = NULL;
120 current_allocator = allocator;
122 if (!current_allocator->free_lists)
124 list = g_chunk_new (GList, current_allocator->mem_chunk);
125 list->data = NULL;
127 else
129 if (current_allocator->free_lists->data)
131 list = current_allocator->free_lists->data;
132 current_allocator->free_lists->data = list->next;
133 list->data = NULL;
135 else
137 list = current_allocator->free_lists;
138 current_allocator->free_lists = list->next;
141 G_UNLOCK (current_allocator);
142 list->next = NULL;
143 list->prev = NULL;
145 return list;
148 GList*
149 g_list_alloc (void)
151 return _g_list_alloc ();
154 void
155 g_list_free (GList *list)
157 if (list)
159 GList *last_node = list;
161 #ifdef ENABLE_GC_FRIENDLY
162 while (last_node->next)
164 last_node->data = NULL;
165 last_node->prev = NULL;
166 last_node = last_node->next;
168 last_node->data = NULL;
169 last_node->prev = NULL;
170 #else /* !ENABLE_GC_FRIENDLY */
171 list->data = list->next;
172 #endif /* ENABLE_GC_FRIENDLY */
174 G_LOCK (current_allocator);
175 last_node->next = current_allocator->free_lists;
176 current_allocator->free_lists = list;
177 G_UNLOCK (current_allocator);
181 static inline void
182 _g_list_free_1 (GList *list)
184 if (list)
186 list->data = NULL;
188 #ifdef ENABLE_GC_FRIENDLY
189 list->prev = NULL;
190 #endif /* ENABLE_GC_FRIENDLY */
192 G_LOCK (current_allocator);
193 list->next = current_allocator->free_lists;
194 current_allocator->free_lists = list;
195 G_UNLOCK (current_allocator);
199 void
200 g_list_free_1 (GList *list)
202 _g_list_free_1 (list);
205 #else /* DISABLE_MEM_POOLS */
207 #define _g_list_alloc g_list_alloc
208 GList*
209 g_list_alloc (void)
211 GList *list;
213 list = g_new0 (GList, 1);
215 return list;
218 void
219 g_list_free (GList *list)
221 GList *last;
223 while (list)
225 last = list;
226 list = list->next;
227 g_free (last);
231 #define _g_list_free_1 g_list_free_1
232 void
233 g_list_free_1 (GList *list)
235 g_free (list);
238 #endif
240 GList*
241 g_list_append (GList *list,
242 gpointer data)
244 GList *new_list;
245 GList *last;
247 new_list = _g_list_alloc ();
248 new_list->data = data;
250 if (list)
252 last = g_list_last (list);
253 /* g_assert (last != NULL); */
254 last->next = new_list;
255 new_list->prev = last;
257 return list;
259 else
260 return new_list;
263 GList*
264 g_list_prepend (GList *list,
265 gpointer data)
267 GList *new_list;
269 new_list = _g_list_alloc ();
270 new_list->data = data;
272 if (list)
274 if (list->prev)
276 list->prev->next = new_list;
277 new_list->prev = list->prev;
279 list->prev = new_list;
280 new_list->next = list;
283 return new_list;
286 GList*
287 g_list_insert (GList *list,
288 gpointer data,
289 gint position)
291 GList *new_list;
292 GList *tmp_list;
294 if (position < 0)
295 return g_list_append (list, data);
296 else if (position == 0)
297 return g_list_prepend (list, data);
299 tmp_list = g_list_nth (list, position);
300 if (!tmp_list)
301 return g_list_append (list, data);
303 new_list = _g_list_alloc ();
304 new_list->data = data;
306 if (tmp_list->prev)
308 tmp_list->prev->next = new_list;
309 new_list->prev = tmp_list->prev;
311 new_list->next = tmp_list;
312 tmp_list->prev = new_list;
314 if (tmp_list == list)
315 return new_list;
316 else
317 return list;
320 GList *
321 g_list_concat (GList *list1, GList *list2)
323 GList *tmp_list;
325 if (list2)
327 tmp_list = g_list_last (list1);
328 if (tmp_list)
329 tmp_list->next = list2;
330 else
331 list1 = list2;
332 list2->prev = tmp_list;
335 return list1;
338 GList*
339 g_list_remove (GList *list,
340 gconstpointer data)
342 GList *tmp;
344 tmp = list;
345 while (tmp)
347 if (tmp->data != data)
348 tmp = tmp->next;
349 else
351 if (tmp->prev)
352 tmp->prev->next = tmp->next;
353 if (tmp->next)
354 tmp->next->prev = tmp->prev;
356 if (list == tmp)
357 list = list->next;
359 _g_list_free_1 (tmp);
361 break;
364 return list;
367 GList*
368 g_list_remove_all (GList *list,
369 gconstpointer data)
371 GList *tmp = list;
373 while (tmp)
375 if (tmp->data != data)
376 tmp = tmp->next;
377 else
379 GList *next = tmp->next;
381 if (tmp->prev)
382 tmp->prev->next = next;
383 else
384 list = next;
385 if (next)
386 next->prev = tmp->prev;
388 _g_list_free_1 (tmp);
389 tmp = next;
392 return list;
395 static inline GList*
396 _g_list_remove_link (GList *list,
397 GList *link)
399 if (link)
401 if (link->prev)
402 link->prev->next = link->next;
403 if (link->next)
404 link->next->prev = link->prev;
406 if (link == list)
407 list = list->next;
409 link->next = NULL;
410 link->prev = NULL;
413 return list;
416 GList*
417 g_list_remove_link (GList *list,
418 GList *link)
420 return _g_list_remove_link (list, link);
423 GList*
424 g_list_delete_link (GList *list,
425 GList *link)
427 list = _g_list_remove_link (list, link);
428 _g_list_free_1 (link);
430 return list;
433 GList*
434 g_list_copy (GList *list)
436 GList *new_list = NULL;
438 if (list)
440 GList *last;
442 new_list = _g_list_alloc ();
443 new_list->data = list->data;
444 last = new_list;
445 list = list->next;
446 while (list)
448 last->next = _g_list_alloc ();
449 last->next->prev = last;
450 last = last->next;
451 last->data = list->data;
452 list = list->next;
456 return new_list;
459 GList*
460 g_list_reverse (GList *list)
462 GList *last;
464 last = NULL;
465 while (list)
467 last = list;
468 list = last->next;
469 last->next = last->prev;
470 last->prev = list;
473 return last;
476 GList*
477 g_list_nth (GList *list,
478 guint n)
480 while ((n-- > 0) && list)
481 list = list->next;
483 return list;
486 gpointer
487 g_list_nth_data (GList *list,
488 guint n)
490 while ((n-- > 0) && list)
491 list = list->next;
493 return list ? list->data : NULL;
496 GList*
497 g_list_find (GList *list,
498 gconstpointer data)
500 while (list)
502 if (list->data == data)
503 break;
504 list = list->next;
507 return list;
510 GList*
511 g_list_find_custom (GList *list,
512 gconstpointer data,
513 GCompareFunc func)
515 g_return_val_if_fail (func != NULL, list);
517 while (list)
519 if (! func (list->data, data))
520 return list;
521 list = list->next;
524 return NULL;
528 gint
529 g_list_position (GList *list,
530 GList *link)
532 gint i;
534 i = 0;
535 while (list)
537 if (list == link)
538 return i;
539 i++;
540 list = list->next;
543 return -1;
546 gint
547 g_list_index (GList *list,
548 gconstpointer data)
550 gint i;
552 i = 0;
553 while (list)
555 if (list->data == data)
556 return i;
557 i++;
558 list = list->next;
561 return -1;
564 GList*
565 g_list_last (GList *list)
567 if (list)
569 while (list->next)
570 list = list->next;
573 return list;
576 GList*
577 g_list_first (GList *list)
579 if (list)
581 while (list->prev)
582 list = list->prev;
585 return list;
588 guint
589 g_list_length (GList *list)
591 guint length;
593 length = 0;
594 while (list)
596 length++;
597 list = list->next;
600 return length;
603 void
604 g_list_foreach (GList *list,
605 GFunc func,
606 gpointer user_data)
608 while (list)
610 (*func) (list->data, user_data);
611 list = list->next;
616 GList*
617 g_list_insert_sorted (GList *list,
618 gpointer data,
619 GCompareFunc func)
621 GList *tmp_list = list;
622 GList *new_list;
623 gint cmp;
625 g_return_val_if_fail (func != NULL, list);
627 if (!list)
629 new_list = _g_list_alloc ();
630 new_list->data = data;
631 return new_list;
634 cmp = (*func) (data, tmp_list->data);
636 while ((tmp_list->next) && (cmp > 0))
638 tmp_list = tmp_list->next;
639 cmp = (*func) (data, tmp_list->data);
642 new_list = _g_list_alloc ();
643 new_list->data = data;
645 if ((!tmp_list->next) && (cmp > 0))
647 tmp_list->next = new_list;
648 new_list->prev = tmp_list;
649 return list;
652 if (tmp_list->prev)
654 tmp_list->prev->next = new_list;
655 new_list->prev = tmp_list->prev;
657 new_list->next = tmp_list;
658 tmp_list->prev = new_list;
660 if (tmp_list == list)
661 return new_list;
662 else
663 return list;
666 static GList *
667 g_list_sort_merge (GList *l1,
668 GList *l2,
669 GFunc compare_func,
670 gboolean use_data,
671 gpointer user_data)
673 GList list, *l, *lprev;
674 gint cmp;
676 l = &list;
677 lprev = NULL;
679 while (l1 && l2)
681 if (use_data)
682 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
683 else
684 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
686 if (cmp <= 0)
688 l->next = l1;
689 l = l->next;
690 l->prev = lprev;
691 lprev = l;
692 l1 = l1->next;
694 else
696 l->next = l2;
697 l = l->next;
698 l->prev = lprev;
699 lprev = l;
700 l2 = l2->next;
703 l->next = l1 ? l1 : l2;
704 l->next->prev = l;
706 return list.next;
709 GList*
710 g_list_sort_real (GList *list,
711 GFunc compare_func,
712 gboolean use_data,
713 gpointer user_data)
715 GList *l1, *l2;
717 if (!list)
718 return NULL;
719 if (!list->next)
720 return list;
722 l1 = list;
723 l2 = list->next;
725 while ((l2 = l2->next) != NULL)
727 if ((l2 = l2->next) == NULL)
728 break;
729 l1 = l1->next;
731 l2 = l1->next;
732 l1->next = NULL;
734 return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
735 g_list_sort_real (l2, compare_func, use_data, user_data),
736 compare_func,
737 use_data,
738 user_data);
741 GList *
742 g_list_sort (GList *list,
743 GCompareFunc compare_func)
745 return g_list_sort_real (list, (GFunc) compare_func, FALSE, NULL);
749 GList *
750 g_list_sort_with_data (GList *list,
751 GCompareDataFunc compare_func,
752 gpointer user_data)
754 return g_list_sort_real (list, (GFunc) compare_func, TRUE, user_data);
757 GList*
758 g_list_sort2 (GList *list,
759 GCompareFunc compare_func)
761 GSList *runs = NULL;
762 GList *tmp;
764 /* Degenerate case. */
765 if (!list) return NULL;
767 /* Assume: list = [12,2,4,11,2,4,6,1,1,12]. */
768 for (tmp = list; tmp; )
770 GList *tmp2;
771 for (tmp2 = tmp;
772 tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
773 tmp2 = tmp2->next)
774 /* Nothing */;
775 runs = g_slist_append (runs, tmp);
776 tmp = tmp2->next;
777 tmp2->next = NULL;
779 /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]]. */
781 while (runs->next)
783 /* We have more than one run. Merge pairwise. */
784 GSList *dst, *src, *dstprev = NULL;
785 dst = src = runs;
786 while (src && src->next)
788 dst->data = g_list_sort_merge (src->data,
789 src->next->data,
790 (GFunc) compare_func,
791 FALSE, NULL);
792 dstprev = dst;
793 dst = dst->next;
794 src = src->next->next;
797 /* If number of runs was odd, just keep the last. */
798 if (src)
800 dst->data = src->data;
801 dstprev = dst;
802 dst = dst->next;
805 dstprev->next = NULL;
806 g_slist_free (dst);
809 /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]]. */
810 /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]]. */
812 list = runs->data;
813 g_slist_free (runs);
814 return list;