Removed POSIX_*. Defined G_THREAD_SOURCE to "gthread-win32.c".
[glib.git] / glist.c
blob6d9f0ddee91c10a228fe75d3b88bcf6e02784df3
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 GList*
487 g_list_nth_prev (GList *list,
488 guint n)
490 while ((n-- > 0) && list)
491 list = list->prev;
493 return list;
496 gpointer
497 g_list_nth_data (GList *list,
498 guint n)
500 while ((n-- > 0) && list)
501 list = list->next;
503 return list ? list->data : NULL;
506 GList*
507 g_list_find (GList *list,
508 gconstpointer data)
510 while (list)
512 if (list->data == data)
513 break;
514 list = list->next;
517 return list;
520 GList*
521 g_list_find_custom (GList *list,
522 gconstpointer data,
523 GCompareFunc func)
525 g_return_val_if_fail (func != NULL, list);
527 while (list)
529 if (! func (list->data, data))
530 return list;
531 list = list->next;
534 return NULL;
538 gint
539 g_list_position (GList *list,
540 GList *link)
542 gint i;
544 i = 0;
545 while (list)
547 if (list == link)
548 return i;
549 i++;
550 list = list->next;
553 return -1;
556 gint
557 g_list_index (GList *list,
558 gconstpointer data)
560 gint i;
562 i = 0;
563 while (list)
565 if (list->data == data)
566 return i;
567 i++;
568 list = list->next;
571 return -1;
574 GList*
575 g_list_last (GList *list)
577 if (list)
579 while (list->next)
580 list = list->next;
583 return list;
586 GList*
587 g_list_first (GList *list)
589 if (list)
591 while (list->prev)
592 list = list->prev;
595 return list;
598 guint
599 g_list_length (GList *list)
601 guint length;
603 length = 0;
604 while (list)
606 length++;
607 list = list->next;
610 return length;
613 void
614 g_list_foreach (GList *list,
615 GFunc func,
616 gpointer user_data)
618 while (list)
620 GList *next = list->next;
621 (*func) (list->data, user_data);
622 list = next;
627 GList*
628 g_list_insert_sorted (GList *list,
629 gpointer data,
630 GCompareFunc func)
632 GList *tmp_list = list;
633 GList *new_list;
634 gint cmp;
636 g_return_val_if_fail (func != NULL, list);
638 if (!list)
640 new_list = _g_list_alloc ();
641 new_list->data = data;
642 return new_list;
645 cmp = (*func) (data, tmp_list->data);
647 while ((tmp_list->next) && (cmp > 0))
649 tmp_list = tmp_list->next;
650 cmp = (*func) (data, tmp_list->data);
653 new_list = _g_list_alloc ();
654 new_list->data = data;
656 if ((!tmp_list->next) && (cmp > 0))
658 tmp_list->next = new_list;
659 new_list->prev = tmp_list;
660 return list;
663 if (tmp_list->prev)
665 tmp_list->prev->next = new_list;
666 new_list->prev = tmp_list->prev;
668 new_list->next = tmp_list;
669 tmp_list->prev = new_list;
671 if (tmp_list == list)
672 return new_list;
673 else
674 return list;
677 static GList *
678 g_list_sort_merge (GList *l1,
679 GList *l2,
680 GFunc compare_func,
681 gboolean use_data,
682 gpointer user_data)
684 GList list, *l, *lprev;
685 gint cmp;
687 l = &list;
688 lprev = NULL;
690 while (l1 && l2)
692 if (use_data)
693 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
694 else
695 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
697 if (cmp <= 0)
699 l->next = l1;
700 l = l->next;
701 l->prev = lprev;
702 lprev = l;
703 l1 = l1->next;
705 else
707 l->next = l2;
708 l = l->next;
709 l->prev = lprev;
710 lprev = l;
711 l2 = l2->next;
714 l->next = l1 ? l1 : l2;
715 l->next->prev = l;
717 return list.next;
720 GList*
721 g_list_sort_real (GList *list,
722 GFunc compare_func,
723 gboolean use_data,
724 gpointer user_data)
726 GList *l1, *l2;
728 if (!list)
729 return NULL;
730 if (!list->next)
731 return list;
733 l1 = list;
734 l2 = list->next;
736 while ((l2 = l2->next) != NULL)
738 if ((l2 = l2->next) == NULL)
739 break;
740 l1 = l1->next;
742 l2 = l1->next;
743 l1->next = NULL;
745 return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
746 g_list_sort_real (l2, compare_func, use_data, user_data),
747 compare_func,
748 use_data,
749 user_data);
752 GList *
753 g_list_sort (GList *list,
754 GCompareFunc compare_func)
756 return g_list_sort_real (list, (GFunc) compare_func, FALSE, NULL);
760 GList *
761 g_list_sort_with_data (GList *list,
762 GCompareDataFunc compare_func,
763 gpointer user_data)
765 return g_list_sort_real (list, (GFunc) compare_func, TRUE, user_data);
768 GList*
769 g_list_sort2 (GList *list,
770 GCompareFunc compare_func)
772 GSList *runs = NULL;
773 GList *tmp;
775 /* Degenerate case. */
776 if (!list) return NULL;
778 /* Assume: list = [12,2,4,11,2,4,6,1,1,12]. */
779 for (tmp = list; tmp; )
781 GList *tmp2;
782 for (tmp2 = tmp;
783 tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
784 tmp2 = tmp2->next)
785 /* Nothing */;
786 runs = g_slist_append (runs, tmp);
787 tmp = tmp2->next;
788 tmp2->next = NULL;
790 /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]]. */
792 while (runs->next)
794 /* We have more than one run. Merge pairwise. */
795 GSList *dst, *src, *dstprev = NULL;
796 dst = src = runs;
797 while (src && src->next)
799 dst->data = g_list_sort_merge (src->data,
800 src->next->data,
801 (GFunc) compare_func,
802 FALSE, NULL);
803 dstprev = dst;
804 dst = dst->next;
805 src = src->next->next;
808 /* If number of runs was odd, just keep the last. */
809 if (src)
811 dst->data = src->data;
812 dstprev = dst;
813 dst = dst->next;
816 dstprev->next = NULL;
817 g_slist_free (dst);
820 /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]]. */
821 /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]]. */
823 list = runs->data;
824 g_slist_free (runs);
825 return list;