Debian Codegraph @

libmowgli-2/src/libmowgli/container/list.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * list.c: Linked lists.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25 #include "core/bootstrap_internal.h"
26
27 static mowgli_heap_t *mowgli_node_heap;
28 static mowgli_heap_t *mowgli_list_heap;
29
30 void
31 mowgli_node_bootstrap(void)
32 {
33 mowgli_node_heap = mowgli_heap_create(sizeof(mowgli_node_t), 1024, BH_NOW);
34 mowgli_list_heap = mowgli_heap_create(sizeof(mowgli_list_t), 64, BH_NOW);
35
36 if ((mowgli_node_heap == NULL) || (mowgli_list_heap == NULL))
37 {
38 mowgli_log("heap allocator failure.");
39 abort();
40 }
41 }
42
43 /* creates a new node */
44 mowgli_node_t *
45 mowgli_node_create(void)
46 {
47 mowgli_node_t *n;
48
49 /* allocate it */
50 n = mowgli_heap_alloc(mowgli_node_heap);
51
52 /* initialize */
53 n->next = n->prev = n->data = NULL;
54
55 /* return a pointer to the new node */
56 return n;
57 }
58
59 /* frees a node */
60 void
61 mowgli_node_free(mowgli_node_t *n)
62 {
63 return_if_fail(n != NULL);
64
65 /* free it */
66 mowgli_heap_free(mowgli_node_heap, n);
67 }
68
69 /* adds a node to the end of a list */
70 void
71 mowgli_node_add(void *data, mowgli_node_t *n, mowgli_list_t *l)
72 {
73 mowgli_node_t *tn;
74
75 return_if_fail(n != NULL);
76 return_if_fail(l != NULL);
77
78 n->next = n->prev = NULL;
79 n->data = data;
80
81 /* first node? */
82 if (l->head == NULL)
83 {
84 l->head = n;
85 l->tail = n;
86 l->count = 1;
87 return;
88 }
89
90 /* use the cached tail. */
91 tn = l->tail;
92
93 /* set the our `prev' to the last node */
94 n->prev = tn;
95
96 /* set the last node's `next' to us */
97 n->prev->next = n;
98
99 /* set the list's `tail' to us */
100 l->tail = n;
101
102 /* up the count */
103 l->count++;
104 }
105
106 /* adds a node to the head of a list */
107 void
108 mowgli_node_add_head(void *data, mowgli_node_t *n, mowgli_list_t *l)
109 {
110 mowgli_node_t *tn;
111
112 return_if_fail(n != NULL);
113 return_if_fail(l != NULL);
114
115 n->next = n->prev = NULL;
116 n->data = data;
117
118 /* first node? */
119 if (l->head == NULL)
120 {
121 l->head = n;
122 l->tail = n;
123 l->count = 1;
124 return;
125 }
126
127 tn = l->head;
128 n->next = tn;
129 tn->prev = n;
130 l->head = n;
131 l->count++;
132 }
133
134 /* adds a node to a list before another node, or to the end */
135 void
136 mowgli_node_add_before(void *data, mowgli_node_t *n, mowgli_list_t *l, mowgli_node_t *before)
137 {
138 return_if_fail(n != NULL);
139 return_if_fail(l != NULL);
140
141 if (before == NULL)
142 {
143 mowgli_node_add(data, n, l);
144 }
145 else if (before == l->head)
146 {
147 mowgli_node_add_head(data, n, l);
148 }
149 else
150 {
151 n->data = data;
152 n->prev = before->prev;
153 n->next = before;
154 before->prev = n;
155
156 if (n->prev != NULL)
157 n->prev->next = n;
158
159 l->count++;
160 }
161 }
162
163 /* adds a node to a list after another node, or to the end */
164 void
165 mowgli_node_add_after(void *data, mowgli_node_t *n, mowgli_list_t *l, mowgli_node_t *before)
166 {
167 return_if_fail(n != NULL);
168 return_if_fail(l != NULL);
169
170 if ((before == NULL) || (before->next == NULL))
171 {
172 mowgli_node_add(data, n, l);
173 }
174 else
175 {
176 n->data = data;
177 n->prev = before;
178 n->next = before->next;
179 before->next = n;
180 n->next->prev = n;
181 l->count++;
182 }
183 }
184
185 /* retrieves a node at `position` position. */
186 mowgli_node_t *
187 mowgli_node_nth(mowgli_list_t *l, size_t pos)
188 {
189 size_t iter;
190 mowgli_node_t *n;
191
192 return_val_if_fail(l != NULL, NULL);
193
194 /* locate the proper position. */
195 if (pos < MOWGLI_LIST_LENGTH(l) / 2)
196 for (iter = 0, n = l->head; iter != pos && n != NULL; iter++, n = n->next)
197 ;
198
199 else
200 for (iter = MOWGLI_LIST_LENGTH(l) - 1, n = l->tail;
201 iter != pos && n != NULL; iter--, n = n->prev)
202 ;
203
204 return n;
205 }
206
207 /* returns the data from node at `position` position, or NULL. */
208 void *
209 mowgli_node_nth_data(mowgli_list_t *l, size_t pos)
210 {
211 mowgli_node_t *n;
212
213 return_val_if_fail(l != NULL, NULL);
214
215 n = mowgli_node_nth(l, pos);
216
217 if (n == NULL)
218 return NULL;
219
220 return n->data;
221 }
222
223 /* inserts a node at `position` position. */
224 void
225 mowgli_node_insert(void *data, mowgli_node_t *n, mowgli_list_t *l, size_t pos)
226 {
227 mowgli_node_t *tn;
228
229 return_if_fail(n != NULL);
230 return_if_fail(l != NULL);
231
232 /* locate the proper position. */
233 tn = mowgli_node_nth(l, pos);
234
235 mowgli_node_add_before(data, n, l, tn);
236 }
237
238 /* retrieves the index position of a node in a list. */
239 ssize_t
240 mowgli_node_index(mowgli_node_t *n, mowgli_list_t *l)
241 {
242 ssize_t iter;
243 mowgli_node_t *tn;
244
245 return_val_if_fail(n != NULL, -1);
246 return_val_if_fail(l != NULL, -1);
247
248 /* locate the proper position. */
249 for (iter = 0, tn = l->head; tn != n && tn != NULL; iter++, tn = tn->next)
250 ;
251
252 return iter < (ssize_t) MOWGLI_LIST_LENGTH(l) ? iter : -1;
253 }
254
255 /* deletes a link between a node and a list. */
256 void
257 mowgli_node_delete(mowgli_node_t *n, mowgli_list_t *l)
258 {
259 return_if_fail(n != NULL);
260 return_if_fail(l != NULL);
261
262 /* are we the head? */
263 if (!n->prev)
264 l->head = n->next;
265 else
266 n->prev->next = n->next;
267
268 /* are we the tail? */
269 if (!n->next)
270 l->tail = n->prev;
271 else
272 n->next->prev = n->prev;
273
274 /* down the count */
275 l->count--;
276 }
277
278 /* finds a node by `data' */
279 mowgli_node_t *
280 mowgli_node_find(void *data, mowgli_list_t *l)
281 {
282 mowgli_node_t *n;
283
284 return_val_if_fail(l != NULL, NULL);
285
286 MOWGLI_LIST_FOREACH(n, l->head)
287
288 if (n->data == data)
289 return n;
290
291 return NULL;
292 }
293
294 /* moves a node from one list to another. */
295 void
296 mowgli_node_move(mowgli_node_t *m, mowgli_list_t *oldlist, mowgli_list_t *newlist)
297 {
298 return_if_fail(m != NULL);
299 return_if_fail(oldlist != NULL);
300 return_if_fail(newlist != NULL);
301
302 /* Assumption: If m->next == NULL, then list->tail == m
303 * and: If m->prev == NULL, then list->head == m
304 */
305 if (m->next != NULL)
306 m->next->prev = m->prev;
307 else
308 oldlist->tail = m->prev;
309
310 if (m->prev != NULL)
311 m->prev->next = m->next;
312 else
313 oldlist->head = m->next;
314
315 m->prev = NULL;
316 m->next = newlist->head;
317
318 if (newlist->head != NULL)
319 newlist->head->prev = m;
320 else if (newlist->tail == NULL)
321 newlist->tail = m;
322
323 newlist->head = m;
324
325 oldlist->count--;
326 newlist->count++;
327 }
328
329 /* creates a new list. */
330 mowgli_list_t *
331 mowgli_list_create(void)
332 {
333 mowgli_list_t *out = mowgli_heap_alloc(mowgli_list_heap);
334
335 return out;
336 }
337
338 /* frees a created list. */
339 void
340 mowgli_list_free(mowgli_list_t *l)
341 {
342 mowgli_heap_free(mowgli_list_heap, l);
343 }
344
345 /* concatenates two lists together. */
346 void
347 mowgli_list_concat(mowgli_list_t *l, mowgli_list_t *l2)
348 {
349 return_if_fail(l != NULL);
350 return_if_fail(l2 != NULL);
351
352 if (l->head == NULL)
353 l->head = l2->head;
354
355 if (l->tail != NULL)
356 {
357 l->tail->next = l2->head;
358
359 if (l->tail->next != NULL)
360 l->tail->next->prev = l->tail;
361 }
362
363 l->tail = l2->tail;
364 l->count += l2->count;
365
366 /* clear out l2 as it is no longer needed. */
367 l2->head = NULL;
368 l2->tail = NULL;
369 l2->count = 0;
370 }
371
372 /* reverse a list -- O(n)! */
373 void
374 mowgli_list_reverse(mowgli_list_t *l)
375 {
376 mowgli_node_t *n, *tn;
377
378 return_if_fail(l != NULL);
379
380 MOWGLI_LIST_FOREACH_SAFE(n, tn, l->head)
381 {
382 mowgli_node_t *tn2 = n->next;
383
384 n->next = n->prev;
385 n->prev = tn2;
386 }
387
388 tn = l->head;
389 l->head = l->tail;
390 l->tail = tn;
391 }
392
393 /* sorts a list -- O(n ^ 2) most likely, i don't want to think about it. --nenolod */
394 void
395 mowgli_list_sort(mowgli_list_t *l, mowgli_list_comparator_t comp, void *opaque)
396 {
397 mowgli_node_t *n, *tn, *n2, *tn2;
398
399 return_if_fail(l != NULL);
400 return_if_fail(comp != NULL);
401
402 MOWGLI_LIST_FOREACH_SAFE(n, tn, l->head)
403 {
404 MOWGLI_LIST_FOREACH_SAFE(n2, tn2, l->head)
405 {
406 int result;
407 int i, i2;
408
409 if (n == n2)
410 continue;
411
412 i = mowgli_node_index(n, l);
413 i2 = mowgli_node_index(n2, l);
414
415 if ((result = comp(n, n2, opaque)) == 0)
416 {
417 continue;
418 }
419 else if ((result < 0) && (i > i2))
420 {
421 mowgli_node_delete(n, l);
422 mowgli_node_add_before(n->data, n, l, n2);
423 }
424 else if ((result > 0) && (i < i2))
425 {
426 mowgli_node_delete(n, l);
427 mowgli_node_add_after(n->data, n, l, n2);
428 }
429 }
430 }
431 }