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