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 new_node (GList *prev, gpointer data, GList *next)
40 GList *node = g_list_alloc ();
52 disconnect_node (GList *node)
55 node->next->prev = node->prev;
57 node->prev->next = node->next;
62 g_list_prepend (GList *list, gpointer data)
64 return new_node (list ? list->prev : NULL, data, list);
68 g_list_free_1 (GList *list)
74 g_list_free (GList *list)
77 GList *next = list->next;
84 g_list_append (GList *list, gpointer data)
86 GList *node = new_node (g_list_last (list), data, NULL);
87 return list ? list : node;
91 g_list_concat (GList *list1, GList *list2)
94 list2->prev = g_list_last (list1);
95 list2->prev->next = list2;
97 return list1 ? list1 : list2;
101 g_list_length (GList *list)
114 g_list_remove (GList *list, gconstpointer data)
116 GList *current = g_list_find (list, data);
122 g_list_free_1 (disconnect_node (current));
128 g_list_remove_link (GList *list, GList *link)
133 disconnect_node (link);
141 g_list_delete_link (GList *list, GList *link)
143 list = g_list_remove_link (list, link);
144 g_list_free_1 (link);
150 g_list_find (GList *list, gconstpointer data)
153 if (list->data == data)
163 g_list_reverse (GList *list)
165 GList *reverse = NULL;
169 list = reverse->next;
171 reverse->next = reverse->prev;
172 reverse->prev = list;
179 g_list_first (GList *list)
191 g_list_last (GList *list)
203 g_list_insert_sorted (GList *list, gpointer data, GCompareFunc func)
211 /* Invariant: !prev || func (prev->data, data) <= 0) */
212 for (current = list; current; current = current->next) {
213 if (func (current->data, data) > 0)
218 GList *node = new_node (prev, data, current);
219 return list == current ? node : list;
223 g_list_insert_before (GList *list, GList *sibling, gpointer data)
226 GList *node = new_node (sibling->prev, data, sibling);
227 return list == sibling ? node : list;
229 return g_list_append (list, data);
233 g_list_foreach (GList *list, GFunc func, gpointer user_data)
236 (*func) (list->data, user_data);
242 g_list_index (GList *list, gconstpointer data)
247 if (list->data == data)
258 g_list_nth (GList *list, guint n)
260 for (; list; list = list->next) {
269 g_list_nth_data (GList *list, guint n)
271 GList *node = g_list_nth (list, n);
272 return node ? node->data : NULL;
276 g_list_copy (GList *list)
281 GList *tmp = new_node (NULL, list->data, NULL);
284 for (list = list->next; list; list = list->next)
285 tmp = new_node (tmp, list->data, NULL);
291 typedef GList *digit;
292 #include "sort.frag.h"
295 g_list_sort (GList *list, GCompareFunc func)
298 if (!list || !list->next)
300 list = do_sort (list, func);
302 /* Fixup: do_sort doesn't update 'prev' pointers */
304 for (current = list; current->next; current = current->next)
305 current->next->prev = current;