Correct rules for making the win32-related files that are made from
[platform/upstream/glib.git] / glist.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "glib.h"
32
33
34 struct _GAllocator /* from gmem.c */
35 {
36   gchar         *name;
37   guint16        n_preallocs;
38   guint          is_unused : 1;
39   guint          type : 4;
40   GAllocator    *last;
41   GMemChunk     *mem_chunk;
42   GList         *free_lists; /* implementation specific */
43 };
44
45 static GAllocator       *current_allocator = NULL;
46 G_LOCK_DEFINE_STATIC (current_allocator);
47
48 /* HOLDS: current_allocator_lock */
49 static void
50 g_list_validate_allocator (GAllocator *allocator)
51 {
52   g_return_if_fail (allocator != NULL);
53   g_return_if_fail (allocator->is_unused == TRUE);
54
55   if (allocator->type != G_ALLOCATOR_LIST)
56     {
57       allocator->type = G_ALLOCATOR_LIST;
58       if (allocator->mem_chunk)
59         {
60           g_mem_chunk_destroy (allocator->mem_chunk);
61           allocator->mem_chunk = NULL;
62         }
63     }
64
65   if (!allocator->mem_chunk)
66     {
67       allocator->mem_chunk = g_mem_chunk_new (allocator->name,
68                                               sizeof (GList),
69                                               sizeof (GList) * allocator->n_preallocs,
70                                               G_ALLOC_ONLY);
71       allocator->free_lists = NULL;
72     }
73
74   allocator->is_unused = FALSE;
75 }
76
77 void
78 g_list_push_allocator(GAllocator *allocator)
79 {
80   G_LOCK (current_allocator);
81   g_list_validate_allocator ( allocator );
82   allocator->last = current_allocator;
83   current_allocator = allocator;
84   G_UNLOCK (current_allocator);
85 }
86
87 void
88 g_list_pop_allocator (void)
89 {
90   G_LOCK (current_allocator);
91   if (current_allocator)
92     {
93       GAllocator *allocator;
94
95       allocator = current_allocator;
96       current_allocator = allocator->last;
97       allocator->last = NULL;
98       allocator->is_unused = TRUE;
99     }
100   G_UNLOCK (current_allocator);
101 }
102
103 GList*
104 g_list_alloc (void)
105 {
106   GList *list;
107
108   G_LOCK (current_allocator);
109   if (!current_allocator)
110     {
111       GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
112                                                128);
113       g_list_validate_allocator (allocator);
114       allocator->last = NULL;
115       current_allocator = allocator;
116     }
117   if (!current_allocator->free_lists)
118     {
119       list = g_chunk_new (GList, current_allocator->mem_chunk);
120       list->data = NULL;
121     }
122   else
123     {
124       if (current_allocator->free_lists->data)
125         {
126           list = current_allocator->free_lists->data;
127           current_allocator->free_lists->data = list->next;
128           list->data = NULL;
129         }
130       else
131         {
132           list = current_allocator->free_lists;
133           current_allocator->free_lists = list->next;
134         }
135     }
136   G_UNLOCK (current_allocator);
137   list->next = NULL;
138   list->prev = NULL;
139   
140   return list;
141 }
142
143 void
144 g_list_free (GList *list)
145 {
146   if (list)
147     {
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);
153     }
154 }
155
156 void
157 g_list_free_1 (GList *list)
158 {
159   if (list)
160     {
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);
166     }
167 }
168
169 GList*
170 g_list_append (GList    *list,
171                gpointer  data)
172 {
173   GList *new_list;
174   GList *last;
175   
176   new_list = g_list_alloc ();
177   new_list->data = data;
178   
179   if (list)
180     {
181       last = g_list_last (list);
182       /* g_assert (last != NULL); */
183       last->next = new_list;
184       new_list->prev = last;
185
186       return list;
187     }
188   else
189     return new_list;
190 }
191
192 GList*
193 g_list_prepend (GList    *list,
194                 gpointer  data)
195 {
196   GList *new_list;
197   
198   new_list = g_list_alloc ();
199   new_list->data = data;
200   
201   if (list)
202     {
203       if (list->prev)
204         {
205           list->prev->next = new_list;
206           new_list->prev = list->prev;
207         }
208       list->prev = new_list;
209       new_list->next = list;
210     }
211   
212   return new_list;
213 }
214
215 GList*
216 g_list_insert (GList    *list,
217                gpointer  data,
218                gint      position)
219 {
220   GList *new_list;
221   GList *tmp_list;
222   
223   if (position < 0)
224     return g_list_append (list, data);
225   else if (position == 0)
226     return g_list_prepend (list, data);
227   
228   tmp_list = g_list_nth (list, position);
229   if (!tmp_list)
230     return g_list_append (list, data);
231   
232   new_list = g_list_alloc ();
233   new_list->data = data;
234   
235   if (tmp_list->prev)
236     {
237       tmp_list->prev->next = new_list;
238       new_list->prev = tmp_list->prev;
239     }
240   new_list->next = tmp_list;
241   tmp_list->prev = new_list;
242   
243   if (tmp_list == list)
244     return new_list;
245   else
246     return list;
247 }
248
249 GList *
250 g_list_concat (GList *list1, GList *list2)
251 {
252   GList *tmp_list;
253   
254   if (list2)
255     {
256       tmp_list = g_list_last (list1);
257       if (tmp_list)
258         tmp_list->next = list2;
259       else
260         list1 = list2;
261       list2->prev = tmp_list;
262     }
263   
264   return list1;
265 }
266
267 GList*
268 g_list_remove (GList    *list,
269                gpointer  data)
270 {
271   GList *tmp;
272   
273   tmp = list;
274   while (tmp)
275     {
276       if (tmp->data != data)
277         tmp = tmp->next;
278       else
279         {
280           if (tmp->prev)
281             tmp->prev->next = tmp->next;
282           if (tmp->next)
283             tmp->next->prev = tmp->prev;
284           
285           if (list == tmp)
286             list = list->next;
287           
288           g_list_free_1 (tmp);
289           
290           break;
291         }
292     }
293   return list;
294 }
295
296 GList*
297 g_list_remove_link (GList *list,
298                     GList *link)
299 {
300   if (link)
301     {
302       if (link->prev)
303         link->prev->next = link->next;
304       if (link->next)
305         link->next->prev = link->prev;
306       
307       if (link == list)
308         list = list->next;
309       
310       link->next = NULL;
311       link->prev = NULL;
312     }
313   
314   return list;
315 }
316
317
318 GList*
319 g_list_delete (GList *list, GList *link)
320 {
321   list = g_list_remove_link (list, link);
322   g_list_free_1 (link);
323
324   return list;
325 }
326
327
328 GList*
329 g_list_copy (GList *list)
330 {
331   GList *new_list = NULL;
332
333   if (list)
334     {
335       GList *last;
336
337       new_list = g_list_alloc ();
338       new_list->data = list->data;
339       last = new_list;
340       list = list->next;
341       while (list)
342         {
343           last->next = g_list_alloc ();
344           last->next->prev = last;
345           last = last->next;
346           last->data = list->data;
347           list = list->next;
348         }
349     }
350
351   return new_list;
352 }
353
354 GList*
355 g_list_reverse (GList *list)
356 {
357   GList *last;
358   
359   last = NULL;
360   while (list)
361     {
362       last = list;
363       list = last->next;
364       last->next = last->prev;
365       last->prev = list;
366     }
367   
368   return last;
369 }
370
371 GList*
372 g_list_nth (GList *list,
373             guint  n)
374 {
375   while ((n-- > 0) && list)
376     list = list->next;
377   
378   return list;
379 }
380
381 gpointer
382 g_list_nth_data (GList     *list,
383                  guint      n)
384 {
385   while ((n-- > 0) && list)
386     list = list->next;
387   
388   return list ? list->data : NULL;
389 }
390
391 GList*
392 g_list_find (GList    *list,
393              gpointer  data)
394 {
395   while (list)
396     {
397       if (list->data == data)
398         break;
399       list = list->next;
400     }
401   
402   return list;
403 }
404
405 GList*
406 g_list_find_custom (GList       *list,
407                     gpointer     data,
408                     GCompareFunc func)
409 {
410   g_return_val_if_fail (func != NULL, list);
411
412   while (list)
413     {
414       if (! func (list->data, data))
415         return list;
416       list = list->next;
417     }
418
419   return NULL;
420 }
421
422
423 gint
424 g_list_position (GList *list,
425                  GList *link)
426 {
427   gint i;
428
429   i = 0;
430   while (list)
431     {
432       if (list == link)
433         return i;
434       i++;
435       list = list->next;
436     }
437
438   return -1;
439 }
440
441 gint
442 g_list_index (GList   *list,
443               gpointer data)
444 {
445   gint i;
446
447   i = 0;
448   while (list)
449     {
450       if (list->data == data)
451         return i;
452       i++;
453       list = list->next;
454     }
455
456   return -1;
457 }
458
459 GList*
460 g_list_last (GList *list)
461 {
462   if (list)
463     {
464       while (list->next)
465         list = list->next;
466     }
467   
468   return list;
469 }
470
471 GList*
472 g_list_first (GList *list)
473 {
474   if (list)
475     {
476       while (list->prev)
477         list = list->prev;
478     }
479   
480   return list;
481 }
482
483 guint
484 g_list_length (GList *list)
485 {
486   guint length;
487   
488   length = 0;
489   while (list)
490     {
491       length++;
492       list = list->next;
493     }
494   
495   return length;
496 }
497
498 void
499 g_list_foreach (GList    *list,
500                 GFunc     func,
501                 gpointer  user_data)
502 {
503   while (list)
504     {
505       (*func) (list->data, user_data);
506       list = list->next;
507     }
508 }
509
510
511 GList*
512 g_list_insert_sorted (GList        *list,
513                       gpointer      data,
514                       GCompareFunc  func)
515 {
516   GList *tmp_list = list;
517   GList *new_list;
518   gint cmp;
519
520   g_return_val_if_fail (func != NULL, list);
521   
522   if (!list) 
523     {
524       new_list = g_list_alloc();
525       new_list->data = data;
526       return new_list;
527     }
528   
529   cmp = (*func) (data, tmp_list->data);
530   
531   while ((tmp_list->next) && (cmp > 0))
532     {
533       tmp_list = tmp_list->next;
534       cmp = (*func) (data, tmp_list->data);
535     }
536
537   new_list = g_list_alloc();
538   new_list->data = data;
539
540   if ((!tmp_list->next) && (cmp > 0))
541     {
542       tmp_list->next = new_list;
543       new_list->prev = tmp_list;
544       return list;
545     }
546    
547   if (tmp_list->prev)
548     {
549       tmp_list->prev->next = new_list;
550       new_list->prev = tmp_list->prev;
551     }
552   new_list->next = tmp_list;
553   tmp_list->prev = new_list;
554  
555   if (tmp_list == list)
556     return new_list;
557   else
558     return list;
559 }
560
561 static GList *
562 g_list_sort_merge (GList       *l1, 
563                    GList       *l2,
564                    GCompareFunc compare_func)
565 {
566   GList list, *l, *lprev;
567
568   l = &list; 
569   lprev = NULL;
570
571   while (l1 && l2)
572     {
573       if (compare_func (l1->data, l2->data) < 0)
574         {
575           l->next = l1;
576           l = l->next;
577           l->prev = lprev; 
578           lprev = l;
579           l1 = l1->next;
580         } 
581       else 
582         {
583           l->next = l2;
584           l = l->next;
585           l->prev = lprev; 
586           lprev = l;
587           l2 = l2->next;
588         }
589     }
590   l->next = l1 ? l1 : l2;
591   l->next->prev = l;
592
593   return list.next;
594 }
595
596 GList* 
597 g_list_sort (GList       *list,
598              GCompareFunc compare_func)
599 {
600   GList *l1, *l2;
601   
602   if (!list) 
603     return NULL;
604   if (!list->next) 
605     return list;
606   
607   l1 = list; 
608   l2 = list->next;
609
610   while ((l2 = l2->next) != NULL)
611     {
612       if ((l2 = l2->next) == NULL) 
613         break;
614       l1 = l1->next;
615     }
616   l2 = l1->next; 
617   l1->next = NULL; 
618
619   return g_list_sort_merge (g_list_sort (list, compare_func),
620                             g_list_sort (l2,   compare_func),
621                             compare_func);
622 }
623
624 GList* 
625 g_list_sort2 (GList       *list,
626               GCompareFunc compare_func)
627 {
628   GSList *runs = NULL;
629   GList *tmp;
630
631   /* Degenerate case.  */
632   if (!list) return NULL;
633
634   /* Assume: list = [12,2,4,11,2,4,6,1,1,12].  */
635   for (tmp = list; tmp; )
636     {
637       GList *tmp2;
638       for (tmp2 = tmp;
639            tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
640            tmp2 = tmp2->next)
641         /* Nothing */;
642       runs = g_slist_append (runs, tmp);
643       tmp = tmp2->next;
644       tmp2->next = NULL;
645     }
646   /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]].  */
647   
648   while (runs->next)
649     {
650       /* We have more than one run.  Merge pairwise.  */
651       GSList *dst, *src, *dstprev = NULL;
652       dst = src = runs;
653       while (src && src->next)
654         {
655           dst->data = g_list_sort_merge (src->data,
656                                          src->next->data,
657                                          compare_func);
658           dstprev = dst;
659           dst = dst->next;
660           src = src->next->next;
661         }
662
663       /* If number of runs was odd, just keep the last.  */
664       if (src)
665         {
666           dst->data = src->data;
667           dstprev = dst;
668           dst = dst->next;
669         }
670
671       dstprev->next = NULL;
672       g_slist_free (dst);
673     }
674
675   /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]].  */
676   /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]].  */
677
678   list = runs->data;
679   g_slist_free (runs);
680   return list;
681 }