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.2 2004/06/03 11:27:09 bernie
17 * Add dual-license information.
19 * Revision 1.1 2004/05/23 15:43:16 bernie
20 * Import mware modules.
40 /*! Template for a list of \a T structures */
41 #define DECLARE_LIST(T) \
42 struct { T *head; T *null; T *tail; }
44 /*! Template for a node in a list of \a T structures */
45 #define DECLARE_NODE(T) \
46 struct { T *succ; T *pred; }
48 /*! Template for a node in a list of \a T structures */
49 #define DECLARE_NODE_ANON(T) \
53 * Iterate over all nodes in a list. This statement defines a for cicle
54 * accepting the following parameters:
55 * \param n node pointer to be used in each iteration
56 * \param l pointer to list
58 #define FOREACHNODE(n,l) \
60 (n) = (typeof(n))((l)->head); \
61 ((Node *)(n))->succ; \
62 (n) = (typeof(n))(((Node *)(n))->succ) \
65 /*! Initialize a list */
68 (l)->head = (Node *)(&(l)->null); \
70 (l)->tail = (Node *)(&(l)->head); \
73 /*! Add node to list head */
74 #define ADDHEAD(l,n) \
76 (n)->succ = (l)->head; \
77 (n)->pred = (Node *)&(l)->head; \
78 (n)->succ->pred = (n); \
82 /*! Add node to list tail */
83 #define ADDTAIL(l,n) \
85 (n)->succ = (Node *)(&(l)->null); \
86 (n)->pred = (l)->tail; \
87 (n)->pred->succ = (n); \
92 * Insert node \a n before node \a ln
93 * Note: you can't pass in a list header as \a ln, but
94 * it is safe to pass list-\>head of an empty list.
96 #define INSERTBEFORE(n,ln) \
99 (n)->pred = (ln)->pred; \
100 (ln)->pred->succ = (n); \
104 /*! Remove \a n from whatever list it is in */
107 (n)->pred->succ = (n)->succ; \
108 (n)->succ->pred = (n)->pred; \
111 /*! Tell whether a list is empty */
112 #define ISLISTEMPTY(l) ( (l)->head == (Node *)(&(l)->null) )
114 /*! \name Extract an element from the head/tail of the list. If the list empty, return NULL. */
116 #define REMHEAD(l) _list_rem_head(l)
117 #define REMTAIL(l) _list_rem_tail(l)
120 /* Prototypes for out-of-line functions */
121 Node *_list_rem_head(List *l);
122 Node *_list_rem_tail(List *l);
125 #endif /* MWARE_LIST_H */