2 * glist.c: Doubly-linked list implementation
5 * Duncan Mak (duncan@novell.com)
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * (C) 2006 Novell, Inc.
34 return g_new0 (GList, 1);
38 _prepend (GList *list, gpointer data)
40 GList *head = g_list_alloc ();
45 head->prev = list->prev;
47 list->prev->next = head;
57 g_list_prepend (GList *list, gpointer data)
59 return _prepend (list, data);
63 g_list_free_1 (GList *list)
69 g_list_free (GList *list)
80 g_list_append (GList *list, gpointer data)
82 return g_list_concat (list, g_list_prepend (NULL, data));
86 _concat (GList *list1, GList *list2)
98 last = g_list_last (list1);
106 g_list_concat (GList *list1, GList *list2)
108 return _concat (list1, list2);
112 g_list_length (GList *list)
125 g_list_remove (GList *list, gconstpointer data)
131 g_list_remove_link (GList *list, GList *link)
137 g_list_delete_link (GList *list, GList *link)
139 list = g_list_remove_link (list, link);
140 g_list_free_1 (link);
146 g_list_find (GList *list, gconstpointer data)
149 if (list->data == data)
159 g_list_reverse (GList *list)
161 GList *reverse = NULL;
165 list = reverse->next;
167 reverse->next = reverse->prev;
168 reverse->prev = list;
175 g_list_first (GList *list)
187 g_list_last (GList *list)
199 g_list_insert_sorted (GList *list, gpointer data, GCompareFunc func)
201 GList *current = NULL;
207 return g_list_prepend (NULL, data);
209 if (func (list->data, data) > 0)
210 return g_list_prepend (list, data);
212 current = list->next;
215 if (func (current->data, data) > 0){
216 current->prev->next = g_list_prepend (current, data);
220 if (current->next == NULL){
221 g_list_append (list, data);
225 current = current->next;
232 g_list_insert_before (GList *list, GList *sibling, gpointer data)
238 g_list_foreach (GList *list, GFunc func, gpointer user_data)
241 (*func) (list->data, user_data);
247 g_list_sort (GList *list, GCompareFunc func)
253 g_list_index (GList *list, gconstpointer data)
258 if (list->data == data)
269 g_list_nth (GList *list, guint n)
288 g_list_nth_data (GList *list, guint n)
290 return g_list_nth(list, n)->data;
294 g_list_copy (GList *list)
301 copy = g_list_prepend (NULL, list->data);
303 if (list->next == NULL)
309 tmp = g_list_concat (tmp, g_list_prepend (NULL, list->next->data));