3 * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
4 * Copyright (C) 2001 Bernardo Innocenti <bernie@develer.com>
5 * This file is part of DevLib - See devlib/README for information.
9 * \author Bernardo Innocenti <bernie@develer.com>
11 * \brief General pourpose double-linked lists
16 * Revision 1.3 2004/07/18 22:01:43 bernie
17 * REMHEAD(), REMTAIL(): Move to list.h as inline functions.
19 * Revision 1.2 2004/06/03 11:27:09 bernie
20 * Add dual-license information.
22 * Revision 1.1 2004/05/23 15:43:16 bernie
23 * Import mware modules.
43 /*! Template for a list of \a T structures */
44 #define DECLARE_LIST(T) \
45 struct { T *head; T *null; T *tail; }
47 /*! Template for a node in a list of \a T structures */
48 #define DECLARE_NODE(T) \
49 struct { T *succ; T *pred; }
51 /*! Template for a node in a list of \a T structures */
52 #define DECLARE_NODE_ANON(T) \
56 * Iterate over all nodes in a list. This statement defines a for cicle
57 * accepting the following parameters:
58 * \param n node pointer to be used in each iteration
59 * \param l pointer to list
61 #define FOREACHNODE(n,l) \
63 (n) = (typeof(n))((l)->head); \
64 ((Node *)(n))->succ; \
65 (n) = (typeof(n))(((Node *)(n))->succ) \
68 /*! Initialize a list */
71 (l)->head = (Node *)(&(l)->null); \
73 (l)->tail = (Node *)(&(l)->head); \
76 /*! Add node to list head */
77 #define ADDHEAD(l,n) \
79 (n)->succ = (l)->head; \
80 (n)->pred = (Node *)&(l)->head; \
81 (n)->succ->pred = (n); \
85 /*! Add node to list tail */
86 #define ADDTAIL(l,n) \
88 (n)->succ = (Node *)(&(l)->null); \
89 (n)->pred = (l)->tail; \
90 (n)->pred->succ = (n); \
95 * Insert node \a n before node \a ln
96 * Note: you can't pass in a list header as \a ln, but
97 * it is safe to pass list-\>head of an empty list.
99 #define INSERTBEFORE(n,ln) \
102 (n)->pred = (ln)->pred; \
103 (ln)->pred->succ = (n); \
107 /*! Remove \a n from whatever list it is in */
110 (n)->pred->succ = (n)->succ; \
111 (n)->succ->pred = (n)->pred; \
114 /*! Tell whether a list is empty */
115 #define ISLISTEMPTY(l) ( (l)->head == (Node *)(&(l)->null) )
118 * \name Unlink a node from the head of the list \a l.
119 * \return Pointer to node, or NULL if the list was empty.
121 INLINE Node *REMHEAD(List *l)
130 n->succ->pred = (Node *)l;
135 * \name Unlink a node from the tail of the list \a l.
136 * \return Pointer to node, or NULL if the list was empty.
138 INLINE Node *REMTAIL(List *l)
147 n->pred->succ = (Node *)&l->null;
151 #endif /* MWARE_LIST_H */