682e566ee6749b89f1931e719e7e01fcd8e93731
[samba] / 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.11  1999/06/19 16:58:06  crh
31  * Renamed the ubi_slRemove() function in ubi_sLinkList to
32  * ubi_slRemoveNext().  I was bothered by the fact that it didn't
33  * match the functionality of the ubi_dlRemove() function in
34  * ubi_dLinkList.  The new name is more 'correct'.
35  *
36  * Revision 0.10  1998/07/24 07:30:20  crh
37  * Added the ubi_dlNewList() macro.
38  *
39  * Revision 0.9  1998/06/04 21:29:27  crh
40  * Upper-cased defined constants (eg UBI_BINTREE_H) in some header files.
41  * This is more "standard", and is what people expect.  Weird, eh?
42  *
43  * Revision 0.8  1998/06/03 18:06:03  crh
44  * Further fiddling with sys_include.h, which has been moved from the .c file
45  * to the .h file.
46  *
47  * Revision 0.7  1998/06/02 01:38:47  crh
48  * Changed include file name from ubi_null.h to sys_include.h to make it
49  * more generic.
50  *
51  * Revision 0.6  1998/05/20 04:38:05  crh
52  * The C file now includes ubi_null.h.  See ubi_null.h for more info.
53  *
54  * Revision 0.5  1998/03/10 02:54:04  crh
55  * Simplified the code and added macros for stack & queue manipulations.
56  *
57  * Revision 0.4  1998/01/03 01:53:44  crh
58  * Added ubi_dlCount() macro.
59  *
60  * Revision 0.3  1997/10/15 03:04:31  crh
61  * Added some handy type casting to the macros.  Added AddHere and RemThis
62  * macros.
63  *
64  * Revision 0.2  1997/10/08 03:08:16  crh
65  * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
66  * macro, which was passing the wrong value for <After> to Insert().
67  *
68  * Revision 0.1  1997/10/07 04:34:38  crh
69  * Initial Revision.
70  *
71  * -------------------------------------------------------------------------- **
72  * This module is similar to the ubi_sLinkList module, but it is neither a
73  * descendant type nor an easy drop-in replacement for the latter.  One key
74  * difference is that the ubi_dlRemove() function removes the indicated node,
75  * while the ubi_slRemoveNext() function (in ubi_sLinkList) removes the node
76  * *following* the indicated node.
77  *
78  * ========================================================================== **
79  */
80
81 #include "sys_include.h"    /* System-specific includes. */
82
83 /* ========================================================================== **
84  * Typedefs...
85  *
86  *  ubi_dlNode    - This is the basic node structure.
87  *  ubi_dlNodePtr - Pointer to a node.
88  *  ubi_dlList    - This is the list header structure.
89  *  ubi_dlListPtr - Pointer to a List (i.e., a list header structure).
90  *
91  */
92
93 typedef struct ubi_dlListNode
94   {
95   struct ubi_dlListNode *Next;
96   struct ubi_dlListNode *Prev;
97   } ubi_dlNode;
98
99 typedef ubi_dlNode *ubi_dlNodePtr;
100
101 typedef struct
102   {
103   ubi_dlNodePtr Head;
104   ubi_dlNodePtr Tail;
105   unsigned long count;
106   } ubi_dlList;
107
108 typedef ubi_dlList *ubi_dlListPtr;
109
110 /* ========================================================================== **
111  * Macros...
112  *
113  *  ubi_dlNewList - Macro used to declare and initialize a new list in one
114  *                  swell foop.  It is used when defining a variable of
115  *                  type ubi_dlList.  The definition
116  *                    static ubi_dlNewList( gerbil );
117  *                  is translated to
118  *                    static ubi_dlList gerbil[1] = {{ NULL, NULL, 0 }};
119  *
120  *  ubi_dlCount   - Return the number of entries currently in the list.
121  *
122  *  ubi_dlAddHead - Add a new node at the head of the list.
123  *  ubi_dlAddNext - Add a node following the given node.
124  *  ubi_dlAddTail - Add a new node at the tail of the list.
125  *                  Note: AddTail evaluates the L parameter twice.
126  *
127  *  ubi_dlRemHead - Remove the node at the head of the list, if any.
128  *                  Note: RemHead evaluates the L parameter twice.
129  *  ubi_dlRemThis - Remove the indicated node.
130  *  ubi_dlRemTail - Remove the node at the tail of the list, if any.
131  *                  Note: RemTail evaluates the L parameter twice.
132  *
133  *  ubi_dlFirst   - Return a pointer to the first node in the list, if any.
134  *  ubi_dlLast    - Return a pointer to the last node in the list, if any.
135  *  ubi_dlNext    - Given a node, return a pointer to the next node.
136  *  ubi_dlPrev    - Given a node, return a pointer to the previous node.
137  *
138  *  ubi_dlPush    - Add a node at the head of the list (synonym of AddHead).
139  *  ubi_dlPop     - Remove a node at the head of the list (synonym of RemHead).
140  *  ubi_dlEnqueue - Add a node at the tail of the list (sysnonym of AddTail).
141  *  ubi_dlDequeue - Remove a node at the head of the list (synonym of RemHead).
142  *
143  *  Note that all of these provide type casting of the parameters.  The
144  *  Add and Rem macros are nothing more than nice front-ends to the
145  *  Insert and Remove operations.
146  *
147  *  Also note that the First, Next and Last macros do no parameter checking!
148  *
149  */
150
151 #define ubi_dlNewList( L ) ubi_dlList (L)[1] = {{ NULL, NULL, 0 }}
152
153 #define ubi_dlCount( L ) (((ubi_dlListPtr)(L))->count)
154
155 #define ubi_dlAddHead( L, N ) \
156         ubi_dlInsert( (ubi_dlListPtr)(L), (ubi_dlNodePtr)(N), NULL )
157
158 #define ubi_dlAddNext( L, N, A ) \
159         ubi_dlInsert( (ubi_dlListPtr)(L), \
160                       (ubi_dlNodePtr)(N), \
161                       (ubi_dlNodePtr)(A) )
162
163 #define ubi_dlAddTail( L, N ) \
164         ubi_dlInsert( (ubi_dlListPtr)(L), \
165                       (ubi_dlNodePtr)(N), \
166                     (((ubi_dlListPtr)(L))->Tail) )
167
168 #define ubi_dlRemHead( L ) ubi_dlRemove( (ubi_dlListPtr)(L), \
169                                          (((ubi_dlListPtr)(L))->Head) )
170
171 #define ubi_dlRemThis( L, N ) ubi_dlRemove( (ubi_dlListPtr)(L), \
172                                             (ubi_dlNodePtr)(N) )
173
174 #define ubi_dlRemTail( L ) ubi_dlRemove( (ubi_dlListPtr)(L), \
175                                          (((ubi_dlListPtr)(L))->Tail) )
176
177 #define ubi_dlFirst( L ) (((ubi_dlListPtr)(L))->Head)
178
179 #define ubi_dlLast( L )  (((ubi_dlListPtr)(L))->Tail)
180
181 #define ubi_dlNext( N )  (((ubi_dlNodePtr)(N))->Next)
182
183 #define ubi_dlPrev( N )  (((ubi_dlNodePtr)(N))->Prev)
184
185 #define ubi_dlPush    ubi_dlAddHead
186 #define ubi_dlPop     ubi_dlRemHead
187 #define ubi_dlEnqueue ubi_dlAddTail
188 #define ubi_dlDequeue ubi_dlRemHead
189
190 /* ========================================================================== **
191  * Function prototypes...
192  */
193
194 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr );
195   /* ------------------------------------------------------------------------ **
196    * Initialize a doubly-linked list header.
197    *
198    *  Input:  ListPtr - A pointer to the list structure that is to be
199    *                    initialized for use.
200    *
201    *  Output: A pointer to the initialized list header (i.e., same as
202    *          <ListPtr>).
203    *
204    * ------------------------------------------------------------------------ **
205    */
206
207 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
208                             ubi_dlNodePtr New,
209                             ubi_dlNodePtr After );
210   /* ------------------------------------------------------------------------ **
211    * Insert a new node into the list.
212    *
213    *  Input:  ListPtr - A pointer to the list into which the node is to
214    *                    be inserted.
215    *          New     - Pointer to the new node.
216    *          After   - NULL, or a pointer to a node that is already in the
217    *                    list.
218    *                    If NULL, then <New> will be added at the head of the
219    *                    list, else it will be added following <After>.
220    * 
221    *  Output: A pointer to the node that was inserted into the list (i.e.,
222    *          the same as <New>).
223    *
224    * ------------------------------------------------------------------------ **
225    */
226
227 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old );
228   /* ------------------------------------------------------------------------ **
229    * Remove a node from the list.
230    *
231    *  Input:  ListPtr - A pointer to the list from which <Old> is to be
232    *                    removed.
233    *          Old     - A pointer to the node that is to be removed from the
234    *                    list.
235    *
236    *  Output: A pointer to the node that was removed (i.e., <Old>).
237    *
238    * ------------------------------------------------------------------------ **
239    */
240
241 /* ================================ The End ================================= */
242 #endif /* UBI_DLINKLIST_H */