chiark / gitweb /
Merge and end branch-hostside-wip-2008-01-25 PROPERLY; cvs up -j branch-hostside...
[trains.git] / hostside / dliste.h
1 /*
2  * dlist.h
3  * Doubly linked lists
4  *
5  * This file is part of epithet, a DNS nameserver.
6  * epithet is Copyright 2001 Ian Jackson.
7  *
8  * epithet is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program and documentation is distributed in the hope that it
14  * will be useful, but without any warranty; without even the implied
15  * warranty of merchantability or fitness for a particular
16  * purpose. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with epithet; if not, write to the Free Software Foundation,
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or email
21  * epithet-maint@chiark.greenend.org.uk.
22  */
23
24 #ifndef DLIST_H
25 #define DLIST_H
26
27
28 #define DLIST_NNODE(nodetype,linktag) struct linktag { nodetype *next, *back; }
29 #define DLIST_NODE(nodetype)          DLIST_NNODE(nodetype, )
30
31 #define DLIST1_PREPEND(head,node,linkmemb)                                    \
32   ((void)((node)->linkmemb.next= (head),                                      \
33           (node)->linkmemb.back= 0,                                           \
34           ((head) ? (head)->linkmemb.back= (node) : 0),                       \
35           (head)= (node)))
36
37 #define DLIST1_REMOVE(head,node,link)                                         \
38   ((void)                                                                     \
39    ((node)->link.next ? ((node)->link.next->link.back= (node)->link.back) :0, \
40     (node)->link.back ? ((node)->link.back->link.next= (node)->link.next)     \
41                       : ((head)=                       (node)->link.next)))
42
43
44 #define DLIST2_NHEAD(nodetype,listtag) struct listtag { nodetype *head,*tail; }
45 #define DLIST2_HEAD(nodetype)          DLIST2_NHEAD(nodetype, )
46
47 #define DLIST2_INIT(list) ((list).head= (list).tail= 0)
48
49 #define DLIST2_REMOVE(list,node,link)                                         \
50   ((void)                                                                     \
51    (((node)->link.back ? ((node)->link.back->link.next= (node)->link.next)    \
52                        : (                 (list).head= (node)->link.next)),  \
53     ((node)->link.next ? ((node)->link.next->link.back= (node)->link.back)    \
54                        : (                 (list).tail= (node)->link.back))))
55
56 #define DLIST2_APPEND(list,node,link)                   \
57   ((void)                                               \
58    ((node)->link.next= 0,                               \
59     (node)->link.back= (list).tail,                     \
60     ((list).tail ? ((list).tail->link.next= (node))     \
61                  : ((list).head= (node))),              \
62     (list).tail= (node)))
63
64 #define DLIST2_PREPEND(list,node,link)                  \
65   ((void)                                               \
66    ((node)->link.next= (list).head,                     \
67     (node)->link.back= 0,                               \
68     ((list).head ? ((list).head->link.back= (node))     \
69                  : ((list).tail= (node))),              \
70     (list).head= (node)))
71
72 #define DLIST2_INSERT_BEFORE(list,newnode,link,refnode) \
73   ((void)                                               \
74    ((newnode)->link.next= (refnode),                    \
75     (newnode)->link.back= (refnode)->link.back,         \
76     ((refnode)->link.back                               \
77        ? ((refnode)->link.back->link.next= (newnode))   \
78        : ((list).head= (newnode))),                     \
79     (refnode)->link.back= (newnode)))
80
81 #define DLIST2_INSERT_AFTER(list,newnode,link,refnode)  \
82   ((void)                                               \
83    ((newnode)->link.back= (refnode),                    \
84     (newnode)->link.next= (refnode)->link.next,         \
85     ((refnode)->link.next                               \
86        ? ((refnode)->link.next->link.back= (newnode))   \
87        : ((list).tail= (newnode))),                     \
88     (refnode)->link.next= (newnode)))))
89      
90 #endif /*DLIST_H*/