proto.h
[tprouty/samba.git] / source / ubiqx / ubi_dLinkList.h
1 #ifndef ubi_dLinkList_H
2 #define ubi_dLinkList_H
3 /* ========================================================================== **
4  *                              ubi_dLinkList.h
5  *
6  *  Copyright (C) 1997, 1998 by Christopher R. Hertel
7  *
8  *  Email: crh@ubiqx.mn.org
9  * -------------------------------------------------------------------------- **
10  *  This module implements simple doubly-linked lists.
11  * -------------------------------------------------------------------------- **
12  *
13  *  This library is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU Library General Public
15  *  License as published by the Free Software Foundation; either
16  *  version 2 of the License, or (at your option) any later version.
17  *
18  *  This library is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *  Library General Public License for more details.
22  *
23  *  You should have received a copy of the GNU Library General Public
24  *  License along with this library; if not, write to the Free
25  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * -------------------------------------------------------------------------- **
28  *
29  * Log: ubi_dLinkList.h,v 
30  * Revision 0.8  1998/06/03 18:06:03  crh
31  * Further fiddling with sys_include.h, which has been moved from the .c file
32  * to the .h file.
33  *
34  * Revision 0.7  1998/06/02 01:38:47  crh
35  * Changed include file name from ubi_null.h to sys_include.h to make it
36  * more generic.
37  *
38  * Revision 0.6  1998/05/20 04:38:05  crh
39  * The C file now includes ubi_null.h.  See ubi_null.h for more info.
40  *
41  * Revision 0.5  1998/03/10 02:54:04  crh
42  * Simplified the code and added macros for stack & queue manipulations.
43  *
44  * Revision 0.4  1998/01/03 01:53:44  crh
45  * Added ubi_dlCount() macro.
46  *
47  * Revision 0.3  1997/10/15 03:04:31  crh
48  * Added some handy type casting to the macros.  Added AddHere and RemThis
49  * macros.
50  *
51  * Revision 0.2  1997/10/08 03:08:16  crh
52  * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
53  * macro, which was passing the wrong value for <After> to Insert().
54  *
55  * Revision 0.1  1997/10/07 04:34:38  crh
56  * Initial Revision.
57  *
58  * -------------------------------------------------------------------------- **
59  * This module is similar to the ubi_sLinkList module, but it is neither a
60  * descendant type nor an easy drop-in replacement for the latter.  One key
61  * difference is that the ubi_dlRemove() function removes the indicated node,
62  * while the ubi_slRemove() function (in ubi_sLinkList) removes the node
63  * *following* the indicated node.
64  *
65  * ========================================================================== **
66  */
67
68 #include "sys_include.h"    /* System-specific includes. */
69
70 /* ========================================================================== **
71  * Typedefs...
72  *
73  *  ubi_dlNode    - This is the basic node structure.
74  *  ubi_dlNodePtr - Pointer to a node.
75  *  ubi_dlList    - This is the list header structure.
76  *  ubi_dlListPtr - Pointer to a List (i.e., a list header structure).
77  *
78  */
79
80 typedef struct ubi_dlListNode
81   {
82   struct ubi_dlListNode *Next;
83   struct ubi_dlListNode *Prev;
84   } ubi_dlNode;
85
86 typedef ubi_dlNode *ubi_dlNodePtr;
87
88 typedef struct
89   {
90   ubi_dlNodePtr Head;
91   ubi_dlNodePtr Tail;
92   unsigned long count;
93   } ubi_dlList;
94
95 typedef ubi_dlList *ubi_dlListPtr;
96
97 /* ========================================================================== **
98  * Macros...
99  *
100  *  ubi_dlCount   - Return the number of entries currently in the list.
101  *
102  *  ubi_dlAddHead - Add a new node at the head of the list.
103  *  ubi_dlAddNext - Add a node following the given node.
104  *  ubi_dlAddTail - Add a new node at the tail of the list.
105  *                  Note: AddTail evaluates the L parameter twice.
106  *
107  *  ubi_dlRemHead - Remove the node at the head of the list, if any.
108  *                  Note: RemHead evaluates the L parameter twice.
109  *  ubi_dlRemThis - Remove the indicated node.
110  *  ubi_dlRemTail - Remove the node at the tail of the list, if any.
111  *                  Note: RemTail evaluates the L parameter twice.
112  *
113  *  ubi_dlFirst   - Return a pointer to the first node in the list, if any.
114  *  ubi_dlLast    - Return a pointer to the last node in the list, if any.
115  *  ubi_dlNext    - Given a node, return a pointer to the next node.
116  *  ubi_dlPrev    - Given a node, return a pointer to the previous node.
117  *
118  *  ubi_dlPush    - Add a node at the head of the list (synonym of AddHead).
119  *  ubi_dlPop     - Remove a node at the head of the list (synonym of RemHead).
120  *  ubi_dlEnqueue - Add a node at the tail of the list (sysnonym of AddTail).
121  *  ubi_dlDequeue - Remove a node at the head of the list (synonym of RemHead).
122  *
123  *  Note that all of these provide type casting of the parameters.  The
124  *  Add and Rem macros are nothing more than nice front-ends to the
125  *  Insert and Remove operations.
126  *
127  *  Also note that there the First, Next and Last macros do no parameter
128  *  checking!
129  *
130  */
131
132 #define ubi_dlCount( L ) (((ubi_dlListPtr)(L))->count)
133
134 #define ubi_dlAddHead( L, N ) \
135         ubi_dlInsert( (ubi_dlListPtr)(L), (ubi_dlNodePtr)(N), NULL )
136
137 #define ubi_dlAddNext( L, N, A ) \
138         ubi_dlInsert( (ubi_dlListPtr)(L), \
139                       (ubi_dlNodePtr)(N), \
140                       (ubi_dlNodePtr)(A) )
141
142 #define ubi_dlAddTail( L, N ) \
143         ubi_dlInsert( (ubi_dlListPtr)(L), \
144                       (ubi_dlNodePtr)(N), \
145                     (((ubi_dlListPtr)(L))->Tail) )
146
147 #define ubi_dlRemHead( L ) ubi_dlRemove( (ubi_dlListPtr)(L), \
148                                          (((ubi_dlListPtr)(L))->Head) )
149
150 #define ubi_dlRemThis( L, N ) ubi_dlRemove( (ubi_dlListPtr)(L), \
151                                             (ubi_dlNodePtr)(N) )
152
153 #define ubi_dlRemTail( L ) ubi_dlRemove( (ubi_dlListPtr)(L), \
154                                          (((ubi_dlListPtr)(L))->Tail) )
155
156 #define ubi_dlFirst( L ) (((ubi_dlListPtr)(L))->Head)
157
158 #define ubi_dlLast( L )  (((ubi_dlListPtr)(L))->Tail)
159
160 #define ubi_dlNext( N )  (((ubi_dlNodePtr)(N))->Next)
161
162 #define ubi_dlPrev( N )  (((ubi_dlNodePtr)(N))->Prev)
163
164 #define ubi_dlPush    ubi_dlAddHead
165 #define ubi_dlPop     ubi_dlRemHead
166 #define ubi_dlEnqueue ubi_dlAddTail
167 #define ubi_dlDequeue ubi_dlRemHead
168
169 /* ========================================================================== **
170  * Function prototypes...
171  */
172
173 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr );
174   /* ------------------------------------------------------------------------ **
175    * Initialize a doubly-linked list header.
176    *
177    *  Input:  ListPtr - A pointer to the list structure that is to be
178    *                    initialized for use.
179    *
180    *  Output: A pointer to the initialized list header (i.e., same as
181    *          <ListPtr>).
182    *
183    * ------------------------------------------------------------------------ **
184    */
185
186 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
187                             ubi_dlNodePtr New,
188                             ubi_dlNodePtr After );
189   /* ------------------------------------------------------------------------ **
190    * Insert a new node into the list.
191    *
192    *  Input:  ListPtr - A pointer to the list into which the node is to
193    *                    be inserted.
194    *          New     - Pointer to the new node.
195    *          After   - NULL, or a pointer to a node that is already in the
196    *                    list.
197    *                    If NULL, then <New> will be added at the head of the
198    *                    list, else it will be added following <After>.
199    * 
200    *  Output: A pointer to the node that was inserted into the list (i.e.,
201    *          the same as <New>).
202    *
203    * ------------------------------------------------------------------------ **
204    */
205
206 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old );
207   /* ------------------------------------------------------------------------ **
208    * Remove a node from the list.
209    *
210    *  Input:  ListPtr - A pointer to the list from which <Old> is to be
211    *                    removed.
212    *          Old     - A pointer to the node that is to be removed from the
213    *                    list.
214    *
215    *  Output: A pointer to the node that was removed (i.e., <Old>).
216    *
217    * ------------------------------------------------------------------------ **
218    */
219
220 /* ================================ The End ================================= */
221 #endif /* ubi_dLinkList_H */