revert accidentally commited change
[glib.git] / gslist.c
blobdaa1822783ddacaba429d25c9ef05eadf0a5c7b2
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 "glib.h"
34 struct _GAllocator /* from gmem.c */
36 gchar *name;
37 guint16 n_preallocs;
38 guint is_unused : 1;
39 guint type : 4;
40 GAllocator *last;
41 GMemChunk *mem_chunk;
42 GSList *free_lists; /* implementation specific */
45 G_LOCK_DEFINE_STATIC (current_allocator);
46 static GAllocator *current_allocator = NULL;
48 /* HOLDS: current_allocator_lock */
49 static void
50 g_slist_validate_allocator (GAllocator *allocator)
52 g_return_if_fail (allocator != NULL);
53 g_return_if_fail (allocator->is_unused == TRUE);
55 if (allocator->type != G_ALLOCATOR_SLIST)
57 allocator->type = G_ALLOCATOR_SLIST;
58 if (allocator->mem_chunk)
60 g_mem_chunk_destroy (allocator->mem_chunk);
61 allocator->mem_chunk = NULL;
65 if (!allocator->mem_chunk)
67 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
68 sizeof (GSList),
69 sizeof (GSList) * allocator->n_preallocs,
70 G_ALLOC_ONLY);
71 allocator->free_lists = NULL;
74 allocator->is_unused = FALSE;
77 void
78 g_slist_push_allocator (GAllocator *allocator)
80 G_LOCK (current_allocator);
81 g_slist_validate_allocator (allocator);
82 allocator->last = current_allocator;
83 current_allocator = allocator;
84 G_UNLOCK (current_allocator);
87 void
88 g_slist_pop_allocator (void)
90 G_LOCK (current_allocator);
91 if (current_allocator)
93 GAllocator *allocator;
95 allocator = current_allocator;
96 current_allocator = allocator->last;
97 allocator->last = NULL;
98 allocator->is_unused = TRUE;
100 G_UNLOCK (current_allocator);
103 static inline GSList*
104 _g_slist_alloc (void)
106 GSList *list;
108 G_LOCK (current_allocator);
109 if (!current_allocator)
111 GAllocator *allocator = g_allocator_new ("GLib default GSList allocator",
112 128);
113 g_slist_validate_allocator (allocator);
114 allocator->last = NULL;
115 current_allocator = allocator;
117 if (!current_allocator->free_lists)
119 list = g_chunk_new (GSList, current_allocator->mem_chunk);
120 list->data = NULL;
122 else
124 if (current_allocator->free_lists->data)
126 list = current_allocator->free_lists->data;
127 current_allocator->free_lists->data = list->next;
128 list->data = NULL;
130 else
132 list = current_allocator->free_lists;
133 current_allocator->free_lists = list->next;
136 G_UNLOCK (current_allocator);
138 list->next = NULL;
140 return list;
143 GSList*
144 g_slist_alloc (void)
146 return _g_slist_alloc ();
149 void
150 g_slist_free (GSList *list)
152 if (list)
154 GSList *last_node = list;
156 #ifdef ENABLE_GC_FRIENDLY
157 while (last_node->next)
159 last_node->data = NULL;
160 last_node = last_node->next;
162 last_node->data = NULL
163 #else /* !ENABLE_GC_FRIENDLY */
164 list->data = list->next;
165 #endif /* ENABLE_GC_FRIENDLY */
167 G_LOCK (current_allocator);
168 last_node->next = current_allocator->free_lists;
169 current_allocator->free_lists = list;
170 G_UNLOCK (current_allocator);
174 static inline void
175 _g_slist_free_1 (GSList *list)
177 if (list)
179 list->data = NULL;
180 G_LOCK (current_allocator);
181 list->next = current_allocator->free_lists;
182 current_allocator->free_lists = list;
183 G_UNLOCK (current_allocator);
187 void
188 g_slist_free_1 (GSList *list)
190 _g_slist_free_1 (list);
193 GSList*
194 g_slist_append (GSList *list,
195 gpointer data)
197 GSList *new_list;
198 GSList *last;
200 new_list = _g_slist_alloc ();
201 new_list->data = data;
203 if (list)
205 last = g_slist_last (list);
206 /* g_assert (last != NULL); */
207 last->next = new_list;
209 return list;
211 else
212 return new_list;
215 GSList*
216 g_slist_prepend (GSList *list,
217 gpointer data)
219 GSList *new_list;
221 new_list = _g_slist_alloc ();
222 new_list->data = data;
223 new_list->next = list;
225 return new_list;
228 GSList*
229 g_slist_insert (GSList *list,
230 gpointer data,
231 gint position)
233 GSList *prev_list;
234 GSList *tmp_list;
235 GSList *new_list;
237 if (position < 0)
238 return g_slist_append (list, data);
239 else if (position == 0)
240 return g_slist_prepend (list, data);
242 new_list = _g_slist_alloc ();
243 new_list->data = data;
245 if (!list)
246 return new_list;
248 prev_list = NULL;
249 tmp_list = list;
251 while ((position-- > 0) && tmp_list)
253 prev_list = tmp_list;
254 tmp_list = tmp_list->next;
257 if (prev_list)
259 new_list->next = prev_list->next;
260 prev_list->next = new_list;
262 else
264 new_list->next = list;
265 list = new_list;
268 return list;
271 GSList *
272 g_slist_concat (GSList *list1, GSList *list2)
274 if (list2)
276 if (list1)
277 g_slist_last (list1)->next = list2;
278 else
279 list1 = list2;
282 return list1;
285 GSList*
286 g_slist_remove (GSList *list,
287 gconstpointer data)
289 GSList *tmp;
290 GSList *prev;
292 prev = NULL;
293 tmp = list;
295 while (tmp)
297 if (tmp->data == data)
299 if (prev)
300 prev->next = tmp->next;
301 if (list == tmp)
302 list = list->next;
304 tmp->next = NULL;
305 g_slist_free (tmp);
307 break;
310 prev = tmp;
311 tmp = tmp->next;
314 return list;
317 static inline GSList*
318 _g_slist_remove_link (GSList *list,
319 GSList *link)
321 GSList *tmp;
322 GSList *prev;
324 prev = NULL;
325 tmp = list;
327 while (tmp)
329 if (tmp == link)
331 if (prev)
332 prev->next = tmp->next;
333 if (list == tmp)
334 list = list->next;
336 tmp->next = NULL;
337 break;
340 prev = tmp;
341 tmp = tmp->next;
344 return list;
347 GSList*
348 g_slist_remove_link (GSList *list,
349 GSList *link)
351 return _g_slist_remove_link (list, link);
354 GSList*
355 g_slist_delete_link (GSList *list,
356 GSList *link)
358 list = _g_slist_remove_link (list, link);
359 _g_slist_free_1 (link);
361 return list;
364 GSList*
365 g_slist_copy (GSList *list)
367 GSList *new_list = NULL;
369 if (list)
371 GSList *last;
373 new_list = _g_slist_alloc ();
374 new_list->data = list->data;
375 last = new_list;
376 list = list->next;
377 while (list)
379 last->next = _g_slist_alloc ();
380 last = last->next;
381 last->data = list->data;
382 list = list->next;
386 return new_list;
389 GSList*
390 g_slist_reverse (GSList *list)
392 GSList *prev = NULL;
394 while (list)
396 GSList *next = list->next;
398 list->next = prev;
400 prev = list;
401 list = next;
404 return prev;
407 GSList*
408 g_slist_nth (GSList *list,
409 guint n)
411 while (n-- > 0 && list)
412 list = list->next;
414 return list;
417 gpointer
418 g_slist_nth_data (GSList *list,
419 guint n)
421 while (n-- > 0 && list)
422 list = list->next;
424 return list ? list->data : NULL;
427 GSList*
428 g_slist_find (GSList *list,
429 gconstpointer data)
431 while (list)
433 if (list->data == data)
434 break;
435 list = list->next;
438 return list;
441 GSList*
442 g_slist_find_custom (GSList *list,
443 gconstpointer data,
444 GCompareFunc func)
446 g_return_val_if_fail (func != NULL, list);
448 while (list)
450 if (! func (list->data, data))
451 return list;
452 list = list->next;
455 return NULL;
458 gint
459 g_slist_position (GSList *list,
460 GSList *link)
462 gint i;
464 i = 0;
465 while (list)
467 if (list == link)
468 return i;
469 i++;
470 list = list->next;
473 return -1;
476 gint
477 g_slist_index (GSList *list,
478 gconstpointer data)
480 gint i;
482 i = 0;
483 while (list)
485 if (list->data == data)
486 return i;
487 i++;
488 list = list->next;
491 return -1;
494 GSList*
495 g_slist_last (GSList *list)
497 if (list)
499 while (list->next)
500 list = list->next;
503 return list;
506 guint
507 g_slist_length (GSList *list)
509 guint length;
511 length = 0;
512 while (list)
514 length++;
515 list = list->next;
518 return length;
521 void
522 g_slist_foreach (GSList *list,
523 GFunc func,
524 gpointer user_data)
526 while (list)
528 (*func) (list->data, user_data);
529 list = list->next;
533 GSList*
534 g_slist_insert_sorted (GSList *list,
535 gpointer data,
536 GCompareFunc func)
538 GSList *tmp_list = list;
539 GSList *prev_list = NULL;
540 GSList *new_list;
541 gint cmp;
543 g_return_val_if_fail (func != NULL, list);
545 if (!list)
547 new_list = _g_slist_alloc ();
548 new_list->data = data;
549 return new_list;
552 cmp = (*func) (data, tmp_list->data);
554 while ((tmp_list->next) && (cmp > 0))
556 prev_list = tmp_list;
557 tmp_list = tmp_list->next;
558 cmp = (*func) (data, tmp_list->data);
561 new_list = _g_slist_alloc ();
562 new_list->data = data;
564 if ((!tmp_list->next) && (cmp > 0))
566 tmp_list->next = new_list;
567 return list;
570 if (prev_list)
572 prev_list->next = new_list;
573 new_list->next = tmp_list;
574 return list;
576 else
578 new_list->next = list;
579 return new_list;
583 static GSList*
584 g_slist_sort_merge (GSList *l1,
585 GSList *l2,
586 GCompareFunc compare_func)
588 GSList list, *l;
590 l=&list;
592 while (l1 && l2)
594 if (compare_func(l1->data,l2->data) < 0)
596 l=l->next=l1;
597 l1=l1->next;
599 else
601 l=l->next=l2;
602 l2=l2->next;
605 l->next= l1 ? l1 : l2;
607 return list.next;
610 GSList*
611 g_slist_sort (GSList *list,
612 GCompareFunc compare_func)
614 GSList *l1, *l2;
616 if (!list)
617 return NULL;
618 if (!list->next)
619 return list;
621 l1 = list;
622 l2 = list->next;
624 while ((l2 = l2->next) != NULL)
626 if ((l2 = l2->next) == NULL)
627 break;
628 l1=l1->next;
630 l2 = l1->next;
631 l1->next = NULL;
633 return g_slist_sort_merge (g_slist_sort (list, compare_func),
634 g_slist_sort (l2, compare_func),
635 compare_func);