Removed all ubiqx includes from includes.h. So far, this was only a problem
[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-1998 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 4.1  1998/05/20 04:35:50  crh
39  * The C file now includes ubi_null.h.  See ubi_null.h for more info.
40  *
41  * Revision 4.0  1998/03/10 03:34:45  crh
42  * Major changes.
43  * By adding the AVL balance field to the base ubi_btNode structure, I no
44  * longer need AVL-specific ReplaceNode(), SwapNodes(), and InitNode()
45  * functions.  The Remove() function is also simplified.  It's all much
46  * cleaner.
47  * This is rev. 4.0.  The 3.x series was dropped.
48  *
49  * Revision 2.5  1997/12/23 04:00:15  crh
50  * In this version, all constants & macros defined in the header file have
51  * the ubi_tr prefix.  Also cleaned up anything that gcc complained about
52  * when run with '-pedantic -fsyntax-only -Wall'.
53  *
54  * Revision 2.4  1997/07/26 04:36:23  crh
55  * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
56  * on backwards with respect to node deletion.  I did some more digging and
57  * discovered that I was not changing the balance values correctly in the
58  * single rotation functions.  Double rotation was working correctly because
59  * the formula for changing the balance values is the same for insertion or
60  * deletion.  Not so for single rotation.
61  *
62  * I have tested the fix by loading the tree with over 44 thousand names,
63  * deleting 2,629 of them (all those in which the second character is 'u')
64  * and then walking the tree recursively to verify that the balance factor of
65  * each node is correct.  Passed.
66  *
67  * Thanks Andrew!
68  *
69  * Also:
70  * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
71  * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
72  *   hoped they would do (see the bottom of the header file).  They work now.
73  *
74  * Revision 2.3  1997/06/03 05:22:07  crh
75  * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
76  * problems.
77  *
78  * Revision 2.2  1995/10/03 22:15:47  CRH
79  * Ubisized!
80  *
81  * Revision 2.1  95/03/09  23:46:44  CRH
82  * Added the ModuleID static string and function.  These modules are now
83  * self-identifying.
84  * 
85  * Revision 2.0  95/03/05  14:11:22  CRH
86  * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
87  * and so includes all of the changes to that module.  In addition, a bug in
88  * the node deletion process has been fixed.
89  *
90  * After rewriting the Locate() function in ubi_BinTree, I decided that it was
91  * time to overhaul this module.  In the process, I discovered a bug related
92  * to node deletion.  To fix the bug, I wrote function Debalance().  A quick
93  * glance will show that it is very similar to the Rebalance() function.  In
94  * previous versions of this module, I tried to include the functionality of
95  * Debalance() within Rebalance(), with poor results.
96  *
97  * Revision 1.0  93/10/15  22:58:48  CRH
98  * With this revision, I have added a set of #define's that provide a single,
99  * standard API to all existing tree modules.  Until now, each of the three
100  * existing modules had a different function and typedef prefix, as follows:
101  *
102  *       Module        Prefix
103  *     ubi_BinTree     ubi_bt
104  *     ubi_AVLtree     ubi_avl
105  *     ubi_SplayTree   ubi_spt
106  *
107  * To further complicate matters, only those portions of the base module
108  * (ubi_BinTree) that were superceeded in the new module had the new names.
109  * For example, if you were using ubi_SplayTree, the locate function was
110  * called "ubi_sptLocate", but the next and previous functions remained
111  * "ubi_btNext" and "ubi_btPrev".
112  *
113  * This was not too terrible if you were familiar with the modules and knew
114  * exactly which tree model you wanted to use.  If you wanted to be able to
115  * change modules (for speed comparisons, etc), things could get messy very
116  * quickly.
117  *
118  * So, I have added a set of defined names that get redefined in any of the
119  * descendant modules.  To use this standardized interface in your code,
120  * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
121  * "ubi_tr".  The "ubi_tr" names will resolve to the correct function or
122  * datatype names for the module that you are using.  Just remember to
123  * include the header for that module in your program file.  Because these
124  * names are handled by the preprocessor, there is no added run-time
125  * overhead.
126  *
127  * Note that the original names do still exist, and can be used if you wish
128  * to write code directly to a specific module.  This should probably only be
129  * done if you are planning to implement a new descendant type, such as
130  * red/black trees.  CRH
131  *
132  *  V0.0 - May, 1990   -  Written by Christopher R. Hertel (CRH).
133  *
134  *  ========================================================================= **
135  */
136
137 #include "ubi_BinTree.h"   /* Base erg binary tree support.       */
138
139 /* -------------------------------------------------------------------------- **
140  *  Function prototypes.
141  * -------------------------------------------------------------------------- **
142  */
143
144 ubi_trBool ubi_avlInsert( ubi_btRootPtr  RootPtr,
145                           ubi_btNodePtr  NewNode,
146                           ubi_btItemPtr  ItemPtr,
147                           ubi_btNodePtr *OldNode );
148   /* ------------------------------------------------------------------------ **
149    * This function uses a non-recursive algorithm to add a new element to
150    * the tree.
151    *
152    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
153    *                       the root of the tree to which NewNode is to be added.
154    *           NewNode  -  a pointer to an ubi_btNode structure that is NOT
155    *                       part of any tree.
156    *           ItemPtr  -  A pointer to the sort key that is stored within
157    *                       *NewNode.  ItemPtr MUST point to information stored
158    *                       in *NewNode or an EXACT DUPLICATE.  The key data
159    *                       indicated by ItemPtr is used to place the new node
160    *                       into the tree.
161    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
162    *                       the tree, a duplicate node may be found.  If
163    *                       duplicates are allowed, then the new node will
164    *                       be simply placed into the tree.  If duplicates
165    *                       are not allowed, however, then one of two things
166    *                       may happen.
167    *                       1) if overwritting *is not* allowed, this
168    *                          function will return FALSE (indicating that
169    *                          the new node could not be inserted), and
170    *                          *OldNode will point to the duplicate that is
171    *                          still in the tree.
172    *                       2) if overwritting *is* allowed, then this
173    *                          function will swap **OldNode for *NewNode.
174    *                          In this case, *OldNode will point to the node
175    *                          that was removed (thus allowing you to free
176    *                          the node).
177    *                          **  If you are using overwrite mode, ALWAYS  **
178    *                          ** check the return value of this parameter! **
179    *                 Note: You may pass NULL in this parameter, the
180    *                       function knows how to cope.  If you do this,
181    *                       however, there will be no way to return a
182    *                       pointer to an old (ie. replaced) node (which is
183    *                       a problem if you are using overwrite mode).
184    *
185    *  Output:  a boolean value indicating success or failure.  The function
186    *           will return FALSE if the node could not be added to the tree.
187    *           Such failure will only occur if duplicates are not allowed,
188    *           nodes cannot be overwritten, AND a duplicate key was found
189    *           within the tree.
190    * ------------------------------------------------------------------------ **
191    */
192
193 ubi_btNodePtr ubi_avlRemove( ubi_btRootPtr RootPtr,
194                              ubi_btNodePtr DeadNode );
195   /* ------------------------------------------------------------------------ **
196    * This function removes the indicated node from the tree, after which the
197    * tree is rebalanced.
198    *
199    *  Input:  RootPtr  -  A pointer to the header of the tree that contains
200    *                      the node to be removed.
201    *          DeadNode -  A pointer to the node that will be removed.
202    *
203    *  Output: This function returns a pointer to the node that was removed
204    *          from the tree (ie. the same as DeadNode).
205    *
206    *  Note:   The node MUST be in the tree indicated by RootPtr.  If not,
207    *          strange and evil things will happen to your trees.
208    * ------------------------------------------------------------------------ **
209    */
210
211 int ubi_avlModuleID( int size, char *list[] );
212   /* ------------------------------------------------------------------------ **
213    * Returns a set of strings that identify the module.
214    *
215    *  Input:  size  - The number of elements in the array <list>.
216    *          list  - An array of pointers of type (char *).  This array
217    *                  should, initially, be empty.  This function will fill
218    *                  in the array with pointers to strings.
219    *  Output: The number of elements of <list> that were used.  If this value
220    *          is less than <size>, the values of the remaining elements are
221    *          not guaranteed.
222    *
223    *  Notes:  Please keep in mind that the pointers returned indicate strings
224    *          stored in static memory.  Don't free() them, don't write over
225    *          them, etc.  Just read them.
226    * ------------------------------------------------------------------------ **
227    */
228
229 /* -------------------------------------------------------------------------- **
230  * Masquarade...
231  *
232  * This set of defines allows you to write programs that will use any of the
233  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
234  * Instead of using ubi_avl... or ubi_bt, use ubi_tr... and select the tree
235  * type by including the appropriate module header.
236  */
237
238 #undef ubi_trInsert
239 #define ubi_trInsert( Rp, Nn, Ip, On ) \
240         ubi_avlInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
241                        (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
242
243 #undef ubi_trRemove
244 #define ubi_trRemove( Rp, Dn ) \
245         ubi_avlRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
246
247 #undef ubi_trModuleID
248 #define ubi_trModuleID( s, l ) ubi_avlModuleID( s, l )
249
250
251 /* =========================== End  ubi_AVLtree.h =========================== */
252 #endif /* ubi_AVLtree_H */