* mini-s390.c (add_general): Adjust offset calculation to take into account of roundi...
[mono.git] / eglib / src / glist.c
blob95bf6820c8c94a134f9f1492d75a46193edda566
1 /*
2 * glist.c: Doubly-linked list implementation
4 * Authors:
5 * Duncan Mak (duncan@novell.com)
6 * Raja R Harinath (rharinath@novell.com)
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * (C) 2006 Novell, Inc.
29 #include <stdio.h>
30 #include <glib.h>
32 GList*
33 g_list_alloc ()
35 return g_new0 (GList, 1);
38 static inline GList*
39 new_node (GList *prev, gpointer data, GList *next)
41 GList *node = g_list_alloc ();
42 node->data = data;
43 node->prev = prev;
44 node->next = next;
45 if (prev)
46 prev->next = node;
47 if (next)
48 next->prev = node;
49 return node;
52 static inline GList*
53 disconnect_node (GList *node)
55 if (node->next)
56 node->next->prev = node->prev;
57 if (node->prev)
58 node->prev->next = node->next;
59 return node;
62 GList *
63 g_list_prepend (GList *list, gpointer data)
65 return new_node (list ? list->prev : NULL, data, list);
68 void
69 g_list_free_1 (GList *list)
71 g_free (list);
74 void
75 g_list_free (GList *list)
77 while (list){
78 GList *next = list->next;
79 g_list_free_1 (list);
80 list = next;
84 GList*
85 g_list_append (GList *list, gpointer data)
87 GList *node = new_node (g_list_last (list), data, NULL);
88 return list ? list : node;
91 GList *
92 g_list_concat (GList *list1, GList *list2)
94 if (list1 && list2) {
95 list2->prev = g_list_last (list1);
96 list2->prev->next = list2;
98 return list1 ? list1 : list2;
101 guint
102 g_list_length (GList *list)
104 guint length = 0;
106 while (list) {
107 length ++;
108 list = list->next;
111 return length;
114 GList*
115 g_list_remove (GList *list, gconstpointer data)
117 GList *current = g_list_find (list, data);
118 if (!current)
119 return list;
121 if (current == list)
122 list = list->next;
123 g_list_free_1 (disconnect_node (current));
125 return list;
128 GList*
129 g_list_remove_link (GList *list, GList *link)
131 if (list == link)
132 list = list->next;
134 disconnect_node (link);
135 link->next = NULL;
136 link->prev = NULL;
138 return list;
141 GList*
142 g_list_delete_link (GList *list, GList *link)
144 list = g_list_remove_link (list, link);
145 g_list_free_1 (link);
147 return list;
150 GList*
151 g_list_find (GList *list, gconstpointer data)
153 while (list){
154 if (list->data == data)
155 return list;
157 list = list->next;
160 return NULL;
163 GList*
164 g_list_reverse (GList *list)
166 GList *reverse = NULL;
168 while (list) {
169 reverse = list;
170 list = reverse->next;
172 reverse->next = reverse->prev;
173 reverse->prev = list;
176 return reverse;
179 GList*
180 g_list_first (GList *list)
182 if (!list)
183 return NULL;
185 while (list->prev)
186 list = list->prev;
188 return list;
191 GList*
192 g_list_last (GList *list)
194 if (!list)
195 return NULL;
197 while (list->next)
198 list = list->next;
200 return list;
203 GList*
204 g_list_insert_sorted (GList *list, gpointer data, GCompareFunc func)
206 GList *prev = NULL;
207 GList *current;
208 GList *node;
210 if (!func)
211 return list;
213 /* Invariant: !prev || func (prev->data, data) <= 0) */
214 for (current = list; current; current = current->next) {
215 if (func (current->data, data) > 0)
216 break;
217 prev = current;
220 node = new_node (prev, data, current);
221 return list == current ? node : list;
224 GList*
225 g_list_insert_before (GList *list, GList *sibling, gpointer data)
227 if (sibling) {
228 GList *node = new_node (sibling->prev, data, sibling);
229 return list == sibling ? node : list;
231 return g_list_append (list, data);
234 void
235 g_list_foreach (GList *list, GFunc func, gpointer user_data)
237 while (list){
238 (*func) (list->data, user_data);
239 list = list->next;
243 gint
244 g_list_index (GList *list, gconstpointer data)
246 gint index = 0;
248 while (list){
249 if (list->data == data)
250 return index;
252 index ++;
253 list = list->next;
256 return -1;
259 GList*
260 g_list_nth (GList *list, guint n)
262 for (; list; list = list->next) {
263 if (n == 0)
264 break;
265 n--;
267 return list;
270 gpointer
271 g_list_nth_data (GList *list, guint n)
273 GList *node = g_list_nth (list, n);
274 return node ? node->data : NULL;
277 GList*
278 g_list_copy (GList *list)
280 GList *copy = NULL;
282 if (list) {
283 GList *tmp = new_node (NULL, list->data, NULL);
284 copy = tmp;
286 for (list = list->next; list; list = list->next)
287 tmp = new_node (tmp, list->data, NULL);
290 return copy;
293 typedef GList list_node;
294 #include "sort.frag.h"
296 GList*
297 g_list_sort (GList *list, GCompareFunc func)
299 GList *current;
300 if (!list || !list->next)
301 return list;
302 list = do_sort (list, func);
304 /* Fixup: do_sort doesn't update 'prev' pointers */
305 list->prev = NULL;
306 for (current = list; current->next; current = current->next)
307 current->next->prev = current;
309 return list;