ca37b8da933948c5fee1b04acfe45bc9077786cd
[platform/upstream/glib.git] / glib / 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 GList*
318 g_list_copy (GList *list)
319 {
320   GList *new_list = NULL;
321
322   if (list)
323     {
324       GList *last;
325
326       new_list = g_list_alloc ();
327       new_list->data = list->data;
328       last = new_list;
329       list = list->next;
330       while (list)
331         {
332           last->next = g_list_alloc ();
333           last->next->prev = last;
334           last = last->next;
335           last->data = list->data;
336           list = list->next;
337         }
338     }
339
340   return new_list;
341 }
342
343 GList*
344 g_list_reverse (GList *list)
345 {
346   GList *last;
347   
348   last = NULL;
349   while (list)
350     {
351       last = list;
352       list = last->next;
353       last->next = last->prev;
354       last->prev = list;
355     }
356   
357   return last;
358 }
359
360 GList*
361 g_list_nth (GList *list,
362             guint  n)
363 {
364   while ((n-- > 0) && list)
365     list = list->next;
366   
367   return list;
368 }
369
370 gpointer
371 g_list_nth_data (GList     *list,
372                  guint      n)
373 {
374   while ((n-- > 0) && list)
375     list = list->next;
376   
377   return list ? list->data : NULL;
378 }
379
380 GList*
381 g_list_find (GList    *list,
382              gpointer  data)
383 {
384   while (list)
385     {
386       if (list->data == data)
387         break;
388       list = list->next;
389     }
390   
391   return list;
392 }
393
394 GList*
395 g_list_find_custom (GList       *list,
396                     gpointer     data,
397                     GCompareFunc func)
398 {
399   g_return_val_if_fail (func != NULL, list);
400
401   while (list)
402     {
403       if (! func (list->data, data))
404         return list;
405       list = list->next;
406     }
407
408   return NULL;
409 }
410
411
412 gint
413 g_list_position (GList *list,
414                  GList *link)
415 {
416   gint i;
417
418   i = 0;
419   while (list)
420     {
421       if (list == link)
422         return i;
423       i++;
424       list = list->next;
425     }
426
427   return -1;
428 }
429
430 gint
431 g_list_index (GList   *list,
432               gpointer data)
433 {
434   gint i;
435
436   i = 0;
437   while (list)
438     {
439       if (list->data == data)
440         return i;
441       i++;
442       list = list->next;
443     }
444
445   return -1;
446 }
447
448 GList*
449 g_list_last (GList *list)
450 {
451   if (list)
452     {
453       while (list->next)
454         list = list->next;
455     }
456   
457   return list;
458 }
459
460 GList*
461 g_list_first (GList *list)
462 {
463   if (list)
464     {
465       while (list->prev)
466         list = list->prev;
467     }
468   
469   return list;
470 }
471
472 guint
473 g_list_length (GList *list)
474 {
475   guint length;
476   
477   length = 0;
478   while (list)
479     {
480       length++;
481       list = list->next;
482     }
483   
484   return length;
485 }
486
487 void
488 g_list_foreach (GList    *list,
489                 GFunc     func,
490                 gpointer  user_data)
491 {
492   while (list)
493     {
494       (*func) (list->data, user_data);
495       list = list->next;
496     }
497 }
498
499
500 GList*
501 g_list_insert_sorted (GList        *list,
502                       gpointer      data,
503                       GCompareFunc  func)
504 {
505   GList *tmp_list = list;
506   GList *new_list;
507   gint cmp;
508
509   g_return_val_if_fail (func != NULL, list);
510   
511   if (!list) 
512     {
513       new_list = g_list_alloc();
514       new_list->data = data;
515       return new_list;
516     }
517   
518   cmp = (*func) (data, tmp_list->data);
519   
520   while ((tmp_list->next) && (cmp > 0))
521     {
522       tmp_list = tmp_list->next;
523       cmp = (*func) (data, tmp_list->data);
524     }
525
526   new_list = g_list_alloc();
527   new_list->data = data;
528
529   if ((!tmp_list->next) && (cmp > 0))
530     {
531       tmp_list->next = new_list;
532       new_list->prev = tmp_list;
533       return list;
534     }
535    
536   if (tmp_list->prev)
537     {
538       tmp_list->prev->next = new_list;
539       new_list->prev = tmp_list->prev;
540     }
541   new_list->next = tmp_list;
542   tmp_list->prev = new_list;
543  
544   if (tmp_list == list)
545     return new_list;
546   else
547     return list;
548 }
549
550 static GList *
551 g_list_sort_merge (GList       *l1, 
552                    GList       *l2,
553                    GCompareFunc compare_func)
554 {
555   GList list, *l, *lprev;
556
557   l = &list; 
558   lprev = NULL;
559
560   while (l1 && l2)
561     {
562       if (compare_func (l1->data, l2->data) < 0)
563         {
564           l->next = l1;
565           l = l->next;
566           l->prev = lprev; 
567           lprev = l;
568           l1 = l1->next;
569         } 
570       else 
571         {
572           l->next = l2;
573           l = l->next;
574           l->prev = lprev; 
575           lprev = l;
576           l2 = l2->next;
577         }
578     }
579   l->next = l1 ? l1 : l2;
580   l->next->prev = l;
581
582   return list.next;
583 }
584
585 GList* 
586 g_list_sort (GList       *list,
587              GCompareFunc compare_func)
588 {
589   GList *l1, *l2;
590   
591   if (!list) 
592     return NULL;
593   if (!list->next) 
594     return list;
595   
596   l1 = list; 
597   l2 = list->next;
598
599   while ((l2 = l2->next) != NULL)
600     {
601       if ((l2 = l2->next) == NULL) 
602         break;
603       l1 = l1->next;
604     }
605   l2 = l1->next; 
606   l1->next = NULL; 
607
608   return g_list_sort_merge (g_list_sort (list, compare_func),
609                             g_list_sort (l2,   compare_func),
610                             compare_func);
611 }
612
613 GList* 
614 g_list_sort2 (GList       *list,
615               GCompareFunc compare_func)
616 {
617   GSList *runs = NULL;
618   GList *tmp;
619
620   /* Degenerate case.  */
621   if (!list) return NULL;
622
623   /* Assume: list = [12,2,4,11,2,4,6,1,1,12].  */
624   for (tmp = list; tmp; )
625     {
626       GList *tmp2;
627       for (tmp2 = tmp;
628            tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
629            tmp2 = tmp2->next)
630         /* Nothing */;
631       runs = g_slist_append (runs, tmp);
632       tmp = tmp2->next;
633       tmp2->next = NULL;
634     }
635   /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]].  */
636   
637   while (runs->next)
638     {
639       /* We have more than one run.  Merge pairwise.  */
640       GSList *dst, *src, *dstprev = NULL;
641       dst = src = runs;
642       while (src && src->next)
643         {
644           dst->data = g_list_sort_merge (src->data,
645                                          src->next->data,
646                                          compare_func);
647           dstprev = dst;
648           dst = dst->next;
649           src = src->next->next;
650         }
651
652       /* If number of runs was odd, just keep the last.  */
653       if (src)
654         {
655           dst->data = src->data;
656           dstprev = dst;
657           dst = dst->next;
658         }
659
660       dstprev->next = NULL;
661       g_slist_free (dst);
662     }
663
664   /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]].  */
665   /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]].  */
666
667   list = runs->data;
668   g_slist_free (runs);
669   return list;
670 }