** Warning: Cannot open xref database.
1 /*
2 Based on gslist.c from glib-1.2.9 (LGPL).
3
4 Adaption to JACK, Copyright (C) 2002 Kai Vehmanen.
5 - replaced use of gtypes with normal ANSI C types
6 - glib's memery allocation routines replaced with
7 malloc/free calls
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 $Id: jslist.h,v 1.2 2003/11/24 23:18:57 node Exp $
24 */
25
26 #ifndef __jack_jslist_h__
27 #define __jack_jslist_h__
28
29 #include <stdlib.h>
30
31 typedef struct _JSList JSList;
32
33 typedef int (*JCompareFunc) (void* a,
34 void* b);
35 struct _JSList
36 {
37 void *data;
38 JSList *next;
39 };
40
41 static __inline__
42 JSList*
43 jack_slist_alloc (void)
44 {
45 JSList *new_list;
46
47 new_list = malloc(sizeof(JSList));
48 new_list->data = NULL;
49 new_list->next = NULL;
50
51 return new_list;
52 }
53
54 static __inline__
55 JSList*
56 jack_slist_prepend (JSList *list,
57 void *data)
58 {
59 JSList *new_list;
60
61 new_list = malloc(sizeof(JSList));
62 new_list->data = data;
63 new_list->next = list;
64
65 return new_list;
66 }
67
68 #define jack_slist_next(slist) ((slist) ? (((JSList *)(slist))->next) : NULL)
69 static __inline__
70 JSList*
71 jack_slist_last (JSList *list)
72 {
73 if (list)
74 {
75 while (list->next)
76 list = list->next;
77 }
78
79 return list;
80 }
81
82 static __inline__
83 JSList*
84 jack_slist_remove_link (JSList *list,
85 JSList *link)
86 {
87 JSList *tmp;
88 JSList *prev;
89
90 prev = NULL;
91 tmp = list;
92
93 while (tmp)
94 {
95 if (tmp == link)
96 {
97 if (prev)
98 prev->next = tmp->next;
99 if (list == tmp)
100 list = list->next;
101
102 tmp->next = NULL;
103 break;
104 }
105
106 prev = tmp;
107 tmp = tmp->next;
108 }
109
110 return list;
111 }
112
113 static __inline__
114 void
115 jack_slist_free (JSList *list)
116 {
117 while (list)
118 {
119 JSList *next = list->next;
120 free(list);
121 list = next;
122 }
123 }
124
125 static __inline__
126 void
127 jack_slist_free_1 (JSList *list)
128 {
129 if (list)
130 {
131 free(list);
132 }
133 }
134
135 static __inline__
136 JSList*
137 jack_slist_remove (JSList *list,
138 void *data)
139 {
140 JSList *tmp;
141 JSList *prev;
142
143 prev = NULL;
144 tmp = list;
145
146 while (tmp)
147 {
148 if (tmp->data == data)
149 {
150 if (prev)
151 prev->next = tmp->next;
152 if (list == tmp)
153 list = list->next;
154
155 tmp->next = NULL;
156 jack_slist_free (tmp);
157
158 break;
159 }
160
161 prev = tmp;
162 tmp = tmp->next;
163 }
164
165 return list;
166 }
167
168 static __inline__
169 unsigned int
170 jack_slist_length (JSList *list)
171 {
172 unsigned int length;
173
174 length = 0;
175 while (list)
176 {
177 length++;
178 list = list->next;
179 }
180
181 return length;
182 }
183
184 static __inline__
185 JSList*
186 jack_slist_find (JSList *list,
187 void *data)
188 {
189 while (list)
190 {
191 if (list->data == data)
192 break;
193 list = list->next;
194 }
195
196 return list;
197 }
198
199 static __inline__
200 JSList*
201 jack_slist_copy (JSList *list)
202 {
203 JSList *new_list = NULL;
204
205 if (list)
206 {
207 JSList *last;
208
209 new_list = jack_slist_alloc ();
210 new_list->data = list->data;
211 last = new_list;
212 list = list->next;
213 while (list)
214 {
215 last->next = jack_slist_alloc ();
216 last = last->next;
217 last->data = list->data;
218 list = list->next;
219 }
220 }
221
222 return new_list;
223 }
224
225 static __inline__
226 JSList*
227 jack_slist_append (JSList *list,
228 void *data)
229 {
230 JSList *new_list;
231 JSList *last;
232
233 new_list = jack_slist_alloc ();
234 new_list->data = data;
235
236 if (list)
237 {
238 last = jack_slist_last (list);
239 last->next = new_list;
240
241 return list;
242 }
243 else
244 return new_list;
245 }
246
247 static __inline__
248 JSList*
249 jack_slist_sort_merge (JSList *l1,
250 JSList *l2,
251 JCompareFunc compare_func)
252 {
253 JSList list, *l;
254
255 l=&list;
256
257 while (l1 && l2)
258 {
259 if (compare_func(l1->data,l2->data) < 0)
260 {
261 l=l->next=l1;
262 l1=l1->next;
263 }
264 else
265 {
266 l=l->next=l2;
267 l2=l2->next;
268 }
269 }
270 l->next= l1 ? l1 : l2;
271
272 return list.next;
273 }
274
275 static __inline__
276 JSList*
277 jack_slist_sort (JSList *list,
278 JCompareFunc compare_func)
279 {
280 JSList *l1, *l2;
281
282 if (!list)
283 return NULL;
284 if (!list->next)
285 return list;
286
287 l1 = list;
288 l2 = list->next;
289
290 while ((l2 = l2->next) != NULL)
291 {
292 if ((l2 = l2->next) == NULL)
293 break;
294 l1=l1->next;
295 }
296 l2 = l1->next;
297 l1->next = NULL;
298
299 return jack_slist_sort_merge (jack_slist_sort (list, compare_func),
300 jack_slist_sort (l2, compare_func),
301 compare_func);
302 }
303
304 #endif /* __jack_jslist_h__ */
305
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.