]> mj.ucw.cz Git - libucw.git/blob - lib/lists.h
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git#v3.10
[libucw.git] / lib / lists.h
1 /*
2  *      UCW Library -- Linked Lists
3  *
4  *      (c) 1997--1999 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_LISTS_H
11 #define _UCW_LISTS_H
12
13 /*
14  * I admit the list structure is very tricky and also somewhat awkward,
15  * but it's both efficient and easy to manipulate once one understands the
16  * basic trick: The list head always contains two synthetic nodes which are
17  * always present in the list: the head and the tail. But as the `next'
18  * entry of the tail and the `prev' entry of the head are both NULL, the
19  * nodes can overlap each other:
20  *
21  *     head    head_node.next
22  *     null    head_node.prev  tail_node.next
23  *     tail                    tail_node.prev
24  */
25
26 typedef struct node {
27   struct node *next, *prev;
28 } node;
29
30 typedef struct list {                   /* In fact two overlayed nodes */
31   struct node *head, *null, *tail;
32 } list;
33
34 #define NODE (node *)
35 #define HEAD(list) ((void *)((list).head))
36 #define TAIL(list) ((void *)((list).tail))
37 #define WALK_LIST(n,list) for(n=HEAD(list);(NODE (n))->next; \
38                                 n=(void *)((NODE (n))->next))
39 #define DO_FOR_ALL(n,list) WALK_LIST(n,list)
40 #define WALK_LIST_DELSAFE(n,nxt,list) \
41      for(n=HEAD(list); nxt=(void *)((NODE (n))->next); n=(void *) nxt)
42 #define WALK_LIST_BACKWARDS(n,list) for(n=TAIL(list);(NODE (n))->prev; \
43                                 n=(void *)((NODE (n))->prev))
44 #define WALK_LIST_BACKWARDS_DELSAFE(n,prv,list) \
45      for(n=TAIL(list); prv=(void *)((NODE (n))->prev); n=(void *) prv)
46
47 #define EMPTY_LIST(list) (!(list).head->next)
48
49 void add_tail(list *, node *);
50 void add_head(list *, node *);
51 void rem_node(node *);
52 void add_tail_list(list *, list *);
53 void init_list(list *);
54 void insert_node(node *, node *);
55
56 #if !defined(_UCW_LISTS_C) && defined(__GNUC__)
57 #define LIST_INLINE extern inline
58 #include "lib/lists.c"
59 #undef LIST_INLINE
60 #else
61 #define LIST_INLINE
62 #endif
63
64 #endif