]> mj.ucw.cz Git - libucw.git/blob - ucw/slists.c
Logging: Implemented log_switch().
[libucw.git] / ucw / slists.c
1 /*
2  *      UCW Library -- Single-Linked Lists
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "ucw/lib.h"
11 #include "ucw/slists.h"
12
13 static inline snode *
14 slist_raw_prev(slist *l, snode *n)
15 {
16   snode *m = &l->head;
17   while (m)
18     {
19       if (n == m->next)
20         return m;
21       m = m->next;
22     }
23   ASSERT(0);
24 }
25
26 void *
27 slist_prev(slist *l, snode *n)
28 {
29   snode *p = slist_raw_prev(l, n);
30   return (p == &l->head) ? NULL : p;
31 }
32
33 void
34 slist_insert_before(slist *l, snode *what, snode *before)
35 {
36   what->next = before;
37   slist_raw_prev(l, before)->next = what;
38 }
39
40 void
41 slist_remove(slist *l, snode *n)
42 {
43   if (n)
44     {
45       snode *p = slist_raw_prev(l, n);
46       slist_remove_after(l, p);
47     }
48 }
49
50 #ifdef TEST
51
52 #include <stdio.h>
53 #include <alloca.h>
54
55 int main(void)
56 {
57   slist l;
58
59   struct x {
60     snode n;
61     int val;
62   };
63
64   slist_init(&l);
65   for (int i=1; i<=10; i++)
66     {
67       struct x *x = alloca(sizeof(*x));
68       x->val = i;
69       if (i % 2)
70         slist_add_head(&l, &x->n);
71       else
72         slist_add_tail(&l, &x->n);
73     }
74
75   struct x *x, *prev;
76   SLIST_WALK_DELSAFE(x, l, prev)
77     if (x->val == 5)
78       slist_remove_after(&l, &prev->n);
79     else if (x->val == 6)
80       slist_remove(&l, &x->n);
81   SLIST_FOR_EACH(struct x *, x, l)
82     printf("%d/", x->val);
83   putchar('\n');
84 }
85
86 #endif