redblack.h

Go to the documentation of this file.
00001 /*                      R E D B L A C K . H
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 /** @addtogroup rb */
00022 /*@{*/
00023 /** @file redblack.h
00024  * @brief
00025  *      The data structures and constants for red-black trees.
00026  *
00027  *      Many of these routines are based on the algorithms in chapter 13
00028  *      of T. H. Cormen, C. E. Leiserson, and R. L. Rivest,
00029  *      _Introduction to algorithms_, MIT Press, Cambridge, MA, 1990.
00030  *
00031  *      @author:        Paul Tanenbaum
00032  */
00033 
00034 #include "bu.h"
00035 #if 0
00036 
00037 /*
00038  *                          R B _ L I S T
00039  *
00040  *                  List of nodes or packages
00041  *
00042  *      LIBREDBLACK(3) uses this structure to maintain lists of
00043  *      all the nodes and all the packages in the tree.  Applications
00044  *      should not muck with these things.  They are maintained only
00045  *      to facilitate freeing rb_trees.
00046  */
00047 struct rb_list
00048 {
00049     struct bu_list      l;
00050     union
00051     {
00052         struct rb_node    *rbl_n;
00053         struct rb_package *rbl_p;
00054     }                   rbl_u;
00055 };
00056 #define rbl_magic       l.magic
00057 #define rbl_node        rbl_u.rbl_n
00058 #define rbl_package     rbl_u.rbl_p
00059 #define RB_LIST_NULL    ((struct rb_list *) 0)
00060 
00061 
00062 /*
00063  *                      R B _ T R E E
00064  *
00065  *      This is the only data structure used in LIBREDBLACK
00066  *      to which application software need make any explicit
00067  *      reference.
00068  *
00069  *      The members of this structure are grouped into three
00070  *      classes:
00071  *          Class I:    Reading is appropriate, when necessary,
00072  *                      but applications should not modify.
00073  *          Class II:   Reading and modifying are both appropriate,
00074  *                      when necessary.
00075  *          Class III:  All access should be through routines
00076  *                      provided in LIBREDBLACK.  Touch these
00077  *                      at your own risk!
00078  */
00079 typedef struct
00080 {
00081     /* CLASS I - Applications may read directly... */
00082     long                rbt_magic;        /* Magic no. for integrity check */
00083     int                 rbt_nm_nodes;     /* Number of nodes */
00084     /* CLASS II - Applications may read/write directly... */
00085     void                (*rbt_print)();   /* Data pretty-print function */
00086     int                 rbt_debug;        /* Debug bits */
00087     char                *rbt_description; /* Comment for diagnostics */
00088     /* CLASS III - Applications should not manipulate directly... */
00089     int                 rbt_nm_orders;    /* Number of simultaneous orders */
00090     int                 (**rbt_order)();  /* Comparison functions */
00091     struct rb_node      **rbt_root;       /* The actual trees */
00092     char                *rbt_unique;      /* Uniqueness flags */
00093     struct rb_node      *rbt_current;     /* Current node */
00094     struct rb_list      rbt_nodes;        /* All nodes */
00095     struct rb_list      rbt_packages;     /* All packages */
00096     struct rb_node      *rbt_empty_node;  /* Sentinel representing nil */
00097 }       rb_tree;
00098 #define RB_TREE_NULL    ((rb_tree *) 0)
00099 #define RB_TREE_MAGIC   0x72627472
00100 
00101 /*
00102  *      Debug bit flags for member rbt_debug
00103  */
00104 #define RB_DEBUG_INSERT 0x00000001      /* Insertion process */
00105 #define RB_DEBUG_UNIQ   0x00000002      /* Uniqueness of inserts */
00106 #define RB_DEBUG_ROTATE 0x00000004      /* Rotation process */
00107 #define RB_DEBUG_OS     0x00000008      /* Order-statistic operations */
00108 #define RB_DEBUG_DELETE 0x00000010      /* Deletion process */
00109 
00110 /*
00111  *                      R B _ P A C K A G E
00112  *
00113  *                  Wrapper for application data
00114  *
00115  *      This structure provides a level of indirection between
00116  *      the application software's data and the red-black nodes
00117  *      in which the data is stored.  It is necessary because of
00118  *      the algorithm for deletion, which generally shuffles data
00119  *      among nodes in the tree.  The package structure allows
00120  *      the application data to remember which node "contains" it
00121  *      for each order.
00122  */
00123 struct rb_package
00124 {
00125     long                rbp_magic;      /* Magic no. for integrity check */
00126     struct rb_node      **rbp_node;     /* Containing nodes */
00127     struct rb_list      *rbp_list_pos;  /* Place in the list of all pkgs. */
00128     void                *rbp_data;      /* Application data */
00129 };
00130 #define RB_PKG_NULL     ((struct rb_package *) 0)
00131 
00132 /*
00133  *                          R B _ N O D E
00134  *
00135  *      For the most part, there is a one-to-one correspondence
00136  *      between nodes and chunks of application data.  When a
00137  *      node is created, all of its package pointers (one per
00138  *      order of the tree) point to the same chunk of data.
00139  *      However, subsequent deletions usually muddy this tidy
00140  *      state of affairs.
00141  */
00142 struct rb_node
00143 {
00144     long                rbn_magic;      /* Magic no. for integrity check */
00145     rb_tree             *rbn_tree;      /* Tree containing this node */
00146     struct rb_node      **rbn_parent;   /* Parents */
00147     struct rb_node      **rbn_left;     /* Left subtrees */
00148     struct rb_node      **rbn_right;    /* Right subtrees */
00149     char                *rbn_color;     /* Colors of this node */
00150     int                 *rbn_size;      /* Sizes of subtrees rooted here */
00151     struct rb_package   **rbn_package;  /* Contents of this node */
00152     int                 rbn_pkg_refs;   /* How many orders are being used? */
00153     struct rb_list      *rbn_list_pos;  /* Place in the list of all nodes */
00154 };
00155 #define RB_NODE_NULL    ((struct rb_node *) 0)
00156 
00157 /*
00158  *      Applications interface to rb_extreme()
00159  */
00160 #define SENSE_MIN       0
00161 #define SENSE_MAX       1
00162 #define rb_min(t,o)     rb_extreme((t), (o), SENSE_MIN)
00163 #define rb_max(t,o)     rb_extreme((t), (o), SENSE_MAX)
00164 #define rb_pred(t,o)    rb_neighbor((t), (o), SENSE_MIN)
00165 #define rb_succ(t,o)    rb_neighbor((t), (o), SENSE_MAX)
00166 
00167 /*
00168  *      Applications interface to rb_walk()
00169  */
00170 #define PREORDER        0
00171 #define INORDER         1
00172 #define POSTORDER       2
00173 
00174 /*
00175  *      Applications interface to LIBREDBLACK
00176  */
00177 BU_EXTERN(rb_tree *rb_create,   (char           *description,
00178                                  int            nm_orders,
00179                                  int            (**order_funcs)()
00180                                 ));
00181 BU_EXTERN(rb_tree *rb_create1,  (char           *description,
00182                                  int            (*order_func)()
00183                                 ));
00184 BU_EXTERN(void *rb_curr,        (rb_tree        *tree,
00185                                  int            order
00186                                 ));
00187 #define         rb_curr1(t)     rb_curr((t), 0)
00188 BU_EXTERN(void rb_delete,       (rb_tree        *tree,
00189                                  int            order
00190                                 ));
00191 #define         rb_delete1(t)   rb_delete((t), 0)
00192 BU_EXTERN(void rb_diagnose_tree,(rb_tree        *tree,
00193                                  int            order,
00194                                  int            trav_type
00195                                 ));
00196 BU_EXTERN(void *rb_extreme,     (rb_tree        *tree,
00197                                  int            order,
00198                                  int            sense
00199                                 ));
00200 BU_EXTERN(void rb_free,         (rb_tree        *tree,
00201                                  void           (*free_data)()
00202                                 ));
00203 #define RB_RETAIN_DATA  ((void (*)()) 0)
00204 #define         rb_free1(t,f)                                           \
00205                 {                                                       \
00206                     RB_CKMAG((t), RB_TREE_MAGIC, "red-black tree");     \
00207                     bu_free((char *) ((t) -> rbt_order),                \
00208                                 "red-black order function");            \
00209                     rb_free(t,f);                                       \
00210                 }
00211 BU_EXTERN(void *rb_select,      (rb_tree        *tree,
00212                                  int            order,
00213                                  int            k
00214                                 ));
00215 #define         rb_select1(t,k) rb_select((t), 0, (k))
00216 BU_EXTERN(int rb_insert,        (rb_tree        *tree,
00217                                  void           *data
00218                                 ));
00219 BU_EXTERN(int rb_is_uniq,       (rb_tree        *tree,
00220                                  int            order
00221                                 ));
00222 #define         rb_is_uniq1(t)  rb_is_uniq((t), 0)
00223 BU_EXTERN(void *rb_neighbor,    (rb_tree        *tree,
00224                                  int            order,
00225                                  int            sense
00226                                 ));
00227 BU_EXTERN(int rb_rank,          (rb_tree        *tree,
00228                                  int            order
00229                                 ));
00230 #define         rb_rank1(t)     rb_rank1((t), 0)
00231 BU_EXTERN(void *rb_search,      (rb_tree        *tree,
00232                                  int            order,
00233                                  void           *data
00234                                 ));
00235 #define         rb_search1(t,d) rb_search((t), 0, (d))
00236 BU_EXTERN(void rb_set_uniqv,    (rb_tree        *tree,
00237                                  bitv_t         vec
00238                                 ));
00239 BU_EXTERN(void rb_summarize_tree,(rb_tree       *tree
00240                                  ));
00241 BU_EXTERN(void rb_uniq_all_off, (rb_tree        *tree
00242                                 ));
00243 BU_EXTERN(void rb_uniq_all_on,  (rb_tree        *tree
00244                                 ));
00245 BU_EXTERN(int rb_uniq_off,      (rb_tree        *tree,
00246                                  int            order
00247                                 ));
00248 #define         rb_uniq_off1(t) rb_uniq_off((t), 0)
00249 BU_EXTERN(int rb_uniq_on,       (rb_tree        *tree,
00250                                  int            order
00251                                 ));
00252 #define         rb_uniq_on1(t)  rb_uniq_on((t), 0)
00253 BU_EXTERN(void rb_walk,         (rb_tree        *tree,
00254                                  int            order,
00255                                  void           (*visit)(),
00256                                  int            trav_type
00257                                 ));
00258 #define         rb_walk1(t,v,d) rb_walk((t), 0, (v), (d))
00259 
00260 #endif
00261 /*@}*/
00262 /*
00263  * Local Variables:
00264  * mode: C
00265  * tab-width: 8
00266  * c-basic-offset: 4
00267  * indent-tabs-mode: t
00268  * End:
00269  * ex: shiftwidth=4 tabstop=8
00270  */
00271 

Generated on Mon Sep 18 01:24:42 2006 for BRL-CAD by  doxygen 1.4.6