Wed, 23 Feb 2005 19:05:01 +0100
Added dlist iteration macros.
58 | 1 | /* |
2 | * libtu/common.h | |
3 | * | |
87
95553f8ea540
Added dlist iteration macros.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
4 | * Copyright (c) Tuomo Valkonen 1999-2005. |
58 | 5 | * |
6 | * You may distribute and modify this library under the terms of either | |
7 | * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. | |
8 | */ | |
9 | ||
10 | #ifndef LIBTU_DLIST_H | |
11 | #define LIBTU_DLIST_H | |
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 | 17 | #define LINK_ITEM(LIST, ITEM, NEXT, PREV) \ |
62 | 18 | (ITEM)->NEXT=NULL; \ |
19 | if((LIST)==NULL){ \ | |
20 | (LIST)=(ITEM); \ | |
21 | (ITEM)->PREV=(ITEM); \ | |
22 | }else{ \ | |
23 | (ITEM)->PREV=(LIST)->PREV; \ | |
24 | (ITEM)->PREV->NEXT=(ITEM); \ | |
25 | (LIST)->PREV=(ITEM); \ | |
26 | } | |
58 | 27 | |
28 | ||
29 | #define LINK_ITEM_FIRST(LIST, ITEM, NEXT, PREV) \ | |
62 | 30 | (ITEM)->NEXT=(LIST); \ |
31 | if((LIST)==NULL){ \ | |
32 | (ITEM)->PREV=(ITEM); \ | |
33 | }else{ \ | |
34 | (ITEM)->PREV=(LIST)->PREV; \ | |
35 | (LIST)->PREV=(ITEM); \ | |
36 | } \ | |
37 | (LIST)=(ITEM); | |
58 | 38 | |
39 | ||
40 | #define LINK_ITEM_LIST LINK_ITEM | |
41 | ||
42 | ||
43 | #define LINK_ITEM_BEFORE(LIST, BEFORE, ITEM, NEXT, PREV) \ | |
62 | 44 | (ITEM)->NEXT=(BEFORE); \ |
45 | (ITEM)->PREV=(BEFORE)->PREV; \ | |
46 | (BEFORE)->PREV=(ITEM); \ | |
47 | if((BEFORE)==(LIST)) \ | |
48 | (LIST)=(ITEM); \ | |
49 | else \ | |
50 | (ITEM)->PREV->NEXT=(ITEM) | |
58 | 51 | |
52 | ||
53 | #define LINK_ITEM_AFTER(LIST, AFTER, ITEM, NEXT, PREV) \ | |
62 | 54 | (ITEM)->NEXT=(AFTER)->NEXT; \ |
55 | (ITEM)->PREV=(AFTER); \ | |
56 | (AFTER)->NEXT=(ITEM); \ | |
57 | if((ITEM)->NEXT==NULL) \ | |
58 | (LIST)->PREV=(ITEM); \ | |
59 | else \ | |
60 | (ITEM)->NEXT->PREV=ITEM; | |
58 | 61 | |
62 | ||
63 | #define UNLINK_ITEM(LIST, ITEM, NEXT, PREV) \ | |
62 | 64 | if((ITEM)->PREV!=NULL){ \ |
65 | if((ITEM)==(LIST)){ \ | |
66 | (LIST)=(ITEM)->NEXT; \ | |
67 | if((LIST)!=NULL) \ | |
68 | (LIST)->PREV=(ITEM)->PREV; \ | |
69 | }else if((ITEM)->NEXT==NULL){ \ | |
70 | (ITEM)->PREV->NEXT=NULL; \ | |
71 | (LIST)->PREV=(ITEM)->PREV; \ | |
72 | }else{ \ | |
73 | (ITEM)->PREV->NEXT=(ITEM)->NEXT; \ | |
74 | (ITEM)->NEXT->PREV=(ITEM)->PREV; \ | |
75 | } \ | |
76 | } \ | |
77 | (ITEM)->NEXT=NULL; \ | |
78 | (ITEM)->PREV=NULL; | |
58 | 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 | 112 | #endif /* LIBTU_DLIST_H */ |