2 * UCW Library -- Circular Linked Lists
4 * (c) 2003--2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
14 * Common header for list nodes.
16 typedef struct cnode {
17 struct cnode *next, *prev;
21 * Circilar linked list.
23 typedef struct clist {
28 * Initialize a new circular linked list. Must be called before any other function.
30 static inline void clist_init(clist *l)
32 cnode *head = &l->head;
33 head->next = head->prev = head;
37 * Return the first node on @l or NULL if @l is empty.
39 static inline void *clist_head(clist *l)
41 return (l->head.next != &l->head) ? l->head.next : NULL;
45 * Return the last node on @l or NULL if @l is empty.
47 static inline void *clist_tail(clist *l)
49 return (l->head.prev != &l->head) ? l->head.prev : NULL;
53 * Find the next node to @n or NULL if @n is the last one.
55 static inline void *clist_next(clist *l, cnode *n)
57 return (n->next != &l->head) ? (void *) n->next : NULL;
61 * Find the previous node to @n or NULL if @n is the first one.
63 static inline void *clist_prev(clist *l, cnode *n)
65 return (n->prev != &l->head) ? (void *) n->prev : NULL;
69 * Return a non-zero value iff @l is empty.
71 static inline int clist_empty(clist *l)
73 return (l->head.next == &l->head);
76 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
77 #define CLIST_WALK_DELSAFE(n,list,tmp) for(n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp)
78 #define CLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
79 #define CLIST_FOR_EACH_DELSAFE(type,n,list,tmp) for(type n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp)
81 #define CLIST_FOR_EACH_BACKWARDS(type,n,list) for(type n=(void*)(list).head.prev; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->prev)
84 * Insert a new node just after the node @after. To insert a new head, use @clist_add_head() instead.
86 static inline void clist_insert_after(cnode *what, cnode *after)
88 cnode *before = after->next;
96 * Insert a new node just before the node @before. To insert a new tail, use @clist_add_tail() instead.
98 static inline void clist_insert_before(cnode *what, cnode *before)
100 cnode *after = before->prev;
108 * Insert a new node in front of all other nodes.
110 static inline void clist_add_head(clist *l, cnode *n)
112 clist_insert_after(n, &l->head);
116 * Insert a new node after all other nodes.
118 static inline void clist_add_tail(clist *l, cnode *n)
120 clist_insert_before(n, &l->head);
126 static inline void clist_remove(cnode *n)
128 cnode *before = n->prev;
129 cnode *after = n->next;
130 before->next = after;
131 after->prev = before;
135 * Remove the first node in @l. The list can be empty.
137 static inline void *clist_remove_head(clist *l)
139 cnode *n = clist_head(l);
146 * Remove the last node in @l. The list can be empty.
148 static inline void *clist_remove_tail(clist *l)
150 cnode *n = clist_tail(l);
157 * Merge two lists by inserting the list @what just after the node @after in a different list.
158 * The first list is then cleared.
160 static inline void clist_insert_list_after(clist *what, cnode *after)
162 if (!clist_empty(what))
164 cnode *w = &what->head;
165 w->prev->next = after->next;
166 after->next->prev = w->prev;
167 w->next->prev = after;
168 after->next = w->next;
174 * Compute the number of nodes in @l. Beware linear time complexity.
176 static inline uns clist_size(clist *l)
179 CLIST_FOR_EACH(cnode *, n, *l)