dlist.h

Wed, 23 Feb 2005 19:05:01 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 23 Feb 2005 19:05:01 +0100
changeset 87
95553f8ea540
parent 62
aae5facf9fc5
child 88
308dfa54da3e
permissions
-rw-r--r--

Added dlist iteration macros.

58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
1 /*
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
2 * libtu/common.h
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
3 *
87
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2005.
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
5 *
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
6 * You may distribute and modify this library under the terms of either
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
8 */
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
9
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
10 #ifndef LIBTU_DLIST_H
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
11 #define LIBTU_DLIST_H
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
12
87
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
13
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
14 /*{{{ Linking */
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
15
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
16
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
17 #define LINK_ITEM(LIST, ITEM, NEXT, PREV) \
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
18 (ITEM)->NEXT=NULL; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
19 if((LIST)==NULL){ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
20 (LIST)=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
21 (ITEM)->PREV=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
22 }else{ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
23 (ITEM)->PREV=(LIST)->PREV; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
24 (ITEM)->PREV->NEXT=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
25 (LIST)->PREV=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
26 }
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
27
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
28
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
29 #define LINK_ITEM_FIRST(LIST, ITEM, NEXT, PREV) \
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
30 (ITEM)->NEXT=(LIST); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
31 if((LIST)==NULL){ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
32 (ITEM)->PREV=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
33 }else{ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
34 (ITEM)->PREV=(LIST)->PREV; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
35 (LIST)->PREV=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
36 } \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
37 (LIST)=(ITEM);
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
38
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
39
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
40 #define LINK_ITEM_LIST LINK_ITEM
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
41
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
42
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
43 #define LINK_ITEM_BEFORE(LIST, BEFORE, ITEM, NEXT, PREV) \
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
44 (ITEM)->NEXT=(BEFORE); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
45 (ITEM)->PREV=(BEFORE)->PREV; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
46 (BEFORE)->PREV=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
47 if((BEFORE)==(LIST)) \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
48 (LIST)=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
49 else \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
50 (ITEM)->PREV->NEXT=(ITEM)
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
51
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
52
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
53 #define LINK_ITEM_AFTER(LIST, AFTER, ITEM, NEXT, PREV) \
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
54 (ITEM)->NEXT=(AFTER)->NEXT; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
55 (ITEM)->PREV=(AFTER); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
56 (AFTER)->NEXT=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
57 if((ITEM)->NEXT==NULL) \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
58 (LIST)->PREV=(ITEM); \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
59 else \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
60 (ITEM)->NEXT->PREV=ITEM;
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
61
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
62
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
63 #define UNLINK_ITEM(LIST, ITEM, NEXT, PREV) \
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
64 if((ITEM)->PREV!=NULL){ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
65 if((ITEM)==(LIST)){ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
66 (LIST)=(ITEM)->NEXT; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
67 if((LIST)!=NULL) \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
68 (LIST)->PREV=(ITEM)->PREV; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
69 }else if((ITEM)->NEXT==NULL){ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
70 (ITEM)->PREV->NEXT=NULL; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
71 (LIST)->PREV=(ITEM)->PREV; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
72 }else{ \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
73 (ITEM)->PREV->NEXT=(ITEM)->NEXT; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
74 (ITEM)->NEXT->PREV=(ITEM)->PREV; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
75 } \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
76 } \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
77 (ITEM)->NEXT=NULL; \
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 58
diff changeset
78 (ITEM)->PREV=NULL;
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
79
87
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
80
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
81 /*}}}*/
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
82
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
83
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
84 /*{{{ Iteration */
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
85
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
86
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
87 #define LIST_FIRST(LIST, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
88 (LIST)
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
89 #define LIST_LAST(LIST, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
90 ((LIST)==NULL ? NULL : LIST_PREV_WRAP(LIST, LIST, NEXT, PREV))
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
91 #define LIST_NEXT(LIST, REG, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
92 ((REG)->NEXT)
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
93 #define LIST_PREV(LIST, REG, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
94 ((REG)->PREV->NEXT ? (REG)->PREV : NULL)
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
95 #define LIST_NEXT_WRAP(LIST, REG, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
96 (((REG) && (REG)->NEXT) ? (REG)->NEXT : (LIST))
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
97 #define LIST_PREV_WRAP(LIST, REG, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
98 ((REG) ? (REG)->PREV : (LIST))
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
99
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
100 #define LIST_FOR_ALL(LIST, NODE, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
101 for(NODE=LIST; NODE!=NULL; NODE=(NODE)->NEXT)
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
102
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
103 #define LIST_FOR_ALL_W_NEXT(LIST, NODE, NXT, NEXT, PREV) \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
104 for(NODE=LL, NXT=(NODE==NULL ? NULL : (NODE)->NEXT); \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
105 NODE!=NULL; \
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
106 NODE=NXT, NXT=(NODE==NULL ? NULL : (NODE)->NEXT))
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
107
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
108
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
109 /*}}}*/
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
110
95553f8ea540 Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
111
58
789df543d0c3 trunk: changeset 1303
tuomov
parents:
diff changeset
112 #endif /* LIBTU_DLIST_H */

mercurial