Changed the mechanism for including the Samba includes.h in the ubiqx code
[samba.git] / source3 / ubiqx / ubi_dLinkList.c
1 /* ========================================================================== **
2  *                              ubi_dLinkList.c
3  *
4  *  Copyright (C) 1997, 1998 by Christopher R. Hertel
5  *
6  *  Email: crh@ubiqx.mn.org
7  * -------------------------------------------------------------------------- **
8  *  This module implements simple doubly-linked lists.
9  * -------------------------------------------------------------------------- **
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Library General Public
13  *  License as published by the Free Software Foundation; either
14  *  version 2 of the License, or (at your option) any later version.
15  *
16  *  This library is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  Library General Public License for more details.
20  *
21  *  You should have received a copy of the GNU Library General Public
22  *  License along with this library; if not, write to the Free
23  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * -------------------------------------------------------------------------- **
26  *
27  * Log: ubi_dLinkList.c,v 
28  * Revision 0.7  1998/06/02 01:38:47  crh
29  * Changed include file name from ubi_null.h to sys_include.h to make it
30  * more generic.
31  *
32  * Revision 0.6  1998/05/20 04:38:05  crh
33  * The C file now includes ubi_null.h.  See ubi_null.h for more info.
34  *
35  * Revision 0.5  1998/03/10 02:55:00  crh
36  * Simplified the code and added macros for stack & queue manipulations.
37  *
38  * Revision 0.4  1998/01/03 01:53:56  crh
39  * Added ubi_dlCount() macro.
40  *
41  * Revision 0.3  1997/10/15 03:05:39  crh
42  * Added some handy type casting to the macros.  Added AddHere and RemThis
43  * macros.
44  *
45  * Revision 0.2  1997/10/08 03:07:21  crh
46  * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
47  * macro, which was passing the wrong value for <After> to Insert().
48  *
49  * Revision 0.1  1997/10/07 04:34:07  crh
50  * Initial Revision.
51  *
52  * -------------------------------------------------------------------------- **
53  * This module is similar to the ubi_sLinkList module, but it is neither a
54  * descendant type nor an easy drop-in replacement for the latter.  One key
55  * difference is that the ubi_dlRemove() function removes the indicated node,
56  * while the ubi_slRemove() function (in ubi_sLinkList) removes the node
57  * *following* the indicated node.
58  *
59  * ========================================================================== **
60  */
61
62 #include "sys_include.h"    /* System-specific includes. */
63 #include "ubi_dLinkList.h"  /* Header for *this* module. */
64
65 /* ========================================================================== **
66  * Functions...
67  */
68
69 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr )
70   /* ------------------------------------------------------------------------ **
71    * Initialize a doubly-linked list header.
72    *
73    *  Input:  ListPtr - A pointer to the list structure that is to be
74    *                    initialized for use.
75    *
76    *  Output: A pointer to the initialized list header (i.e., same as
77    *          <ListPtr>).
78    *
79    * ------------------------------------------------------------------------ **
80    */
81   {
82   ListPtr->Head  = NULL;
83   ListPtr->Tail  = NULL;
84   ListPtr->count = 0;
85   return( ListPtr );
86   } /* ubi_dlInitList */
87
88 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
89                             ubi_dlNodePtr New,
90                             ubi_dlNodePtr After )
91   /* ------------------------------------------------------------------------ **
92    * Insert a new node into the list.
93    *
94    *  Input:  ListPtr - A pointer to the list into which the node is to
95    *                    be inserted.
96    *          New     - Pointer to the new node.
97    *          After   - NULL, or a pointer to a node that is already in the
98    *                    list.
99    *                    If NULL, then <New> will be added at the head of the
100    *                    list, else it will be added following <After>.
101    * 
102    *  Output: A pointer to the node that was inserted into the list (i.e.,
103    *          the same as <New>).
104    *
105    * ------------------------------------------------------------------------ **
106    */
107   {
108   ubi_dlNodePtr PredNode = After ? After : (ubi_dlNodePtr)ListPtr;
109
110   New->Next         = PredNode->Next;
111   New->Prev         = After;
112   PredNode->Next    = New;
113   if( New->Next )
114     New->Next->Prev = New;
115   else
116     ListPtr->Tail   = New;
117
118   (ListPtr->count)++;
119
120   return( New );
121   } /* ubi_dlInsert */
122
123 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old )
124   /* ------------------------------------------------------------------------ **
125    * Remove a node from the list.
126    *
127    *  Input:  ListPtr - A pointer to the list from which <Old> is to be
128    *                    removed.
129    *          Old     - A pointer to the node that is to be removed from the
130    *                    list.
131    *
132    *  Output: A pointer to the node that was removed (i.e., <Old>).
133    *
134    * ------------------------------------------------------------------------ **
135    */
136   {
137   if( Old )
138     {
139     if( Old->Next )
140       Old->Next->Prev = Old->Prev;
141     else
142       ListPtr->Tail = Old->Prev;
143
144     if( Old->Prev )
145       Old->Prev->Next = Old->Next;
146     else
147       ListPtr->Head = Old->Next;
148
149     (ListPtr->count)--;
150     }
151
152   return( Old );
153   } /* ubi_dlRemove */
154
155 /* ================================ The End ================================= */