c7cac0ed1e53f8e247e7b569a2393767904aafb9
[samba.git] / source3 / ubiqx / ubi_AVLtree.h
1 #ifndef ubi_AVLtree_H
2 #define ubi_AVLtree_H
3 /* ========================================================================== **
4  *                              ubi_AVLtree.h
5  *
6  *  Copyright (C) 1991-1997 by Christopher R. Hertel
7  *
8  *  Email: crh@ubiqx.mn.org
9  * -------------------------------------------------------------------------- **
10  *
11  *  This module provides an implementation of AVL height balanced binary
12  *  trees.  (Adelson-Velskii, Landis 1962)
13  *
14  *  This header file contains the basic AVL structure and pointer typedefs
15  *  as well as the prototypes needed to access the functions in the AVL
16  *  module ubi_AVLtree.  The .c file implements the low-level height balancing
17  *  routines that manage the AVL tree, plus all of the basic primops for
18  *  adding, searching for, and deleting nodes.
19  *
20  * -------------------------------------------------------------------------- **
21  *
22  *  This library is free software; you can redistribute it and/or
23  *  modify it under the terms of the GNU Library General Public
24  *  License as published by the Free Software Foundation; either
25  *  version 2 of the License, or (at your option) any later version.
26  *
27  *  This library is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30  *  Library General Public License for more details.
31  *
32  *  You should have received a copy of the GNU Library General Public
33  *  License along with this library; if not, write to the Free
34  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35  *
36  * -------------------------------------------------------------------------- **
37  * Log: ubi_AVLtree.h,v
38  * Revision 3.1  1997/12/18 06:27:01  crh
39  * Fixed some comment bugs.
40  *
41  * Revision 3.0  1997/12/08 05:39:01  crh
42  * This is a new major revision level.  The handling of the pointers in the
43  * ubi_trNode structure was redesigned.  The result is that there are fewer
44  * macros floating about, and fewer cases in which values have to be
45  * incremented or decremented.  See ubi_BinTree for more information.
46  *
47  * Revision 2; 1995/03/05 - 1997/12/07:
48  * An overhaul to the node delete process.  I had gotten it wrong in a
49  * couple of places, thought I'd fixed it, and then found that I'd missed
50  * something more.  Thanks to Andrew Leppard for the bug report!
51  * 
52  * Revision 1;  93/10/15 - 95/03/05:
53  * Added the ubi_tr defines.  See ubi_BinTree.h for more info.
54  *
55  *  V0.0 - May, 1990   -  Written by Christopher R. Hertel (CRH).
56  *
57  *  ========================================================================= **
58  */
59
60 #include "ubi_BinTree.h"   /* Base erg binary tree support.       */
61
62 /*  ------------------------------------------------------------------------- **
63  *  AVL Tree Node Structure:  This structure defines the basic elements of
64  *       the AVL tree nodes.  In general you *SHOULD NOT PLAY WITH THESE
65  *       FIELDS*!  But, of course, I have to put the structure into this
66  *       header so that you can use the structure as a building block.
67  *
68  *  The fields are as follows:
69  *    leftlink -  A space filler.  This field will be accessed as Link[-1].
70  *    Link     -  An array of pointers.  These pointers are manipulated by the
71  *                BT and AVL routines, and indicate the left and right child
72  *                nodes, plus the parent node.  By keeping track of the parent
73  *                pointer, we avoid the need for recursive routines or hand-
74  *                tooled stacks to keep track of our path back to the root.
75  *                The use of these pointers is subject to change without
76  *                notice.
77  *    gender   -  For tree rebalancing purposes, it is necessary that each node
78  *                know whether it is the left or right child of its parent, or
79  *                if it is the root.  This information is stored in this field.
80  *    balance  -  This field is also needed for AVL balancing purposes.  It
81  *                indicates which subtree of the current node is longer, or if
82  *                the subtrees are, in fact, balanced with respect to each
83  *                other.
84  *
85  */
86
87 typedef struct ubi_avlNodeStruct
88   {
89   struct ubi_avlNodeStruct *leftlink;
90   struct ubi_avlNodeStruct *Link[2];
91   signed char               gender; 
92   signed char               balance;
93   } ubi_avlNode;
94
95 typedef ubi_avlNode *ubi_avlNodePtr;    /* a Pointer to an AVL node. */
96
97 /* -------------------------------------------------------------------------- **
98  *  Function prototypes...
99  */
100
101 ubi_avlNodePtr ubi_avlInitNode( ubi_avlNodePtr NodePtr );
102   /* ------------------------------------------------------------------------ **
103    * Initialize a tree node.
104    *
105    *  Input:   NodePtr  - a pointer to a ubi_btNode structure to be
106    *                      initialized.
107    *  Output:  a pointer to the initialized ubi_avlNode structure (ie. the
108    *           same as the input pointer).
109    * ------------------------------------------------------------------------ **
110    */
111
112 ubi_trBool ubi_avlInsert( ubi_btRootPtr   RootPtr,
113                           ubi_avlNodePtr  NewNode,
114                           ubi_btItemPtr   ItemPtr,
115                           ubi_avlNodePtr *OldNode );
116   /* ------------------------------------------------------------------------ **
117    * This function uses a non-recursive algorithm to add a new element to
118    * the tree.
119    *
120    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
121    *                       the root of the tree to which NewNode is to be added.
122    *           NewNode  -  a pointer to an ubi_avlNode structure that is NOT
123    *                       part of any tree.
124    *           ItemPtr  -  A pointer to the sort key that is stored within
125    *                       *NewNode.  ItemPtr MUST point to information stored
126    *                       in *NewNode or an EXACT DUPLICATE.  The key data
127    *                       indicated by ItemPtr is used to place the new node
128    *                       into the tree.
129    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
130    *                       the tree, a duplicate node may be found.  If
131    *                       duplicates are allowed, then the new node will
132    *                       be simply placed into the tree.  If duplicates
133    *                       are not allowed, however, then one of two things
134    *                       may happen.
135    *                       1) if overwritting *is not* allowed, this
136    *                          function will return FALSE (indicating that
137    *                          the new node could not be inserted), and
138    *                          *OldNode will point to the duplicate that is
139    *                          still in the tree.
140    *                       2) if overwritting *is* allowed, then this
141    *                          function will swap **OldNode for *NewNode.
142    *                          In this case, *OldNode will point to the node
143    *                          that was removed (thus allowing you to free
144    *                          the node).
145    *                          **  If you are using overwrite mode, ALWAYS  **
146    *                          ** check the return value of this parameter! **
147    *                 Note: You may pass NULL in this parameter, the
148    *                       function knows how to cope.  If you do this,
149    *                       however, there will be no way to return a
150    *                       pointer to an old (ie. replaced) node (which is
151    *                       a problem if you are using overwrite mode).
152    *
153    *  Output:  a boolean value indicating success or failure.  The function
154    *           will return FALSE if the node could not be added to the tree.
155    *           Such failure will only occur if duplicates are not allowed,
156    *           nodes cannot be overwritten, AND a duplicate key was found
157    *           within the tree.
158    * ------------------------------------------------------------------------ **
159    */
160
161 ubi_avlNodePtr ubi_avlRemove( ubi_btRootPtr  RootPtr,
162                               ubi_avlNodePtr DeadNode );
163   /* ------------------------------------------------------------------------ **
164    * This function removes the indicated node from the tree, after which the
165    * tree is rebalanced.
166    *
167    *  Input:  RootPtr  -  A pointer to the header of the tree that contains
168    *                      the node to be removed.
169    *          DeadNode -  A pointer to the node that will be removed.
170    *
171    *  Output: This function returns a pointer to the node that was removed
172    *          from the tree (ie. the same as DeadNode).
173    *
174    *  Note:   The node MUST be in the tree indicated by RootPtr.  If not,
175    *          strange and evil things will happen to your trees.
176    * ------------------------------------------------------------------------ **
177    */
178
179 int ubi_avlModuleID( int size, char *list[] );
180   /* ------------------------------------------------------------------------ **
181    * Returns a set of strings that identify the module.
182    *
183    *  Input:  size  - The number of elements in the array <list>.
184    *          list  - An array of pointers of type (char *).  This array
185    *                  should, initially, be empty.  This function will fill
186    *                  in the array with pointers to strings.
187    *  Output: The number of elements of <list> that were used.  If this value
188    *          is less than <size>, the values of the remaining elements are
189    *          not guaranteed.
190    *
191    *  Notes:  Please keep in mind that the pointers returned indicate strings
192    *          stored in static memory.  Don't free() them, don't write over
193    *          them, etc.  Just read them.
194    * ------------------------------------------------------------------------ **
195    */
196
197 /* -------------------------------------------------------------------------- **
198  * Masquarade...
199  *
200  * This set of defines allows you to write programs that will use any of the
201  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
202  * Instead of using ubi_avl... or ubi_bt, use ubi_tr... and select the tree
203  * type by including the appropriate module header.
204  */
205
206 #undef ubi_trNode
207 #undef ubi_trNodePtr
208 #define ubi_trNode    ubi_avlNode
209 #define ubi_trNodePtr ubi_avlNodePtr
210
211 #undef ubi_trInitNode
212 #define ubi_trInitNode( Np ) ubi_avlInitNode( (ubi_avlNodePtr)(Np) )
213
214 #undef ubi_trInsert
215 #define ubi_trInsert( Rp, Nn, Ip, On ) \
216         ubi_avlInsert( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Nn), \
217                        (ubi_btItemPtr)(Ip), (ubi_avlNodePtr *)(On) )
218
219 #undef ubi_trRemove
220 #define ubi_trRemove( Rp, Dn ) \
221         ubi_avlRemove( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Dn) )
222
223 #undef ubi_trLocate
224 #define ubi_trLocate( Rp, Ip, Op ) \
225         (ubi_avlNodePtr)ubi_btLocate( (ubi_btRootPtr)(Rp), \
226                                       (ubi_btItemPtr)(Ip), \
227                                       (ubi_trCompOps)(Op) )
228
229 #undef ubi_trFind
230 #define ubi_trFind( Rp, Ip ) \
231         (ubi_avlNodePtr)ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
232
233 #undef ubi_trNext
234 #define ubi_trNext( P ) (ubi_avlNodePtr)ubi_btNext( (ubi_btNodePtr)(P) )
235
236 #undef ubi_trPrev
237 #define ubi_trPrev( P ) (ubi_avlNodePtr)ubi_btPrev( (ubi_btNodePtr)(P) )
238
239 #undef ubi_trFirst
240 #define ubi_trFirst( P ) (ubi_avlNodePtr)ubi_btFirst( (ubi_btNodePtr)(P) )
241
242 #undef ubi_trLast
243 #define ubi_trLast( P ) (ubi_avlNodePtr)ubi_btLast( (ubi_btNodePtr)(P) )
244
245 #undef ubi_trFirstOf
246 #define ubi_trFirstOf( Rp, Ip, P ) \
247         (ubi_avlNodePtr)ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
248                        (ubi_btItemPtr)(Ip), \
249                        (ubi_btNodePtr)(P) )
250
251 #undef ubi_trLastOf
252 #define ubi_trLastOf( Rp, Ip, P ) \
253         (ubi_avlNodePtr)ubi_btLastOf( (ubi_btRootPtr)(Rp), \
254                                       (ubi_btItemPtr)(Ip), \
255                                       (ubi_btNodePtr)(P) )
256
257 #undef ubi_trLeafNode
258 #define ubi_trLeafNode( Nd ) \
259         (ubi_avlNodePtr)ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
260
261 #undef ubi_trModuleID
262 #define ubi_trModuleID( s, l ) ubi_avlModuleID( s, l )
263
264
265 /* =========================== End  ubi_AVLtree.h =========================== */
266 #endif /* ubi_AVLtree_H */