Initial commit.
[amiga/xmodule.git] / ListMacros.h
1 #ifndef LISTMACROS_H
2 #define LISTMACROS_H
3 /*
4 **      $VER: ListMacros.h 2.1 (1.9.97)
5 **
6 **      Copyright (C) 1996,97 Bernardo Innocenti. All rights reserved.
7 **
8 **      Use 4 chars wide TABs to read this source
9 **
10 **      Some handy macros for list operations.  Using these macros is faster
11 **      than calling their exec.library equivalents, but they will eventually
12 **      make your code a little bigger and are also subject to common macro
13 **      side effects.
14 */
15
16
17 #define NEWLIST(l)      ( (l)->lh_TailPred = (struct Node *)(l),                        \
18                                         (l)->lh_Tail = 0,                                                                       \
19                                         (l)->lh_Head = (struct Node *)(&((l)->lh_Tail)) )
20
21 #define ADDHEAD(l,n) ( (n)->ln_Pred = (struct Node *)(l),                               \
22                                         (n)->ln_Succ = (l)->lh_Head,                                            \
23                                         (l)->lh_Head->ln_Pred = (n),                                            \
24                                         (l)->lh_Head = (n) )
25
26 #define ADDTAIL(l,n) ( (n)->ln_Succ = (struct Node *)(&((l)->lh_Tail)), \
27                                         (n)->ln_Pred = (l)->lh_TailPred,                                        \
28                                         (l)->lh_TailPred->ln_Succ = (n),                                        \
29                                         (l)->lh_TailPred = (n) )
30
31 #define REMOVE(n)       ( (n)->ln_Succ->ln_Pred = (n)->ln_Pred,                         \
32                                         (n)->ln_Pred->ln_Succ = (n)->ln_Succ )
33
34 #define GETHEAD(l)      ( (l)->lh_Head->ln_Succ ? (l)->lh_Head : (struct Node *)NULL )
35
36 #define GETTAIL(l)  ( (l)->lh_TailPred->ln_Succ ? (l)->lh_TailPred : (struct Node *)NULL )
37
38 #define GETSUCC(n)  ( (n)->ln_Succ->ln_Succ ? (n)->ln_Succ : (struct Node *)NULL )
39
40 #define GETPRED(n)  ( (n)->ln_Pred->ln_Pred ? (n)->ln_Pred : (struct Node *)NULL )
41
42
43 #ifdef __GNUC__
44
45 #define REMHEAD(l)                                                                                                                      \
46 ({                                                                                                                                                      \
47         struct Node *n = (l)->lh_Head;                                                                                  \
48         n->ln_Succ ?                                                                                                                    \
49                 (l)->lh_Head = n->ln_Succ,                                                                                      \
50                         (l)->lh_Head->ln_Pred = (struct Node *)(l),                                             \
51                         n :                                                                                                                             \
52                 NULL;                                                                                                                           \
53 })
54
55 #define REMTAIL(l)                                                                                                                      \
56 ({                                                                                                                                                      \
57         struct Node *n = (l)->lh_TailPred;                                                                              \
58         n->ln_Pred ?                                                                                                                    \
59                 (l)->lh_TailPred = n->ln_Pred,                                                                          \
60                         (l)->lh_TailPred->ln_Succ = (struct Node *)(&((l)->lh_Tail)),   \
61                         n :                                                                                                                             \
62                 NULL;                                                                                                                           \
63 })
64
65
66 #else
67
68 /* These two can't be implemented as macros without the GCC ({...}) language extension */
69
70 INLINE struct Node *REMHEAD(struct List *l)
71 {
72         struct Node *n = l->lh_Head;
73
74         if (n->ln_Succ)
75         {
76                 l->lh_Head = n->ln_Succ;
77                 l->lh_Head->ln_Pred = (struct Node *)l;
78                 return n;
79         }
80         return NULL;
81 }
82
83 INLINE struct Node *REMTAIL(struct List *l)
84 {
85         struct Node *n = l->lh_TailPred;
86
87         if (n->ln_Pred)
88         {
89                 l->lh_TailPred = n->ln_Pred;
90                 l->lh_TailPred->ln_Succ = (struct Node *)(&(l->lh_Tail));
91                 return n;
92         }
93         return NULL;
94 }
95
96 #endif
97
98 #endif /* !LISTMACROS_H */