These are the ubiqx modules, as included with the Samba distribution.
[samba.git] / source3 / ubiqx / ubi_dLinkList.c
1 /* ========================================================================== **
2  *                              ubi_dLinkList.c
3  *
4  *  Copyright (C) 1997 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 1.2  1997/10/15 03:11:46  crh
29  * These are the ubiqx modules, as included with the Samba distribution.
30  * Updated the linked list module, which has new and changed macros.
31  *
32  * Revision 0.3  1997/10/15 03:05:39  crh
33  * Added some handy type casting to the macros.  Added AddHere and RemThis
34  * macros.
35  *
36  * Revision 0.2  1997/10/08 03:07:21  crh
37  * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
38  * macro, which was passing the wrong value for <After> to Insert().
39  *
40  * Revision 0.1  1997/10/07 04:34:07  crh
41  * Initial Revision.
42  *
43  *
44  * ========================================================================== **
45  */
46
47 #include "ubi_dLinkList.h"
48
49 /* ========================================================================== **
50  * Functions...
51  */
52
53 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr )
54   /* ------------------------------------------------------------------------ **
55    * Initialize a doubly-linked list header.
56    *
57    *  Input:  ListPtr - A pointer to the list structure that is to be
58    *                    initialized for use.
59    *
60    *  Output: A pointer to the initialized list header (i.e., same as
61    *          <ListPtr>).
62    *
63    * ------------------------------------------------------------------------ **
64    */
65   {
66   ListPtr->Head  = NULL;
67   ListPtr->Tail  = NULL;
68   ListPtr->count = 0;
69   return( ListPtr );
70   } /* ubi_dlInitList */
71
72 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
73                             ubi_dlNodePtr New,
74                             ubi_dlNodePtr After )
75   /* ------------------------------------------------------------------------ **
76    * Insert a new node into the list.
77    *
78    *  Input:  ListPtr - A pointer to the list into which the node is to
79    *                    be inserted.
80    *          New     - Pointer to the new node.
81    *          After   - NULL, or a pointer to a node that is already in the
82    *                    list.
83    *                    If NULL, then <New> will be added at the head of the
84    *                    list, else it will be added following <After>.
85    * 
86    *  Output: A pointer to the node that was inserted into the list (i.e.,
87    *          the same as <New>).
88    *
89    * ------------------------------------------------------------------------ **
90    */
91   {
92   if( NULL == After )
93     {
94     New->Next           = ListPtr->Head;
95     New->Prev           = NULL;
96     if( NULL != ListPtr->Head )
97       ListPtr->Head->Prev = New;
98     else
99       ListPtr->Tail       = New;
100     ListPtr->Head       = New;
101     }
102   else
103     {
104     New->Next         = After->Next;
105     New->Prev         = After;
106     if( NULL != After->Next )
107       After->Next->Prev = New;
108     else
109       ListPtr->Tail       = New;
110     After->Next       = New;
111     }
112
113   ++(ListPtr->count);
114
115   return( New );
116   } /* ubi_dlInsert */
117
118 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old )
119   /* ------------------------------------------------------------------------ **
120    * Remove a node from the list.
121    *
122    *  Input:  ListPtr - A pointer to the list from which <Old> is to be
123    *                    removed.
124    *          Old     - A pointer to the node that is to be removed from the
125    *                    list.
126    *
127    *  Output: A pointer to the node that was removed (i.e., <Old>).
128    *
129    * ------------------------------------------------------------------------ **
130    */
131   {
132   if( NULL != Old )
133     {
134     if( Old->Next )
135       Old->Next->Prev = Old->Prev;
136     else
137       ListPtr->Tail = Old->Prev;
138
139     if( Old->Prev )
140       Old->Prev->Next = Old->Next;
141     else
142       ListPtr->Head = Old->Next;
143
144     --(ListPtr->count);
145     }
146
147   return( Old );
148   } /* ubi_dlRemove */
149
150
151 /* ================================ The End ================================= */