]> mj.ucw.cz Git - libucw.git/blob - lib/slists.h
Rewritten ff-binary.
[libucw.git] / lib / slists.h
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 #ifndef _UCW_SLISTS_H
11 #define _UCW_SLISTS_H
12
13 typedef struct snode {
14   struct snode *next;
15 } snode;
16
17 typedef struct slist {
18   struct snode head, *last;
19 } slist;
20
21 static inline void *slist_head(slist *l)
22 {
23   return l->head.next;
24 }
25
26 static inline void *slist_tail(slist *l)
27 {
28   return l->last;
29 }
30
31 static inline void *slist_next(snode *n)
32 {
33   return n->next;
34 }
35
36 static inline int slist_empty(slist *l)
37 {
38   return !l->head.next;
39 }
40
41 #define SLIST_WALK(n,list) for(n=(void*)(list).head.next; (n); (n)=(void*)((snode*)(n))->next)
42 #define SLIST_WALK_DELSAFE(n,list,prev) for((prev)=(void*)&(list).head; (n)=(void*)((snode*)prev)->next; (prev)=(((snode*)(prev))->next==(snode*)(n) ? (void*)(n) : (void*)(prev)))
43 #define SLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; n; n=(void*)((snode*)(n))->next)
44
45 static inline void slist_insert_after(slist *l, snode *what, snode *after)
46 {
47   what->next = after->next;
48   after->next = what;
49   if (!what->next)
50     l->last = what;
51 }
52
53 static inline void slist_add_head(slist *l, snode *n)
54 {
55   n->next = l->head.next;
56   l->head.next = n;
57   if (!l->last)
58     l->last = n;
59 }
60
61 static inline void slist_add_tail(slist *l, snode *n)
62 {
63   if (l->last)
64     l->last->next = n;
65   else
66     l->head.next = n;
67   n->next = NULL;
68   l->last = n;
69 }
70
71 static inline void slist_init(slist *l)
72 {
73   l->head.next = l->last = NULL;
74 }
75
76 static inline void slist_remove_after(slist *l, snode *after)
77 {
78   snode *n = after->next;
79   after->next = n->next;
80   if (l->last == n)
81     l->last = (after == &l->head) ? NULL : after;
82 }
83
84 /* Non-trivial functions */
85
86 void *slist_prev(slist *l, snode *n);
87 void slist_insert_before(slist *l, snode *what, snode *before);
88 void slist_remove(slist *l, snode *n);
89
90 #endif